forked from A/superagent-mocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
355 lines (322 loc) · 9.53 KB
/
test.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* global describe, it, before, navigator */
'use strict';
/**
* Dependencies
*/
var request = require('superagent');
var should = require('should');
var mock = process.env.SM_COV
? require('./index-cov')(request)
: require('./index')(request);
describe('superagent mock', function() {
beforeEach(function() {
mock.clearRoutes();
mock.timeout = 0;
});
describe('API', function() {
it('should mock for get', function(done) {
mock.get('/topics/:id', function(req) {
req.params.id.should.be.equal('1');
return { id: req.params.id };
});
request.get('/topics/1').end(function(_, data) {
data.should.have.property('id', '1');
done();
});
});
it('should mock multiple requests', function(done) {
mock.get('/thread/:id', function(req) {
return { id: req.params.id };
});
var finished = 0;
var r1 = request.get('/thread/1');
var r2 = request.get('/thread/2');
r1.end(function(_, data) {
data.should.have.property('id', '1');
if (++finished == 2) done();
});
r2.end(function(_, data) {
data.should.have.property('id', '2');
if (++finished == 2) done();
});
});
it('should mock for post', function(done) {
mock.post('/topics/:id', function(req) {
return {
id: req.params.id,
content: req.body.content
};
});
request
.post('/topics/5', { content: 'Hello world' })
.end(function(_, data) {
data.should.have.property('id', '5');
data.should.have.property('content', 'Hello world');
done();
});
});
it('should mock for put', function(done) {
mock.put('/topics/:id', function(req) {
return { id: req.params.id, content: req.body.content };
});
request
.put('/topics/7', { id: 7, content: 'hello world!11' })
.end(function(_, data) {
data.should.have.property('id', '7');
data.should.have.property('content', 'hello world!11');
done();
});
});
it('should mock for patch', function(done) {
mock.patch('/topics/:id', function(req) {
return { id: req.params.id, content: req.body.content };
});
request
.patch('/topics/7', { id: 7, content: 'hello world!11' })
.end(function(_, data) {
data.should.have.property('id', '7');
data.should.have.property('content', 'hello world!11');
done();
});
});
it('should mock for delete', function(done) {
mock.del('/topics/:id', function(req) {
return { id: req.params.id, content: req.body.content };
});
request
.del('/topics/7', { id: 7, content: 'yay' })
.end(function(_, data) {
data.should.have.property('id', '7');
data.should.have.property('content', 'yay');
done(); // just done
});
});
it('should be async', function(done) {
var isAsync = true;
mock.get('/async', function(req) {
isAsync = false;
});
request
.get('/async')
.end();
isAsync.should.be.true;
done();
});
it('should work correct with unmocked requests', function(done) {
request
.get('http://example.com')
.query({ foo: 'bar' })
.end(function(err, res) {
done(err);
});
});
it('should work with custom timeout', function(done) {
var startedAt = +new Date();
mock.timeout = 100;
mock.get('/timeout', noop);
request
.get('/timeout')
.end(function(err, res) {
var finishedAt = +new Date();
var offset = finishedAt - startedAt;
offset.should.be.above(mock.timeout - 1);
done(err);
});
});
it('should work with custom timeout function', function(done) {
var startedAt = +new Date();
mock.get('/timeout', noop);
mock.timeout = function () { return 200; };
request
.get('/timeout')
.end(function(err, res) {
var finishedAt = +new Date();
var offset = finishedAt - startedAt;
offset.should.be.above(199);
done(err);
});
});
it('should clear registered routes', function(done) {
mock.get('/topics', noop);
mock.clearRoutes();
request
.get('/topics')
.end(function(err, res) {
should.throws(function() {
should.ifError(err);
}, /ECONNREFUSED/);
done();
});
});
it('should clear registered specific route', function(done) {
mock
.get('/topics', noop)
.get('/posters', function() {
return { id: 7 };
});
mock.clearRoute('get', '/topics');
request
.get('/topics')
.end(function(err, res) {
should.throws(function() {
should.ifError(err);
}, /ECONNREFUSED/);
request
.get('/posters')
.end(function(_, data) {
data.should.have.property('id', 7);
done();
});
});
});
it('should provide error when method throws', function(done) {
var error = Error('This should be in the callback!');
mock.get('http://example.com', function(req) {
throw error;
});
request
.get('http://example.com')
.end(function(err, res) {
err.should.equal(error);
done();
});
});
it('should not treat a 204 as an error', function(done) {
mock.get('/topics/:id', function(req) {
return {status: 204};
});
request.get('/topics/1')
.end(function(err, data) {
should.not.exist(err);
data.should.have.property('status', 204);
done();
});
});
it('should support status code in response', function(done) {
mock.get('/topics/:id', function(req) {
return {body: {}, status: 500};
});
request.get('/topics/1')
.end(function(err, data) {
err.should.have.property('status', 500);
err.should.have.property('response');
should.deepEqual(err.response, {body: {}, status: 500});
done();
});
});
it('should support headers', function(done) {
mock.get('/topics/:id', function(req) {
return req.headers;
});
request.get('/topics/1')
.set('My-Header', 'my-Value')
.set('User-Agent', 'Opera Mini')
.end(function(_, data) {
// lowercase
data.should.have.property('my-header', 'my-Value')
data.should.have.property('user-agent', 'Opera Mini')
done();
});
});
it('should support multiple headers', function(done) {
mock.get('/', function(req) {
return req.headers;
});
request.get('/')
.set({
'My-Header': 'my-Value',
'User-Agent': 'Opera Mini',
})
.end(function(_, data) {
data.should.have.property('my-header', 'my-Value')
data.should.have.property('user-agent', 'Opera Mini')
done();
})
})
it('should throw error when header isn\'t a string', function() {
mock.get('/topics/:id', function(req) {
return req.headers;
});
should.throws(function() {
request.get('/topics/1')
.set(42, 'my-Value')
.end(function(_, data) {
done();
});
should.ifError(err);
}, /Header keys must be strings/);
});
it('should pass data from send()', function(done) {
mock.post('/topics/:id', function(req) {
return req.body;
});
request
.post('/topics/5')
.send({ content: 'Hello world' })
.end(function(_, data) {
data.should.have.property('content', 'Hello world');
done();
})
;
});
it('should pass non-object data from send()', function(done) {
mock.post('/topics/:id', function(req) {
return { body: req.body };
});
request
.post('/topics/6')
.send('foo bar baz')
.end(function(_, data) {
should.equal(data.body, 'foo bar baz');
done();
})
;
});
it('should rewrite post() data by send()', function(done) {
mock.post('/topics/:id', function(req) {
return req.body;
});
request
.post('/topics/5', { content: 'Hello Universe' })
.send({ content: 'Hello world', title: 'Yay!' })
.end(function(_, data) {
data.should.have.property('title', 'Yay!');
data.should.have.property('content', 'Hello world');
done();
})
;
});
it('should parse parameters from query()', function(done) {
mock.get('/topics/:id', function(req) {
return req;
});
request
.get('/topics/5')
.query('hello=world')
.query('xx=yy&zz=0')
.query({ test: 'yay' })
.query({ foo: 'bar', baz: 'bat' })
.end(function(_, data) {
data.should.have.property('query');
should.deepEqual(data.query, {
hello: 'world',
xx: 'yy',
zz: '0',
test: 'yay',
foo: 'bar',
baz: 'bat'
});
done();
})
;
});
it('should remove patches by unmock()', function() {
mock.unmock(request);
(request._patchedBySuperagentMocker === void 0).should.be.true;
});
});
});
/**
* Just noop
*/
function noop() {};