-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
200 lines (169 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var express = require("express");
var methods = require("methods");
var assert = require('assert-diff');
var querystring = require("querystring");
var EventEmitter = require("events").EventEmitter;
var util = require("util");
module.exports = function(port, middlewares) {
var app = express();
app.on("error", function(err) {
throw err;
});
app.use(express.json());
app.use(express.urlencoded());
// If the user has defined custom middlewares...
if(middlewares) {
// Iterate over them...
middlewares.forEach(function(middleware) {
// ... and injet them into the Express app
app.use(middleware);
});
}
app.use(function(req, res, next){
if (req.is('text/*')) {
req.text = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ req.text += chunk });
req.on('end', next);
} else {
next();
}
});
var server;
if(port) {
server = app.listen(port);
} else {
server = app.listen();
}
methods.forEach(function(method) {
server[method] = function(path) {
return new Assertion(app, method, path);
}
});
server.clean = function() {
app._router.map = {};
}
return server;
}
function Assertion(app, method, path) {
var self = this;
this.app = app;
this.method = method;
this.path = path;
this.headers = {};
this.isDone = false;
this.removeWhenMet = true;
this.parseExpectedRequestBody = function() {
if(!self.headers["content-type"]) {
if(typeof self.data == "string") {
return self.requestBody = querystring.parse(self.data);
}
}
self.requestBody = self.data;
}
}
Assertion.prototype.send = function(data) {
this.data = data;
return this;
}
Assertion.prototype.query = function(qs) {
var q;
if(typeof qs == "string") {
q = querystring.parse(qs);
} else {
q = qs;
}
if(!this.qs) {
this.qs = {};
}
for(var n in q) {
this.qs[n] = q[n];
}
return this;
}
Assertion.prototype.set = function(name, value) {
this.headers[name.toLowerCase()] = value;
return this;
}
Assertion.prototype.delay = function(ms) {
this.delay_ms = ms;
return this;
}
Assertion.prototype.persist = function() {
this.removeWhenMet = false;
return this;
}
Assertion.prototype.reply = function(status, responseBody, responseHeaders) {
this.parseExpectedRequestBody();
var self = this;
this.app[this.method](this.path, function(req, res) {
if(self.qs) {
assert.deepEqual(req.query, self.qs);
}
if(self.requestBody) {
if(req.text) {
assert.deepEqual(req.text, self.requestBody);
} else {
assert.deepEqual(req.body, self.requestBody);
}
}
for(var name in self.headers) {
assert.deepEqual(req.headers[name], self.headers[name]);
}
if(responseHeaders) {
res.set(responseHeaders);
}
var reply = function() {
self.handler.emit("done");
// Remove route from express since the expectation was met
// Unless this mock is suposed to persist
if (self.removeWhenMet) self.app._router.map[self.method].splice(req._route_index, 1);
if (typeof responseBody === 'function') {
res.status(status).send(responseBody(req));
} else {
res.status(status).send(responseBody);
}
};
if(self.delay_ms) {
setTimeout(reply, self.delay_ms);
} else {
reply();
}
});
this.handler = new Handler(this);
return this.handler;
}
function Handler(assertion) {
this.defaults = {
waitTimeout: 2000
};
var self = this;
this.assertion = assertion;
this.isDone = false;
this.on("done", function() {
self.isDone = true;
});
}
util.inherits(Handler, EventEmitter);
Handler.prototype.done = function() {
if(!this.isDone) {
throw new Error(this.assertion.method + " " + this.assertion.path + " was not made yet.");
}
}
Handler.prototype.wait = function(ms, fn) {
if(!fn && typeof ms == "function") {
fn = ms;
ms = this.defaults.waitTimeout;
}
var self = this;
var timeout = null;
var cb = function() {
clearTimeout(timeout);
fn();
}
this.once("done", cb);
timeout = setTimeout(function() {
self.removeListener("done", cb);
fn(new Error(self.assertion.method + " " + self.assertion.path + " was not called within " + ms + "ms."));
}, ms);
}