nock 7.7.3 → 8.0.0

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.
@@ -25,7 +25,7 @@ function testNock (t) {
25
25
  }, function(res) {
26
26
 
27
27
  t.equal(res.statusCode, 200);
28
- res.on('end', function() {
28
+ res.once('end', function() {
29
29
  t.ok(dataCalled);
30
30
  scope.done();
31
31
  t.end();
@@ -55,6 +55,11 @@ function nockBackWithFixture (t, scopesLoaded) {
55
55
  });
56
56
  }
57
57
 
58
+ function setOriginalModeOnEnd(t, nockBack) {
59
+ t.once('end', function() {
60
+ nockBack.setMode(originalMode);
61
+ });
62
+ }
58
63
 
59
64
 
60
65
 
@@ -87,21 +92,15 @@ tap.test('nockBack wild tests', function (nw) {
87
92
  testNock(t);
88
93
  });
89
94
 
90
-
91
95
  nw.test('nock back doesn\'t do anything', function (t) {
92
96
  nockBackWithFixture(t, false);
93
97
  });
94
98
 
95
- })
96
- .on('end', function () {
97
-
98
- nockBack.setMode(originalMode);
99
+ setOriginalModeOnEnd(nw, nockBack);
99
100
 
101
+ nw.end();
100
102
  });
101
103
 
102
-
103
-
104
-
105
104
  tap.test('nockBack dryrun tests', function (nw) {
106
105
 
107
106
  // Manually disable net connectivity to confirm that dryrun enables it.
@@ -179,7 +178,7 @@ tap.test('nockBack dryrun tests', function (nw) {
179
178
 
180
179
  });
181
180
 
182
- req.on('error', function(err) {
181
+ req.once('error', function(err) {
183
182
  if (err.code !== 'ECONNREFUSED') {
184
183
  throw err;
185
184
  }
@@ -189,16 +188,12 @@ tap.test('nockBack dryrun tests', function (nw) {
189
188
  req.end();
190
189
  });
191
190
  });
192
- })
193
- .on('end', function () {
194
191
 
195
- nockBack.setMode(originalMode);
192
+ setOriginalModeOnEnd(nw, nockBack);
196
193
 
194
+ nw.end();
197
195
  });
198
196
 
199
-
200
-
201
-
202
197
  tap.test('nockBack record tests', function (nw) {
203
198
  nockBack.setMode('record');
204
199
 
@@ -318,16 +313,10 @@ tap.test('nockBack record tests', function (nw) {
318
313
  });
319
314
 
320
315
  nw.end();
321
- })
322
- .on('end', function () {
323
-
324
- nockBack.setMode(originalMode);
325
316
 
317
+ setOriginalModeOnEnd(nw, nockBack);
326
318
  });
327
319
 
328
-
329
-
330
-
331
320
  tap.test('nockBack lockdown tests', function (nw) {
332
321
  nockBack.fixtures = __dirname + '/fixtures';
333
322
  nockBack.setMode('lockdown');
@@ -358,9 +347,8 @@ tap.test('nockBack lockdown tests', function (nw) {
358
347
 
359
348
  req.end();
360
349
  });
361
- })
362
- .on('end', function () {
363
350
 
364
- nockBack.setMode(originalMode);
351
+ setOriginalModeOnEnd(nw, nockBack);
365
352
 
353
+ nw.end();
366
354
  });
@@ -10,6 +10,12 @@ var fs = require('fs');
10
10
  var originalMode;
11
11
  var fixture;
12
12
 
13
+ function rimrafOnEnd(t) {
14
+ t.once('end', function() {
15
+ rimraf.sync(fixture);
16
+ });
17
+ }
18
+
13
19
  test('setup', function(t) {
14
20
  originalMode = nockBack.currentMode;
15
21
 
@@ -41,8 +47,8 @@ test('recording', function(t) {
41
47
  res.resume();
42
48
  });
43
49
  });
44
- }).once('end', function() {
45
- rimraf.sync(fixture);
50
+
51
+ rimrafOnEnd(t);
46
52
  });
47
53
 
48
54
  test('passes custom options to recorder', function(t) {
@@ -60,9 +66,8 @@ test('passes custom options to recorder', function(t) {
60
66
  // See https://nodejs.org/api/stream.html#stream_class_stream_readable
61
67
  res.resume();
62
68
  });
63
- })
64
- }).once('end', function() {
65
- rimraf.sync(fixture);
69
+ });
70
+ rimrafOnEnd(t);
66
71
  });
67
72
 
68
73
  test('teardown', function(t) {
@@ -23,7 +23,7 @@ test('emits request and replied events', function(t) {
23
23
  });
24
24
 
25
25
  test('emits no match when no match and no mock', function(t) {
26
- nock.emitter.on('no match', function(req) {
26
+ nock.emitter.once('no match', function(req) {
27
27
  t.end();
28
28
  });
29
29
 
@@ -15,15 +15,15 @@ var restify = require('restify');
15
15
  var domain = require('domain');
16
16
  var hyperquest = require('hyperquest');
17
17
  var _ = require('lodash');
18
+ var async = require('async');
18
19
 
19
20
  var globalCount;
20
21
 
21
22
  nock.enableNetConnect();
22
23
 
23
- test("setup", function(t) {
24
- globalCount = Object.keys(global).length;
25
- t.end();
26
- });
24
+ globalCount = Object.keys(global).length;
25
+ var acceptableLeaks = [
26
+ '_key', '__core-js_shared__', 'fetch', 'Response', 'Headers', 'Request'];
27
27
 
28
28
  test("double activation throws exception", function(t) {
29
29
  nock.restore();
@@ -288,7 +288,6 @@ test("post", function(t) {
288
288
  });
289
289
 
290
290
 
291
-
292
291
  test("post with empty response body", function(t) {
293
292
  var scope = nock('http://www.google.com')
294
293
  .post('/form')
@@ -1107,6 +1106,8 @@ test("body data is differentiating", function(t) {
1107
1106
  req.end('def');
1108
1107
  });
1109
1108
 
1109
+ t.end();
1110
+
1110
1111
  });
1111
1112
 
1112
1113
  test("chaining", function(t) {
@@ -1117,41 +1118,30 @@ test("chaining", function(t) {
1117
1118
  .post('/form')
1118
1119
  .reply(201, "OK!");
1119
1120
 
1120
- function endOne(t) {
1121
- repliedCount += 1;
1122
- if (t === 2) {
1123
- scope.done();
1124
- }
1125
- t.end();
1126
- }
1121
+ t.tearDown(scope.done.bind(scope));
1127
1122
 
1128
- t.test("post", function(t) {
1129
- var dataCalled;
1130
- var req = http.request({
1131
- host: "www.spiffy.com"
1132
- , method: 'POST'
1133
- , path: '/form'
1134
- , port: 80
1135
- }, function(res) {
1123
+ t.test("post", function(t) {
1124
+ var req = http.request({
1125
+ host: "www.spiffy.com"
1126
+ , method: 'POST'
1127
+ , path: '/form'
1128
+ , port: 80
1129
+ }, function(res) {
1136
1130
 
1137
- t.equal(res.statusCode, 201);
1138
- res.on('end', function() {
1139
- t.ok(dataCalled);
1140
- endOne(t);
1141
- });
1142
- res.on('data', function(data) {
1143
- dataCalled = true;
1144
- t.ok(data instanceof Buffer, "data should be buffer");
1145
- t.equal(data.toString(), "OK!", "response should match");
1146
- });
1131
+ t.equal(res.statusCode, 201);
1132
+ res.once('data', function(data) {
1133
+ t.ok(data instanceof Buffer, "data should be buffer");
1134
+ t.equal(data.toString(), "OK!", "response should match");
1147
1135
 
1136
+ res.once('end', t.end.bind(t));
1148
1137
  });
1149
1138
 
1150
- req.end();
1151
1139
  });
1152
1140
 
1141
+ req.end();
1142
+ });
1143
+
1153
1144
  t.test("get", function(t) {
1154
- var dataCalled;
1155
1145
  var req = http.request({
1156
1146
  host: "www.spiffy.com"
1157
1147
  , method: 'GET'
@@ -1160,21 +1150,19 @@ test("chaining", function(t) {
1160
1150
  }, function(res) {
1161
1151
 
1162
1152
  t.equal(res.statusCode, 200);
1163
- res.on('end', function() {
1164
- t.ok(dataCalled);
1165
- scope.done();
1166
- t.end();
1167
- });
1168
- res.on('data', function(data) {
1169
- dataCalled = true;
1153
+ res.once('data', function(data) {
1170
1154
  t.ok(data instanceof Buffer, "data should be buffer");
1171
1155
  t.equal(data.toString(), "Hello World!", "response should match");
1156
+
1157
+ res.once('end', t.end.bind(t));
1172
1158
  });
1173
1159
 
1174
1160
  });
1175
1161
 
1176
1162
  req.end();
1177
1163
  });
1164
+
1165
+ t.end();
1178
1166
  });
1179
1167
 
1180
1168
  test("encoding", function(t) {
@@ -1526,7 +1514,6 @@ test("abort request", function(t) {
1526
1514
  res.on('close', function(err) {
1527
1515
  t.equal(err.code, 'aborted');
1528
1516
  scope.done();
1529
- t.end();
1530
1517
  });
1531
1518
 
1532
1519
  res.on('end', function() {
@@ -1535,6 +1522,7 @@ test("abort request", function(t) {
1535
1522
 
1536
1523
  req.once('error', function(err) {
1537
1524
  t.equal(err.code, 'ECONNRESET');
1525
+ t.end();
1538
1526
  });
1539
1527
 
1540
1528
  req.abort();
@@ -2457,8 +2445,6 @@ test('persists interceptors', function(t) {
2457
2445
  });
2458
2446
 
2459
2447
  test("persist reply with file", function(t) {
2460
- var dataCalled = false
2461
-
2462
2448
  var scope = nock('http://www.filereplier.com')
2463
2449
  .persist()
2464
2450
  .get('/')
@@ -2466,7 +2452,8 @@ test("persist reply with file", function(t) {
2466
2452
  .get('/test')
2467
2453
  .reply(200, 'Yay!');
2468
2454
 
2469
- for (var i=0; i < 2; i++) {
2455
+ async.each([1,2], function(_, cb) {
2456
+ var dataCalled = false;
2470
2457
  var req = http.request({
2471
2458
  host: "www.filereplier.com"
2472
2459
  , path: '/'
@@ -2474,8 +2461,9 @@ test("persist reply with file", function(t) {
2474
2461
  }, function(res) {
2475
2462
 
2476
2463
  t.equal(res.statusCode, 200);
2477
- res.on('end', function() {
2464
+ res.once('end', function() {
2478
2465
  t.ok(dataCalled);
2466
+ cb();
2479
2467
  });
2480
2468
  res.on('data', function(data) {
2481
2469
  dataCalled = true;
@@ -2484,11 +2472,10 @@ test("persist reply with file", function(t) {
2484
2472
 
2485
2473
  });
2486
2474
  req.end();
2487
- }
2488
- t.end();
2489
-
2475
+ }, t.end.bind(t));
2490
2476
  });
2491
2477
 
2478
+
2492
2479
  test('(re-)activate after restore', function(t) {
2493
2480
  var scope = nock('http://google.com')
2494
2481
  .get('/')
@@ -2728,9 +2715,9 @@ test('enable real HTTP request only for google.com, via string', function(t) {
2728
2715
  throw "should not deliver this request"
2729
2716
  }).on('error', function (err) {
2730
2717
  t.equal(err.message, 'Nock: Not allow net connect for "www.amazon.com:80/"');
2718
+ t.end();
2731
2719
  });
2732
2720
 
2733
- t.end();
2734
2721
  nock.enableNetConnect();
2735
2722
  });
2736
2723
 
@@ -2761,10 +2748,10 @@ test('repeating once', function(t) {
2761
2748
 
2762
2749
  http.get('http://zombo.com', function(res) {
2763
2750
  t.equal(200, res.statusCode, 'first request');
2751
+ t.end();
2764
2752
  });
2765
2753
 
2766
2754
  nock.cleanAll()
2767
- t.end();
2768
2755
 
2769
2756
  nock.enableNetConnect();
2770
2757
  });
@@ -2777,16 +2764,12 @@ test('repeating twice', function(t) {
2777
2764
  .twice()
2778
2765
  .reply(200, "Hello World!");
2779
2766
 
2780
- for (var i=0; i < 2; i++) {
2767
+ async.each([1,2], function(_, cb) {
2781
2768
  http.get('http://zombo.com', function(res) {
2782
- t.equal(200, res.statusCode, 'first request');
2769
+ t.equal(200, res.statusCode);
2770
+ cb();
2783
2771
  });
2784
- };
2785
-
2786
- nock.cleanAll()
2787
- t.end();
2788
-
2789
- nock.enableNetConnect();
2772
+ }, t.end.bind(t));
2790
2773
  });
2791
2774
 
2792
2775
  test('repeating thrice', function(t) {
@@ -2797,16 +2780,12 @@ test('repeating thrice', function(t) {
2797
2780
  .thrice()
2798
2781
  .reply(200, "Hello World!");
2799
2782
 
2800
- for (var i=0; i < 3; i++) {
2783
+ async.each([1,2,3], function(_, cb) {
2801
2784
  http.get('http://zombo.com', function(res) {
2802
- t.equal(200, res.statusCode, 'first request');
2785
+ t.equal(200, res.statusCode);
2786
+ cb();
2803
2787
  });
2804
- };
2805
-
2806
- nock.cleanAll()
2807
- t.end();
2808
-
2809
- nock.enableNetConnect();
2788
+ }, t.end.bind(t));
2810
2789
  });
2811
2790
 
2812
2791
  test('repeating response 4 times', function(t) {
@@ -2817,19 +2796,14 @@ test('repeating response 4 times', function(t) {
2817
2796
  .times(4)
2818
2797
  .reply(200, "Hello World!");
2819
2798
 
2820
- for (var i=0; i < 4; i++) {
2799
+ async.each([1,2,3,4], function(_, cb) {
2821
2800
  http.get('http://zombo.com', function(res) {
2822
2801
  t.equal(200, res.statusCode, 'first request');
2802
+ cb();
2823
2803
  });
2824
- };
2825
-
2826
- nock.cleanAll()
2827
- t.end();
2828
-
2829
- nock.enableNetConnect();
2804
+ }, t.end.bind(t));
2830
2805
  });
2831
2806
 
2832
-
2833
2807
  test('superagent works', function(t) {
2834
2808
  var responseText = 'Yay superagent!';
2835
2809
  var headers = { 'Content-Type': 'text/plain'};
@@ -2927,7 +2901,10 @@ test('response is an http.IncomingMessage instance', function(t) {
2927
2901
  function checkDuration(t, ms) {
2928
2902
  var _end = t.end;
2929
2903
  var start = process.hrtime();
2904
+ var ended = false;
2930
2905
  t.end = function () {
2906
+ if (ended) return;
2907
+ ended = true;
2931
2908
  var fin = process.hrtime(start);
2932
2909
  var finMs =
2933
2910
  (fin[0] * 1e+9) + // seconds -> ms
@@ -2942,72 +2919,65 @@ function checkDuration(t, ms) {
2942
2919
  }
2943
2920
 
2944
2921
  test('calling delay could cause mikealRequest timeout error', function(t) {
2945
- var scope = nock('http://funk')
2946
- .get('/')
2947
- .delay({
2948
- head: 300
2949
- })
2950
- .reply(200, 'OK');
2922
+ var scope = nock('http://funk')
2923
+ .get('/')
2924
+ .delay({
2925
+ head: 300
2926
+ })
2927
+ .reply(200, 'OK');
2951
2928
 
2952
- mikealRequest({
2953
- uri: 'http://funk',
2954
- method: 'GET',
2955
- timeout: 100
2956
- }, function (err) {
2957
- scope.done();
2958
- t.equal(err && err.code, "ETIMEDOUT");
2959
- t.end();
2960
- });
2929
+ mikealRequest({
2930
+ uri: 'http://funk',
2931
+ method: 'GET',
2932
+ timeout: 100
2933
+ }, function (err) {
2934
+ scope.done();
2935
+ t.equal(err && err.code, "ETIMEDOUT");
2936
+ t.end();
2937
+ });
2961
2938
  });
2962
2939
 
2963
- test('Body delay does not have impact on timeoput', function(t) {
2964
- var scope = nock('http://funk')
2965
- .get('/')
2966
- .delay({
2967
- head: 300,
2968
- body: 300
2969
- })
2970
- .reply(200, 'OK');
2940
+ test('Body delay does not have impact on timeout', function(t) {
2941
+ var scope = nock('http://funk')
2942
+ .get('/')
2943
+ .delay({
2944
+ head: 300,
2945
+ body: 300
2946
+ })
2947
+ .reply(200, 'OK');
2971
2948
 
2972
- mikealRequest({
2973
- uri: 'http://funk',
2974
- method: 'GET',
2975
- timeout: 500
2976
- }, function (err, r, body) {
2977
- t.equal(err, null);
2978
- t.equal(body, 'OK');
2979
- t.equal(r.statusCode, 200);
2980
- scope.done();
2981
- t.end();
2982
- });
2949
+ mikealRequest({
2950
+ uri: 'http://funk',
2951
+ method: 'GET',
2952
+ timeout: 500
2953
+ }, function (err, r, body) {
2954
+ t.equal(err, null);
2955
+ t.equal(body, 'OK');
2956
+ t.equal(r.statusCode, 200);
2957
+ scope.done();
2958
+ t.end();
2959
+ });
2983
2960
  });
2984
2961
 
2985
2962
  test('calling delay with "body" and "head" delays the response', function (t) {
2986
- checkDuration(t, 600);
2987
-
2988
- nock('http://funk')
2989
- .get('/')
2990
- .delay({
2991
- head: 300,
2992
- body: 300
2993
- })
2994
- .reply(200, 'OK');
2995
-
2996
- http.get('http://funk/', function (res) {
2997
- res.setEncoding('utf8');
2998
-
2999
- var body = '';
2963
+ checkDuration(t, 600);
3000
2964
 
3001
- res.on('data', function(chunk) {
3002
- body += chunk;
3003
- });
2965
+ nock('http://funk')
2966
+ .get('/')
2967
+ .delay({
2968
+ head: 300,
2969
+ body: 300
2970
+ })
2971
+ .reply(200, 'OK');
3004
2972
 
3005
- res.once('end', function() {
3006
- t.equal(body, 'OK');
3007
- t.end();
3008
- });
2973
+ http.get('http://funk/', function (res) {
2974
+ res.once('data', function(data) {
2975
+ t.equal(data.toString(), 'OK');
2976
+ res.once('end', t.end.bind(t));
3009
2977
  });
2978
+ });
3010
2979
  });
2980
+
3011
2981
  test('calling delay with "body" delays the response body', function (t) {
3012
2982
  checkDuration(t, 100);
3013
2983
 
@@ -3469,6 +3439,7 @@ test('define() works with binary buffers', function(t) {
3469
3439
  });
3470
3440
 
3471
3441
  test('issue #163 - Authorization header isn\'t mocked', function(t) {
3442
+ nock.enableNetConnect();
3472
3443
  function makeRequest(cb) {
3473
3444
  var r = http.request(
3474
3445
  {
@@ -4038,6 +4009,7 @@ test('response readable pull stream works as expected', function(t) {
4038
4009
  , port: 80
4039
4010
  }, function(res) {
4040
4011
 
4012
+ var ended = false;
4041
4013
  var responseBody = '';
4042
4014
  t.equal(res.statusCode, 200);
4043
4015
  res.on('readable', function() {
@@ -4045,7 +4017,8 @@ test('response readable pull stream works as expected', function(t) {
4045
4017
  while (null !== (chunk = res.read())) {
4046
4018
  responseBody += chunk.toString();
4047
4019
  }
4048
- if (chunk === null) {
4020
+ if (chunk === null && ! ended) {
4021
+ ended = true;
4049
4022
  t.equal(responseBody, "this is the response body yeah");
4050
4023
  t.end();
4051
4024
  }
@@ -4408,7 +4381,7 @@ test('remove interceptor removes given interceptor for regex path', function(t)
4408
4381
  res.setEncoding('utf8');
4409
4382
  t.equal(res.statusCode, 202);
4410
4383
 
4411
- res.on('data', function(data) {
4384
+ res.once('data', function(data) {
4412
4385
  t.equal(data, 'other-content');
4413
4386
  });
4414
4387
 
@@ -4480,28 +4453,37 @@ test('you must setup an interceptor for each request', function(t) {
4480
4453
 
4481
4454
  test('calling socketDelay will emit a timeout', function (t) {
4482
4455
  nock('http://www.example.com')
4483
- .get('/')
4484
- .socketDelay(10000)
4485
- .reply(200, 'OK');
4456
+ .get('/')
4457
+ .socketDelay(10000)
4458
+ .reply(200, 'OK');
4459
+
4460
+ var timedout = false;
4461
+ var ended = false;
4486
4462
 
4487
4463
  var req = http.request('http://www.example.com', function (res) {
4488
- res.setEncoding('utf8');
4464
+ res.setEncoding('utf8');
4489
4465
 
4490
- var body = '';
4466
+ var body = '';
4491
4467
 
4492
- res.on('data', function(chunk) {
4493
- body += chunk;
4494
- });
4468
+ res.on('data', function(chunk) {
4469
+ body += chunk;
4470
+ });
4495
4471
 
4496
- res.once('end', function() {
4497
- t.fail('socket did not timeout when idle');
4498
- t.end();
4499
- });
4472
+ res.once('end', function() {
4473
+ ended = true;
4474
+ if (! timedout) {
4475
+ t.fail('socket did not timeout when idle');
4476
+ t.end();
4477
+ }
4478
+ });
4500
4479
  });
4501
4480
 
4502
4481
  req.setTimeout(5000, function () {
4482
+ timedout = true;
4483
+ if (! ended) {
4503
4484
  t.ok(true);
4504
4485
  t.end();
4486
+ }
4505
4487
  });
4506
4488
 
4507
4489
  req.end();
@@ -4871,9 +4853,10 @@ test("teardown", function(t) {
4871
4853
  var leaks = Object.keys(global)
4872
4854
  .splice(globalCount, Number.MAX_VALUE);
4873
4855
 
4874
- if (leaks.length == 1 && leaks[0] == '_key') {
4875
- leaks = [];
4876
- }
4856
+ leaks = leaks.filter(function(key) {
4857
+ return acceptableLeaks.indexOf(key) == -1;
4858
+ });
4859
+
4877
4860
  t.deepEqual(leaks, [], 'No leaks');
4878
4861
  t.end();
4879
4862
  });