nock 9.0.9 → 9.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.npmignore +2 -0
  2. package/.travis.yml +1 -1
  3. package/CHANGELOG.md +9 -4
  4. package/lib/intercept.js +5 -0
  5. package/lib/match_body.js +1 -1
  6. package/package.json +6 -2
  7. package/.nyc_output/0cfc9aa0c50eae765941a555f8482a68.json +0 -1
  8. package/.nyc_output/0ecdf591be62cb4571ebf9c4df59da37.json +0 -1
  9. package/.nyc_output/1df5c7adcf4885ae588bf8be3e508036.json +0 -1
  10. package/.nyc_output/2db2fc1c81ebebec5066673d7751e720.json +0 -1
  11. package/.nyc_output/2f9ccd224c0ce9793bb47cb95818e613.json +0 -1
  12. package/.nyc_output/302922273ee8b73467d3d8f160d41764.json +0 -1
  13. package/.nyc_output/402abf6bfd9ba80ba17d063c7aff8c5c.json +0 -1
  14. package/.nyc_output/4192a6afdd7ca9d506a9007e60f313c6.json +0 -1
  15. package/.nyc_output/4db6e0367725f3697aff401f14e83d8e.json +0 -1
  16. package/.nyc_output/51727eb8342f2b80aad8f5cf58247bb8.json +0 -1
  17. package/.nyc_output/53edcbd3a01e01726702fd59ad18528c.json +0 -1
  18. package/.nyc_output/58b1c38b2f1be3dcecbca9bab068a18f.json +0 -1
  19. package/.nyc_output/5c23da3e51b1f6cb82b2337ac4b79674.json +0 -1
  20. package/.nyc_output/6e39c409325db21e0a6645f24d62ea78.json +0 -1
  21. package/.nyc_output/71230f985baca9b3c3a5c96f7db17ce3.json +0 -1
  22. package/.nyc_output/75853f8bad86af123b6ab837115f9154.json +0 -1
  23. package/.nyc_output/79b9684388f3f8d578c0960b2cc38567.json +0 -1
  24. package/.nyc_output/83796d88fa4f05b5616674d83b9ea40a.json +0 -1
  25. package/.nyc_output/9d996c278cdb585d083b6f2cd90514a7.json +0 -1
  26. package/.nyc_output/a5f208db2341f743b5cd29d3fb47f97c.json +0 -1
  27. package/.nyc_output/ae6685d46041b4ede6cc2c43ca0e6021.json +0 -1
  28. package/.nyc_output/b0199893e52a27aa0d6c7ffdc52ae9d1.json +0 -1
  29. package/.nyc_output/d5d71e5d97fce722c611ef2e65170947.json +0 -1
  30. package/.nyc_output/d91d008aa4d6431aa91127a791303d24.json +0 -1
  31. package/.nyc_output/deceb761e61a0f642f51a304fae33d8a.json +0 -1
  32. package/.nyc_output/dfceb102965a150e5812732c638882ef.json +0 -1
  33. package/.nyc_output/e0656fe48b57b517e2c979a8a1578639.json +0 -1
  34. package/.nyc_output/e59e4c5a4d398d7ff45be9f48a01a932.json +0 -1
  35. package/.nyc_output/fecdd97de458efb187320ed600a1253c.json +0 -1
  36. package/tests/browserify-public/.gitkeep +0 -0
  37. package/tests/browserify-public/browserify-bundle.js +0 -43055
  38. package/tests/browserify-public/index.html +0 -2
  39. package/tests/fixtures/browserify-script.js +0 -19
  40. package/tests/fixtures/goodRequest.json +0 -26
  41. package/tests/fixtures/wrongUri.json +0 -50
  42. package/tests/test_abort.js +0 -59
  43. package/tests/test_aws_dynamo.js +0 -39
  44. package/tests/test_back.js +0 -341
  45. package/tests/test_back_2.js +0 -75
  46. package/tests/test_basic_auth.js +0 -79
  47. package/tests/test_body_match.js +0 -68
  48. package/tests/test_browserify.js +0 -54
  49. package/tests/test_common.js +0 -242
  50. package/tests/test_complex_querystring.js +0 -158
  51. package/tests/test_content_encoding.js +0 -26
  52. package/tests/test_data.js +0 -28
  53. package/tests/test_dynamic_mock.js +0 -92
  54. package/tests/test_encode_querystring.js +0 -22
  55. package/tests/test_events.js +0 -63
  56. package/tests/test_gzip_request.js +0 -74
  57. package/tests/test_https_allowunmocked.js +0 -25
  58. package/tests/test_intercept.js +0 -5009
  59. package/tests/test_ipv6.js +0 -55
  60. package/tests/test_isomorphic_fetch.js +0 -104
  61. package/tests/test_net_connect.js +0 -17
  62. package/tests/test_nock_off.js +0 -26
  63. package/tests/test_recorder.js +0 -896
  64. package/tests/test_redirects.js +0 -28
  65. package/tests/test_request_promise.js +0 -25
  66. package/tests/test_s3.js +0 -99
  67. package/tests/test_scope_interceptors.js +0 -11
  68. package/tests/test_timeout.js +0 -21
  69. package/tests/test_url_encoding.js +0 -17
@@ -1,5009 +0,0 @@
1
- 'use strict';
2
-
3
- var fs = require('fs');
4
- var nock = require('../.');
5
- var http = require('http');
6
- var https = require('https');
7
- var util = require('util');
8
- var events = require('events');
9
- var stream = require('stream');
10
- var test = require('tap').test;
11
- var mikealRequest = require('request');
12
- var superagent = require('superagent');
13
- var needle = require("needle");
14
- var restify = require('restify');
15
- var domain = require('domain');
16
- var hyperquest = require('hyperquest');
17
- var _ = require('lodash');
18
- var async = require('async');
19
-
20
- var globalCount;
21
-
22
- nock.enableNetConnect();
23
-
24
- globalCount = Object.keys(global).length;
25
- var acceptableLeaks = [
26
- '_key', '__core-js_shared__', 'fetch', 'Response', 'Headers', 'Request'];
27
-
28
- test("double activation throws exception", function(t) {
29
- nock.restore();
30
- t.false(nock.isActive());
31
- try {
32
- nock.activate();
33
- t.true(nock.isActive());
34
- nock.activate();
35
- // This line should never be reached.
36
- t.false(true);
37
- } catch(e) {
38
- t.equal(e.toString(), 'Error: Nock already active');
39
- }
40
- t.true(nock.isActive());
41
- t.end();
42
- });
43
-
44
- test("allow override works (2)", function(t) {
45
- var scope =
46
- nock("https://httpbin.org",{allowUnmocked: true}).
47
- post("/post").
48
- reply(200, "99problems");
49
-
50
- var options = {
51
- method: "POST",
52
- uri: "https://httpbin.org/post",
53
- json: {
54
- some: "data"
55
- }
56
- };
57
-
58
- mikealRequest(options, function(err, resp, body) {
59
- scope.done();
60
- t.end();
61
- return console.log(resp.statusCode, body);
62
- });
63
- });
64
-
65
- test("reply callback's requestBody should automatically parse to JSON", function(t) {
66
- var requestBodyFixture = {
67
- id: 1,
68
- name: 'bob'
69
- };
70
-
71
- var scope = nock('http://service')
72
- .post('/endpoint')
73
- .reply(200, function(uri, requestBody) {
74
- t.deepEqual(requestBody, requestBodyFixture);
75
- requestBody.id = 'overwrite';
76
-
77
- return requestBody;
78
- });
79
-
80
- var options = {
81
- method: "POST",
82
- url: "http://service/endpoint",
83
- body: requestBodyFixture,
84
- json: true
85
- };
86
-
87
- mikealRequest(options, function(err, resp, body) {
88
- scope.done();
89
- t.equal(resp.statusCode, 200);
90
- var expect = _.defaults({
91
- id: 'overwrite'
92
- }, requestBodyFixture);
93
- t.deepEqual(expect, body);
94
- t.end();
95
- });
96
- });
97
-
98
- test("reply can take a callback", function(t) {
99
- var dataCalled = false;
100
-
101
- var scope = nock('http://www.google.com')
102
- .get('/')
103
- .reply(200, function(path, requestBody, callback) {
104
- callback(null, "Hello World!");
105
- });
106
-
107
- var req = http.request({
108
- host: "www.google.com",
109
- path: '/',
110
- port: 80
111
- }, function(res) {
112
-
113
- t.equal(res.statusCode, 200, "Status code is 200");
114
- res.on('end', function() {
115
- t.ok(dataCalled, "data handler was called");
116
- scope.done();
117
- t.end();
118
- });
119
- res.on('data', function(data) {
120
- dataCalled = true;
121
- t.ok(data instanceof Buffer, "data should be buffer");
122
- t.equal(data.toString(), "Hello World!", "response should match");
123
- });
124
-
125
- });
126
-
127
- req.end();
128
- });
129
-
130
- test("reply takes a callback for status code", function(t) {
131
- t.plan(3);
132
-
133
- var statusCode = 202;
134
- var responseBody = "Hello, world!";
135
- var headers = {
136
- 'X-Custom-Header': 'abcdef'
137
- };
138
-
139
- var scope = nock("http://www.google.com")
140
- .get("/test-path/")
141
- .reply(function(path, requestBody, cb) {
142
- setTimeout(function() {
143
- cb(null, [statusCode, responseBody, headers]);
144
- }, 1);
145
- });
146
-
147
- var req = http.request({
148
- host: "www.google.com",
149
- path: "/test-path/",
150
- port: 80
151
- }, function(res) {
152
-
153
- t.equal(res.statusCode, statusCode, "sends status code");
154
- t.deepEqual(res.headers, headers, "sends headers");
155
- res.on('data', function(data) {
156
- t.equal(data.toString(), responseBody, "sends request body");
157
- });
158
- res.on('end', function() {
159
- scope.done();
160
- });
161
-
162
- });
163
-
164
- req.end();
165
- });
166
-
167
- test("reply should throw on error on the callback", function(t) {
168
- var dataCalled = false;
169
-
170
- var scope = nock('http://www.google.com')
171
- .get('/')
172
- .reply(500, function(path, requestBody, callback) {
173
- callback(new Error("Database failed"));
174
- });
175
-
176
- var req = http.request({
177
- host: "www.google.com",
178
- path: '/',
179
- port: 80
180
- }, function(res) {
181
- t.equal(res.statusCode, 500, "Status code is 500");
182
-
183
- res.on('data', function(data) {
184
- dataCalled = true;
185
- t.ok(data instanceof Buffer, "data should be buffer");
186
- t.ok(data.toString().indexOf("Error: Database failed") === 0, "response should match");
187
- });
188
-
189
- res.on('end', function() {
190
- t.ok(dataCalled, "data handler was called");
191
- scope.done();
192
- t.end();
193
- });
194
- });
195
-
196
- req.end();
197
- });
198
-
199
- test("get gets mocked", function(t) {
200
- var dataCalled = false;
201
-
202
- var scope = nock('http://www.google.com')
203
- .get('/')
204
- .reply(200, "Hello World!");
205
-
206
- var req = http.request({
207
- host: "www.google.com",
208
- path: '/',
209
- port: 80
210
- }, function(res) {
211
-
212
- t.equal(res.statusCode, 200, "Status code is 200");
213
- res.on('end', function() {
214
- t.ok(dataCalled, "data handler was called");
215
- scope.done();
216
- t.end();
217
- });
218
- res.on('data', function(data) {
219
- dataCalled = true;
220
- t.ok(data instanceof Buffer, "data should be buffer");
221
- t.equal(data.toString(), "Hello World!", "response should match");
222
- });
223
-
224
- });
225
-
226
- req.end();
227
- });
228
-
229
- test("get gets mocked with relative base path", function(t) {
230
- var dataCalled = false;
231
-
232
- var scope = nock('http://www.google.com/abc')
233
- .get('/def')
234
- .reply(200, "Hello World!");
235
-
236
- var req = http.request({
237
- host: "www.google.com",
238
- path: '/abc/def',
239
- port: 80
240
- }, function(res) {
241
-
242
- t.equal(res.statusCode, 200);
243
- res.on('end', function() {
244
- t.ok(dataCalled);
245
- scope.done();
246
- t.end();
247
- });
248
- res.on('data', function(data) {
249
- dataCalled = true;
250
- t.ok(data instanceof Buffer, "data should be buffer");
251
- t.equal(data.toString(), "Hello World!", "response should match");
252
- });
253
-
254
- });
255
-
256
- req.end();
257
- });
258
-
259
- test("post", function(t) {
260
- var dataCalled = false;
261
-
262
- var scope = nock('http://www.google.com')
263
- .post('/form')
264
- .reply(201, "OK!");
265
-
266
- var req = http.request({
267
- host: "www.google.com",
268
- method: 'POST',
269
- path: '/form',
270
- port: 80
271
- }, function(res) {
272
-
273
- t.equal(res.statusCode, 201);
274
- res.on('end', function() {
275
- t.ok(dataCalled);
276
- scope.done();
277
- t.end();
278
- });
279
- res.on('data', function(data) {
280
- dataCalled = true;
281
- t.ok(data instanceof Buffer, "data should be buffer");
282
- t.equal(data.toString(), "OK!", "response should match");
283
- });
284
-
285
- });
286
-
287
- req.end();
288
- });
289
-
290
- test("post with empty response body", function(t) {
291
- var scope = nock('http://www.google.com')
292
- .post('/form')
293
- .reply(200);
294
-
295
- var req = http.request({
296
- host: "www.google.com",
297
- method: 'POST',
298
- path: '/form',
299
- port: 80
300
- }, function(res) {
301
-
302
- t.equal(res.statusCode, 200);
303
- res.on('end', function() {
304
- scope.done();
305
- t.end();
306
- });
307
- res.on('data', function() {
308
- t.fail("No body should be returned");
309
- });
310
-
311
- });
312
- req.end();
313
- });
314
-
315
- test("post, lowercase", function(t) {
316
- var dataCalled = false;
317
-
318
- var scope = nock('http://www.google.com')
319
- .post('/form')
320
- .reply(200, "OK!");
321
-
322
- var req = http.request({
323
- host: "www.google.com",
324
- method: 'post',
325
- path: '/form',
326
- port: 80
327
- }, function(res) {
328
-
329
- t.equal(res.statusCode, 200);
330
- res.on('end', function() {
331
- t.ok(dataCalled);
332
- scope.done();
333
- t.end();
334
- });
335
- res.on('data', function(data) {
336
- dataCalled = true;
337
- t.ok(data instanceof Buffer, "data should be buffer");
338
- t.equal(data.toString(), "OK!", "response should match");
339
- });
340
-
341
- });
342
-
343
- req.end();
344
- });
345
-
346
- test("get with reply callback", function(t) {
347
- var scope = nock('http://www.google.com')
348
- .get('/')
349
- .reply(200, function() {
350
- return 'OK!';
351
- });
352
-
353
- var req = http.request({
354
- host: "www.google.com",
355
- path: '/',
356
- port: 80
357
- }, function(res) {
358
- res.on('end', function() {
359
- scope.done();
360
- t.end();
361
- });
362
- res.on('data', function(data) {
363
- t.equal(data.toString(), 'OK!', 'response should match');
364
- });
365
- });
366
-
367
- req.end();
368
- });
369
-
370
- test("get to different subdomain with reply callback and filtering scope", function(t) {
371
- // We scope for www.google.com but through scope filtering we
372
- // will accept any <subdomain>.google.com
373
- var scope = nock('http://www.google.com', {
374
- filteringScope: function(scope) {
375
- return /^http:\/\/.*\.google\.com/.test(scope);
376
- }
377
- })
378
- .get('/')
379
- .reply(200, function() {
380
- return 'OK!';
381
- });
382
-
383
- var req = http.request({
384
- host: "any-subdomain-will-do.google.com",
385
- path: '/',
386
- port: 80
387
- }, function(res) {
388
- res.on('end', function() {
389
- scope.done();
390
- t.end();
391
- });
392
- res.on('data', function(data) {
393
- t.equal(data.toString(), 'OK!', 'response should match');
394
- });
395
- });
396
-
397
- req.end();
398
- });
399
-
400
- test("get with reply callback returning object", function(t) {
401
- var scope = nock('http://www.googlezzzz.com')
402
- .get('/')
403
- .reply(200, function() {
404
- return { message: 'OK!' };
405
- });
406
-
407
- var req = http.request({
408
- host: "www.googlezzzz.com",
409
- path: '/',
410
- port: 80
411
- }, function(res) {
412
- res.on('end', function() {
413
- scope.done();
414
- t.end();
415
- });
416
- res.on('data', function(data) {
417
- t.equal(data.toString(), JSON.stringify({ message: 'OK!' }),
418
- 'response should match');
419
- });
420
- });
421
-
422
- req.end();
423
- });
424
-
425
- test("get with reply callback returning array with headers", function(t) {
426
- var scope = nock('http://replyheaderland')
427
- .get('/')
428
- .reply(function() {
429
- return [202, 'body', {'x-key': 'value', 'x-key-2': 'value 2'}];
430
- });
431
-
432
- http.get({
433
- host: "replyheaderland",
434
- path: '/',
435
- port: 80,
436
- }, function(res) {
437
- res.setEncoding('utf8');
438
- t.equal(res.statusCode, 202);
439
- console.log('res.headers:', res.headers);
440
- t.deepEqual(res.headers, {
441
- 'x-key': 'value',
442
- 'x-key-2': 'value 2',
443
- });
444
- t.deepEqual(res.rawHeaders, [
445
- 'x-key', 'value',
446
- 'x-key-2', 'value 2']);
447
- res.on('data', function(data) {
448
- t.equal(data, 'body');
449
- res.once('end', t.end.bind(t));
450
- });
451
- });
452
- });
453
-
454
- test("post with reply callback, uri, and request body", function(t) {
455
- var input = 'key=val';
456
-
457
- var scope = nock('http://www.google.com')
458
- .post('/echo', input)
459
- .reply(200, function(uri, body) {
460
- return ['OK', uri, body].join(' ');
461
- });
462
-
463
- var req = http.request({
464
- host: "www.google.com"
465
- , method: 'POST'
466
- , path: '/echo'
467
- , port: 80
468
- }, function(res) {
469
- res.on('end', function() {
470
- scope.done();
471
- t.end();
472
- });
473
- res.on('data', function(data) {
474
- t.equal(data.toString(), 'OK /echo key=val' , 'response should match');
475
- });
476
- });
477
-
478
- req.write(input);
479
- req.end();
480
- });
481
-
482
- test("post with regexp as spec", function(t) {
483
- var scope = nock('http://www.google.com')
484
- .post('/echo', /key=v.?l/g)
485
- .reply(200, function(uri, body) {
486
- return ['OK', uri, body].join(' ');
487
- });
488
-
489
- var req = http.request({
490
- host: "www.google.com"
491
- , method: 'POST'
492
- , path: '/echo'
493
- , port: 80
494
- }, function(res) {
495
- res.on('end', function() {
496
- scope.done();
497
- t.end();
498
- });
499
- res.on('data', function(data) {
500
- t.equal(data.toString(), 'OK /echo key=val' , 'response should match');
501
- });
502
- });
503
-
504
- req.write('key=val');
505
- req.end();
506
- });
507
-
508
- test("post with function as spec", function(t) {
509
- var scope = nock('http://www.google.com')
510
- .post('/echo', function(body) {
511
- return body === 'key=val';
512
- })
513
- .reply(200, function(uri, body) {
514
- return ['OK', uri, body].join(' ');
515
- });
516
-
517
- var req = http.request({
518
- host: "www.google.com"
519
- , method: 'POST'
520
- , path: '/echo'
521
- , port: 80
522
- }, function(res) {
523
- res.on('end', function() {
524
- scope.done();
525
- t.end();
526
- });
527
- res.on('data', function(data) {
528
- t.equal(data.toString(), 'OK /echo key=val' , 'response should match');
529
- });
530
- });
531
-
532
- req.write('key=val');
533
- req.end();
534
- });
535
-
536
- test("post with chaining on call", function(t) {
537
- var input = 'key=val';
538
-
539
- var scope = nock('http://www.google.com')
540
- .post('/echo', input)
541
- .reply(200, function(uri, body) {
542
- return ['OK', uri, body].join(' ');
543
- });
544
-
545
- var req = http.request({
546
- host: "www.google.com"
547
- , method: 'POST'
548
- , path: '/echo'
549
- , port: 80
550
- }, function(res) {
551
- res.on('end', function() {
552
- scope.done();
553
- t.end();
554
- });
555
- res.on('data', function(data) {
556
- t.equal(data.toString(), 'OK /echo key=val' , 'response should match');
557
- });
558
- }).on('error', function(error){
559
- t.equal(error, null);
560
- t.end();
561
- });
562
- req.end(input);
563
- });
564
-
565
- test("reply with callback and filtered path and body", function(t) {
566
- var noPrematureExecution = false;
567
-
568
- var scope = nock('http://www.realcallback.com')
569
- .filteringPath(/.*/, '*')
570
- .filteringRequestBody(/.*/, '*')
571
- .post('*', '*')
572
- .reply(200, function(uri, body) {
573
- t.assert(noPrematureExecution);
574
- return ['OK', uri, body].join(' ');
575
- });
576
-
577
- var req = http.request({
578
- host: "www.realcallback.com"
579
- , method: 'POST'
580
- , path: '/original/path'
581
- , port: 80
582
- }, function(res) {
583
- t.equal(res.statusCode, 200);
584
- res.on('end', function() {
585
- scope.done();
586
- t.end();
587
- });
588
- res.on('data', function(data) {
589
- t.equal(data.toString(), 'OK /original/path original=body' , 'response should match');
590
- });
591
- });
592
-
593
- noPrematureExecution = true;
594
- req.end('original=body');
595
- });
596
-
597
- test("isDone", function(t) {
598
- var scope = nock('http://www.google.com')
599
- .get('/')
600
- .reply(200, "Hello World!");
601
-
602
- t.notOk(scope.isDone(), "not done when a request is outstanding");
603
-
604
- var req = http.request({
605
- host: "www.google.com"
606
- , path: '/'
607
- , port: 80
608
- }, function(res) {
609
- t.equal(res.statusCode, 200);
610
- res.on('end', function() {
611
- t.ok(scope.isDone(), "done after request is made");
612
- scope.done();
613
- t.end();
614
- });
615
- // Streams start in 'paused' mode and must be started.
616
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
617
- res.resume();
618
- });
619
-
620
- req.end();
621
- });
622
-
623
- test("request headers exposed", function(t) {
624
-
625
- var scope = nock('http://www.headdy.com')
626
- .get('/')
627
- .reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
628
-
629
- var req = http.get({
630
- host: "www.headdy.com"
631
- , method: 'GET'
632
- , path: '/'
633
- , port: 80
634
- , headers: {'X-My-Headers': 'My custom Header value'}
635
- }, function(res) {
636
- res.on('end', function() {
637
- scope.done();
638
- t.end();
639
- });
640
- // Streams start in 'paused' mode and must be started.
641
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
642
- res.resume();
643
- });
644
-
645
- t.equivalent(req._headers, {'x-my-headers': 'My custom Header value', 'host': 'www.headdy.com'});
646
- });
647
-
648
- test("headers work", function(t) {
649
-
650
- var scope = nock('http://www.headdy.com')
651
- .get('/')
652
- .reply(200, "Hello World!", {'X-My-Headers': 'My Header value'});
653
-
654
- var req = http.request({
655
- host: "www.headdy.com"
656
- , method: 'GET'
657
- , path: '/'
658
- , port: 80
659
- }, function(res) {
660
- t.equal(res.statusCode, 200);
661
- res.on('end', function() {
662
- t.equivalent(res.headers, {'x-my-headers': 'My Header value'});
663
- scope.done();
664
- t.end();
665
- });
666
- // Streams start in 'paused' mode and must be started.
667
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
668
- res.resume();
669
- });
670
-
671
- req.end();
672
-
673
- });
674
-
675
- test("reply headers work with function", function(t) {
676
-
677
- var scope = nock('http://replyheadersworkwithfunction.xxx')
678
- .get('/')
679
- .reply(200, function() {
680
- return 'ABC';
681
- }, {'X-My-Headers': 'My custom header value'});
682
-
683
- var req = http.get({
684
- host: "replyheadersworkwithfunction.xxx",
685
- path: '/',
686
- port: 80
687
- }, function(res) {
688
- t.equivalent(res.headers, {'x-my-headers': 'My custom header value'});
689
- scope.done();
690
- t.end();
691
-
692
- });
693
- });
694
-
695
- test("reply headers as function work", function(t) {
696
- var scope = nock('http://example.com')
697
- .get('/')
698
- .reply(200, 'boo!', {
699
- 'X-My-Headers': function (req, res, body) {
700
- return body.toString();
701
- }
702
- });
703
-
704
- var req = http.get({
705
- host: 'example.com',
706
- path: '/'
707
- }, function (res) {
708
- t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
709
- t.equivalent(res.rawHeaders, ['X-My-Headers', 'boo!']); // 67
710
- t.end();
711
- });
712
- });
713
-
714
- test("reply headers as function are evaluated only once per request", function(t) {
715
- var counter = 0;
716
- var scope = nock('http://example.com')
717
- .get('/')
718
- .reply(200, 'boo!', {
719
- 'X-My-Headers': function (req, res, body) {
720
- ++counter;
721
- return body.toString();
722
- }
723
- });
724
-
725
- var req = http.get({
726
- host: 'example.com',
727
- path: '/'
728
- }, function (res) {
729
- t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
730
- t.equivalent(res.rawHeaders, ['X-My-Headers', 'boo!']);
731
- t.equal(counter, 1);
732
- t.end();
733
- });
734
- });
735
-
736
- test("reply headers as function are evaluated on each request", function(t) {
737
- var counter = 0;
738
- var scope = nock('http://example.com')
739
- .get('/')
740
- .times(2)
741
- .reply(200, 'boo!', {
742
- 'X-My-Headers': function (req, res, body) {
743
- return ++counter + '';
744
- }
745
- });
746
-
747
- var req = http.get({
748
- host: 'example.com',
749
- path: '/'
750
- }, function (res) {
751
- t.equivalent(res.headers, { 'x-my-headers': '1' });
752
- t.equivalent(res.rawHeaders, ['X-My-Headers', '1']);
753
- t.equal(counter, 1);
754
- http.get({
755
- host: 'example.com',
756
- path: '/'
757
- }, function (res) {
758
- t.equivalent(res.headers, { 'x-my-headers': '2' });
759
- t.equivalent(res.rawHeaders, ['X-My-Headers', '2']);
760
- t.equal(counter, 2);
761
- t.end();
762
- });
763
- });
764
- });
765
-
766
- test("match headers", function(t) {
767
- var scope = nock('http://www.headdy.com')
768
- .get('/')
769
- .matchHeader('x-my-headers', 'My custom Header value')
770
- .reply(200, "Hello World!");
771
-
772
- http.get({
773
- host: "www.headdy.com"
774
- , method: 'GET'
775
- , path: '/'
776
- , port: 80
777
- , headers: {'X-My-Headers': 'My custom Header value'}
778
- }, function(res) {
779
- res.setEncoding('utf8');
780
- t.equal(res.statusCode, 200);
781
-
782
- res.on('data', function(data) {
783
- t.equal(data, 'Hello World!');
784
- });
785
-
786
- res.on('end', function() {
787
- scope.done();
788
- t.end();
789
- });
790
- });
791
-
792
- });
793
-
794
- test("multiple match headers", function(t) {
795
- var scope = nock('http://www.headdy.com')
796
- .get('/')
797
- .matchHeader('x-my-headers', 'My custom Header value')
798
- .reply(200, "Hello World!")
799
- .get('/')
800
- .matchHeader('x-my-headers', 'other value')
801
- .reply(200, "Hello World other value!");
802
-
803
- http.get({
804
- host: "www.headdy.com"
805
- , method: 'GET'
806
- , path: '/'
807
- , port: 80
808
- , headers: {'X-My-Headers': 'other value'}
809
- }, function(res) {
810
- res.setEncoding('utf8');
811
- t.equal(res.statusCode, 200);
812
-
813
- res.on('data', function(data) {
814
- t.equal(data, 'Hello World other value!');
815
- });
816
-
817
- res.on('end', function() {
818
- http.get({
819
- host: "www.headdy.com"
820
- , method: 'GET'
821
- , path: '/'
822
- , port: 80
823
- , headers: {'X-My-Headers': 'My custom Header value'}
824
- }, function(res) {
825
- res.setEncoding('utf8');
826
- t.equal(res.statusCode, 200);
827
-
828
- res.on('data', function(data) {
829
- t.equal(data, 'Hello World!');
830
- });
831
-
832
- res.on('end', function() {
833
- scope.done();
834
- t.end();
835
- });
836
- });
837
- });
838
- });
839
-
840
- });
841
-
842
- test("match headers with regexp", function(t) {
843
- var scope = nock('http://www.headier.com')
844
- .get('/')
845
- .matchHeader('x-my-headers', /My He.d.r [0-9.]+/)
846
- .reply(200, "Hello World!");
847
-
848
- http.get({
849
- host: "www.headier.com"
850
- , method: 'GET'
851
- , path: '/'
852
- , port: 80
853
- , headers: {'X-My-Headers': 'My Header 1.0'}
854
- }, function(res) {
855
- res.setEncoding('utf8');
856
- t.equal(res.statusCode, 200);
857
-
858
- res.on('data', function(data) {
859
- t.equal(data, 'Hello World!');
860
- });
861
-
862
- res.on('end', function() {
863
- scope.done();
864
- t.end();
865
- });
866
- });
867
-
868
- });
869
-
870
- test("match headers on number with regexp", function(t) {
871
- var scope = nock('http://www.headier.com')
872
- .get('/')
873
- .matchHeader('x-my-headers', /\d+/)
874
- .reply(200, "Hello World!");
875
-
876
- http.get({
877
- host: "www.headier.com"
878
- , method: 'GET'
879
- , path: '/'
880
- , port: 80
881
- , headers: {'X-My-Headers': 123}
882
- }, function(res) {
883
- res.setEncoding('utf8');
884
- t.equal(res.statusCode, 200);
885
-
886
- res.on('data', function(data) {
887
- t.equal(data, 'Hello World!');
888
- });
889
-
890
- res.on('end', function() {
891
- scope.done();
892
- t.end();
893
- });
894
- });
895
-
896
- });
897
-
898
- test("match headers with function", function(t) {
899
- var scope = nock('http://www.headier.com')
900
- .get('/')
901
- .matchHeader('x-my-headers', function (val) {
902
- return val > 123;
903
- })
904
- .reply(200, "Hello World!");
905
-
906
- http.get({
907
- host: "www.headier.com"
908
- , method: 'GET'
909
- , path: '/'
910
- , port: 80
911
- , headers: {'X-My-Headers': 456}
912
- }, function(res) {
913
- res.setEncoding('utf8');
914
- t.equal(res.statusCode, 200);
915
-
916
- res.on('data', function(data) {
917
- t.equal(data, 'Hello World!');
918
- });
919
-
920
- res.on('end', function() {
921
- scope.done();
922
- t.end();
923
- });
924
- });
925
-
926
- });
927
-
928
- test("match all headers", function(t) {
929
- var scope = nock('http://api.headdy.com')
930
- .matchHeader('accept', 'application/json')
931
- .get('/one')
932
- .reply(200, { hello: "world" })
933
- .get('/two')
934
- .reply(200, { a: 1, b: 2, c: 3 });
935
-
936
- var ended = 0;
937
- function callback() {
938
- ended += 1;
939
- if (ended === 2) {
940
- scope.done();
941
- t.end();
942
- }
943
- }
944
-
945
- http.get({
946
- host: "api.headdy.com"
947
- , path: '/one'
948
- , port: 80
949
- , headers: {'Accept': 'application/json'}
950
- }, function(res) {
951
- res.setEncoding('utf8');
952
- t.equal(res.statusCode, 200);
953
-
954
- res.on('data', function(data) {
955
- t.equal(data, '{"hello":"world"}');
956
- });
957
-
958
- res.on('end', callback);
959
- });
960
-
961
- http.get({
962
- host: "api.headdy.com"
963
- , path: '/two'
964
- , port: 80
965
- , headers: {'accept': 'application/json'}
966
- }, function(res) {
967
- res.setEncoding('utf8');
968
- t.equal(res.statusCode, 200);
969
-
970
- res.on('data', function(data) {
971
- t.equal(data, '{"a":1,"b":2,"c":3}');
972
- });
973
-
974
- res.on('end', callback);
975
- });
976
-
977
- });
978
-
979
- test("header manipulation", function(t) {
980
- var scope = nock('http://example.com')
981
- .get('/accounts')
982
- .reply(200, { accounts: [{ id: 1, name: 'Joe Blow' }] })
983
- , req;
984
-
985
- req = http.get({ host: 'example.com', path: '/accounts' }, function (res) {
986
- res.on('end', function () {
987
- scope.done();
988
- t.end();
989
- });
990
- // Streams start in 'paused' mode and must be started.
991
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
992
- res.resume();
993
- });
994
-
995
- req.setHeader('X-Custom-Header', 'My Value');
996
- t.equal(req.getHeader('X-Custom-Header'), 'My Value', 'Custom header was not set');
997
-
998
- req.removeHeader('X-Custom-Header');
999
- t.notOk(req.getHeader('X-Custom-Header'), 'Custom header was not removed');
1000
-
1001
- req.end();
1002
- });
1003
-
1004
- test("head", function(t) {
1005
- var dataCalled = false;
1006
-
1007
- var scope = nock('http://www.google.com')
1008
- .head('/form')
1009
- .reply(201, "OK!");
1010
-
1011
- var req = http.request({
1012
- host: "www.google.com"
1013
- , method: 'HEAD'
1014
- , path: '/form'
1015
- , port: 80
1016
- }, function(res) {
1017
-
1018
- t.equal(res.statusCode, 201);
1019
- res.on('end', function() {
1020
- scope.done();
1021
- t.end();
1022
- });
1023
- // Streams start in 'paused' mode and must be started.
1024
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1025
- res.resume();
1026
- });
1027
-
1028
- req.end();
1029
- });
1030
-
1031
- test("body data is differentiating", function(t) {
1032
- var doneCount = 0
1033
- , scope = nock('http://www.boddydiff.com')
1034
- .post('/', 'abc')
1035
- .reply(200, "Hey 1")
1036
- .post('/', 'def')
1037
- .reply(200, "Hey 2");
1038
-
1039
- function done(t) {
1040
- doneCount += 1;
1041
- t.end();
1042
- };
1043
-
1044
-
1045
- t.test("A", function(t) {
1046
- var req = http.request({
1047
- host: "www.boddydiff.com"
1048
- , method: 'POST'
1049
- , path: '/'
1050
- , port: 80
1051
- }, function(res) {
1052
- var dataCalled = false;
1053
- t.equal(res.statusCode, 200);
1054
- res.on('end', function() {
1055
- t.ok(dataCalled);
1056
- done(t);
1057
- });
1058
- res.on('data', function(data) {
1059
- dataCalled = true;
1060
- t.ok(data instanceof Buffer, "data should be buffer");
1061
- t.equal(data.toString(), "Hey 1", "response should match");
1062
- });
1063
- });
1064
-
1065
- req.end('abc');
1066
- });
1067
-
1068
- t.test("B", function(t) {
1069
- var req = http.request({
1070
- host: "www.boddydiff.com"
1071
- , method: 'POST'
1072
- , path: '/'
1073
- , port: 80
1074
- }, function(res) {
1075
- var dataCalled = false;
1076
- t.equal(res.statusCode, 200);
1077
- res.on('end', function() {
1078
- t.ok(dataCalled);
1079
- done(t);
1080
- });
1081
- res.on('data', function(data) {
1082
- dataCalled = true;
1083
- t.ok(data instanceof Buffer, "data should be buffer");
1084
- t.equal(data.toString(), "Hey 2", "response should match");
1085
- });
1086
- });
1087
-
1088
- req.end('def');
1089
- });
1090
-
1091
- t.end();
1092
-
1093
- });
1094
-
1095
- test("chaining", function(t) {
1096
- var repliedCount = 0;
1097
- var scope = nock('http://www.spiffy.com')
1098
- .get('/')
1099
- .reply(200, "Hello World!")
1100
- .post('/form')
1101
- .reply(201, "OK!");
1102
-
1103
- t.tearDown(scope.done.bind(scope));
1104
-
1105
- t.test("post", function(t) {
1106
- var req = http.request({
1107
- host: "www.spiffy.com"
1108
- , method: 'POST'
1109
- , path: '/form'
1110
- , port: 80
1111
- }, function(res) {
1112
-
1113
- t.equal(res.statusCode, 201);
1114
- res.once('data', function(data) {
1115
- t.ok(data instanceof Buffer, "data should be buffer");
1116
- t.equal(data.toString(), "OK!", "response should match");
1117
-
1118
- res.once('end', t.end.bind(t));
1119
- });
1120
-
1121
- });
1122
-
1123
- req.end();
1124
- });
1125
-
1126
- t.test("get", function(t) {
1127
- var req = http.request({
1128
- host: "www.spiffy.com"
1129
- , method: 'GET'
1130
- , path: '/'
1131
- , port: 80
1132
- }, function(res) {
1133
-
1134
- t.equal(res.statusCode, 200);
1135
- res.once('data', function(data) {
1136
- t.ok(data instanceof Buffer, "data should be buffer");
1137
- t.equal(data.toString(), "Hello World!", "response should match");
1138
-
1139
- res.once('end', t.end.bind(t));
1140
- });
1141
-
1142
- });
1143
-
1144
- req.end();
1145
- });
1146
-
1147
- t.end();
1148
- });
1149
-
1150
- test("encoding", function(t) {
1151
- var dataCalled = false
1152
-
1153
- var scope = nock('http://www.encoderz.com')
1154
- .get('/')
1155
- .reply(200, "Hello World!");
1156
-
1157
- var req = http.request({
1158
- host: "www.encoderz.com"
1159
- , path: '/'
1160
- , port: 80
1161
- }, function(res) {
1162
-
1163
- res.setEncoding('base64');
1164
-
1165
- t.equal(res.statusCode, 200);
1166
- res.on('end', function() {
1167
- t.ok(dataCalled);
1168
- scope.done();
1169
- t.end();
1170
- });
1171
- res.on('data', function(data) {
1172
- dataCalled = true;
1173
- t.type(data, 'string', "data should be string");
1174
- t.equal(data, "SGVsbG8gV29ybGQh", "response should match base64 encoding");
1175
- });
1176
-
1177
- });
1178
-
1179
- req.end();
1180
- });
1181
-
1182
- test("reply with file", function(t) {
1183
- var dataCalled = false
1184
-
1185
- var scope = nock('http://www.filereplier.com')
1186
- .get('/')
1187
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt')
1188
- .get('/test')
1189
- .reply(200, 'Yay!');
1190
-
1191
- var req = http.request({
1192
- host: "www.filereplier.com"
1193
- , path: '/'
1194
- , port: 80
1195
- }, function(res) {
1196
-
1197
- t.equal(res.statusCode, 200);
1198
- res.on('end', function() {
1199
- t.ok(dataCalled);
1200
- t.end();
1201
- });
1202
- res.on('data', function(data) {
1203
- dataCalled = true;
1204
- t.equal(data.toString(), "Hello from the file!", "response should match");
1205
- });
1206
-
1207
- });
1208
-
1209
- req.end();
1210
-
1211
- });
1212
-
1213
- test("reply with file and pipe response", function(t) {
1214
- var scope = nock('http://www.files.com')
1215
- .get('/')
1216
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt')
1217
-
1218
- var req = http.get({
1219
- host: "www.files.com"
1220
- , path: '/'
1221
- , port: 80
1222
- }, function(res) {
1223
- var str = '';
1224
- var fakeStream = new(require('stream').Stream);
1225
- fakeStream.writable = true;
1226
-
1227
- fakeStream.write = function(d) {
1228
- str += d;
1229
- };
1230
-
1231
- fakeStream.end = function() {
1232
- t.equal(str, "Hello from the file!", "response should match");
1233
- t.end();
1234
- };
1235
-
1236
- res.pipe(fakeStream);
1237
- res.setEncoding('utf8');
1238
- t.equal(res.statusCode, 200);
1239
-
1240
- });
1241
-
1242
- });
1243
-
1244
- test("reply with file with headers", function(t) {
1245
- var dataCalled = false
1246
-
1247
- var scope = nock('http://www.filereplier2.com')
1248
- .get('/')
1249
- .replyWithFile(200, __dirname + '/../assets/reply_file_2.txt.gz', {
1250
- 'content-encoding': 'gzip'
1251
- });
1252
-
1253
- var req = http.request({
1254
- host: "www.filereplier2.com"
1255
- , path: '/'
1256
- , port: 80
1257
- }, function(res) {
1258
-
1259
- t.equal(res.statusCode, 200);
1260
- res.on('end', function() {
1261
- t.ok(dataCalled);
1262
- t.end();
1263
- });
1264
- res.on('data', function(data) {
1265
- dataCalled = true;
1266
- t.equal(data.length, 57);
1267
- });
1268
-
1269
- });
1270
-
1271
- req.end();
1272
-
1273
- });
1274
-
1275
- test("reply with file with mikeal/request", function(t) {
1276
- var scope = nock('http://www.files.com')
1277
- .get('/')
1278
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt')
1279
-
1280
- var options = { uri: 'http://www.files.com/', onResponse: true };
1281
- mikealRequest('http://www.files.com/', function(err, res, body) {
1282
- if (err) {
1283
- throw err;
1284
- }
1285
-
1286
- res.setEncoding('utf8');
1287
- t.equal(res.statusCode, 200);
1288
-
1289
- t.equal(body, "Hello from the file!", "response should match");
1290
- t.end();
1291
- });
1292
-
1293
- });
1294
-
1295
- test("reply with JSON", function(t) {
1296
- var dataCalled = false
1297
-
1298
- var scope = nock('http://www.jsonreplier.com')
1299
- .get('/')
1300
- .reply(200, {hello: "world"});
1301
-
1302
- var req = http.request({
1303
- host: "www.jsonreplier.com"
1304
- , path: '/'
1305
- , port: 80
1306
- }, function(res) {
1307
-
1308
- res.setEncoding('utf8');
1309
- t.equal(res.statusCode, 200);
1310
- t.notOk(res.headers['date']);
1311
- t.notOk(res.headers['content-length']);
1312
- t.equal(res.headers['content-type'], 'application/json');
1313
- res.on('end', function() {
1314
- t.ok(dataCalled);
1315
- scope.done();
1316
- t.end();
1317
- });
1318
- res.on('data', function(data) {
1319
- dataCalled = true;
1320
- t.equal(data.toString(), '{"hello":"world"}', "response should match");
1321
- });
1322
-
1323
- });
1324
-
1325
- req.end();
1326
-
1327
- });
1328
-
1329
- test("reply with content-length header", function(t){
1330
- var scope = nock('http://www.jsonreplier.com')
1331
- .replyContentLength()
1332
- .get('/')
1333
- .reply(200, {hello: "world"});
1334
-
1335
- var req = http.get({
1336
- host: "www.jsonreplier.com"
1337
- , path: '/'
1338
- , port: 80
1339
- }, function(res) {
1340
- t.equal(res.headers['content-length'], 17);
1341
- res.on('end', function() {
1342
- scope.done();
1343
- t.end();
1344
- });
1345
- // Streams start in 'paused' mode and must be started.
1346
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1347
- res.resume();
1348
- });
1349
- });
1350
-
1351
- test("reply with date header", function(t){
1352
- var date = new Date();
1353
-
1354
- var scope = nock('http://www.jsonreplier.com')
1355
- .replyDate(date)
1356
- .get('/')
1357
- .reply(200, {hello: "world"});
1358
-
1359
- var req = http.get({
1360
- host: "www.jsonreplier.com"
1361
- , path: '/'
1362
- , port: 80
1363
- }, function(res) {
1364
- console.error(res.headers);
1365
- t.equal(res.headers['date'], date.toUTCString());
1366
- res.on('end', function() {
1367
- scope.done();
1368
- t.end();
1369
- });
1370
- // Streams start in 'paused' mode and must be started.
1371
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1372
- res.resume();
1373
- });
1374
- });
1375
-
1376
- test("filter path with function", function(t) {
1377
- var scope = nock('http://www.filterurls.com')
1378
- .filteringPath(function(path) {
1379
- return '/?a=2&b=1';
1380
- })
1381
- .get('/?a=2&b=1')
1382
- .reply(200, "Hello World!");
1383
-
1384
- var req = http.request({
1385
- host: "www.filterurls.com"
1386
- , method: 'GET'
1387
- , path: '/?a=1&b=2'
1388
- , port: 80
1389
- }, function(res) {
1390
- t.equal(res.statusCode, 200);
1391
- res.on('end', function() {
1392
- scope.done();
1393
- t.end();
1394
- });
1395
- // Streams start in 'paused' mode and must be started.
1396
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1397
- res.resume();
1398
- });
1399
-
1400
- req.end();
1401
- });
1402
-
1403
- test("filter path with regexp", function(t) {
1404
- var scope = nock('http://www.filterurlswithregexp.com')
1405
- .filteringPath(/\d/g, '3')
1406
- .get('/?a=3&b=3')
1407
- .reply(200, "Hello World!");
1408
-
1409
- var req = http.request({
1410
- host: "www.filterurlswithregexp.com"
1411
- , method: 'GET'
1412
- , path: '/?a=1&b=2'
1413
- , port: 80
1414
- }, function(res) {
1415
- t.equal(res.statusCode, 200);
1416
- res.on('end', function() {
1417
- scope.done();
1418
- t.end();
1419
- });
1420
- // Streams start in 'paused' mode and must be started.
1421
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1422
- res.resume();
1423
- });
1424
-
1425
- req.end();
1426
- });
1427
-
1428
- test("filter body with function", function(t) {
1429
- var filteringRequestBodyCounter = 0;
1430
-
1431
- var scope = nock('http://www.filterboddiez.com')
1432
- .filteringRequestBody(function(body) {
1433
- ++filteringRequestBodyCounter;
1434
- t.equal(body, 'mamma mia');
1435
- return 'mamma tua';
1436
- })
1437
- .post('/', 'mamma tua')
1438
- .reply(200, "Hello World!");
1439
-
1440
- var req = http.request({
1441
- host: "www.filterboddiez.com"
1442
- , method: 'POST'
1443
- , path: '/'
1444
- , port: 80
1445
- }, function(res) {
1446
- t.equal(res.statusCode, 200);
1447
- res.on('end', function() {
1448
- scope.done();
1449
- t.equal(filteringRequestBodyCounter, 1);
1450
- t.end();
1451
- });
1452
- // Streams start in 'paused' mode and must be started.
1453
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1454
- res.resume();
1455
- });
1456
-
1457
- req.end('mamma mia');
1458
- });
1459
-
1460
- test("filter body with regexp", function(t) {
1461
- var scope = nock('http://www.filterboddiezregexp.com')
1462
- .filteringRequestBody(/mia/, 'nostra')
1463
- .post('/', 'mamma nostra')
1464
- .reply(200, "Hello World!");
1465
-
1466
- var req = http.request({
1467
- host: "www.filterboddiezregexp.com"
1468
- , method: 'POST'
1469
- , path: '/'
1470
- , port: 80
1471
- }, function(res) {
1472
- t.equal(res.statusCode, 200);
1473
- res.on('end', function() {
1474
- scope.done();
1475
- t.end();
1476
- });
1477
- // Streams start in 'paused' mode and must be started.
1478
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1479
- res.resume();
1480
- });
1481
-
1482
- req.end('mamma mia');
1483
- });
1484
-
1485
- test("abort request", function(t) {
1486
- var scope = nock('http://www.google.com')
1487
- .get('/hey')
1488
- .reply(200, 'nobody');
1489
-
1490
- var req = http.request({
1491
- host: 'www.google.com'
1492
- , path: '/hey'
1493
- });
1494
-
1495
- req.on('response', function(res) {
1496
- res.on('close', function(err) {
1497
- t.equal(err.code, 'aborted');
1498
- scope.done();
1499
- });
1500
-
1501
- res.on('end', function() {
1502
- t.true(false, 'this should never execute');
1503
- });
1504
-
1505
- req.once('error', function(err) {
1506
- t.equal(err.code, 'ECONNRESET');
1507
- t.end();
1508
- });
1509
-
1510
- req.abort();
1511
- });
1512
-
1513
- req.end();
1514
- });
1515
-
1516
- test("pause response before data", function(t) {
1517
- var scope = nock('http://www.mouse.com')
1518
- .get('/pauser')
1519
- .reply(200, 'nobody');
1520
-
1521
- var req = http.request({
1522
- host: 'www.mouse.com'
1523
- , path: '/pauser'
1524
- });
1525
-
1526
- req.on('response', function(res) {
1527
- res.pause();
1528
-
1529
- var waited = false;
1530
- setTimeout(function() {
1531
- waited = true;
1532
- res.resume();
1533
- }, 500);
1534
-
1535
- res.on('data', function(data) {
1536
- t.true(waited);
1537
- });
1538
-
1539
- res.on('end', function() {
1540
- scope.done();
1541
- t.end();
1542
- });
1543
- });
1544
-
1545
- req.end();
1546
- });
1547
-
1548
- test("pause response after data", function(t) {
1549
- var response = new stream.PassThrough();
1550
- var scope = nock('http://pauseme.com')
1551
- .get('/')
1552
- // Node does not pause the 'end' event so we need to use a stream to simulate
1553
- // multiple 'data' events.
1554
- .reply(200, response);
1555
-
1556
- var req = http.get({
1557
- host: 'pauseme.com'
1558
- , path: '/'
1559
- }, function(res) {
1560
- var waited = false;
1561
- setTimeout(function() {
1562
- waited = true;
1563
- res.resume();
1564
- }, 500);
1565
-
1566
- res.on('data', function(data) {
1567
- res.pause();
1568
- });
1569
-
1570
- res.on('end', function() {
1571
- t.true(waited);
1572
- scope.done();
1573
- t.end();
1574
- });
1575
- });
1576
-
1577
- // Manually simulate multiple 'data' events.
1578
- response.emit("data", "one");
1579
- setTimeout(function () {
1580
- response.emit("data", "two");
1581
- response.end();
1582
- }, 0);
1583
- });
1584
-
1585
- test("response pipe", function(t) {
1586
- var dest = (function() {
1587
- function Constructor() {
1588
- events.EventEmitter.call(this);
1589
-
1590
- this.buffer = new Buffer(0);
1591
- this.writable = true;
1592
- }
1593
-
1594
- util.inherits(Constructor, events.EventEmitter);
1595
-
1596
- Constructor.prototype.end = function() {
1597
- this.emit('end');
1598
- };
1599
-
1600
- Constructor.prototype.write = function(chunk) {
1601
- var buf = new Buffer(this.buffer.length + chunk.length);
1602
-
1603
- this.buffer.copy(buf);
1604
- chunk.copy(buf, this.buffer.length);
1605
-
1606
- this.buffer = buf;
1607
-
1608
- return true;
1609
- };
1610
-
1611
- return new Constructor();
1612
- })();
1613
-
1614
- var scope = nock('http://pauseme.com')
1615
- .get('/')
1616
- .reply(200, 'nobody');
1617
-
1618
- var req = http.get({
1619
- host: 'pauseme.com'
1620
- , path: '/'
1621
- }, function(res) {
1622
- dest.on('pipe', function() {
1623
- t.pass('should emit "pipe" event')
1624
- });
1625
-
1626
- dest.on('end', function() {
1627
- scope.done();
1628
- t.equal(dest.buffer.toString(), 'nobody');
1629
- t.end();
1630
- });
1631
-
1632
- res.pipe(dest);
1633
- });
1634
- });
1635
-
1636
- test("response pipe without implicit end", function(t) {
1637
- var dest = (function() {
1638
- function Constructor() {
1639
- events.EventEmitter.call(this);
1640
-
1641
- this.buffer = new Buffer(0);
1642
- this.writable = true;
1643
- }
1644
-
1645
- util.inherits(Constructor, events.EventEmitter);
1646
-
1647
- Constructor.prototype.end = function() {
1648
- this.emit('end');
1649
- };
1650
-
1651
- Constructor.prototype.write = function(chunk) {
1652
- var buf = new Buffer(this.buffer.length + chunk.length);
1653
-
1654
- this.buffer.copy(buf);
1655
- chunk.copy(buf, this.buffer.length);
1656
-
1657
- this.buffer = buf;
1658
-
1659
- return true;
1660
- };
1661
-
1662
- return new Constructor();
1663
- })();
1664
-
1665
- var scope = nock('http://pauseme.com')
1666
- .get('/')
1667
- .reply(200, 'nobody');
1668
-
1669
- var req = http.get({
1670
- host: 'pauseme.com'
1671
- , path: '/'
1672
- }, function(res) {
1673
- dest.on('end', function() {
1674
- t.fail('should not call end implicitly');
1675
- });
1676
-
1677
- res.on('end', function() {
1678
- scope.done();
1679
- t.pass('should emit end event');
1680
- t.end();
1681
- });
1682
-
1683
- res.pipe(dest, {end: false});
1684
- });
1685
- });
1686
-
1687
- test("chaining API", function(t) {
1688
- var scope = nock('http://chainchomp.com')
1689
- .get('/one')
1690
- .reply(200, 'first one')
1691
- .get('/two')
1692
- .reply(200, 'second one');
1693
-
1694
- http.get({
1695
- host: 'chainchomp.com'
1696
- , path: '/one'
1697
- }, function(res) {
1698
- res.setEncoding('utf8');
1699
- t.equal(res.statusCode, 200, 'status should be ok');
1700
- res.on('data', function(data) {
1701
- t.equal(data, 'first one', 'should be equal to first reply');
1702
- });
1703
-
1704
- res.on('end', function() {
1705
-
1706
- http.get({
1707
- host: 'chainchomp.com'
1708
- , path: '/two'
1709
- }, function(res) {
1710
- res.setEncoding('utf8');
1711
- t.equal(res.statusCode, 200, 'status should be ok');
1712
- res.on('data', function(data) {
1713
- t.equal(data, 'second one', 'should be qual to second reply');
1714
- });
1715
-
1716
- res.on('end', function() {
1717
- scope.done();
1718
- t.end();
1719
- });
1720
- });
1721
-
1722
- });
1723
- });
1724
- });
1725
-
1726
- test("same URI", function(t) {
1727
- var scope = nock('http://sameurii.com')
1728
- .get('/abc')
1729
- .reply(200, 'first one')
1730
- .get('/abc')
1731
- .reply(200, 'second one');
1732
-
1733
- http.get({
1734
- host: 'sameurii.com'
1735
- , path: '/abc'
1736
- }, function(res) {
1737
- res.on('data', function(data) {
1738
- res.setEncoding('utf8');
1739
- t.equal(data.toString(), 'first one', 'should be qual to first reply');
1740
- res.on('end', function() {
1741
- http.get({
1742
- host: 'sameurii.com'
1743
- , path: '/abc'
1744
- }, function(res) {
1745
- res.setEncoding('utf8');
1746
- res.on('data', function(data) {
1747
- t.equal(data.toString(), 'second one', 'should be qual to second reply');
1748
- res.on('end', function() {
1749
- scope.done();
1750
- t.end();
1751
- });
1752
- });
1753
- });
1754
- });
1755
- });
1756
- });
1757
- });
1758
-
1759
- test("can use hostname instead of host", function(t) {
1760
- var scope = nock('http://www.google.com')
1761
- .get('/')
1762
- .reply(200, "Hello World!");
1763
-
1764
- var req = http.request({
1765
- hostname: "www.google.com"
1766
- , path: '/'
1767
- }, function(res) {
1768
-
1769
- t.equal(res.statusCode, 200);
1770
- res.on('end', function() {
1771
- scope.done();
1772
- t.end();
1773
- });
1774
- // Streams start in 'paused' mode and must be started.
1775
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1776
- res.resume();
1777
- });
1778
-
1779
- req.end();
1780
- });
1781
-
1782
- test('hostname is case insensitive', function(t) {
1783
- var scope = nock('http://caseinsensitive.com')
1784
- .get('/path')
1785
- .reply(200, "hey");
1786
-
1787
- var options = {
1788
- hostname: 'cASEinsensitivE.com',
1789
- path: '/path',
1790
- method: 'GET'
1791
- };
1792
-
1793
- var req = http.request(options, function(res) {
1794
- scope.done();
1795
- t.end();
1796
- });
1797
-
1798
- req.end();
1799
- });
1800
-
1801
- test("can take a port", function(t) {
1802
- var scope = nock('http://www.myserver.com:3333')
1803
- .get('/')
1804
- .reply(200, "Hello World!");
1805
-
1806
- var req = http.request({
1807
- hostname: "www.myserver.com"
1808
- , path: '/'
1809
- , port: 3333
1810
- }, function(res) {
1811
-
1812
- t.equal(res.statusCode, 200);
1813
- res.on('end', function() {
1814
- scope.done();
1815
- t.end();
1816
- });
1817
- // Streams start in 'paused' mode and must be started.
1818
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
1819
- res.resume();
1820
- });
1821
-
1822
- req.end();
1823
- });
1824
-
1825
- test("can use https", function(t) {
1826
- var dataCalled = false
1827
-
1828
- var scope = nock('https://google.com')
1829
- .get('/')
1830
- .reply(200, "Hello World!");
1831
-
1832
- var req = https.request({
1833
- host: "google.com"
1834
- , path: '/'
1835
- }, function(res) {
1836
- t.equal(res.statusCode, 200);
1837
- res.on('end', function() {
1838
- t.ok(dataCalled, 'data event called');
1839
- scope.done();
1840
- t.end();
1841
- });
1842
- res.on('data', function(data) {
1843
- dataCalled = true;
1844
- t.ok(data instanceof Buffer, "data should be buffer");
1845
- t.equal(data.toString(), "Hello World!", "response should match");
1846
- });
1847
- });
1848
-
1849
- req.end();
1850
- });
1851
-
1852
- test("emits error if https route is missing", function(t) {
1853
- var dataCalled = false
1854
-
1855
- var scope = nock('https://google.com')
1856
- .get('/')
1857
- .reply(200, "Hello World!");
1858
-
1859
- var req = https.request({
1860
- host: "google.com"
1861
- , path: '/abcdef892932'
1862
- }, function(res) {
1863
- throw new Error('should not come here!');
1864
- });
1865
-
1866
- req.end();
1867
-
1868
- // This listener is intentionally after the end call so make sure that
1869
- // listeners added after the end will catch the error
1870
- req.on('error', function (err) {
1871
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com/abcdef892932"}, null, 2));
1872
- t.end();
1873
- });
1874
- });
1875
-
1876
- test("emits error if https route is missing", function(t) {
1877
- var dataCalled = false
1878
-
1879
- var scope = nock('https://google.com:123')
1880
- .get('/')
1881
- .reply(200, "Hello World!");
1882
-
1883
- var req = https.request({
1884
- host: "google.com",
1885
- port: 123,
1886
- path: '/dsadsads'
1887
- }, function(res) {
1888
- throw new Error('should not come here!');
1889
- });
1890
-
1891
- req.end();
1892
-
1893
- // This listener is intentionally after the end call so make sure that
1894
- // listeners added after the end will catch the error
1895
- req.on('error', function (err) {
1896
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://google.com:123/dsadsads"}, null, 2));
1897
- t.end();
1898
- });
1899
- });
1900
-
1901
- test("can use ClientRequest using GET", function(t) {
1902
-
1903
- var dataCalled = false
1904
-
1905
- var scope = nock('http://www2.clientrequester.com')
1906
- .get('/dsad')
1907
- .reply(202, "HEHE!");
1908
-
1909
- var req = new http.ClientRequest({
1910
- host: "www2.clientrequester.com"
1911
- , path: '/dsad'
1912
- });
1913
- req.end();
1914
-
1915
- req.on('response', function(res) {
1916
- t.equal(res.statusCode, 202);
1917
- res.on('end', function() {
1918
- t.ok(dataCalled, "data event was called");
1919
- scope.done();
1920
- t.end();
1921
- });
1922
- res.on('data', function(data) {
1923
- dataCalled = true;
1924
- t.ok(data instanceof Buffer, "data should be buffer");
1925
- t.equal(data.toString(), "HEHE!", "response should match");
1926
- });
1927
- });
1928
-
1929
- req.end();
1930
- });
1931
-
1932
- test("can use ClientRequest using POST", function(t) {
1933
-
1934
- var dataCalled = false
1935
-
1936
- var scope = nock('http://www2.clientrequester.com')
1937
- .post('/posthere/please', 'heyhey this is the body')
1938
- .reply(201, "DOOONE!");
1939
-
1940
- var req = new http.ClientRequest({
1941
- host: "www2.clientrequester.com"
1942
- , path: '/posthere/please'
1943
- , method: 'POST'
1944
- });
1945
- req.write('heyhey this is the body');
1946
- req.end();
1947
-
1948
- req.on('response', function(res) {
1949
- t.equal(res.statusCode, 201);
1950
- res.on('end', function() {
1951
- t.ok(dataCalled, "data event was called");
1952
- scope.done();
1953
- t.end();
1954
- });
1955
- res.on('data', function(data) {
1956
- dataCalled = true;
1957
- t.ok(data instanceof Buffer, "data should be buffer");
1958
- t.equal(data.toString(), "DOOONE!", "response should match");
1959
- });
1960
- });
1961
-
1962
- req.end();
1963
- });
1964
-
1965
- test("same url matches twice", function(t) {
1966
- var scope = nock('http://www.twicematcher.com')
1967
- .get('/hey')
1968
- .reply(200, "First match")
1969
- .get('/hey')
1970
- .reply(201, "Second match");
1971
-
1972
- var replied = 0;
1973
-
1974
- function callback() {
1975
- replied += 1;
1976
- if (replied == 2) {
1977
- scope.done();
1978
- t.end();
1979
- }
1980
- }
1981
-
1982
- http.get({
1983
- host: "www.twicematcher.com"
1984
- , path: '/hey'
1985
- }, function(res) {
1986
- t.equal(res.statusCode, 200);
1987
-
1988
- res.on('data', function(data) {
1989
- t.equal(data.toString(), 'First match', 'should match first request response body');
1990
- });
1991
-
1992
- res.on('end', callback);
1993
- });
1994
-
1995
- http.get({
1996
- host: "www.twicematcher.com"
1997
- , path: '/hey'
1998
- }, function(res) {
1999
- t.equal(res.statusCode, 201);
2000
-
2001
- res.on('data', function(data) {
2002
- t.equal(data.toString(), 'Second match', 'should match second request response body');
2003
- });
2004
-
2005
- res.on('end', callback);
2006
- });
2007
-
2008
- });
2009
-
2010
- test("scopes are independent", function(t) {
2011
- var scope1 = nock('http://www34.google.com')
2012
- .get('/')
2013
- .reply(200, "Hello World!");
2014
- var scope2 = nock('http://www34.google.com')
2015
- .get('/')
2016
- .reply(200, "Hello World!");
2017
-
2018
- var req = http.request({
2019
- host: "www34.google.com"
2020
- , path: '/'
2021
- , port: 80
2022
- }, function(res) {
2023
- res.on('end', function() {
2024
- t.ok(scope1.isDone());
2025
- t.ok(! scope2.isDone()); // fails
2026
- t.end();
2027
- });
2028
- // Streams start in 'paused' mode and must be started.
2029
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
2030
- res.resume();
2031
- });
2032
-
2033
- req.end();
2034
- });
2035
-
2036
- test("two scopes with the same request are consumed", function(t) {
2037
- var scope1 = nock('http://www36.google.com')
2038
- .get('/')
2039
- .reply(200, "Hello World!");
2040
-
2041
- var scope2 = nock('http://www36.google.com')
2042
- .get('/')
2043
- .reply(200, "Hello World!");
2044
-
2045
- var doneCount = 0;
2046
- function done() {
2047
- doneCount += 1;
2048
- if (doneCount == 2) {
2049
- t.end();
2050
- }
2051
- }
2052
-
2053
- for (var i = 0; i < 2; i += 1) {
2054
- var req = http.request({
2055
- host: "www36.google.com"
2056
- , path: '/'
2057
- , port: 80
2058
- }, function(res) {
2059
- res.on('end', done);
2060
- // Streams start in 'paused' mode and must be started.
2061
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
2062
- res.resume();
2063
- });
2064
-
2065
- req.end();
2066
- }
2067
- });
2068
-
2069
- test("allow unmocked option works", {skip: process.env.AIRPLANE}, function(t) {
2070
- var scope = nock('http://www.google.com', {allowUnmocked: true})
2071
- .get('/abc')
2072
- .reply(200, 'Hey!')
2073
- .get('/wont/get/here')
2074
- .reply(200, 'Hi!');
2075
-
2076
- function secondIsDone() {
2077
- t.ok(! scope.isDone());
2078
- http.request({
2079
- host: "www.google.com"
2080
- , path: "/"
2081
- , port: 80
2082
- }, function(res) {
2083
- res.destroy();
2084
- t.assert(res.statusCode < 400 && res.statusCode >= 200, 'GET Google Home page');
2085
- t.end();
2086
- }
2087
- ).end();
2088
- }
2089
-
2090
- function firstIsDone() {
2091
- t.ok(! scope.isDone());
2092
- http.request({
2093
- host: "www.google.com"
2094
- , path: "/does/not/exist/dskjsakdj"
2095
- , port: 80
2096
- }, function(res) {
2097
- t.assert(res.statusCode === 404, 'Google say it does not exist');
2098
- res.on('data', function() {});
2099
- res.on('end', secondIsDone);
2100
- }
2101
- ).end();
2102
- }
2103
-
2104
- http.request({
2105
- host: "www.google.com"
2106
- , path: "/abc"
2107
- , port: 80
2108
- }, function(res) {
2109
- res.on('end', firstIsDone);
2110
- // Streams start in 'paused' mode and must be started.
2111
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
2112
- res.resume();
2113
- }
2114
- ).end();
2115
- });
2116
-
2117
- test("default reply headers work", function(t) {
2118
- var scope = nock('http://default.reply.headers.com')
2119
- .defaultReplyHeaders({'X-Powered-By': 'Meeee', 'X-Another-Header': 'Hey man!'})
2120
- .get('/')
2121
- .reply(200, '', {A: 'b'});
2122
-
2123
- function done(res) {
2124
- t.deepEqual(res.headers, {'x-powered-by': 'Meeee', 'x-another-header': 'Hey man!', a: 'b'});
2125
- t.end();
2126
- }
2127
-
2128
- http.request({
2129
- host: 'default.reply.headers.com'
2130
- , path: '/'
2131
- }, done).end();
2132
- });
2133
-
2134
- test("default reply headers as functions work", function(t) {
2135
- var date = (new Date()).toUTCString();
2136
- var message = 'A message.';
2137
-
2138
- var scope = nock('http://default.reply.headers.com')
2139
- .defaultReplyHeaders({
2140
- 'Content-Length' : function (req, res, body) {
2141
- return body.length;
2142
- },
2143
-
2144
- 'Date': function () {
2145
- return date;
2146
- },
2147
-
2148
- 'Foo': function () {
2149
- return 'foo';
2150
- }
2151
- })
2152
- .get('/')
2153
- .reply(200, message, {foo: 'bar'});
2154
-
2155
- http.request({
2156
- host: 'default.reply.headers.com',
2157
- path: '/'
2158
- }, function (res) {
2159
- t.deepEqual(
2160
- res.headers,
2161
- {
2162
- 'content-length': message.length,
2163
- 'date': date,
2164
- 'foo': 'bar'
2165
- }
2166
- );
2167
- t.end();
2168
- }
2169
- ).end();
2170
- });
2171
-
2172
- test("JSON encoded replies set the content-type header", function(t) {
2173
- var scope = nock('http://localhost')
2174
- .get('/')
2175
- .reply(200, {
2176
- A: 'b'
2177
- });
2178
-
2179
- function done(res) {
2180
- scope.done();
2181
- t.equal(res.statusCode, 200);
2182
- t.equal(res.headers['content-type'], 'application/json');
2183
- t.end();
2184
- }
2185
-
2186
- http.request({
2187
- host: 'localhost'
2188
- , path: '/'
2189
- }, done).end();
2190
- });
2191
-
2192
- test("JSON encoded replies does not overwrite existing content-type header", function(t) {
2193
- var scope = nock('http://localhost')
2194
- .get('/')
2195
- .reply(200, {
2196
- A: 'b'
2197
- }, {
2198
- 'Content-Type': 'unicorns'
2199
- });
2200
-
2201
- function done(res) {
2202
- scope.done();
2203
- t.equal(res.statusCode, 200);
2204
- t.equal(res.headers['content-type'], 'unicorns');
2205
- t.end();
2206
- }
2207
-
2208
- http.request({
2209
- host: 'localhost'
2210
- , path: '/'
2211
- }, done).end();
2212
- });
2213
-
2214
- test("blank response doesn't have content-type application/json attached to it", function(t) {
2215
- var scope = nock('http://localhost')
2216
- .get('/')
2217
- .reply(200);
2218
-
2219
- function done(res) {
2220
- t.equal(res.statusCode, 200);
2221
- t.notEqual(res.headers['content-type'], "application/json");
2222
- t.end();
2223
- }
2224
-
2225
- http.request({
2226
- host: 'localhost'
2227
- , path: '/'
2228
- }, done).end();
2229
- });
2230
-
2231
- test('clean all works', function(t) {
2232
- var scope = nock('http://amazon.com')
2233
- .get('/nonexistent')
2234
- .reply(200);
2235
-
2236
- var req = http.get({host: 'amazon.com', path: '/nonexistent'}, function(res) {
2237
- t.assert(res.statusCode === 200, "should mock before cleanup");
2238
-
2239
- nock.cleanAll();
2240
-
2241
- var req = http.get({host: 'amazon.com', path: '/nonexistent'}, function(res) {
2242
- res.destroy();
2243
- t.assert(res.statusCode !== 200, "should clean up properly");
2244
- t.end();
2245
- }).on('error', function(err) {
2246
- t.end();
2247
- });
2248
- });
2249
-
2250
- });
2251
-
2252
- test('is done works', function(t) {
2253
- var scope = nock('http://amazon.com')
2254
- .get('/nonexistent')
2255
- .reply(200);
2256
-
2257
- t.ok(!nock.isDone());
2258
-
2259
- var req = http.get({host: 'amazon.com', path: '/nonexistent'}, function(res) {
2260
- t.assert(res.statusCode === 200, "should mock before cleanup");
2261
- t.ok(nock.isDone());
2262
- t.end();
2263
- });
2264
- });
2265
-
2266
- test('pending mocks works', function(t) {
2267
- var scope = nock('http://amazon.com')
2268
- .get('/nonexistent')
2269
- .reply(200);
2270
-
2271
- t.deepEqual(nock.pendingMocks(), ['GET http://amazon.com:80/nonexistent']);
2272
-
2273
- var req = http.get({host: 'amazon.com', path: '/nonexistent'}, function(res) {
2274
- t.assert(res.statusCode === 200, "should mock before cleanup");
2275
- t.deepEqual(nock.pendingMocks(), []);
2276
- t.end();
2277
- });
2278
- });
2279
-
2280
- test('pending mocks doesn\'t include optional mocks', function(t) {
2281
- var scope = nock('http://example.com')
2282
- .get('/nonexistent')
2283
- .optionally()
2284
- .reply(200);
2285
-
2286
- t.deepEqual(nock.pendingMocks(), []);
2287
- t.end();
2288
- });
2289
-
2290
- test('optional mocks are still functional', function(t) {
2291
- var scope = nock('http://example.com')
2292
- .get('/abc')
2293
- .optionally()
2294
- .reply(200);
2295
-
2296
- var req = http.get({host: 'example.com', path: '/abc'}, function(res) {
2297
- t.assert(res.statusCode === 200, "should still mock requests");
2298
- t.deepEqual(nock.pendingMocks(), []);
2299
- t.end();
2300
- });
2301
- });
2302
-
2303
- test('isDone is true with optional mocks outstanding', function(t) {
2304
- var scope = nock('http://example.com')
2305
- .get('/abc')
2306
- .optionally()
2307
- .reply(200);
2308
-
2309
- t.ok(scope.isDone());
2310
- t.end();
2311
- });
2312
-
2313
- test('optional but persisted mocks persist, but never appear as pending', function(t) {
2314
- var scope = nock('http://example.com')
2315
- .get('/123')
2316
- .optionally()
2317
- .reply(200)
2318
- .persist();
2319
-
2320
- t.deepEqual(nock.pendingMocks(), []);
2321
- var req = http.get({host: 'example.com', path: '/123'}, function(res) {
2322
- t.assert(res.statusCode === 200, "should mock first request");
2323
- t.deepEqual(nock.pendingMocks(), []);
2324
-
2325
- var req = http.get({host: 'example.com', path: '/123'}, function(res) {
2326
- t.assert(res.statusCode === 200, "should mock second request");
2327
- t.deepEqual(nock.pendingMocks(), []);
2328
- t.end();
2329
- });
2330
- });
2331
- });
2332
-
2333
- test('optional repeated mocks execute repeatedly, but never appear as pending', function(t) {
2334
- var scope = nock('http://example.com')
2335
- .get('/456')
2336
- .optionally()
2337
- .times(2)
2338
- .reply(200);
2339
-
2340
- t.deepEqual(nock.pendingMocks(), []);
2341
- var req = http.get({host: 'example.com', path: '/456'}, function(res) {
2342
- t.assert(res.statusCode === 200, "should mock first request");
2343
- t.deepEqual(nock.pendingMocks(), []);
2344
-
2345
- var req = http.get({host: 'example.com', path: '/456'}, function(res) {
2346
- t.assert(res.statusCode === 200, "should mock second request");
2347
- t.deepEqual(nock.pendingMocks(), []);
2348
- t.end();
2349
- });
2350
- });
2351
- });
2352
-
2353
- test('activeMocks returns optional mocks only before they\'re completed', function(t) {
2354
- nock.cleanAll();
2355
- var scope = nock('http://example.com')
2356
- .get('/optional')
2357
- .optionally()
2358
- .reply(200);
2359
-
2360
- t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/optional"]);
2361
- var req = http.get({host: 'example.com', path: '/optional'}, function(res) {
2362
- t.deepEqual(nock.activeMocks(), []);
2363
- t.end();
2364
- });
2365
- });
2366
-
2367
- test('activeMocks always returns persisted mocks', function(t) {
2368
- nock.cleanAll();
2369
- var scope = nock('http://example.com')
2370
- .get('/persisted')
2371
- .reply(200)
2372
- .persist();
2373
-
2374
- t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/persisted"]);
2375
- var req = http.get({host: 'example.com', path: '/persisted'}, function(res) {
2376
- t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/persisted"]);
2377
- t.end();
2378
- });
2379
- });
2380
-
2381
- test('activeMocks returns incomplete mocks', function(t) {
2382
- nock.cleanAll();
2383
- var scope = nock('http://example.com')
2384
- .get('/incomplete')
2385
- .reply(200);
2386
-
2387
- t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/incomplete"]);
2388
- t.end();
2389
- });
2390
-
2391
- test('activeMocks doesn\'t return completed mocks', function(t) {
2392
- nock.cleanAll();
2393
- var scope = nock('http://example.com')
2394
- .get('/complete-me')
2395
- .reply(200);
2396
-
2397
- var req = http.get({host: 'example.com', path: '/complete-me'}, function(res) {
2398
- t.deepEqual(nock.activeMocks(), []);
2399
- t.end();
2400
- });
2401
- });
2402
-
2403
- test('username and password works', function(t) {
2404
- var scope = nock('http://passwordyy.com')
2405
- .get('/')
2406
- .reply(200, "Welcome, username");
2407
-
2408
- http.request({
2409
- hostname: 'passwordyy.com',
2410
- auth: "username:password",
2411
- path: '/'
2412
- }, function(res) {
2413
- scope.done();
2414
- t.end();
2415
- }).end();
2416
- });
2417
-
2418
- test('works with mikeal/request and username and password', function(t) {
2419
- var scope = nock('http://passwordyyyyy.com')
2420
- .get('/abc')
2421
- .reply(200, "Welcome, username");
2422
-
2423
- mikealRequest({uri: 'http://username:password@passwordyyyyy.com/abc', log:true}, function(err, res, body) {
2424
- t.ok(! err, 'error');
2425
- t.ok(scope.isDone());
2426
- t.equal(body, "Welcome, username");
2427
- t.end();
2428
- });
2429
-
2430
- });
2431
-
2432
- test('different ports work works', function(t) {
2433
- var scope = nock('http://abc.portyyyy.com:8081')
2434
- .get('/pathhh')
2435
- .reply(200, "Welcome, username");
2436
-
2437
- http.request({
2438
- hostname: 'abc.portyyyy.com',
2439
- port: 8081,
2440
- path: '/pathhh'
2441
- }, function(res) {
2442
- scope.done();
2443
- t.end();
2444
- }).end();
2445
- });
2446
-
2447
- test('different ports work work with Mikeal request', function(t) {
2448
- var scope = nock('http://abc.portyyyy.com:8082')
2449
- .get('/pathhh')
2450
- .reply(200, "Welcome to Mikeal Request!");
2451
-
2452
- mikealRequest.get('http://abc.portyyyy.com:8082/pathhh', function(err, res, body) {
2453
- t.ok(! err, 'no error');
2454
- t.equal(body, 'Welcome to Mikeal Request!');
2455
- t.ok(scope.isDone());
2456
- t.end();
2457
- });
2458
- });
2459
-
2460
- test('explicitly specifiying port 80 works', function(t) {
2461
- var scope = nock('http://abc.portyyyy.com:80')
2462
- .get('/pathhh')
2463
- .reply(200, "Welcome, username");
2464
-
2465
- http.request({
2466
- hostname: 'abc.portyyyy.com',
2467
- port: 80,
2468
- path: '/pathhh'
2469
- }, function(res) {
2470
- scope.done();
2471
- t.end();
2472
- }).end();
2473
- });
2474
-
2475
- test('post with object', function(t) {
2476
- var scope = nock('http://uri')
2477
- .post('/claim', {some_data: "something"})
2478
- .reply(200);
2479
-
2480
- http.request({
2481
- hostname: 'uri',
2482
- port: 80,
2483
- method: "POST",
2484
- path: '/claim'
2485
- }, function(res) {
2486
- scope.done();
2487
- t.end();
2488
- }).end('{"some_data":"something"}');
2489
-
2490
- });
2491
-
2492
- test('accept string as request target', function(t) {
2493
- var dataCalled = false;
2494
- var scope = nock('http://www.example.com')
2495
- .get('/')
2496
- .reply(200, "Hello World!");
2497
-
2498
- http.get('http://www.example.com', function(res) {
2499
- t.equal(res.statusCode, 200);
2500
-
2501
- res.on('data', function(data) {
2502
- dataCalled = true;
2503
- t.ok(data instanceof Buffer, "data should be buffer");
2504
- t.equal(data.toString(), "Hello World!", "response should match");
2505
- });
2506
-
2507
- res.on('end', function() {
2508
- t.ok(dataCalled);
2509
- scope.done();
2510
- t.end();
2511
- });
2512
- });
2513
- });
2514
-
2515
- test('request has path', function(t) {
2516
- var scope = nock('http://haspath.com')
2517
- .get('/the/path/to/infinity')
2518
- .reply(200);
2519
-
2520
- var req = http.request({
2521
- hostname: 'haspath.com',
2522
- port: 80,
2523
- method: "GET",
2524
- path: '/the/path/to/infinity'
2525
- }, function(res) {
2526
- scope.done();
2527
- t.equal(req.path, '/the/path/to/infinity', 'should have req.path set to /the/path/to/infinity');
2528
- t.end();
2529
- });
2530
- req.end();
2531
- });
2532
-
2533
- test('persists interceptors', function(t) {
2534
- var scope = nock('http://persisssists.con')
2535
- .persist()
2536
- .get('/')
2537
- .reply(200, "Persisting all the way");
2538
-
2539
- t.ok(!scope.isDone());
2540
- http.get('http://persisssists.con/', function(res) {
2541
- t.ok(scope.isDone());
2542
- http.get('http://persisssists.con/', function(res) {
2543
- t.ok(scope.isDone());
2544
- t.end();
2545
- }).end();
2546
- }).end();
2547
- });
2548
-
2549
- test('Persisted interceptors are in pendingMocks initially', function(t) {
2550
- var scope = nock('http://example.com')
2551
- .get('/abc')
2552
- .reply(200, "Persisted reply")
2553
- .persist();
2554
-
2555
- t.deepEqual(scope.pendingMocks(), ["GET http://example.com:80/abc"]);
2556
- t.end();
2557
- });
2558
-
2559
- test('Persisted interceptors are not in pendingMocks after the first request', function(t) {
2560
- var scope = nock('http://example.com')
2561
- .get('/def')
2562
- .reply(200, "Persisted reply")
2563
- .persist();
2564
-
2565
- http.get('http://example.com/def', function(res) {
2566
- t.deepEqual(scope.pendingMocks(), []);
2567
- t.end();
2568
- });
2569
- });
2570
-
2571
- test("persist reply with file", function(t) {
2572
- var scope = nock('http://www.filereplier.com')
2573
- .persist()
2574
- .get('/')
2575
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt')
2576
- .get('/test')
2577
- .reply(200, 'Yay!');
2578
-
2579
- async.each([1,2], function(_, cb) {
2580
- var dataCalled = false;
2581
- var req = http.request({
2582
- host: "www.filereplier.com"
2583
- , path: '/'
2584
- , port: 80
2585
- }, function(res) {
2586
-
2587
- t.equal(res.statusCode, 200);
2588
- res.once('end', function() {
2589
- t.ok(dataCalled);
2590
- cb();
2591
- });
2592
- res.on('data', function(data) {
2593
- dataCalled = true;
2594
- t.equal(data.toString(), "Hello from the file!", "response should match");
2595
- });
2596
-
2597
- });
2598
- req.end();
2599
- }, t.end.bind(t));
2600
- });
2601
-
2602
- test('(re-)activate after restore', {skip: process.env.AIRPLANE}, function(t) {
2603
- var scope = nock('http://google.com')
2604
- .get('/')
2605
- .reply(200, 'Hello, World!');
2606
-
2607
- nock.restore();
2608
- t.false(nock.isActive());
2609
-
2610
- http.get('http://google.com/', function(res) {
2611
- res.resume();
2612
- res.on('end', function() {
2613
- t.ok(!scope.isDone());
2614
-
2615
- nock.activate();
2616
- t.true(nock.isActive());
2617
- http.get('http://google.com', function(res) {
2618
- res.resume();
2619
- res.on('end', function() {
2620
- t.ok(scope.isDone());
2621
- t.end();
2622
- });
2623
- }).end();
2624
- });
2625
- }).end();
2626
- });
2627
-
2628
- test("allow unmocked option works with https", {skip: process.env.AIRPLANE}, function(t) {
2629
- t.plan(5)
2630
- var scope = nock('https://www.google.com', {allowUnmocked: true})
2631
- .get('/abc')
2632
- .reply(200, 'Hey!')
2633
- .get('/wont/get/here')
2634
- .reply(200, 'Hi!');
2635
-
2636
- function secondIsDone() {
2637
- t.ok(! scope.isDone());
2638
- https.request({
2639
- host: "www.google.com"
2640
- , path: "/"
2641
- }, function(res) {
2642
- res.resume();
2643
- t.ok(true, 'Google replied to /');
2644
- res.destroy();
2645
- t.assert(res.statusCode < 400 && res.statusCode >= 200, 'GET Google Home page');
2646
- }).end();
2647
- }
2648
-
2649
- function firstIsDone() {
2650
- t.ok(! scope.isDone(), 'scope is not done');
2651
- https.request({
2652
- host: "www.google.com"
2653
- , path: "/does/not/exist/dskjsakdj"
2654
- }, function(res) {
2655
- t.equal(404, res.statusCode, 'real google response status code');
2656
- res.on('data', function() {});
2657
- res.on('end', secondIsDone);
2658
- }).end();
2659
- }
2660
-
2661
- https.request({
2662
- host: "www.google.com"
2663
- , path: "/abc"
2664
- }, function(res) {
2665
- res.on('end', firstIsDone);
2666
- // Streams start in 'paused' mode and must be started.
2667
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
2668
- res.resume();
2669
- }).end();
2670
- });
2671
-
2672
- test('allow unmocked post with json data', {skip: process.env.AIRPLANE}, function(t) {
2673
- var scope = nock('https://httpbin.org', { allowUnmocked: true }).
2674
- get("/abc").
2675
- reply(200, "Hey!");
2676
-
2677
- var options = {
2678
- method: 'POST',
2679
- uri: 'https://httpbin.org/post',
2680
- json: { some: 'data' }
2681
- };
2682
-
2683
- mikealRequest(options, function(err, resp, body) {
2684
- t.equal(200, resp.statusCode)
2685
- t.end();
2686
- });
2687
- });
2688
-
2689
- test('allow unmocked passthrough with mismatched bodies', {skip: process.env.AIRPLANE}, function(t) {
2690
- var scope = nock('http://httpbin.org', { allowUnmocked: true }).
2691
- post("/post", {some: 'otherdata'}).
2692
- reply(404, "Hey!");
2693
-
2694
- var options = {
2695
- method: 'POST',
2696
- uri: 'http://httpbin.org/post',
2697
- json: { some: 'data' }
2698
- };
2699
-
2700
- mikealRequest(options, function(err, resp, body) {
2701
- t.equal(200, resp.statusCode)
2702
- t.end();
2703
- });
2704
- });
2705
-
2706
- test('allow unordered body with json encoding', function(t) {
2707
- var scope =
2708
- nock('http://wtfjs.org')
2709
- .post('/like-wtf', {
2710
- foo: 'bar',
2711
- bar: 'foo'
2712
- })
2713
- .reply(200, 'Heyyyy!');
2714
-
2715
- mikealRequest({
2716
- uri: 'http://wtfjs.org/like-wtf',
2717
- method: 'POST',
2718
- json: {
2719
- bar: 'foo',
2720
- foo: 'bar'
2721
- }},
2722
- function (e, r, body) {
2723
- t.equal(body, 'Heyyyy!');
2724
- scope.done();
2725
- t.end();
2726
- });
2727
- });
2728
-
2729
- test('allow unordered body with form encoding', function(t) {
2730
- var scope =
2731
- nock('http://wtfjs.org')
2732
- .post('/like-wtf', {
2733
- foo: 'bar',
2734
- bar: 'foo'
2735
- })
2736
- .reply(200, 'Heyyyy!');
2737
-
2738
- mikealRequest({
2739
- uri: 'http://wtfjs.org/like-wtf',
2740
- method: 'POST',
2741
- form: {
2742
- bar: 'foo',
2743
- foo: 'bar'
2744
- }},
2745
- function (e, r, body) {
2746
- t.equal(body, 'Heyyyy!');
2747
- scope.done();
2748
- t.end();
2749
- });
2750
- });
2751
-
2752
- test('allow string json spec', function(t) {
2753
- var bodyObject = {bar: 'foo', foo: 'bar'};
2754
-
2755
- var scope =
2756
- nock('http://wtfjs.org')
2757
- .post('/like-wtf', JSON.stringify(bodyObject))
2758
- .reply(200, 'Heyyyy!');
2759
-
2760
- mikealRequest({
2761
- uri: 'http://wtfjs.org/like-wtf',
2762
- method: 'POST',
2763
- json: {
2764
- bar: 'foo',
2765
- foo: 'bar'
2766
- }},
2767
- function (e, r, body) {
2768
- t.equal(body, 'Heyyyy!');
2769
- scope.done();
2770
- t.end();
2771
- });
2772
- });
2773
-
2774
- test('has a req property on the response', function(t) {
2775
- var scope = nock('http://wtfjs.org').get('/like-wtf').reply(200);
2776
- var req = http.request('http://wtfjs.org/like-wtf', function(res) {
2777
- res.on('end', function() {
2778
- t.ok(res.req, "req property doesn't exist");
2779
- scope.done();
2780
- t.end();
2781
- });
2782
- // Streams start in 'paused' mode and must be started.
2783
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
2784
- res.resume();
2785
- });
2786
- req.end();
2787
- });
2788
-
2789
- test('disabled real HTTP request', function(t) {
2790
- nock.disableNetConnect();
2791
-
2792
- http.get('http://www.amazon.com', function(res) {
2793
- throw "should not request this";
2794
- }).on('error', function(err) {
2795
- t.equal(err.message, 'Nock: Not allow net connect for "www.amazon.com:80/"');
2796
- t.end();
2797
- });
2798
-
2799
- nock.enableNetConnect();
2800
- });
2801
-
2802
- test('NetConnectNotAllowedError is instance of Error', function(t) {
2803
- nock.disableNetConnect();
2804
-
2805
- http.get('http://www.amazon.com', function(res) {
2806
- throw "should not request this";
2807
- }).on('error', function (err) {
2808
- t.type(err, 'Error');
2809
- t.end();
2810
- });
2811
-
2812
- nock.enableNetConnect();
2813
- });
2814
-
2815
- test('NetConnectNotAllowedError exposes the stack', function(t) {
2816
- nock.disableNetConnect();
2817
-
2818
- http.get('http://www.amazon.com', function(res) {
2819
- throw "should not request this";
2820
- }).on('error', function (err) {
2821
- t.notEqual(err.stack, undefined);
2822
- t.end();
2823
- });
2824
-
2825
- nock.enableNetConnect();
2826
- });
2827
-
2828
- test('enable real HTTP request only for google.com, via string', {skip: process.env.AIRPLANE}, function(t) {
2829
- nock.enableNetConnect('google.com');
2830
-
2831
- http.get('http://google.com.br/').on('error', function(err) {
2832
- throw err;
2833
- });
2834
-
2835
- http.get('http://www.amazon.com', function(res) {
2836
- throw "should not deliver this request"
2837
- }).on('error', function (err) {
2838
- t.equal(err.message, 'Nock: Not allow net connect for "www.amazon.com:80/"');
2839
- t.end();
2840
- });
2841
-
2842
- nock.enableNetConnect();
2843
- });
2844
-
2845
- test('enable real HTTP request only for google.com, via regexp', {skip: process.env.AIRPLANE}, function(t) {
2846
- nock.enableNetConnect(/google\.com/);
2847
-
2848
- http.get('http://google.com.br/').on('error', function(err) {
2849
- throw err;
2850
- });
2851
-
2852
- http.get('http://www.amazon.com', function(res) {
2853
- throw "should not request this";
2854
- }).on('error', function (err) {
2855
- t.equal(err.message, 'Nock: Not allow net connect for "www.amazon.com:80/"');
2856
- t.end();
2857
- });
2858
-
2859
- nock.enableNetConnect();
2860
- });
2861
-
2862
- test('repeating once', function(t) {
2863
- nock.disableNetConnect();
2864
-
2865
- var _mock = nock('http://zombo.com')
2866
- .get('/')
2867
- .once()
2868
- .reply(200, "Hello World!");
2869
-
2870
- http.get('http://zombo.com', function(res) {
2871
- t.equal(200, res.statusCode, 'first request');
2872
- t.end();
2873
- });
2874
-
2875
- nock.cleanAll()
2876
-
2877
- nock.enableNetConnect();
2878
- });
2879
-
2880
- test('repeating twice', function(t) {
2881
- nock.disableNetConnect();
2882
-
2883
- var _mock = nock('http://zombo.com')
2884
- .get('/')
2885
- .twice()
2886
- .reply(200, "Hello World!");
2887
-
2888
- async.each([1,2], function(_, cb) {
2889
- http.get('http://zombo.com', function(res) {
2890
- t.equal(200, res.statusCode);
2891
- cb();
2892
- });
2893
- }, t.end.bind(t));
2894
- });
2895
-
2896
- test('repeating thrice', function(t) {
2897
- nock.disableNetConnect();
2898
-
2899
- var _mock = nock('http://zombo.com')
2900
- .get('/')
2901
- .thrice()
2902
- .reply(200, "Hello World!");
2903
-
2904
- async.each([1,2,3], function(_, cb) {
2905
- http.get('http://zombo.com', function(res) {
2906
- t.equal(200, res.statusCode);
2907
- cb();
2908
- });
2909
- }, t.end.bind(t));
2910
- });
2911
-
2912
- test('repeating response 4 times', function(t) {
2913
- nock.disableNetConnect();
2914
-
2915
- var _mock = nock('http://zombo.com')
2916
- .get('/')
2917
- .times(4)
2918
- .reply(200, "Hello World!");
2919
-
2920
- async.each([1,2,3,4], function(_, cb) {
2921
- http.get('http://zombo.com', function(res) {
2922
- t.equal(200, res.statusCode, 'first request');
2923
- cb();
2924
- });
2925
- }, t.end.bind(t));
2926
- });
2927
-
2928
- test('superagent works', function(t) {
2929
- var responseText = 'Yay superagent!';
2930
- var headers = { 'Content-Type': 'text/plain'};
2931
- nock('http://superagent.cz')
2932
- .get('/somepath')
2933
- .reply(200, responseText, headers);
2934
-
2935
- superagent
2936
- .get('http://superagent.cz/somepath')
2937
- .end(function(err, res) {
2938
- t.equal(res.text, responseText);
2939
- t.end();
2940
- });
2941
- });
2942
-
2943
- test('superagent works with query string', function(t) {
2944
- var responseText = 'Yay superagentzzz';
2945
- var headers = { 'Content-Type': 'text/plain'};
2946
- nock('http://superagent.cz')
2947
- .get('/somepath?a=b')
2948
- .reply(200, responseText, headers);
2949
-
2950
- superagent
2951
- .get('http://superagent.cz/somepath?a=b')
2952
- .end(function(err, res) {
2953
- t.equal(res.text, responseText);
2954
- t.end();
2955
- });
2956
- });
2957
-
2958
- test('superagent posts', function(t) {
2959
- nock('http://superagent.cz')
2960
- .post('/somepath?b=c')
2961
- .reply(204);
2962
-
2963
- superagent
2964
- .post('http://superagent.cz/somepath?b=c')
2965
- .send('some data')
2966
- .end(function(err, res) {
2967
- t.equal(res.status, 204);
2968
- t.end();
2969
- });
2970
- });
2971
-
2972
- test('response is streams2 compatible', function(t) {
2973
- var responseText = 'streams2 streams2 streams2';
2974
- nock('http://stream2hostnameftw')
2975
- .get('/somepath')
2976
- .reply(200, responseText);
2977
-
2978
-
2979
- http.request({
2980
- host: "stream2hostnameftw"
2981
- , path: "/somepath"
2982
- }, function(res) {
2983
- res.setEncoding('utf8');
2984
-
2985
- var body = '';
2986
-
2987
- res.on('readable', function() {
2988
- var buf;
2989
- while (buf = res.read())
2990
- body += buf;
2991
- });
2992
-
2993
- res.once('end', function() {
2994
- t.equal(body, responseText);
2995
- t.end();
2996
- });
2997
-
2998
- }).end();
2999
-
3000
- });
3001
-
3002
- test('response is an http.IncomingMessage instance', function(t) {
3003
- var responseText = 'incoming message!';
3004
- nock('http://example.com')
3005
- .get('/somepath')
3006
- .reply(200, responseText);
3007
-
3008
-
3009
- http.request({
3010
- host: "example.com"
3011
- , path: "/somepath"
3012
- }, function(res) {
3013
-
3014
- res.resume();
3015
- t.true(res instanceof http.IncomingMessage);
3016
- t.end();
3017
-
3018
- }).end();
3019
-
3020
- });
3021
-
3022
- function checkDuration(t, ms) {
3023
- var _end = t.end;
3024
- var start = process.hrtime();
3025
- var ended = false;
3026
- t.end = function () {
3027
- if (ended) return;
3028
- ended = true;
3029
- var fin = process.hrtime(start);
3030
- var finMs =
3031
- (fin[0] * 1e+9) + // seconds -> ms
3032
- (fin[1] * 1e-6); // nanoseconds -> ms
3033
-
3034
- /// innaccurate timers
3035
- ms = ms * 0.9;
3036
-
3037
- t.ok(finMs >= ms, 'Duration of ' + Math.round(finMs) + 'ms should be longer than ' + ms + 'ms');
3038
- _end.call(t);
3039
- };
3040
- }
3041
-
3042
- test('calling delay could cause mikealRequest timeout error', function(t) {
3043
- var scope = nock('http://funk')
3044
- .get('/')
3045
- .delay({
3046
- head: 300
3047
- })
3048
- .reply(200, 'OK');
3049
-
3050
- mikealRequest({
3051
- uri: 'http://funk',
3052
- method: 'GET',
3053
- timeout: 100
3054
- }, function (err) {
3055
- scope.done();
3056
- t.equal(err && err.code, "ETIMEDOUT");
3057
- t.end();
3058
- });
3059
- });
3060
-
3061
- test('Body delay does not have impact on timeout', function(t) {
3062
- var scope = nock('http://funk')
3063
- .get('/')
3064
- .delay({
3065
- head: 300,
3066
- body: 300
3067
- })
3068
- .reply(200, 'OK');
3069
-
3070
- mikealRequest({
3071
- uri: 'http://funk',
3072
- method: 'GET',
3073
- timeout: 500
3074
- }, function (err, r, body) {
3075
- t.equal(err, null);
3076
- t.equal(body, 'OK');
3077
- t.equal(r.statusCode, 200);
3078
- scope.done();
3079
- t.end();
3080
- });
3081
- });
3082
-
3083
- test('calling delay with "body" and "head" delays the response', function (t) {
3084
- checkDuration(t, 600);
3085
-
3086
- nock('http://funk')
3087
- .get('/')
3088
- .delay({
3089
- head: 300,
3090
- body: 300
3091
- })
3092
- .reply(200, 'OK');
3093
-
3094
- http.get('http://funk/', function (res) {
3095
- res.once('data', function(data) {
3096
- t.equal(data.toString(), 'OK');
3097
- res.once('end', t.end.bind(t));
3098
- });
3099
- });
3100
- });
3101
-
3102
- test('calling delay with "body" delays the response body', function (t) {
3103
- checkDuration(t, 100);
3104
-
3105
- nock('http://funk')
3106
- .get('/')
3107
- .delay({
3108
- body: 100
3109
- })
3110
- .reply(200, 'OK');
3111
-
3112
- http.get('http://funk/', function (res) {
3113
- res.setEncoding('utf8');
3114
-
3115
- var body = '';
3116
-
3117
- res.on('data', function(chunk) {
3118
- body += chunk;
3119
- });
3120
-
3121
- res.once('end', function() {
3122
- t.equal(body, 'OK');
3123
- t.end();
3124
- });
3125
- });
3126
- });
3127
-
3128
- test('calling delayBody delays the response', function (t) {
3129
- checkDuration(t, 100);
3130
-
3131
- nock('http://funk')
3132
- .get('/')
3133
- .delayBody(100)
3134
- .reply(200, 'OK');
3135
-
3136
- http.get('http://funk/', function (res) {
3137
- res.setEncoding('utf8');
3138
-
3139
- var body = '';
3140
-
3141
- res.on('data', function(chunk) {
3142
- body += chunk;
3143
- });
3144
-
3145
- res.once('end', function() {
3146
- t.equal(body, 'OK');
3147
- t.end();
3148
- });
3149
- });
3150
- });
3151
-
3152
- test('calling delay delays the response', function (t) {
3153
- checkDuration(t, 100);
3154
-
3155
- nock('http://funk')
3156
- .get('/')
3157
- .delay(100)
3158
- .reply(200, 'OK');
3159
-
3160
- http.get('http://funk/', function (res) {
3161
- res.setEncoding('utf8');
3162
-
3163
- var body = '';
3164
-
3165
- res.on('data', function(chunk) {
3166
- body += chunk;
3167
- });
3168
-
3169
- res.once('end', function() {
3170
- t.equal(body, 'OK');
3171
- t.end();
3172
- });
3173
- });
3174
- });
3175
-
3176
- test('using reply callback with delay provides proper arguments', function (t) {
3177
- nock('http://localhost')
3178
- .get('/')
3179
- .delay(100)
3180
- .reply(200, function (path, requestBody) {
3181
- t.equal(path, '/', 'path arg should be set');
3182
- t.equal(requestBody, 'OK', 'requestBody arg should be set');
3183
- t.end();
3184
- });
3185
-
3186
- http.request('http://localhost/', function () {}).end('OK');
3187
- });
3188
-
3189
- test('using reply callback with delay can reply JSON', function (t) {
3190
- nock('http://delayfunctionreplyjson')
3191
- .get('/')
3192
- .delay(100)
3193
- .reply(200, function (path, requestBody) {
3194
- return {a:1};
3195
- });
3196
-
3197
- mikealRequest.get({
3198
- url: 'http://delayfunctionreplyjson/',
3199
- json: true,
3200
- }, function (err, res, body) {
3201
- t.equals(res.headers['content-type'], 'application/json');
3202
- t.deepEqual(body, {a:1});
3203
- t.end();
3204
- });
3205
- });
3206
-
3207
- test('delay works with replyWithFile', function (t) {
3208
- checkDuration(t, 100);
3209
- nock('http://localhost')
3210
- .get('/')
3211
- .delay(100)
3212
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt');
3213
-
3214
- http.request('http://localhost/', function (res) {
3215
- res.setEncoding('utf8');
3216
-
3217
- var body = '';
3218
-
3219
- res.on('data', function(chunk) {
3220
- body += chunk;
3221
- });
3222
-
3223
- res.once('end', function() {
3224
- t.equal(body, 'Hello from the file!', 'the body should eql the text from the file');
3225
- t.end();
3226
- });
3227
- }).end('OK');
3228
- });
3229
-
3230
- test('delay works with when you return a generic stream from the reply callback', function (t) {
3231
- checkDuration(t, 100);
3232
- nock('http://localhost')
3233
- .get('/')
3234
- .delay(100)
3235
- .reply(200, function (path, reqBody) {
3236
- return fs.createReadStream(__dirname + '/../assets/reply_file_1.txt');
3237
- });
3238
-
3239
- http.request('http://localhost/', function (res) {
3240
- res.setEncoding('utf8');
3241
-
3242
- var body = '';
3243
-
3244
- res.on('data', function(chunk) {
3245
- body += chunk;
3246
- });
3247
-
3248
- res.once('end', function() {
3249
- t.equal(body, 'Hello from the file!', 'the body should eql the text from the file');
3250
- t.end();
3251
- });
3252
- }).end('OK');
3253
- });
3254
-
3255
- test('delay works with replyWithError', function (t) {
3256
- nock('http://errorland')
3257
- .get('/')
3258
- .delay(100)
3259
- .replyWithError('this is an error message');
3260
-
3261
- setTimeout(function() {
3262
- req.once('error', function(err) {
3263
- t.equal(err.message, 'this is an error message');
3264
- t.end();
3265
- });
3266
- }, 100);
3267
-
3268
- var req = http.get('http://errorland/');
3269
-
3270
- });
3271
-
3272
- test("finish event fired before end event (bug-139)", function(t) {
3273
- var scope = nock('http://www.filterboddiezregexp.com')
3274
- .filteringRequestBody(/mia/, 'nostra')
3275
- .post('/', 'mamma nostra')
3276
- .reply(200, "Hello World!");
3277
-
3278
- var finishCalled = false;
3279
- var req = http.request({
3280
- host: "www.filterboddiezregexp.com"
3281
- , method: 'POST'
3282
- , path: '/'
3283
- , port: 80
3284
- }, function(res) {
3285
- t.equal(finishCalled, true);
3286
- t.equal(res.statusCode, 200);
3287
- res.on('end', function() {
3288
- scope.done();
3289
- t.end();
3290
- });
3291
- // Streams start in 'paused' mode and must be started.
3292
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
3293
- res.resume();
3294
- });
3295
-
3296
- req.on('finish', function() {
3297
- finishCalled = true;
3298
- });
3299
-
3300
- req.end('mamma mia');
3301
-
3302
- });
3303
-
3304
- test('when a stream is used for the response body, it will not be read until after the response event', {skip: !stream.Readable}, function (t) {
3305
- var responseEvent = false;
3306
- var text = 'Hello World\n';
3307
-
3308
- function SimpleStream(opt) {
3309
- stream.Readable.call(this, opt);
3310
- }
3311
- util.inherits(SimpleStream, stream.Readable);
3312
- SimpleStream.prototype._read = function() {
3313
- t.ok(responseEvent);
3314
- this.push(text);
3315
- this.push(null);
3316
- };
3317
-
3318
- nock('http://localhost')
3319
- .get('/')
3320
- .reply(200, function (path, reqBody) {
3321
- return new SimpleStream();
3322
- });
3323
-
3324
- http.get('http://localhost/', function (res) {
3325
- responseEvent = true;
3326
- res.setEncoding('utf8');
3327
-
3328
- var body = '';
3329
-
3330
- res.on('data', function(chunk) {
3331
- body += chunk;
3332
- });
3333
-
3334
- res.once('end', function() {
3335
- t.equal(body, text);
3336
- t.end();
3337
- });
3338
- });
3339
- });
3340
-
3341
- test('calling delayConnection delays the connection', function (t) {
3342
- checkDuration(t, 100);
3343
-
3344
- nock('http://funk')
3345
- .get('/')
3346
- .delayConnection(100)
3347
- .reply(200, 'OK');
3348
-
3349
- http.get('http://funk/', function (res) {
3350
- res.setEncoding('utf8');
3351
-
3352
- var body = '';
3353
-
3354
- res.on('data', function(chunk) {
3355
- body += chunk;
3356
- });
3357
-
3358
- res.once('end', function() {
3359
- t.equal(body, 'OK');
3360
- t.end();
3361
- });
3362
- });
3363
- });
3364
-
3365
- test('using reply callback with delayConnection provides proper arguments', function (t) {
3366
- nock('http://localhost')
3367
- .get('/')
3368
- .delayConnection(100)
3369
- .reply(200, function (path, requestBody) {
3370
- t.equal(path, '/', 'path arg should be set');
3371
- t.equal(requestBody, 'OK', 'requestBody arg should be set');
3372
- t.end();
3373
- });
3374
-
3375
- http.request('http://localhost/', function () {}).end('OK');
3376
- });
3377
-
3378
- test('delayConnection works with replyWithFile', function (t) {
3379
- checkDuration(t, 100);
3380
- nock('http://localhost')
3381
- .get('/')
3382
- .delayConnection(100)
3383
- .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt');
3384
-
3385
- http.request('http://localhost/', function (res) {
3386
- res.setEncoding('utf8');
3387
-
3388
- var body = '';
3389
-
3390
- res.on('data', function(chunk) {
3391
- body += chunk;
3392
- });
3393
-
3394
- res.once('end', function() {
3395
- t.equal(body, 'Hello from the file!', 'the body should eql the text from the file');
3396
- t.end();
3397
- });
3398
- }).end('OK');
3399
- });
3400
-
3401
- test('delayConnection works with when you return a generic stream from the reply callback', function (t) {
3402
- checkDuration(t, 100);
3403
- nock('http://localhost')
3404
- .get('/')
3405
- .delayConnection(100)
3406
- .reply(200, function (path, reqBody) {
3407
- return fs.createReadStream(__dirname + '/../assets/reply_file_1.txt');
3408
- });
3409
-
3410
- var req = http.request('http://localhost/', function (res) {
3411
- res.setEncoding('utf8');
3412
-
3413
- var body = '';
3414
-
3415
- res.on('data', function(chunk) {
3416
- body += chunk;
3417
- });
3418
-
3419
- res.once('end', function() {
3420
- t.equal(body, 'Hello from the file!', 'the body should eql the text from the file');
3421
- t.end();
3422
- });
3423
- }).end('OK');
3424
- });
3425
-
3426
- test('define() is backward compatible', function(t) {
3427
- var nockDef = {
3428
- "scope":"http://example.com",
3429
- // "port" has been deprecated
3430
- "port":12345,
3431
- "method":"GET",
3432
- "path":"/",
3433
- // "reply" has been deprected
3434
- "reply":"500"
3435
- };
3436
-
3437
- var nocks = nock.define([nockDef]);
3438
-
3439
- t.ok(nocks);
3440
-
3441
- var req = new http.request({
3442
- host: 'example.com',
3443
- port: nockDef.port,
3444
- method: nockDef.method,
3445
- path: nockDef.path
3446
- }, function(res) {
3447
- t.equal(res.statusCode, 500);
3448
-
3449
- res.once('end', function() {
3450
- t.end();
3451
- });
3452
- // Streams start in 'paused' mode and must be started.
3453
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
3454
- res.resume();
3455
- });
3456
-
3457
- req.on('error', function(err) {
3458
- console.error(err);
3459
- // This should never happen.
3460
- t.ok(false, 'Error should never occur.');
3461
- t.end();
3462
- });
3463
-
3464
- req.end();
3465
-
3466
- });
3467
-
3468
- test('define() works with non-JSON responses', function(t) {
3469
- var nockDef = {
3470
- "scope":"http://example.com",
3471
- "method":"POST",
3472
- "path":"/",
3473
- "body":"�",
3474
- "status":200,
3475
- "response":"�"
3476
- };
3477
-
3478
- var nocks = nock.define([nockDef]);
3479
-
3480
- t.ok(nocks);
3481
-
3482
- var req = new http.request({
3483
- host: 'example.com',
3484
- method: nockDef.method,
3485
- path: nockDef.path
3486
- }, function(res) {
3487
- t.equal(res.statusCode, nockDef.status);
3488
-
3489
- var dataChunks = [];
3490
-
3491
- res.on('data', function(chunk) {
3492
- dataChunks.push(chunk);
3493
- });
3494
-
3495
- res.once('end', function() {
3496
- var response = Buffer.concat(dataChunks);
3497
- t.equal(response.toString('utf8'), nockDef.response, 'responses match');
3498
- t.end();
3499
- });
3500
- });
3501
-
3502
- req.on('error', function(err) {
3503
- console.error(err);
3504
- // This should never happen.
3505
- t.ok(false, 'Error should never occur.');
3506
- t.end();
3507
- });
3508
-
3509
- req.write(nockDef.body);
3510
- req.end();
3511
-
3512
- });
3513
-
3514
- test('define() works with binary buffers', function(t) {
3515
- var nockDef = {
3516
- "scope":"http://example.com",
3517
- "method":"POST",
3518
- "path":"/",
3519
- "body":"8001",
3520
- "status":200,
3521
- "response":"8001"
3522
- };
3523
-
3524
- var nocks = nock.define([nockDef]);
3525
-
3526
- t.ok(nocks);
3527
-
3528
- var req = new http.request({
3529
- host: 'example.com',
3530
- method: nockDef.method,
3531
- path: nockDef.path
3532
- }, function(res) {
3533
- t.equal(res.statusCode, nockDef.status);
3534
-
3535
- var dataChunks = [];
3536
-
3537
- res.on('data', function(chunk) {
3538
- dataChunks.push(chunk);
3539
- });
3540
-
3541
- res.once('end', function() {
3542
- var response = Buffer.concat(dataChunks);
3543
- t.equal(response.toString('hex'), nockDef.response, 'responses match');
3544
- t.end();
3545
- });
3546
- });
3547
-
3548
- req.on('error', function(err) {
3549
- console.error(err);
3550
- // This should never happen.
3551
- t.ok(false, 'Error should never occur.');
3552
- t.end();
3553
- });
3554
-
3555
- req.write(new Buffer(nockDef.body, 'hex'));
3556
- req.end();
3557
-
3558
- });
3559
-
3560
- test('issue #163 - Authorization header isn\'t mocked', {skip: process.env.AIRPLANE}, function(t) {
3561
- nock.enableNetConnect();
3562
- function makeRequest(cb) {
3563
- var r = http.request(
3564
- {
3565
- hostname: 'www.example.com',
3566
- path: '/',
3567
- method: 'GET',
3568
- auth: 'foo:bar'
3569
- },
3570
- function(res) {
3571
- cb(res.req._headers);
3572
- }
3573
- );
3574
- r.end();
3575
- }
3576
-
3577
- makeRequest(function(headers) {
3578
- var n = nock('http://www.example.com', {
3579
- reqheaders: { 'authorization': 'Basic Zm9vOmJhcg==' }
3580
- }).get('/').reply(200);
3581
-
3582
- makeRequest(function(nockHeader) {
3583
- n.done();
3584
- t.equivalent(headers, nockHeader);
3585
- t.end();
3586
- });
3587
- });
3588
- });
3589
-
3590
- test('define() uses reqheaders', function(t) {
3591
- var nockDef = {
3592
- "scope":"http://example.com",
3593
- "method":"GET",
3594
- "path":"/",
3595
- "status":200,
3596
- "reqheaders": {
3597
- host: 'example.com',
3598
- 'authorization': 'Basic Zm9vOmJhcg=='
3599
- }
3600
- };
3601
-
3602
- var nocks = nock.define([nockDef]);
3603
-
3604
- t.ok(nocks);
3605
-
3606
- var req = new http.request({
3607
- host: 'example.com',
3608
- method: nockDef.method,
3609
- path: nockDef.path,
3610
- auth: 'foo:bar'
3611
- }, function(res) {
3612
- t.equal(res.statusCode, nockDef.status);
3613
-
3614
- res.once('end', function() {
3615
- t.equivalent(res.req._headers, nockDef.reqheaders);
3616
- t.end();
3617
- });
3618
- // Streams start in 'paused' mode and must be started.
3619
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
3620
- res.resume();
3621
- });
3622
- req.end();
3623
-
3624
- });
3625
-
3626
- test('sending binary and receiving JSON should work ', function(t) {
3627
- var scope = nock('http://example.com')
3628
- .filteringRequestBody(/.*/, '*')
3629
- .post('/some/path', '*')
3630
- .reply(201, { foo: '61' }, {
3631
- 'Content-Type': 'application/json'
3632
- });
3633
-
3634
- mikealRequest({
3635
- method: 'POST',
3636
- uri: 'http://example.com/some/path',
3637
- body: new Buffer('ffd8ffe000104a46494600010101006000600000ff', 'hex'),
3638
- headers: { 'Accept': 'application/json', 'Content-Length': 23861 }
3639
- }, function(err, res, body) {
3640
- scope.done();
3641
-
3642
- t.equal(res.statusCode, 201);
3643
- t.equal(body.length, 12);
3644
-
3645
- var json;
3646
- try {
3647
- json = JSON.parse(body);
3648
- } catch (e) {
3649
- json = {};
3650
- }
3651
-
3652
- t.equal(json.foo, '61');
3653
- t.end();
3654
- }
3655
- );
3656
- });
3657
-
3658
- test('fix #146 - resume() is automatically invoked when the response is drained', function(t) {
3659
- var replyLength = 1024 * 1024;
3660
- var replyBuffer = new Buffer((new Array(replyLength + 1)).join("."));
3661
- t.equal(replyBuffer.length, replyLength);
3662
-
3663
- nock("http://www.abc.com")
3664
- .get("/abc")
3665
- .reply(200, replyBuffer);
3666
-
3667
- needle.get("http://www.abc.com/abc", function(err, res, buffer) {
3668
- t.notOk(err);
3669
- t.ok(res);
3670
- t.ok(buffer);
3671
- t.same(buffer, replyBuffer);
3672
- t.end();
3673
- });
3674
- });
3675
-
3676
- test("handles get with restify client", function(t) {
3677
- var scope =
3678
- nock("https://www.example.com").
3679
- get("/get").
3680
- reply(200, 'get');
3681
-
3682
- var client = restify.createClient({
3683
- url: 'https://www.example.com'
3684
- })
3685
-
3686
- client.get('/get', function(err, req, res) {
3687
- req.on('result', function(err, res) {
3688
- res.body = '';
3689
- res.setEncoding('utf8');
3690
- res.on('data', function(chunk) {
3691
- res.body += chunk;
3692
- });
3693
-
3694
- res.on('end', function() {
3695
- t.equal(res.body, 'get')
3696
- t.end();
3697
- scope.done();
3698
- });
3699
- });
3700
- });
3701
- });
3702
-
3703
- test("handles post with restify client", function(t) {
3704
- var scope =
3705
- nock("https://www.example.com").
3706
- post("/post", 'hello world').
3707
- reply(200, 'post');
3708
-
3709
- var client = restify.createClient({
3710
- url: 'https://www.example.com'
3711
- })
3712
-
3713
- client.post('/post', function(err, req, res) {
3714
- req.on('result', function(err, res) {
3715
- res.body = '';
3716
- res.setEncoding('utf8');
3717
- res.on('data', function(chunk) {
3718
- res.body += chunk;
3719
- });
3720
-
3721
- res.on('end', function() {
3722
- t.equal(res.body, 'post')
3723
- t.end();
3724
- scope.done();
3725
- });
3726
- });
3727
-
3728
- req.write('hello world');
3729
- req.end();
3730
- });
3731
- });
3732
-
3733
- test("handles get with restify JsonClient", function(t) {
3734
- var scope =
3735
- nock("https://www.example.com").
3736
- get("/get").
3737
- reply(200, {get: 'ok'});
3738
-
3739
- var client = restify.createJsonClient({
3740
- url: 'https://www.example.com'
3741
- })
3742
-
3743
- client.get('/get', function(err, req, res, obj) {
3744
- t.equal(obj.get, 'ok');
3745
- t.end();
3746
- scope.done();
3747
- });
3748
- });
3749
-
3750
- test("handles post with restify JsonClient", function(t) {
3751
- var scope =
3752
- nock("https://www.example.com").
3753
- post("/post", {username: 'banana'}).
3754
- reply(200, {post: 'ok'});
3755
-
3756
- var client = restify.createJsonClient({
3757
- url: 'https://www.example.com'
3758
- })
3759
-
3760
- client.post('/post', {username: 'banana'}, function(err, req, res, obj) {
3761
- t.equal(obj.post, 'ok');
3762
- t.end();
3763
- scope.done();
3764
- });
3765
- });
3766
-
3767
- test("handles 404 with restify JsonClient", function(t) {
3768
- var scope =
3769
- nock("https://www.example.com").
3770
- put("/404").
3771
- reply(404);
3772
-
3773
- var client = restify.createJsonClient({
3774
- url: 'https://www.example.com'
3775
- })
3776
-
3777
- client.put('/404', function(err, req, res, obj) {
3778
- t.equal(res.statusCode, 404);
3779
- t.end();
3780
- scope.done();
3781
- });
3782
- });
3783
-
3784
- test("handles 500 with restify JsonClient", function(t) {
3785
- var scope =
3786
- nock("https://www.example.com").
3787
- delete("/500").
3788
- reply(500);
3789
-
3790
- var client = restify.createJsonClient({
3791
- url: 'https://www.example.com'
3792
- })
3793
-
3794
- client.del('/500', function(err, req, res, obj) {
3795
- t.equal(res.statusCode, 500);
3796
- t.end();
3797
- scope.done();
3798
- });
3799
- });
3800
-
3801
- test('test request timeout option', function(t) {
3802
-
3803
- nock('http://example.com')
3804
- .get('/test')
3805
- .reply(200, JSON.stringify({ foo: 'bar' }));
3806
-
3807
- var options = {
3808
- url: 'http://example.com/test',
3809
- method: 'GET',
3810
- timeout: 2000
3811
- };
3812
-
3813
- mikealRequest(options, function(err, res, body) {
3814
- t.strictEqual(err, null);
3815
- t.equal(body, '{"foo":"bar"}');
3816
- t.end();
3817
- });
3818
- });
3819
-
3820
- test('done fails when specified request header is missing', function(t) {
3821
- var scope = nock('http://example.com', {
3822
- reqheaders: {
3823
- "X-App-Token": "apptoken",
3824
- "X-Auth-Token": "apptoken"
3825
- }
3826
- })
3827
- .post('/resource')
3828
- .reply(200, { status: "ok" });
3829
-
3830
- var d = domain.create();
3831
-
3832
- d.run(function() {
3833
- mikealRequest({
3834
- method: 'POST',
3835
- uri: 'http://example.com/resource',
3836
- headers: {
3837
- "X-App-Token": "apptoken"
3838
- }
3839
- });
3840
- });
3841
-
3842
- d.once('error', function(err) {
3843
- t.ok(err.message.match(/No match/));
3844
- t.end();
3845
- });
3846
- });
3847
-
3848
- test('matches request header with regular expression', function(t) {
3849
- var scope = nock('http://example.com', {
3850
- reqheaders: {
3851
- "X-My-Super-Power": /.+/
3852
- }
3853
- })
3854
- .post('/superpowers')
3855
- .reply(200, { status: "ok" });
3856
-
3857
-
3858
- mikealRequest({
3859
- method: 'POST',
3860
- uri: 'http://example.com/superpowers',
3861
- headers: {
3862
- "X-My-Super-Power": "mullet growing"
3863
- }
3864
- }, function(err, res, body) {
3865
- t.strictEqual(err, null);
3866
- t.equal(body, '{"status":"ok"}');
3867
- t.end();
3868
- });
3869
- });
3870
-
3871
- test('request header satisfies the header function', function(t) {
3872
- var scope = nock('http://example.com', {
3873
- reqheaders: {
3874
- "X-My-Super-Power": function(value) {
3875
- return value === "mullet growing";
3876
- }
3877
- }
3878
- })
3879
- .post('/superpowers')
3880
- .reply(200, { status: "ok" });
3881
-
3882
-
3883
- mikealRequest({
3884
- method: 'POST',
3885
- uri: 'http://example.com/superpowers',
3886
- headers: {
3887
- "X-My-Super-Power": "mullet growing"
3888
- }
3889
- }, function(err, res, body) {
3890
- t.strictEqual(err, null);
3891
- t.equal(body, '{"status":"ok"}');
3892
- t.end();
3893
- });
3894
- });
3895
-
3896
- test('done fails when specified request header doesn\'t match regular expression', function(t) {
3897
- var scope = nock('http://example.com', {
3898
- reqheaders: {
3899
- "X-My-Super-Power": /Mullet.+/
3900
- }
3901
- })
3902
- .post('/resource')
3903
- .reply(200, { status: "ok" });
3904
-
3905
- var d = domain.create();
3906
-
3907
- d.run(function() {
3908
- mikealRequest({
3909
- method: 'POST',
3910
- uri: 'http://example.com/superpowers',
3911
- headers: {
3912
- "X-My-Super-Power": "mullet growing"
3913
- }
3914
- });
3915
- });
3916
-
3917
- d.once('error', function(err) {
3918
- t.ok(err.message.match(/No match/));
3919
- t.end();
3920
- });
3921
- });
3922
-
3923
- test('done fails when specified request header doesn\'t satisfy the header function', function(t) {
3924
- var scope = nock('http://example.com', {
3925
- reqheaders: {
3926
- "X-My-Super-Power": function (value) {
3927
- return value === 'Mullet Growing';
3928
- }
3929
- }
3930
- })
3931
- .post('/resource')
3932
- .reply(200, { status: "ok" });
3933
-
3934
- var d = domain.create();
3935
-
3936
- d.run(function() {
3937
- mikealRequest({
3938
- method: 'POST',
3939
- uri: 'http://example.com/superpowers',
3940
- headers: {
3941
- "X-My-Super-Power": "mullet growing"
3942
- }
3943
- });
3944
- });
3945
-
3946
- d.once('error', function(err) {
3947
- t.ok(err.message.match(/No match/));
3948
- t.end();
3949
- });
3950
- });
3951
-
3952
- test('done does not fail when specified request header is not missing', function(t) {
3953
- var scope = nock('http://example.com', {
3954
- reqheaders: {
3955
- "X-App-Token": "apptoken",
3956
- "X-Auth-Token": "apptoken"
3957
- }
3958
- })
3959
- .post('/resource')
3960
- .reply(200, { status: "ok" });
3961
-
3962
- mikealRequest({
3963
- method: 'POST',
3964
- uri: 'http://example.com/resource',
3965
- headers: {
3966
- "X-App-Token": "apptoken",
3967
- "X-Auth-Token": "apptoken"
3968
- }
3969
- }, function(err, res, body) {
3970
- t.type(err, 'null');
3971
- t.equal(res.statusCode, 200);
3972
- t.end();
3973
- });
3974
-
3975
- });
3976
-
3977
- test('done fails when specified bad request header is present', function (t) {
3978
- var scope = nock('http://example.com', {
3979
- badheaders: ['cookie']
3980
- })
3981
- .post('/resource')
3982
- .reply(200, { status: 'ok' });
3983
-
3984
- var d = domain.create();
3985
-
3986
- d.run(function() {
3987
- mikealRequest({
3988
- method: 'POST',
3989
- uri: 'http://example.com/resource',
3990
- headers: {
3991
- 'Cookie': 'cookie'
3992
- }
3993
- });
3994
- });
3995
-
3996
- d.once('error', function (err) {
3997
- t.ok(err.message.match(/No match/));
3998
- t.end();
3999
- });
4000
- });
4001
-
4002
- test('mikeal/request with delayConnection and request.timeout', function(t) {
4003
- var endpoint = nock("http://some-server.com")
4004
- .post("/")
4005
- .delayConnection(1000)
4006
- .reply(200, {});
4007
-
4008
- mikealRequest.post({
4009
- url: "http://some-server.com/",
4010
- timeout: 10
4011
- },
4012
- function (err) {
4013
- t.type(err, 'Error');
4014
- t.equal(err && err.code, "ETIMEDOUT");
4015
- t.end();
4016
- });
4017
- });
4018
-
4019
- test("get correct filtering with scope and request headers filtering", function(t) {
4020
- var responseText = 'OK!';
4021
- var responseHeaders = { 'Content-Type': 'text/plain'};
4022
- var requestHeaders = { host: 'a.subdomain.of.google.com' };
4023
-
4024
- var scope = nock('http://a.subdomain.of.google.com', {
4025
- filteringScope: function(scope) {
4026
- return (/^http:\/\/.*\.google\.com/).test(scope);
4027
- }
4028
- })
4029
- .get('/somepath')
4030
- .reply(200, responseText, responseHeaders);
4031
-
4032
- var dataCalled = false;
4033
- var host = 'some.other.subdomain.of.google.com';
4034
- var req = http.get({
4035
- host: host,
4036
- method: 'GET',
4037
- path: '/somepath',
4038
- port: 80
4039
- }, function(res) {
4040
- res.on('data', function(data) {
4041
- dataCalled = true;
4042
- t.equal(data.toString(), responseText);
4043
- });
4044
- res.on('end', function() {
4045
- t.true(dataCalled);
4046
- scope.done();
4047
- t.end();
4048
- });
4049
- });
4050
-
4051
- t.equivalent(req._headers, { host: requestHeaders.host });
4052
-
4053
- });
4054
-
4055
- test('mocking succeeds even when mocked and specified request header names have different cases', function(t) {
4056
- var scope = nock('http://example.com', {
4057
- reqheaders: {
4058
- "x-app-token": "apptoken",
4059
- "x-auth-token": "apptoken"
4060
- }
4061
- })
4062
- .post('/resource')
4063
- .reply(200, { status: "ok" });
4064
-
4065
- mikealRequest({
4066
- method: 'POST',
4067
- uri: 'http://example.com/resource',
4068
- headers: {
4069
- "X-App-TOKEN": "apptoken",
4070
- "X-Auth-TOKEN": "apptoken"
4071
- }
4072
- }, function(err, res, body) {
4073
- t.type(err, 'null');
4074
- t.equal(res.statusCode, 200);
4075
- t.end();
4076
- });
4077
-
4078
- });
4079
-
4080
- test('mocking succeeds even when host request header is not specified', function(t) {
4081
- var scope = nock('http://example.com')
4082
- .post('/resource')
4083
- .reply(200, { status: "ok" });
4084
-
4085
- mikealRequest({
4086
- method: 'POST',
4087
- uri: 'http://example.com/resource',
4088
- headers: {
4089
- "X-App-TOKEN": "apptoken",
4090
- "X-Auth-TOKEN": "apptoken"
4091
- }
4092
- }, function(err, res, body) {
4093
- t.type(err, 'null');
4094
- t.equal(res.statusCode, 200);
4095
- t.end();
4096
- });
4097
-
4098
- });
4099
-
4100
- test('mikeal/request with strictSSL: true', function(t) {
4101
- var scope = nock('https://strictssl.com')
4102
- .post('/what')
4103
- .reply(200, { status: "ok" });
4104
-
4105
- mikealRequest({
4106
- method: 'POST',
4107
- uri: 'https://strictssl.com/what',
4108
- strictSSL: true
4109
- }, function(err, res, body) {
4110
- t.type(err, 'null');
4111
- t.equal(res && res.statusCode, 200);
4112
- t.end();
4113
- });
4114
-
4115
- });
4116
-
4117
- test('response readable pull stream works as expected', function(t) {
4118
- var scope = nock('http://streamingalltheway.com')
4119
- .get('/ssstream')
4120
- .reply(200, "this is the response body yeah");
4121
-
4122
- var req = http.request({
4123
- host: "streamingalltheway.com"
4124
- , path: '/ssstream'
4125
- , port: 80
4126
- }, function(res) {
4127
-
4128
- var ended = false;
4129
- var responseBody = '';
4130
- t.equal(res.statusCode, 200);
4131
- res.on('readable', function() {
4132
- var chunk;
4133
- while (null !== (chunk = res.read())) {
4134
- responseBody += chunk.toString();
4135
- }
4136
- if (chunk === null && ! ended) {
4137
- ended = true;
4138
- t.equal(responseBody, "this is the response body yeah");
4139
- t.end();
4140
- }
4141
- });
4142
- });
4143
-
4144
- req.end();
4145
- });
4146
-
4147
- test(".setNoDelay", function(t) {
4148
- var dataCalled = false
4149
-
4150
- var scope = nock('http://nodelayyy.com')
4151
- .get('/yay')
4152
- .reply(200, "Hi");
4153
-
4154
- var req = http.request({
4155
- host: "nodelayyy.com"
4156
- , path: '/yay'
4157
- , port: 80
4158
- }, function(res) {
4159
-
4160
- t.equal(res.statusCode, 200);
4161
- res.on('end', t.end.bind(t));
4162
- // Streams start in 'paused' mode and must be started.
4163
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
4164
- res.resume();
4165
-
4166
- });
4167
-
4168
- req.setNoDelay(true);
4169
-
4170
- req.end();
4171
- });
4172
-
4173
- test("match basic authentication header", function(t) {
4174
- var username = 'testuser'
4175
- , password = 'testpassword'
4176
- , authString = username + ":" + password
4177
- , encrypted = (new Buffer(authString)).toString( 'base64' );
4178
-
4179
- var scope = nock('http://www.headdy.com')
4180
- .get('/')
4181
- .matchHeader('Authorization', function(val) {
4182
- var expected = 'Basic ' + encrypted;
4183
- return val == expected;
4184
- })
4185
- .reply(200, "Hello World!");
4186
-
4187
- http.get({
4188
- host: "www.headdy.com"
4189
- , path: '/'
4190
- , port: 80
4191
- , auth: authString
4192
- }, function(res) {
4193
- res.setEncoding('utf8');
4194
- t.equal(res.statusCode, 200);
4195
-
4196
- res.on('data', function(data) {
4197
- t.equal(data, 'Hello World!');
4198
- });
4199
-
4200
- res.on('end', function() {
4201
- scope.done();
4202
- t.end();
4203
- });
4204
- });
4205
-
4206
- });
4207
-
4208
- test('request emits socket', function(t) {
4209
- var scope = nock('http://gotzsocketz.com')
4210
- .get('/')
4211
- .reply(200, "hey");
4212
-
4213
- var req = http.get('http://gotzsocketz.com');
4214
- req.once('socket', function(socket) {
4215
- t.equal(this, req);
4216
- t.type(socket, Object);
4217
- t.type(socket.getPeerCertificate(), 'string');
4218
- t.end();
4219
- });
4220
- });
4221
-
4222
- test('socket emits connect and secureConnect', function(t) {
4223
- t.plan(3);
4224
-
4225
- var scope = nock('http://gotzsocketz.com')
4226
- .post('/')
4227
- .reply(200, "hey");
4228
-
4229
- var req = http.request({
4230
- host: "gotzsocketz.com"
4231
- , path: '/'
4232
- , method: 'POST'
4233
- });
4234
-
4235
- req.on('socket', function(socket) {
4236
- socket.once('connect', function() {
4237
- req.end();
4238
- t.ok(true);
4239
- });
4240
- socket.once('secureConnect', function() {
4241
- t.ok(true);
4242
- });
4243
- });
4244
-
4245
- req.once('response', function(res) {
4246
- res.setEncoding('utf8');
4247
- res.on('data', function(d) {
4248
- t.equal(d, 'hey');
4249
- });
4250
- });
4251
- });
4252
-
4253
- test('socket setKeepAlive', function(t) {
4254
- var scope = nock('http://setkeepalive.com')
4255
- .get('/')
4256
- .reply(200, "hey");
4257
-
4258
- var req = http.get('http://setkeepalive.com');
4259
- req.once('socket', function(socket) {
4260
- socket.setKeepAlive(true);
4261
- t.end();
4262
- });
4263
- });
4264
-
4265
- test('abort destroys socket', function(t) {
4266
- var scope = nock('http://socketdestroyer.com')
4267
- .get('/')
4268
- .reply(200, "hey");
4269
-
4270
- var req = http.get('http://socketdestroyer.com');
4271
- req.once('error', function() {
4272
- // ignore
4273
- });
4274
- req.once('socket', function(socket) {
4275
- req.abort();
4276
- t.ok(socket.destroyed);
4277
- t.end();
4278
- });
4279
-
4280
- });
4281
-
4282
- test('hyperquest works', function(t) {
4283
- nock('http://hyperquest.com')
4284
- .get('/somepath')
4285
- .reply(200, 'Yay hyperquest!');
4286
-
4287
- var req = hyperquest('http://hyperquest.com/somepath');
4288
- var reply = '';
4289
- req.on('data', function(d) {
4290
- reply += d;
4291
- });
4292
- req.once('end', function() {
4293
- t.equals(reply, 'Yay hyperquest!');
4294
- t.end();
4295
- });
4296
- });
4297
-
4298
- test('match domain using regexp', function (t) {
4299
- var scope = nock(/regexexample\.com/)
4300
- .get('/resources')
4301
- .reply(200, 'Match regex');
4302
-
4303
- mikealRequest.get('http://www.regexexample.com/resources', function(err, res, body) {
4304
- t.type(err, 'null');
4305
- t.equal(res.statusCode, 200);
4306
- t.equal(body, 'Match regex');
4307
-
4308
- t.end();
4309
- });
4310
- });
4311
-
4312
- test('match multiple interceptors with regexp domain (issue-508)', function (t) {
4313
- var scope = nock(/chainregex/)
4314
- .get('/')
4315
- .reply(200, 'Match regex')
4316
- .get('/')
4317
- .reply(500, 'Match second intercept');
4318
-
4319
- mikealRequest.get('http://www.chainregex.com', function(err, res, body) {
4320
- t.type(err, 'null');
4321
- t.equal(res.statusCode, 200);
4322
- t.equal(body, 'Match regex');
4323
-
4324
- mikealRequest.get('http://www.chainregex.com', function(err, res, body) {
4325
- t.type(err, 'null');
4326
- t.equal(res.statusCode, 500);
4327
- t.equal(body, 'Match second intercept');
4328
-
4329
- t.end();
4330
- });
4331
- });
4332
- });
4333
-
4334
- test('match domain using intercept callback', function (t) {
4335
- var validUrl = [
4336
- '/cats',
4337
- '/dogs'
4338
- ];
4339
-
4340
- nock('http://www.interceptexample.com')
4341
- .get(function(uri) {
4342
- return validUrl.indexOf(uri) >= 0;
4343
- })
4344
- .reply(200, 'Match intercept')
4345
- .get('/cats')
4346
- .reply(200, 'Match intercept 2');
4347
-
4348
- mikealRequest.get('http://www.interceptexample.com/cats', function(err, res, body) {
4349
- t.type(err, 'null');
4350
- t.equal(res.statusCode, 200);
4351
- t.equal(body, 'Match intercept');
4352
-
4353
- // This one should match the second .get()
4354
- mikealRequest.get('http://www.interceptexample.com/cats', function(err, res, body) {
4355
- t.type(err, 'null');
4356
- t.equal(res.statusCode, 200);
4357
- t.equal(body, 'Match intercept 2');
4358
- t.end();
4359
- });
4360
- });
4361
- });
4362
-
4363
- test('match path using regexp', function (t) {
4364
- var scope = nock('http://www.pathregex.com')
4365
- .get(/regex$/)
4366
- .reply(200, 'Match regex');
4367
-
4368
- mikealRequest.get('http://www.pathregex.com/resources/regex', function(err, res, body) {
4369
- t.type(err, 'null');
4370
- t.equal(res.statusCode, 200);
4371
- t.equal(body, 'Match regex');
4372
- t.end();
4373
- });
4374
- });
4375
-
4376
- test('remove interceptor for GET resource', function(t) {
4377
- var scope = nock('http://example.org')
4378
- .get('/somepath')
4379
- .reply(200, 'hey');
4380
-
4381
- var mocks = scope.pendingMocks();
4382
- t.deepEqual(mocks, ['GET http://example.org:80/somepath']);
4383
-
4384
- var result = nock.removeInterceptor({
4385
- hostname : 'example.org',
4386
- path : '/somepath'
4387
- });
4388
- t.ok(result, 'result should be true');
4389
-
4390
- nock('http://example.org')
4391
- .get('/somepath')
4392
- .reply(202, 'other-content');
4393
-
4394
- http.get({
4395
- host: 'example.org',
4396
- path : '/somepath'
4397
- }, function(res) {
4398
- res.setEncoding('utf8');
4399
- t.equal(res.statusCode, 202);
4400
-
4401
- res.on('data', function(data) {
4402
- t.equal(data, 'other-content');
4403
- });
4404
-
4405
- res.on('end', function() {
4406
- t.end();
4407
- });
4408
- });
4409
- });
4410
-
4411
- test('remove interceptor removes given interceptor', function(t) {
4412
- var givenInterceptor = nock('http://example.org')
4413
- .get('/somepath');
4414
- var scope = givenInterceptor
4415
- .reply(200, 'hey');
4416
-
4417
- var mocks = scope.pendingMocks();
4418
- t.deepEqual(mocks, ['GET http://example.org:80/somepath']);
4419
-
4420
- var result = nock.removeInterceptor(givenInterceptor);
4421
- t.ok(result, 'result should be true');
4422
-
4423
- nock('http://example.org')
4424
- .get('/somepath')
4425
- .reply(202, 'other-content');
4426
-
4427
- http.get({
4428
- host: 'example.org',
4429
- path : '/somepath'
4430
- }, function(res) {
4431
- res.setEncoding('utf8');
4432
- t.equal(res.statusCode, 202);
4433
-
4434
- res.on('data', function(data) {
4435
- t.equal(data, 'other-content');
4436
- });
4437
-
4438
- res.on('end', function() {
4439
- t.end();
4440
- });
4441
- });
4442
- });
4443
-
4444
- test('remove interceptor removes interceptor from pending requests', function(t) {
4445
- var givenInterceptor = nock('http://example.org')
4446
- .get('/somepath');
4447
- var scope = givenInterceptor
4448
- .reply(200, 'hey');
4449
-
4450
- var mocks = scope.pendingMocks();
4451
- t.deepEqual(mocks, ['GET http://example.org:80/somepath']);
4452
-
4453
- var result = nock.removeInterceptor(givenInterceptor);
4454
- t.ok(result, 'result should be true');
4455
-
4456
- var mocksAfterRemove = scope.pendingMocks();
4457
- t.deepEqual(mocksAfterRemove, [ ]);
4458
- t.end();
4459
- });
4460
-
4461
- test('remove interceptor removes given interceptor for https', function(t) {
4462
- var givenInterceptor = nock('https://example.org')
4463
- .get('/somepath');
4464
- var scope = givenInterceptor
4465
- .reply(200, 'hey');
4466
-
4467
- var mocks = scope.pendingMocks();
4468
- t.deepEqual(mocks, ['GET https://example.org:443/somepath']);
4469
-
4470
- var result = nock.removeInterceptor(givenInterceptor);
4471
- t.ok(result, 'result should be true');
4472
-
4473
- nock('https://example.org')
4474
- .get('/somepath')
4475
- .reply(202, 'other-content');
4476
-
4477
- https.get({
4478
- host: 'example.org',
4479
- path : '/somepath'
4480
- }, function(res) {
4481
- res.setEncoding('utf8');
4482
- t.equal(res.statusCode, 202);
4483
-
4484
- res.on('data', function(data) {
4485
- t.equal(data, 'other-content');
4486
- });
4487
-
4488
- res.on('end', function() {
4489
- t.end();
4490
- });
4491
- });
4492
- });
4493
-
4494
- test('remove interceptor removes given interceptor for regex path', function(t) {
4495
- var givenInterceptor = nock('http://example.org')
4496
- .get(/somePath$/);
4497
- var scope = givenInterceptor
4498
- .reply(200, 'hey');
4499
-
4500
- var mocks = scope.pendingMocks();
4501
- t.deepEqual(mocks, ['GET http://example.org:80//somePath$/']);
4502
-
4503
- var result = nock.removeInterceptor(givenInterceptor);
4504
- t.ok(result, 'result should be true');
4505
-
4506
- nock('http://example.org')
4507
- .get(/somePath$/)
4508
- .reply(202, 'other-content');
4509
-
4510
- http.get({
4511
- host: 'example.org',
4512
- path : '/get-somePath'
4513
- }, function(res) {
4514
- res.setEncoding('utf8');
4515
- t.equal(res.statusCode, 202);
4516
-
4517
- res.once('data', function(data) {
4518
- t.equal(data, 'other-content');
4519
- });
4520
-
4521
- res.on('end', function() {
4522
- t.end();
4523
- });
4524
- });
4525
- });
4526
-
4527
- test('remove interceptor for not found resource', function(t) {
4528
- var result = nock.removeInterceptor({
4529
- hostname : 'example.org',
4530
- path : '/somepath'
4531
- });
4532
- t.notOk(result, 'result should be false as no interceptor was found');
4533
- t.end();
4534
- });
4535
-
4536
- test('isDone() must consider repeated responses', function(t) {
4537
- var scope = nock('http://www.example.com')
4538
- .get('/')
4539
- .times(2)
4540
- .reply(204);
4541
-
4542
- function makeRequest(callback) {
4543
- var req = http.request({
4544
- host: "www.example.com",
4545
- path: '/',
4546
- port: 80
4547
- }, function(res) {
4548
- t.equal(res.statusCode, 204);
4549
- res.on('end', callback);
4550
- // Streams start in 'paused' mode and must be started.
4551
- // See https://nodejs.org/api/stream.html#stream_class_stream_readable
4552
- res.resume();
4553
- });
4554
- req.end();
4555
- }
4556
-
4557
- t.notOk(scope.isDone(), "should not be done before all requests");
4558
- makeRequest(function() {
4559
- t.notOk(scope.isDone(), "should not yet be done after the first request");
4560
- makeRequest(function() {
4561
- t.ok(scope.isDone(), "should be done after the two requests are made");
4562
- scope.done();
4563
- t.end();
4564
- });
4565
- });
4566
- });
4567
-
4568
- test('you must setup an interceptor for each request', function(t) {
4569
- var scope = nock('http://www.example.com')
4570
- .get('/hey')
4571
- .reply(200, 'First match');
4572
-
4573
- mikealRequest.get('http://www.example.com/hey', function(error, res, body) {
4574
- t.equal(res.statusCode, 200);
4575
- t.equal(body, 'First match', 'should match first request response body');
4576
-
4577
- mikealRequest.get('http://www.example.com/hey', function(error, res, body) {
4578
- t.equal(error && error.toString(), 'Error: Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://www.example.com/hey","headers":{"host":"www.example.com"}}, null, 2));
4579
- scope.done();
4580
- t.end();
4581
- });
4582
- });
4583
- });
4584
-
4585
- test('calling socketDelay will emit a timeout', function (t) {
4586
- nock('http://www.example.com')
4587
- .get('/')
4588
- .socketDelay(10000)
4589
- .reply(200, 'OK');
4590
-
4591
- var timedout = false;
4592
- var ended = false;
4593
-
4594
- var req = http.request('http://www.example.com', function (res) {
4595
- res.setEncoding('utf8');
4596
-
4597
- var body = '';
4598
-
4599
- res.on('data', function(chunk) {
4600
- body += chunk;
4601
- });
4602
-
4603
- res.once('end', function() {
4604
- ended = true;
4605
- if (! timedout) {
4606
- t.fail('socket did not timeout when idle');
4607
- t.end();
4608
- }
4609
- });
4610
- });
4611
-
4612
- req.setTimeout(5000, function () {
4613
- timedout = true;
4614
- if (! ended) {
4615
- t.ok(true);
4616
- t.end();
4617
- }
4618
- });
4619
-
4620
- req.end();
4621
- });
4622
-
4623
- test('calling socketDelay not emit a timeout if not idle for long enough', function (t) {
4624
- nock('http://www.example.com')
4625
- .get('/')
4626
- .socketDelay(10000)
4627
- .reply(200, 'OK');
4628
-
4629
- var req = http.request('http://www.example.com', function (res) {
4630
- res.setEncoding('utf8');
4631
-
4632
- var body = '';
4633
-
4634
- res.on('data', function(chunk) {
4635
- body += chunk;
4636
- });
4637
-
4638
- res.once('end', function() {
4639
- t.equal(body, 'OK');
4640
- t.end();
4641
- });
4642
- });
4643
-
4644
- req.setTimeout(60000, function () {
4645
- t.fail('socket timed out unexpectedly');
4646
- t.end();
4647
- });
4648
-
4649
- req.end();
4650
- });
4651
-
4652
- test("replyWithError returns an error on request", function(t) {
4653
- var scope = nock('http://www.google.com')
4654
- .post('/echo')
4655
- .replyWithError('Service not found');
4656
-
4657
- var req = http.request({
4658
- host: "www.google.com"
4659
- , method: 'POST'
4660
- , path: '/echo'
4661
- , port: 80
4662
- });
4663
-
4664
- // An error should have have been raised
4665
- req.on('error', function(e) {
4666
- scope.done();
4667
- t.equal(e.message, 'Service not found');
4668
- t.end();
4669
- });
4670
-
4671
- req.end();
4672
- });
4673
-
4674
- test("replyWithError allows json response", function(t) {
4675
- var scope = nock('http://www.google.com')
4676
- .post('/echo')
4677
- .replyWithError({message: "Service not found", code: 'test'});
4678
-
4679
- var req = http.request({
4680
- host: "www.google.com"
4681
- , method: 'POST'
4682
- , path: '/echo'
4683
- , port: 80
4684
- });
4685
-
4686
- // An error should have have been raised
4687
- req.on('error', function(e) {
4688
- scope.done();
4689
- t.equal(e.message, 'Service not found');
4690
- t.equal(e.code, 'test');
4691
- t.end();
4692
- });
4693
-
4694
- req.end();
4695
- });
4696
-
4697
- test('no content type provided', function(t) {
4698
- var scope = nock('http://nocontenttype.com')
4699
- .replyContentLength()
4700
- .post('/httppost', function() {
4701
- return true
4702
- })
4703
- .reply(401, "");
4704
-
4705
- var req = http.request({
4706
- host: "nocontenttype.com",
4707
- path: '/httppost',
4708
- method: 'POST',
4709
- headers: {}
4710
- }, function(res) {
4711
- console.log('haz response');
4712
- res.on('data', function() {});
4713
- res.once('end', function() {
4714
- console.log('response ended');
4715
- scope.done();
4716
- t.ok(true);
4717
- t.end();
4718
- });
4719
- }).end('WHAA');
4720
-
4721
- });
4722
-
4723
- test('query() matches a query string of the same name=value', function (t) {
4724
- var scope = nock('http://google.com')
4725
- .get('/')
4726
- .query({foo:'bar'})
4727
- .reply(200);
4728
-
4729
- mikealRequest('http://google.com/?foo=bar', function(err, res) {
4730
- if (err) throw err;
4731
- t.equal(res.statusCode, 200);
4732
- t.end();
4733
- })
4734
- });
4735
-
4736
- test('query() matches multiple query strings of the same name=value', function (t) {
4737
- var scope = nock('http://google.com')
4738
- .get('/')
4739
- .query({foo:'bar',baz:'foz'})
4740
- .reply(200);
4741
-
4742
- mikealRequest('http://google.com/?foo=bar&baz=foz', function(err, res) {
4743
- if (err) throw err;
4744
- t.equal(res.statusCode, 200);
4745
- t.end();
4746
- })
4747
- });
4748
-
4749
- test('query() matches multiple query strings of the same name=value regardless of order', function (t) {
4750
- var scope = nock('http://google.com')
4751
- .get('/')
4752
- .query({foo:'bar',baz:'foz'})
4753
- .reply(200);
4754
-
4755
- mikealRequest('http://google.com/?baz=foz&foo=bar', function(err, res) {
4756
- if (err) throw err;
4757
- t.equal(res.statusCode, 200);
4758
- t.end();
4759
- })
4760
- });
4761
-
4762
- test('query() matches query values regardless of their type of declaration', function (t) {
4763
- var scope = nock('http://google.com')
4764
- .get('/')
4765
- .query({num:1,bool:true,empty:null,str:'fou'})
4766
- .reply(200);
4767
-
4768
- mikealRequest('http://google.com/?num=1&bool=true&empty=&str=fou', function(err, res) {
4769
- if (err) throw err;
4770
- t.equal(res.statusCode, 200);
4771
- t.end();
4772
- })
4773
- });
4774
-
4775
- test('query() doesn\'t match query values of requests without query string', function (t) {
4776
- var scope1 = nock('http://google.com')
4777
- .get('/')
4778
- .query({num:1,bool:true,empty:null,str:'fou'})
4779
- .reply(200, 'scope1');
4780
-
4781
- var scope2 = nock('http://google.com')
4782
- .get('/')
4783
- .reply(200, 'scope2');
4784
-
4785
- mikealRequest('http://google.com/', function(err, res) {
4786
- if (err) throw err;
4787
- t.equal(res.statusCode, 200);
4788
- t.equal(res.body, 'scope2');
4789
- t.end();
4790
- })
4791
- });
4792
-
4793
- test('query() matches a query string using regexp', function (t) {
4794
- var scope = nock('http://google.com')
4795
- .get('/')
4796
- .query({foo:/.*/})
4797
- .reply(200);
4798
-
4799
- mikealRequest('http://google.com/?foo=bar', function(err, res) {
4800
- if (err) throw err;
4801
- t.equal(res.statusCode, 200);
4802
- t.end();
4803
- })
4804
- });
4805
-
4806
- test('query() matches a query string that contains special RFC3986 characters', function (t) {
4807
- var scope = nock('http://google.com')
4808
- .get('/')
4809
- .query({'foo&bar':'hello&world'})
4810
- .reply(200);
4811
-
4812
- var options = {
4813
- uri: 'http://google.com/',
4814
- qs: {
4815
- 'foo&bar': 'hello&world'
4816
- }
4817
- };
4818
-
4819
- mikealRequest(options, function(err, res) {
4820
- if (err) throw err;
4821
- t.equal(res.statusCode, 200);
4822
- t.end();
4823
- })
4824
- });
4825
-
4826
- test('query() expects unencoded query params', function (t) {
4827
- var scope = nock('http://google.com')
4828
- .get('/')
4829
- .query({'foo':'hello%20world'})
4830
- .reply(200);
4831
-
4832
- mikealRequest('http://google.com?foo=hello%20world', function(err, res) {
4833
- t.similar(err.toString(), /Error: Nock: No match for request/);
4834
- t.end();
4835
- });
4836
- });
4837
-
4838
- test('query() matches a query string with pre-encoded values', function (t) {
4839
- var scope = nock('http://google.com', { encodedQueryParams: true })
4840
- .get('/')
4841
- .query({'foo':'hello%20world'})
4842
- .reply(200);
4843
-
4844
- mikealRequest('http://google.com?foo=hello%20world', function(err, res) {
4845
- if (err) throw err;
4846
- t.equal(res.statusCode, 200);
4847
- t.end();
4848
- })
4849
- });
4850
-
4851
- test('query() with "true" will allow all query strings to pass', function (t) {
4852
- var scope = nock('http://google.com')
4853
- .get('/')
4854
- .query(true)
4855
- .reply(200);
4856
-
4857
- mikealRequest('http://google.com/?foo=bar&a=1&b=2', function(err, res) {
4858
- if (err) throw err;
4859
- t.equal(res.statusCode, 200);
4860
- t.end();
4861
- })
4862
- });
4863
-
4864
- test('query() with "{}" will allow a match against ending in ?', function (t) {
4865
- var scope = nock('http://querystringmatchland.com')
4866
- .get('/noquerystring')
4867
- .query({})
4868
- .reply(200);
4869
-
4870
- mikealRequest('http://querystringmatchland.com/noquerystring?', function(err, res) {
4871
- if (err) throw err;
4872
- t.equal(res.statusCode, 200);
4873
- t.end();
4874
- })
4875
- });
4876
-
4877
- test('query() with a function, function called with actual queryObject',function(t){
4878
- var queryObject;
4879
-
4880
- var queryValidator = function(qs){
4881
- queryObject= qs;
4882
- return true;
4883
- };
4884
-
4885
- var scope = nock('http://google.com')
4886
- .get('/')
4887
- .query(queryValidator)
4888
- .reply(200);
4889
-
4890
- mikealRequest('http://google.com/?foo=bar&a=1&b=2', function(err, res) {
4891
- if (err) throw err;
4892
- t.deepEqual(queryObject,{foo:'bar',a:'1',b:'2'});
4893
- t.equal(res.statusCode, 200);
4894
- t.end();
4895
- })
4896
- });
4897
-
4898
- test('query() with a function, function return true the query treat as matched', function(t){
4899
- var alwasyTrue = function(){
4900
- return true;
4901
- };
4902
-
4903
- var scope = nock('http://google.com')
4904
- .get('/')
4905
- .query(alwasyTrue)
4906
- .reply(200);
4907
-
4908
- mikealRequest('http://google.com/?igore=the&actual=query', function(err, res) {
4909
- if (err) throw err;
4910
- t.equal(res.statusCode, 200);
4911
- t.end();
4912
- })
4913
- });
4914
-
4915
- test('query() with a function, function return false the query treat as Un-matched', function(t){
4916
-
4917
- var alwayFalse = function(){
4918
- return false;
4919
- };
4920
-
4921
- var scope = nock('http://google.com')
4922
- .get('/')
4923
- .query(alwayFalse)
4924
- .reply(200);
4925
-
4926
- mikealRequest('http://google.com/?i=should&pass=?', function(err, res) {
4927
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?i=should&pass=?","headers":{"host":"google.com"}}, null, 2));
4928
- t.end();
4929
- })
4930
- });
4931
-
4932
- test('query() will not match when a query string does not match name=value', function (t) {
4933
- var scope = nock('https://c.com')
4934
- .get('/b')
4935
- .query({foo:'bar'})
4936
- .reply(200);
4937
-
4938
- mikealRequest('https://c.com/b?foo=baz', function(err, res) {
4939
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://c.com/b?foo=baz","headers":{"host":"c.com"}}, null, 2));
4940
- t.end();
4941
- })
4942
- });
4943
-
4944
- test('query() will not match when a query string is present that was not registered', function (t) {
4945
- var scope = nock('https://b.com')
4946
- .get('/c')
4947
- .query({foo:'bar'})
4948
- .reply(200);
4949
-
4950
- mikealRequest('https://b.com/c?foo=bar&baz=foz', function(err, res) {
4951
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://b.com/c?foo=bar&baz=foz","headers":{"host":"b.com"}}, null, 2));
4952
- t.end();
4953
- })
4954
- });
4955
-
4956
- test('query() will not match when a query string is malformed', function (t) {
4957
- var scope = nock('https://a.com')
4958
- .get('/d')
4959
- .query({foo:'bar'})
4960
- .reply(200);
4961
-
4962
- mikealRequest('https://a.com/d?foobar', function(err, res) {
4963
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"https://a.com/d?foobar","headers":{"host":"a.com"}}, null, 2));
4964
- t.end();
4965
- })
4966
- });
4967
-
4968
- test('query() will not match when a query string has fewer correct values than expected', function (t) {
4969
- var scope = nock('http://google.com')
4970
- .get('/')
4971
- .query({
4972
- num:1,
4973
- bool:true,
4974
- empty:null,
4975
- str:'fou'
4976
- })
4977
- .reply(200);
4978
-
4979
- mikealRequest('http://google.com/?num=1str=fou', function(err, res) {
4980
- t.equal(err.message.trim(), 'Nock: No match for request ' + JSON.stringify({"method":"GET","url":"http://google.com/?num=1str=fou","headers":{"host":"google.com"}}, null, 2));
4981
- t.end();
4982
- })
4983
- });
4984
-
4985
- test('query(true) will match when the path has no query', function (t) {
4986
- var scope = nock('http://google.com')
4987
- .get('/')
4988
- .query(true)
4989
- .reply(200);
4990
-
4991
- mikealRequest('http://google.com', function(err, res) {
4992
- t.ok(!err, 'no error');
4993
- t.ok(res);
4994
- t.equal(res.statusCode, 200);
4995
- t.end();
4996
- })
4997
- });
4998
-
4999
- test("teardown", function(t) {
5000
- var leaks = Object.keys(global)
5001
- .splice(globalCount, Number.MAX_VALUE);
5002
-
5003
- leaks = leaks.filter(function(key) {
5004
- return acceptableLeaks.indexOf(key) == -1;
5005
- });
5006
-
5007
- t.deepEqual(leaks, [], 'No leaks');
5008
- t.end();
5009
- });