-
Notifications
You must be signed in to change notification settings - Fork 1
/
vanilli-test.js
583 lines (494 loc) · 19 KB
/
vanilli-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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* jshint expr:true */
var vanilli = require('../../lib/vanilli'),
chai = require('chai'),
expect = require('chai').expect,
portfinder = require('portfinder');
portfinder.basePort = 14000;
chai.config.includeStack = true;
chai.use(require('chai-http'));
describe('Vanilli', function () {
var client,
config = {
logLevel: "error",
static: {
root: "test/e2e/static",
"default": "something.html",
include: [ "**/*.html", { path: '/foo', target: 'foo.html' }, { path: '/bar', useDefault: true } ],
exclude: [ "**/*.xxx" ]
}
},
dummyStatus = 666,
dummyUrl = "/some/url";
before(function (done) {
portfinder.getPort(function (err, port) {
if (err) {
done(err);
} else {
vanilli.listen(port, config);
client = chai.request("http://localhost:" + port);
done();
}
});
});
after(function () {
vanilli.stop();
});
afterEach(function () {
vanilli.clear();
});
it('listens on the configured port', function (done) {
client.get('/_vanilli/ping')
.end(function (err, res) {
expect(res).to.be.json;
expect(res).to.have.status(200);
expect(res.body.ping).to.equal("pong");
done();
});
});
it('serves up the stub matching the incoming request', function (done) {
vanilli.stub(
vanilli.onGet("/another/url").respondWith(123),
vanilli.onGet("/my/url").respondWith(234),
vanilli.onGet("/yet/another/url").respondWith(345)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(234);
done();
});
});
it('serves up the stub matching the incoming request containing repeated query param', function (done) {
vanilli.stub(
vanilli.onGet("/my/url", { query: { param1: [ "a", "b" ] } }).respondWith(123)
);
client.get("/my/url?param1=a¶m1=b")
.end(function (err, res) {
expect(res).to.have.status(123);
done();
});
});
it('serves up the stub matching the lowest priority', function (done) {
vanilli.stub(
vanilli.onGet("/my/url", { priority: 1 }).respondWith(123),
vanilli.onGet("/my/url", { priority: 0 }).respondWith(234),
vanilli.onGet("/my/url", { priority: 2 }).respondWith(678)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(234);
done();
});
});
it('considers stub priority as 0 if not explicitly specified', function (done) {
vanilli.stub(
vanilli.onGet("/my/url", { priority: 1 }).respondWith(123),
vanilli.onGet("/my/url").respondWith(234)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(234);
done();
});
});
it('serves up the last matched stub', function (done) {
vanilli.stub(
vanilli.onGet("/my/url").respondWith(123),
vanilli.onGet("/my/url").respondWith(234)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(234);
done();
});
});
it('serves up the non-default matched stub', function (done) {
vanilli.stub(
vanilli.onGet("/my/url").respondWith(234)
);
vanilli.stubDefault(
vanilli.onGet("/my/url").respondWith(123)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(234);
done();
});
});
it('serves up the default stub if no matches found', function (done) {
vanilli.stub(
vanilli.onGet("/another/url").respondWith(234)
);
vanilli.stubDefault(
vanilli.onGet("/my/url").respondWith(123)
);
client.get("/my/url")
.end(function (err, res) {
expect(res).to.have.status(123);
done();
});
});
it('adds headers from matching stub to response', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus, {
headers: {
myheader1: "value1",
myheader2: "value2"
}
})
);
client.get(dummyUrl)
.end(function (err, res) {
expect(res).to.have.header("myheader1", "value1");
expect(res).to.have.header("myheader2", "value2");
done();
});
});
it('adds body from matching stub to response', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus, {
body: {
some: "content"
},
contentType: "application/json"
})
);
client.get(dummyUrl)
.end(function (err, res) {
expect(res.body).to.deep.equal({ some: "content" });
expect(res).to.have.header('content-type', "application/json");
done();
});
});
it('overrides explicit Content-Type header with content type of body', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus, {
body: {
some: "content"
},
contentType: "application/json",
headers: {
"Content-Type": "something/else"
}
})
);
client.get(dummyUrl)
.end(function (err, res) {
expect(res).to.have.header('content-type', "application/json");
done();
});
});
it('serves up 500 if no matching stub is found', function (done) {
vanilli.stub(
vanilli.onGet("/my/url").respondWith(dummyStatus)
);
client.get("/another/url")
.end(function (err, res) {
expect(res).to.have.status(404);
done();
});
});
it('can be cleared down of stubs', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(123)
);
vanilli.clear();
client.get(dummyUrl)
.end(function (err, res) {
expect(res).to.have.status(404);
done();
});
});
it('uses CORS to allow requests to vanilli from other ports', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus)
);
client.get(dummyUrl)
.end(function (err, res) {
expect(res).to.have.header('access-control-allow-origin', "*");
done();
});
});
it('echoes specified request headers back as access-control-allow-headers header for CORS preflight request', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus)
);
client.options(dummyUrl)
.set("access-control-request-headers", "header1, header2, header3")
.end(function (err, res) {
expect(res).to.have.header('access-control-allow-headers', "header1, header2, header3");
done();
});
});
it('echoes specified request method back as access-control-allow-methods header for CORS preflight request', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus)
);
client.options(dummyUrl)
.set("access-control-request-methods", "DELETE")
.end(function (err, res) {
expect(res).to.have.header('access-control-allow-methods', "DELETE");
done();
});
});
it('does not echo back CORS preflight response headers for non-preflight request', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus)
);
client.get(dummyUrl)
.end(function (err, res) {
expect(res).not.to.have.header('access-control-allow-methods');
expect(res).not.to.have.header('access-control-allow-headers');
done();
});
});
it("only responds after waiting the length of time specified by the stub", function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus)
.wait(200)
);
var startResponse = (new Date()).getTime();
client.get(dummyUrl)
.buffer()
.end(function () {
var endResponse = (new Date()).getTime();
expect(endResponse - startResponse).to.be.greaterThan(200);
done();
});
});
it("supports 'wait' specified before 'respondWith' syntactic sugar", function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).wait(200).respondWith(dummyStatus)
);
var startResponse = (new Date()).getTime();
client.get(dummyUrl)
.buffer()
.end(function () {
var endResponse = (new Date()).getTime();
expect(endResponse - startResponse).to.be.greaterThan(200);
done();
});
});
it('uses correct content type for response when content type not supported by a registered restify formatter', function (done) {
vanilli.stub(
vanilli.onGet(dummyUrl).respondWith(dummyStatus, {
body: "<html><body>some page</body></html>",
contentType: "text/html"
})
);
client.get(dummyUrl)
.buffer()
.end(function (err, res) {
expect(res.text).to.equal('<html><body>some page</body></html>');
expect(res.header['content-type']).to.equal("text/html");
done();
});
});
it('can parse custom content-types for request and responses', function (done) {
vanilli.stub(
vanilli.onPost(dummyUrl, { body: { a: 'A' }, contentType: 'application/vnd.custom+json' })
.respondWith(123, { contentType: "application/vnd.custom2+json" })
);
client.post(dummyUrl)
.type('application/vnd.custom+json')
.send(JSON.stringify({ a: 'A' }))
.end(function (err, res) {
expect(res).to.have.status(123);
expect(res.header['content-type']).to.equal('application/vnd.custom2+json');
done();
});
});
it('can match stubs on post body, despite extra fields', function (done) {
vanilli.stub(
vanilli.onPost(dummyUrl, { body: { a: 'A' }, contentType: 'application/json' })
.respondWith(123)
);
client.post(dummyUrl)
.send({ a: 'A', b: 'B' })
.end(function (err, res) {
expect(res).to.have.status(123);
done();
});
});
it('can serve HEAD requests', function (done) {
vanilli.stub(
vanilli.onHead(dummyUrl)
.respondWith(123)
);
client.head(dummyUrl)
.end(function (err, res) {
expect(res).to.have.status(123);
done();
});
});
describe('static content', function () {
it('is served if request meets criteria of static filter', function (done) {
client.get('/something.html')
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
it('is not served if request meets include criteria but not excludes', function (done) {
client.get('/exists.xxx')
.end(function (err, res) {
expect(res).to.have.status(404);
done();
});
});
it('serves up 404 if request meets criteria of static filter but no matching static content exists', function (done) {
client.get('/doesnotexist.html')
.end(function (err, res) {
expect(res).to.have.status(404);
done();
});
});
it('serves up default resource for / path', function (done) {
client.get('/')
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
it('serves up default resource for no path', function (done) {
client.get('')
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
it('serves up default resource proxy routes using default as target', function (done) {
client.get('/foo')
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
it('serves up specified target resource for proxy route if request meets criteria of static filter', function (done) {
client.get('/bar')
.end(function (err, res) {
expect(res).to.have.status(200);
done();
});
});
});
describe('JSONP', function () {
it('is used to wrap response non-json entity as string passed to specified callback', function (done) {
vanilli.stub(
vanilli.onGet("/my/url").respondWith(dummyStatus, {
body: "somecontent",
contentType: "text/plain"
})
);
client.get("/my/url?callback=mycallback")
.buffer()
.end(function (err, res) {
expect(res).to.have.header('content-type', "application/javascript");
expect(res.text).to.equal("typeof mycallback === 'function' && mycallback(\"somecontent\");");
done();
});
});
it('is used to wrap json response json entity as and object passed to specified callback', function (done) {
vanilli.stub(
vanilli.onGet("/my/url").respondWith(dummyStatus, {
body: {
some: "content"
},
contentType: "application/json"
})
);
client.get("/my/url?callback=mycallback")
.buffer()
.end(function (err, res) {
expect(res).to.have.header('content-type', "application/javascript");
expect(res.text).to.equal("typeof mycallback === 'function' && mycallback({\"some\":\"content\"});");
done();
});
});
});
describe('captures', function () {
describe('getting the most recent capture', function () {
it('contain request entity itself', function (done) {
var captureId = "mycapture";
vanilli.stub(
vanilli.onPost("/my/url")
.respondWith(dummyStatus)
.capture(captureId)
);
client.post('/my/url')
.send({ some: "content" })
.end(function () {
var capture = vanilli.getCapture(captureId);
expect(capture.body).to.deep.equal({ some: "content" });
expect(capture.contentType).to.deep.equal("application/json");
done();
});
});
it('contain request headers', function (done) {
var captureId = "mycapture";
vanilli.stub(
vanilli.onPost("/my/url")
.respondWith(dummyStatus)
.capture(captureId)
);
client.post('/my/url')
.set('My-Header', "myvalue")
.send("somecontent")
.end(function () {
client.get('/_vanilli/captures/' + captureId)
.end(function () {
var capture = vanilli.getCapture(captureId);
expect(capture.headers["my-header"]).to.equal("myvalue");
done();
});
});
});
it('contain query params', function (done) {
var captureId = "mycapture";
vanilli.stub(
vanilli.onPost("/my/url")
.respondWith(dummyStatus)
.capture(captureId)
);
client.post('/my/url?param1=value1¶m2=value2')
.send("somecontent")
.end(function () {
client.get('/_vanilli/captures/' + captureId)
.end(function () {
var capture = vanilli.getCapture(captureId);
expect(capture.query).to.deep.equal({
param1: "value1",
param2: "value2"
});
done();
});
});
});
});
});
describe('getting all captures for a given capture id', function () {
it('returns an empty array if no captures', function () {
expect(vanilli.getCaptures('foo')).to.eql([]);
});
it('returns an array of captures', function (done) {
var captureId = "mycapture";
vanilli.stub(
vanilli.onPost("/my/url")
.respondWith(dummyStatus, { times: 2 })
.capture(captureId)
);
client.post('/my/url')
.send({ some: "content" })
.end(function () {
client.post('/my/url')
.send({ some: "other content" })
.end(function () {
var captures = vanilli.getCaptures(captureId);
expect(captures).to.have.length(2);
expect(captures[0].body).to.deep.equal({ some: "content" });
expect(captures[1].body).to.deep.equal({ some: "other content" });
done();
});
});
});
});
});