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,896 +0,0 @@
1
- 'use strict';
2
-
3
- var nock = require('../.')
4
- , test = require('tap').test
5
- , http = require('http')
6
- , https = require('https')
7
- , _ = require('lodash')
8
- , debug = require('debug')('nock.test_recorder')
9
- , mikealRequest = require('request')
10
- , superagent = require('superagent')
11
- , rest = require('restler');
12
-
13
- var globalCount;
14
-
15
- test("setup", function(t) {
16
- globalCount = Object.keys(global).length;
17
- t.end();
18
- });
19
-
20
- test('recording turns off nock interception (backward compatibility behavior)', function(t) {
21
-
22
- // We ensure that there are no overrides.
23
- nock.restore();
24
- t.false(nock.isActive());
25
- // We active the nock overriding - as it's done by merely loading nock.
26
- nock.activate();
27
- t.true(nock.isActive());
28
- // We start recording.
29
- nock.recorder.rec();
30
- // Nothing happens (nothing has been thrown) - which was the original behavior -
31
- // and mocking has been deactivated.
32
- t.false(nock.isActive());
33
-
34
- t.end();
35
-
36
- });
37
-
38
- test('records', {skip: process.env.AIRPLANE}, function(t) {
39
- nock.restore();
40
- nock.recorder.clear();
41
- t.equal(nock.recorder.play().length, 0);
42
- var options = { method: 'POST'
43
- , host:'google.com'
44
- , port:80
45
- , path:'/' }
46
- ;
47
-
48
- nock.recorder.rec(true);
49
- var req = http.request(options, function(res) {
50
- res.resume();
51
- var ret;
52
- res.once('end', function() {
53
- nock.restore();
54
- ret = nock.recorder.play();
55
- t.equal(ret.length, 1);
56
- t.type(ret[0], 'string');
57
- t.equal(ret[0].indexOf("\nnock('http://google.com:80', {\"encodedQueryParams\":true})\n .post('/', \"ABCDEF\")\n .reply("), 0);
58
- t.end();
59
- });
60
- });
61
- req.end('ABCDEF');
62
- });
63
-
64
- test('records objects', {skip: process.env.AIRPLANE}, function(t) {
65
- nock.restore();
66
- nock.recorder.clear();
67
- t.equal(nock.recorder.play().length, 0);
68
- var options = { method: 'POST'
69
- , host:'google.com'
70
- , path:'/' }
71
- ;
72
-
73
- nock.recorder.rec({
74
- dont_print: true,
75
- output_objects: true
76
- });
77
- var req = http.request(options, function(res) {
78
- res.resume();
79
- res.once('end', function() {
80
- nock.restore();
81
- var ret = nock.recorder.play();
82
- t.equal(ret.length, 1);
83
- ret = ret[0];
84
- t.type(ret, 'object');
85
- t.equal(ret.scope, "http://google.com:80");
86
- t.equal(ret.method, "POST");
87
- t.ok(typeof(ret.status) !== 'undefined');
88
- t.ok(typeof(ret.response) !== 'undefined');
89
- t.end();
90
- });
91
- });
92
- req.end('012345');
93
- });
94
-
95
- test('records and replays objects correctly', {skip: process.env.AIRPLANE}, function(t) {
96
-
97
- nock.restore();
98
- nock.recorder.clear();
99
- t.equal(nock.recorder.play().length, 0);
100
-
101
- nock.recorder.rec({
102
- dont_print: true,
103
- output_objects: true
104
- });
105
-
106
- var makeRequest = function(callback) {
107
- superagent
108
- .get('http://google.com')
109
- .end(callback);
110
- };
111
-
112
- makeRequest(function(err, resp) {
113
-
114
- t.ok(!err);
115
- t.ok(resp);
116
- t.ok(resp.headers);
117
-
118
- nock.restore();
119
- var nockDefs = nock.recorder.play();
120
- nock.recorder.clear();
121
- nock.activate();
122
-
123
- t.equal(nockDefs.length, 2);
124
- var nocks = nock.define(nockDefs);
125
-
126
- makeRequest(function(mockedErr, mockedResp) {
127
-
128
- t.equal(err, mockedErr);
129
- t.deepEqual(mockedResp.body, resp.body);
130
-
131
- _.each(nocks, function(nock) {
132
- nock.done();
133
- });
134
-
135
- t.end();
136
-
137
- });
138
- });
139
-
140
- });
141
-
142
- test('records and replays correctly with filteringRequestBody', {skip: process.env.AIRPLANE}, function(t) {
143
-
144
- nock.restore();
145
- nock.recorder.clear();
146
- t.equal(nock.recorder.play().length, 0);
147
-
148
- nock.recorder.rec({
149
- dont_print: true,
150
- output_objects: true
151
- });
152
-
153
- var makeRequest = function(callback) {
154
- superagent
155
- .get('http://google.com')
156
- .end(callback);
157
- };
158
-
159
- makeRequest(function(err, resp) {
160
-
161
- t.ok(!err);
162
- t.ok(resp);
163
- t.ok(resp.headers);
164
-
165
- nock.restore();
166
- var nockDefs = nock.recorder.play();
167
- nock.recorder.clear();
168
- nock.activate();
169
-
170
- t.equal(nockDefs.length, 2);
171
- var nockDef = _.first(nockDefs);
172
- var filteringRequestBodyCounter = 0;
173
- nockDef.filteringRequestBody = function(body, aRecodedBody) {
174
- ++filteringRequestBodyCounter;
175
- t.strictEqual(body, aRecodedBody);
176
- return body;
177
- };
178
- var nocks = nock.define(nockDefs);
179
-
180
- makeRequest(function(mockedErr, mockedResp) {
181
-
182
- t.equal(err, mockedErr);
183
- t.deepEqual(mockedResp.body, resp.body);
184
-
185
- _.each(nocks, function(nock) {
186
- nock.done();
187
- });
188
-
189
- t.strictEqual(filteringRequestBodyCounter, 1);
190
- t.end();
191
-
192
- });
193
- });
194
-
195
- });
196
-
197
- test('checks if callback is specified', {skip: process.env.AIRPLANE}, function(t) {
198
- var options = {
199
- host: 'www.google.com', method: 'GET', path: '/', port: 80
200
- };
201
-
202
- nock.restore();
203
- nock.recorder.clear();
204
- t.equal(nock.recorder.play().length, 0);
205
- nock.recorder.rec(true);
206
-
207
- http.request(options).end();
208
- t.end();
209
- });
210
-
211
- test('when request body is json, it goes unstringified', {skip: process.env.AIRPLANE}, function(t) {
212
- var payload = {a: 1, b: true};
213
- var options = {
214
- method: 'POST',
215
- host: 'www.google.com',
216
- path: '/',
217
- port: 80
218
- };
219
-
220
- nock.restore();
221
- nock.recorder.clear();
222
- nock.recorder.rec(true);
223
-
224
- var request = http.request(options, function(res) {
225
- res.resume();
226
- res.once('end', function() {
227
- var ret = nock.recorder.play();
228
- t.ok(ret.length >= 1);
229
- ret = ret[1] || ret[0];
230
- t.equal(ret.indexOf("\nnock('http://www.google.com:80', {\"encodedQueryParams\":true})\n .post('/', {\"a\":1,\"b\":true})\n .reply("), 0);
231
- t.end();
232
- });
233
- });
234
-
235
- request.end(JSON.stringify(payload));
236
- });
237
-
238
- test('when request body is json, it goes unstringified in objects', {skip: process.env.AIRPLANE}, function(t) {
239
- var payload = {a: 1, b: true};
240
- var options = {
241
- method: 'POST',
242
- host: 'www.google.com',
243
- path: '/',
244
- port: 80
245
- };
246
-
247
- nock.restore();
248
- nock.recorder.clear();
249
- nock.recorder.rec({
250
- dont_print: true,
251
- output_objects: true
252
- });
253
-
254
- var request = http.request(options, function(res) {
255
- res.resume();
256
- res.once('end', function() {
257
- var ret = nock.recorder.play();
258
- t.ok(ret.length >= 1);
259
- ret = ret[1] || ret[0];
260
- t.type(ret, 'object');
261
- t.equal(ret.scope, "http://www.google.com:80");
262
- t.equal(ret.method, "POST");
263
- t.ok(ret.body && ret.body.a && ret.body.a === payload.a && ret.body.b && ret.body.b === payload.b);
264
- t.ok(typeof(ret.status) !== 'undefined');
265
- t.ok(typeof(ret.response) !== 'undefined');
266
- t.end();
267
- });
268
- });
269
-
270
- request.end(JSON.stringify(payload));
271
- });
272
-
273
- test('records nonstandard ports', function(t) {
274
- nock.restore();
275
- nock.recorder.clear();
276
- t.equal(nock.recorder.play().length, 0);
277
-
278
- var REQUEST_BODY = 'ABCDEF';
279
- var RESPONSE_BODY = '012345';
280
-
281
- // Create test http server and perform the tests while it's up.
282
- var testServer = http.createServer(function(req, res) {
283
- res.write(RESPONSE_BODY);
284
- res.end();
285
- }).listen(8081, function(err) {
286
-
287
- t.equal(err, undefined);
288
-
289
- var options = { host:'localhost'
290
- , port:testServer.address().port
291
- , path:'/' }
292
- ;
293
-
294
- var rec_options = {
295
- dont_print: true,
296
- output_objects: true
297
- };
298
-
299
- nock.recorder.rec(rec_options);
300
-
301
- var req = http.request(options, function(res) {
302
- res.resume();
303
- res.once('end', function() {
304
- nock.restore();
305
- var ret = nock.recorder.play();
306
- t.equal(ret.length, 1);
307
- ret = ret[0];
308
- t.type(ret, 'object');
309
- t.equal(ret.scope, "http://localhost:" + options.port);
310
- t.equal(ret.method, "GET");
311
- t.equal(ret.body, REQUEST_BODY);
312
- t.equal(ret.status, 200);
313
- t.equal(ret.response, RESPONSE_BODY);
314
- t.end();
315
-
316
- // Close the test server, we are done with it.
317
- testServer.close();
318
- });
319
- });
320
-
321
- req.end(REQUEST_BODY);
322
- });
323
-
324
- });
325
-
326
- test('rec() throws when reenvoked with already recorder requests', function(t) {
327
- nock.restore();
328
- nock.recorder.clear();
329
- t.equal(nock.recorder.play().length, 0);
330
-
331
- nock.recorder.rec();
332
- try {
333
- nock.recorder.rec();
334
- // This line should never be reached.
335
- t.ok(false);
336
- t.end();
337
- } catch(e) {
338
- t.equal(e.toString(), 'Error: Nock recording already in progress');
339
- t.end();
340
- }
341
- });
342
-
343
- test('records https correctly', {skip: process.env.AIRPLANE}, function(t) {
344
- nock.restore();
345
- nock.recorder.clear();
346
- t.equal(nock.recorder.play().length, 0);
347
-
348
- var options = { method: 'POST'
349
- , host:'google.com'
350
- , path:'/' }
351
- ;
352
-
353
- nock.recorder.rec({
354
- dont_print: true,
355
- output_objects: true
356
- });
357
-
358
- var req = https.request(options, function(res) {
359
- res.resume();
360
- var ret;
361
- res.once('end', function() {
362
- nock.restore();
363
- ret = nock.recorder.play();
364
- t.equal(ret.length, 1);
365
- ret = ret[0];
366
- t.type(ret, 'object');
367
- t.equal(ret.scope, "https://google.com:443");
368
- t.equal(ret.method, "POST");
369
- t.ok(typeof(ret.status) !== 'undefined');
370
- t.ok(typeof(ret.response) !== 'undefined');
371
- t.end();
372
- });
373
- });
374
- req.end('012345');
375
- });
376
-
377
- test('records request headers correctly', {skip: process.env.AIRPLANE}, function(t) {
378
- nock.restore();
379
- nock.recorder.clear();
380
- t.equal(nock.recorder.play().length, 0);
381
-
382
- nock.recorder.rec({
383
- dont_print: true,
384
- output_objects: true,
385
- enable_reqheaders_recording: true
386
- });
387
-
388
- var req = http.request({
389
- hostname: 'www.example.com',
390
- path: '/',
391
- method: 'GET',
392
- auth: 'foo:bar'
393
- }, function(res) {
394
- res.resume();
395
- res.once('end', function() {
396
- nock.restore();
397
- var ret = nock.recorder.play();
398
- t.equal(ret.length, 1);
399
- ret = ret[0];
400
- t.type(ret, 'object');
401
- t.equivalent(ret.reqheaders, {
402
- host: 'www.example.com',
403
- 'authorization': 'Basic Zm9vOmJhcg=='
404
- });
405
- t.end();
406
- });
407
- }
408
- );
409
- req.end();
410
- });
411
-
412
- test('records and replays gzipped nocks correctly', {skip: process.env.AIRPLANE}, function(t) {
413
-
414
- nock.restore();
415
- nock.recorder.clear();
416
- t.equal(nock.recorder.play().length, 0);
417
-
418
- nock.recorder.rec({
419
- dont_print: true,
420
- output_objects: true
421
- });
422
-
423
- var makeRequest = function(callback) {
424
- superagent.get('https://bit.ly/1hKHiTe', callback);
425
- };
426
-
427
- makeRequest(function(err, resp) {
428
-
429
- t.ok(!err);
430
- t.ok(resp);
431
- t.ok(resp.headers);
432
- t.equal(resp.headers['content-encoding'], 'gzip');
433
-
434
- nock.restore();
435
- var nockDefs = nock.recorder.play();
436
- nock.recorder.clear();
437
- nock.activate();
438
-
439
- // Original bit.ly request is redirected to the target page.
440
- t.true(nockDefs.length > 1);
441
- var nocks = nock.define(nockDefs);
442
-
443
- makeRequest(function(mockedErr, mockedResp) {
444
-
445
- t.equal(err, mockedErr);
446
- t.deepEqual(mockedResp.body, resp.body);
447
- t.equal(mockedResp.headers['content-encoding'], 'gzip');
448
-
449
- _.each(nocks, function(nock) {
450
- nock.done();
451
- });
452
-
453
- t.end();
454
-
455
- });
456
- });
457
-
458
- });
459
-
460
- test('records and replays gzipped nocks correctly when gzip is returned as a string', {skip: process.env.AIRPLANE}, function(t) {
461
-
462
- nock.restore();
463
- nock.recorder.clear();
464
- t.equal(nock.recorder.play().length, 0);
465
-
466
- nock.recorder.rec({
467
- dont_print: true,
468
- output_objects: true
469
- });
470
-
471
- var makeRequest = function(callback) {
472
- rest.get('http://bit.ly/1hKHiTe', {'headers':{'Accept-Encoding':'gzip'}})
473
- .on('fail', function(data, response){
474
- t.ok(false);
475
- t.end();
476
- })
477
- .on('error', function(error, response) {
478
- t.ok(false);
479
- t.end();
480
- })
481
- .on('success', callback);
482
- };
483
-
484
- makeRequest(function(err, resp) {
485
-
486
- t.ok(resp);
487
- t.ok(resp.headers);
488
- t.equal(resp.headers['content-encoding'], 'gzip');
489
-
490
- nock.restore();
491
- var nockDefs = nock.recorder.play();
492
- nock.recorder.clear();
493
- nock.activate();
494
-
495
- // Original bit.ly request is redirected to the target page.
496
- t.true(nockDefs.length > 1);
497
- var nocks = nock.define(nockDefs);
498
-
499
- makeRequest(function(mockedErr, mockedResp) {
500
-
501
- t.equal(err, mockedErr);
502
- t.deepEqual(mockedResp.body, resp.body);
503
- t.equal(mockedResp.headers['content-encoding'], 'gzip');
504
-
505
- _.each(nocks, function(nock) {
506
- nock.done();
507
- });
508
-
509
- t.end();
510
-
511
- });
512
- });
513
-
514
- });
515
-
516
- test('records and replays nocks correctly', {skip: process.env.AIRPLANE}, function(t) {
517
-
518
- nock.restore();
519
- nock.recorder.clear();
520
- t.equal(nock.recorder.play().length, 0);
521
-
522
- nock.recorder.rec({
523
- dont_print: true,
524
- output_objects: true
525
- });
526
-
527
- var makeRequest = function(callback) {
528
-
529
- var options = {
530
- method: 'GET',
531
- uri: 'http://bit.ly/1hKHiTe',
532
- };
533
-
534
- mikealRequest(options, callback);
535
-
536
- };
537
-
538
- makeRequest(function(err, resp, body) {
539
-
540
- t.ok(!err);
541
- t.ok(resp);
542
- t.ok(body);
543
-
544
- nock.restore();
545
- var nockDefs = nock.recorder.play();
546
- nock.recorder.clear();
547
- nock.activate();
548
-
549
- // Original bit.ly request is redirected to the target page.
550
- t.true(nockDefs.length > 1);
551
- var nocks = nock.define(nockDefs);
552
-
553
- makeRequest(function(mockedErr, mockedResp, mockedBody) {
554
-
555
- t.equal(err, mockedErr);
556
- t.equal(body, mockedBody);
557
-
558
- _.each(nocks, function(nock) {
559
- nock.done();
560
- });
561
-
562
- t.end();
563
-
564
- });
565
- });
566
-
567
- });
568
-
569
- test('doesn\'t record request headers by default', {skip: process.env.AIRPLANE}, function(t) {
570
- nock.restore();
571
- nock.recorder.clear();
572
- t.equal(nock.recorder.play().length, 0);
573
-
574
- nock.recorder.rec({
575
- dont_print: true,
576
- output_objects: true
577
- });
578
-
579
- var req = http.request({
580
- hostname: 'www.example.com',
581
- path: '/',
582
- method: 'GET',
583
- auth: 'foo:bar'
584
- }, function(res) {
585
- res.resume();
586
- res.once('end', function() {
587
- nock.restore();
588
- var ret = nock.recorder.play();
589
- t.equal(ret.length, 1);
590
- ret = ret[0];
591
- t.type(ret, 'object');
592
- t.false(ret.reqheaders);
593
- t.end();
594
- });
595
- }
596
- );
597
- req.end();
598
- });
599
-
600
- test('will call a custom logging function', {skip: process.env.AIRPLANE}, function(t) {
601
- // This also tests that use_separator is on by default.
602
- nock.restore();
603
- nock.recorder.clear();
604
- t.equal(nock.recorder.play().length, 0);
605
-
606
- var record = [];
607
- var arrayLog = function(content) {
608
- record.push(content);
609
- }
610
-
611
- nock.recorder.rec({
612
- logging: arrayLog
613
- });
614
-
615
- var req = http.request({
616
- hostname: 'www.example.com',
617
- path: '/',
618
- method: 'GET',
619
- auth: 'foo:bar'
620
- }, function(res) {
621
- res.resume();
622
- res.once('end', function() {
623
- nock.restore();
624
-
625
- t.equal(record.length, 1)
626
- var ret = record[0]
627
- t.type(ret, 'string');
628
- t.end();
629
- });
630
- }
631
- );
632
- req.end();
633
- });
634
-
635
- test('use_separator:false is respected', {skip: process.env.AIRPLANE}, function(t) {
636
- nock.restore();
637
- nock.recorder.clear();
638
- t.equal(nock.recorder.play().length, 0);
639
-
640
- var record = [];
641
- var arrayLog = function(content) {
642
- record.push(content);
643
- }
644
-
645
- nock.recorder.rec({
646
- logging: arrayLog,
647
- output_objects: true,
648
- use_separator: false,
649
- });
650
-
651
- var req = http.request({
652
- hostname: 'www.example.com',
653
- path: '/',
654
- method: 'GET',
655
- auth: 'foo:bar'
656
- }, function(res) {
657
- res.resume();
658
- res.once('end', function() {
659
- nock.restore();
660
- t.equal(record.length, 1)
661
- var ret = record[0];
662
- t.type(ret, 'object'); // this is still an object, because the "cut here" strings have not been appended
663
- t.end();
664
- });
665
- }
666
- );
667
- req.end();
668
- });
669
-
670
- test('records request headers except user-agent if enable_reqheaders_recording is set to true', {skip: process.env.AIRPLANE}, function(t) {
671
- nock.restore();
672
- nock.recorder.clear();
673
- t.equal(nock.recorder.play().length, 0);
674
-
675
- nock.recorder.rec({
676
- dont_print: true,
677
- output_objects: true,
678
- enable_reqheaders_recording: true
679
- });
680
-
681
- var req = http.request({
682
- hostname: 'www.example.com',
683
- path: '/',
684
- method: 'GET',
685
- auth: 'foo:bar'
686
- }, function(res) {
687
- res.resume();
688
- res.once('end', function() {
689
- nock.restore();
690
- var ret = nock.recorder.play();
691
- t.equal(ret.length, 1);
692
- ret = ret[0];
693
- t.type(ret, 'object');
694
- t.true(ret.reqheaders);
695
- t.false(ret.reqheaders['user-agent']);
696
- t.end();
697
- });
698
- }
699
- );
700
- req.end();
701
- });
702
-
703
- test('includes query parameters from superagent', {skip: process.env.AIRPLANE}, function(t) {
704
- nock.restore();
705
- nock.recorder.clear();
706
- t.equal(nock.recorder.play().length, 0);
707
-
708
- nock.recorder.rec({
709
- dont_print: true,
710
- output_objects: true
711
- });
712
-
713
- superagent.get('http://google.com')
714
- .query({q: 'test search' })
715
- .end(function(res) {
716
- nock.restore();
717
- var ret = nock.recorder.play();
718
- t.true(ret.length >= 1);
719
- t.equal(ret[0].path, '/?q=test%20search');
720
- t.end();
721
- });
722
- });
723
-
724
- test('works with clients listening for readable', {skip: process.env.AIRPLANE}, function(t) {
725
- nock.restore();
726
- nock.recorder.clear();
727
- t.equal(nock.recorder.play().length, 0);
728
-
729
- var REQUEST_BODY = 'ABCDEF';
730
- var RESPONSE_BODY = '012345';
731
-
732
- // Create test http server and perform the tests while it's up.
733
- var testServer = http.createServer(function(req, res) {
734
- res.write(RESPONSE_BODY);
735
- res.end();
736
- }).listen(8081, function(err) {
737
-
738
- // t.equal(err, undefined);
739
-
740
- var options = { host:'localhost'
741
- , port:testServer.address().port
742
- , path:'/' }
743
- ;
744
-
745
- var rec_options = {
746
- dont_print: true,
747
- output_objects: true
748
- };
749
-
750
- nock.recorder.rec(rec_options);
751
-
752
- var req = http.request(options, function(res) {
753
- var readableCount = 0;
754
- var chunkCount = 0;
755
- res.on('readable', function() {
756
- ++readableCount;
757
- var chunk;
758
- while (null !== (chunk = res.read())) {
759
- t.equal(chunk.toString(), RESPONSE_BODY);
760
- ++chunkCount;
761
- }
762
- });
763
- res.once('end', function() {
764
- nock.restore();
765
- var ret = nock.recorder.play();
766
- t.equal(ret.length, 1);
767
- ret = ret[0];
768
- t.type(ret, 'object');
769
- t.equal(readableCount, 1);
770
- t.equal(chunkCount, 1);
771
- t.equal(ret.scope, "http://localhost:" + options.port);
772
- t.equal(ret.method, "GET");
773
- t.equal(ret.body, REQUEST_BODY);
774
- t.equal(ret.status, 200);
775
- t.equal(ret.response, RESPONSE_BODY);
776
- t.end();
777
-
778
- // Close the test server, we are done with it.
779
- testServer.close();
780
- });
781
- });
782
-
783
- req.end(REQUEST_BODY);
784
- });
785
-
786
- });
787
-
788
- test('outputs query string parameters using query()', {skip: process.env.AIRPLANE}, function(t) {
789
- nock.restore();
790
- nock.recorder.clear();
791
- t.equal(nock.recorder.play().length, 0);
792
-
793
- nock.recorder.rec(true);
794
-
795
- var makeRequest = function(callback) {
796
- superagent
797
- .get('https://example.com/')
798
- .query({'param1':1,'param2':2})
799
- .end(callback);
800
- };
801
-
802
- makeRequest(function(err, resp) {
803
- t.ok(!err, err && err.message || 'no error');
804
- t.ok(resp, 'have response');
805
- t.ok(resp.headers, 'have headers');
806
-
807
- var ret = nock.recorder.play();
808
- t.equal(ret.length, 1);
809
- t.type(ret[0], 'string');
810
- var match = "\nnock('https://example.com:443', {\"encodedQueryParams\":true})\n .get('/')\n .query({\"param1\":\"1\",\"param2\":\"2\"})\n .reply(";
811
- t.equal(ret[0].substring(0, match.length), match);
812
- t.end();
813
- });
814
- });
815
-
816
- test('removes query params from from that path and puts them in query()', {skip: process.env.AIRPLANE}, function(t) {
817
- nock.restore();
818
- nock.recorder.clear();
819
- t.equal(nock.recorder.play().length, 0);
820
- var options = { method: 'POST'
821
- , host:'google.com'
822
- , port:80
823
- , path:'/?param1=1&param2=2' }
824
- ;
825
-
826
- nock.recorder.rec(true);
827
- var req = http.request(options, function(res) {
828
- res.resume();
829
- var ret;
830
- res.once('end', function() {
831
- nock.restore();
832
- ret = nock.recorder.play();
833
- t.equal(ret.length, 1);
834
- t.type(ret[0], 'string');
835
- t.equal(ret[0].indexOf("\nnock('http://google.com:80', {\"encodedQueryParams\":true})\n .post('/', \"ABCDEF\")\n .query({\"param1\":\"1\",\"param2\":\"2\"})\n .reply("), 0);
836
- t.end();
837
- });
838
- });
839
- req.end('ABCDEF');
840
- });
841
-
842
- test("respects http.request() consumers", function(t) {
843
- // Create test http server and perform the tests while it's up.
844
- var testServer = http.createServer(function(req, res) {
845
- res.write('foo');
846
- setTimeout(function() {
847
- res.end('bar');
848
- }, 25);
849
- }).listen(8082, function(err) {
850
- t.equal(err, undefined);
851
-
852
- nock.restore();
853
- nock.recorder.clear();
854
- nock.recorder.rec({
855
- dont_print: true,
856
- output_objects: true
857
- });
858
-
859
-
860
- var options = { host:'localhost'
861
- , port:testServer.address().port
862
- , path:'/' }
863
- ;
864
- var req = http.request(options, function (res) {
865
- var buffer = new Buffer('');
866
-
867
- setTimeout(function () {
868
- res
869
- .on('data', function(data) {
870
- buffer = Buffer.concat([buffer, data]);
871
- })
872
- .on('end', function() {
873
- nock.restore();
874
- t.equal(buffer.toString(), 'foobar');
875
- t.end();
876
-
877
- // Close the test server, we are done with it.
878
- testServer.close();
879
- });
880
- });
881
- }, 50);
882
-
883
- req.end();
884
- });
885
- });
886
-
887
- test("teardown", function(t) {
888
- var leaks = Object.keys(global)
889
- .splice(globalCount, Number.MAX_VALUE);
890
-
891
- if (leaks.length == 1 && leaks[0] == '_key') {
892
- leaks = [];
893
- }
894
- t.deepEqual(leaks, [], 'No leaks');
895
- t.end();
896
- });