nock 8.0.0 → 8.2.2

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 (42) hide show
  1. package/.travis.yml +1 -0
  2. package/CHANGELOG.md +107 -114
  3. package/README.md +103 -42
  4. package/lib/common.js +15 -1
  5. package/lib/intercept.js +24 -11
  6. package/lib/interceptor.js +21 -22
  7. package/lib/match_body.js +1 -1
  8. package/lib/recorder.js +6 -3
  9. package/lib/request_overrider.js +3 -3
  10. package/lib/scope.js +30 -28
  11. package/package.json +10 -7
  12. package/tests/test_back.js +5 -2
  13. package/tests/test_body_match.js +40 -0
  14. package/tests/test_browserify.js +54 -52
  15. package/tests/test_intercept.js +195 -47
  16. package/tests/test_isomorphic_fetch.js +45 -45
  17. package/.npmignore +0 -2
  18. package/coverage/coverage.json +0 -1
  19. package/coverage/lcov-report/base.css +0 -212
  20. package/coverage/lcov-report/index.html +0 -106
  21. package/coverage/lcov-report/nock/index.html +0 -93
  22. package/coverage/lcov-report/nock/index.js.html +0 -98
  23. package/coverage/lcov-report/nock/lib/back.js.html +0 -1022
  24. package/coverage/lcov-report/nock/lib/common.js.html +0 -1070
  25. package/coverage/lcov-report/nock/lib/delayed_body.js.html +0 -305
  26. package/coverage/lcov-report/nock/lib/global_emitter.js.html +0 -71
  27. package/coverage/lcov-report/nock/lib/index.html +0 -223
  28. package/coverage/lcov-report/nock/lib/intercept.js.html +0 -1253
  29. package/coverage/lcov-report/nock/lib/interceptor.js.html +0 -1670
  30. package/coverage/lcov-report/nock/lib/match_body.js.html +0 -275
  31. package/coverage/lcov-report/nock/lib/mixin.js.html +0 -107
  32. package/coverage/lcov-report/nock/lib/recorder.js.html +0 -1323
  33. package/coverage/lcov-report/nock/lib/request_overrider.js.html +0 -1643
  34. package/coverage/lcov-report/nock/lib/scope.js.html +0 -1133
  35. package/coverage/lcov-report/nock/lib/socket.js.html +0 -272
  36. package/coverage/lcov-report/prettify.css +0 -1
  37. package/coverage/lcov-report/prettify.js +0 -1
  38. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  39. package/coverage/lcov-report/sorter.js +0 -158
  40. package/coverage/lcov.info +0 -2428
  41. package/tests/browserify-public/browserify-bundle.js +0 -34332
  42. package/tests/test.js +0 -32
@@ -192,7 +192,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
192
192
  req.once = req.on = function(event, listener) {
193
193
  // emit a fake socket.
194
194
  if (event == 'socket') {
195
- listener(req.socket);
195
+ listener.call(req, req.socket);
196
196
  req.socket.emit('connect', req.socket);
197
197
  req.socket.emit('secureConnect', req.socket);
198
198
  }
@@ -384,7 +384,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
384
384
  responseBody = responseBody[1];
385
385
 
386
386
  response.rawHeaders = response.rawHeaders || [];
387
- Object.keys(response.headers).forEach(function(key) {
387
+ Object.keys(response.headers).forEach(function(key) {
388
388
  response.rawHeaders.push(key, response.headers[key]);
389
389
  });
390
390
  }
@@ -450,7 +450,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
450
450
  var key = response.rawHeaders[rawHeaderIndex];
451
451
  var value = response.rawHeaders[rawHeaderIndex + 1];
452
452
  if (typeof value === "function") {
453
- response.rawHeaders[rawHeaderIndex + 1] = evaluatedHeaders[key];
453
+ response.rawHeaders[rawHeaderIndex + 1] = evaluatedHeaders[key.toLowerCase()];
454
454
  }
455
455
  }
456
456
 
package/lib/scope.js CHANGED
@@ -118,39 +118,40 @@ Scope.prototype.delete = function _delete(uri, requestBody, options) {
118
118
  return this.intercept(uri, 'DELETE', requestBody, options);
119
119
  };
120
120
 
121
+ Scope.prototype.options = function _options(uri, requestBody, options) {
122
+ return this.intercept(uri, 'OPTIONS', requestBody, options);
123
+ };
124
+
121
125
  Scope.prototype.pendingMocks = function pendingMocks() {
122
- return Object.keys(this.keyedInterceptors);
126
+ var self = this;
127
+
128
+ var pendingInterceptorKeys = Object.keys(this.keyedInterceptors).filter(function (key) {
129
+ var interceptorList = self.keyedInterceptors[key];
130
+ var pendingInterceptors = interceptorList.filter(function (interceptor) {
131
+ // TODO: This assumes that completed mocks are removed from the keyedInterceptors list
132
+ // (when persistence is off). We should change that (and this) in future.
133
+ var persistedAndUsed = self._persist && interceptor.interceptionCounter > 0;
134
+ return !persistedAndUsed && !interceptor.optional;
135
+ });
136
+ return pendingInterceptors.length > 0;
137
+ });
138
+
139
+ return pendingInterceptorKeys;
123
140
  };
124
141
 
142
+ // Returns all keyedInterceptors that are active.
143
+ // This incomplete interceptors, persisted but complete interceptors, and
144
+ // optional interceptors, but not non-persisted and completed interceptors.
145
+ Scope.prototype.activeMocks = function activeMocks() {
146
+ return Object.keys(this.keyedInterceptors);
147
+ }
148
+
125
149
  Scope.prototype.isDone = function isDone() {
126
150
  var self = this;
127
151
  // if nock is turned off, it always says it's done
128
152
  if (! globalIntercept.isOn()) { return true; }
129
153
 
130
- var keys = Object.keys(this.keyedInterceptors);
131
- if (keys.length === 0) {
132
- return true;
133
- } else {
134
- var doneHostCount = 0;
135
-
136
- keys.forEach(function(key) {
137
- var doneInterceptorCount = 0;
138
-
139
- self.keyedInterceptors[key].forEach(function(interceptor) {
140
- var isRequireDoneDefined = !_.isUndefined(interceptor.options.requireDone);
141
- if (isRequireDoneDefined && interceptor.options.requireDone === false) {
142
- doneInterceptorCount += 1;
143
- } else if (self._persist && interceptor.interceptionCounter > 0) {
144
- doneInterceptorCount += 1;
145
- }
146
- });
147
-
148
- if (doneInterceptorCount === self.keyedInterceptors[key].length ) {
149
- doneHostCount += 1;
150
- }
151
- });
152
- return (doneHostCount === keys.length);
153
- }
154
+ return this.pendingMocks().length === 0;
154
155
  };
155
156
 
156
157
  Scope.prototype.done = function done() {
@@ -294,7 +295,7 @@ function define(nockDefs) {
294
295
  , npath = nockDef.path
295
296
  , method = nockDef.method.toLowerCase() || "get"
296
297
  , status = getStatusFromDefinition(nockDef)
297
- , headers = nockDef.headers || {}
298
+ , rawHeaders = nockDef.rawHeaders || []
298
299
  , reqheaders = nockDef.reqheaders || {}
299
300
  , body = nockDef.body || ''
300
301
  , options = nockDef.options || {};
@@ -318,7 +319,7 @@ function define(nockDefs) {
318
319
  if (body==="*") {
319
320
  nock = startScope(nscope, options).filteringRequestBody(function() {
320
321
  return "*";
321
- })[method](npath, "*").reply(status, response, headers);
322
+ })[method](npath, "*").reply(status, response, rawHeaders);
322
323
  } else {
323
324
  nock = startScope(nscope, options);
324
325
  // If request headers were specified filter by them.
@@ -330,7 +331,7 @@ function define(nockDefs) {
330
331
  if (nockDef.filteringRequestBody) {
331
332
  nock.filteringRequestBody(nockDef.filteringRequestBody);
332
333
  }
333
- nock.intercept(npath, method, body).reply(status, response, headers);
334
+ nock.intercept(npath, method, body).reply(status, response, rawHeaders);
334
335
  }
335
336
 
336
337
  nocks.push(nock);
@@ -346,6 +347,7 @@ module.exports = extend(startScope, {
346
347
  isActive: globalIntercept.isActive,
347
348
  isDone: globalIntercept.isDone,
348
349
  pendingMocks: globalIntercept.pendingMocks,
350
+ activeMocks: globalIntercept.activeMocks,
349
351
  removeInterceptor: globalIntercept.removeInterceptor,
350
352
  disableNetConnect: globalIntercept.disableNetConnect,
351
353
  enableNetConnect: globalIntercept.enableNetConnect,
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "8.0.0",
10
+ "version": "8.2.2",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "contributors": [
13
13
  {
@@ -167,24 +167,27 @@
167
167
  "debug": "^2.2.0",
168
168
  "deep-equal": "^1.0.0",
169
169
  "json-stringify-safe": "^5.0.1",
170
- "lodash": "^4.8.2",
170
+ "lodash": "~4.9.0",
171
171
  "mkdirp": "^0.5.0",
172
172
  "propagate": "0.4.0",
173
173
  "qs": "^6.0.2"
174
174
  },
175
175
  "devDependencies": {
176
- "async": "^1.5.2",
176
+ "async": "^2.1.1",
177
177
  "aws-sdk": "^2.0.15",
178
178
  "browserify": "^13.0.0",
179
+ "changelog": "^1.0.7",
179
180
  "coveralls": "^2.11.2",
180
181
  "hyperquest": "^1.3.0",
181
182
  "isomorphic-fetch": "^2.2.0",
182
183
  "istanbul": "^0.4.2",
183
184
  "jshint": "^2.5.6",
185
+ "markdown-toc": "^0.13.0",
184
186
  "needle": "^1.0.0",
185
187
  "node-static": "^0.7.7",
188
+ "nyc": "^8.3.0",
186
189
  "pre-commit": "1.1.2",
187
- "request": "2.70.0",
190
+ "request": "2.71.0",
188
191
  "request-promise": "^2.0.1",
189
192
  "restify": "^4.0.4",
190
193
  "restler": "3.4.0",
@@ -194,9 +197,9 @@
194
197
  "zombie": "^4.2.1"
195
198
  },
196
199
  "scripts": {
197
- "test": "node --harmony tests/test.js",
198
- "coverage": "node --harmony node_modules/istanbul/lib/cli.js cover tests/test.js && istanbul check-coverage",
199
- "coveralls": "cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
200
+ "test": "tap --harmony ./tests/test_*.js",
201
+ "coverage": "nyc --reporter=lcov tap --harmony ./tests/test_*.js",
202
+ "coveralls": "cat ./coverage/lcov.info | coveralls",
200
203
  "jshint": "jshint lib/*.js",
201
204
  "changelog": "changelog nock all -m > CHANGELOG.md"
202
205
  },
@@ -116,7 +116,10 @@ tap.test('nockBack dryrun tests', function (nw) {
116
116
  , port: 80
117
117
  }, function(res) {
118
118
 
119
- t.ok([200, 302].indexOf(res.statusCode) >= 0);
119
+ res.on('data', function() {
120
+ //node v 0.10 requires this listener
121
+ });
122
+ t.ok([200, 301, 302].indexOf(res.statusCode) >= 0);
120
123
  t.end();
121
124
 
122
125
  });
@@ -156,7 +159,7 @@ tap.test('nockBack dryrun tests', function (nw) {
156
159
  , port: 80
157
160
  }, function(res) {
158
161
 
159
- t.ok([200, 302].indexOf(res.statusCode) >= 0);
162
+ t.ok([200, 301, 302].indexOf(res.statusCode) >= 0);
160
163
  res.on('end', function() {
161
164
  var doneFails = false;
162
165
 
@@ -26,3 +26,43 @@ test('match body with regex', function (t) {
26
26
  });
27
27
 
28
28
  });
29
+
30
+ test('match body with regex inside array', function (t) {
31
+
32
+ nock('http://encodingsareus.com')
33
+ .post('/', {items: [{name: /t.+/}]})
34
+ .reply(200);
35
+
36
+ mikealRequest({
37
+ url: 'http://encodingsareus.com/',
38
+ method: 'post',
39
+ json: {
40
+ items: [{
41
+ name: 'test'
42
+ }]
43
+ },
44
+ }, function(err, res) {
45
+ if (err) throw err;
46
+ assert.equal(res.statusCode, 200);
47
+ t.end();
48
+ });
49
+ })
50
+
51
+ test('match body with empty object inside', function (t) {
52
+
53
+ nock('http://encodingsareus.com')
54
+ .post('/', { obj: {}})
55
+ .reply(200);
56
+
57
+ mikealRequest({
58
+ url: 'http://encodingsareus.com/',
59
+ method: 'post',
60
+ json: {
61
+ obj: {}
62
+ },
63
+ }, function(err, res) {
64
+ if (err) throw err;
65
+ assert.equal(res.statusCode, 200);
66
+ t.end();
67
+ });
68
+ })
@@ -1,54 +1,56 @@
1
1
  'use strict';
2
2
 
3
- var fs = require('fs');
4
- var path = require('path');
5
- var http = require('http');
6
- var browserify = require('browserify');
7
- var Static = require('node-static');
8
- var Browser = require('zombie');
9
-
10
- var test = require('tap').test;
11
- var before = test;
12
- var after = test;
13
-
14
- var nock = require('../.');
15
-
16
- nock.enableNetConnect();
17
-
18
- var server;
19
-
20
- before('prepare bundle', {timeout: 10000}, function(t) {
21
- var b = browserify();
22
- b.add(path.join(__dirname, 'fixtures', 'browserify-script.js'));
23
- b.bundle().pipe(fs.createWriteStream(path.join(__dirname, 'browserify-public', 'browserify-bundle.js'))).once('finish', function() {
24
- t.end();
25
- });
26
- });
27
-
28
- before('start server', function(t) {
29
- var file = new Static.Server(path.join(__dirname, 'browserify-public'));
30
- server = http.createServer(function(req, res) {
31
- file.serve(req, res);
32
- });
33
-
34
- server.listen(8080, t.end.bind(t));
35
- });
36
-
37
- test('run bundle', function(t) {
38
- Browser.localhost('server.com', 8080);
39
-
40
- var browser = new Browser();
41
-
42
- browser.on('error', function(err) {
43
- console.error('BROWSER ERROR: ' + err.stack);
44
- });
45
-
46
- browser.visit('/', function() {
47
- browser.assert.text('#content', 'boop');
48
- t.end();
49
- });
50
- });
51
-
52
- after('stop server', function(t) {
53
- server.close(t.end.bind(t));
54
- });
3
+ if (process.versions.node >= '0.11' ) {
4
+ var fs = require('fs');
5
+ var path = require('path');
6
+ var http = require('http');
7
+ var browserify = require('browserify');
8
+ var Static = require('node-static');
9
+ var Browser = require('zombie');
10
+
11
+ var test = require('tap').test;
12
+ var before = test;
13
+ var after = test;
14
+
15
+ var nock = require('../.');
16
+
17
+ nock.enableNetConnect();
18
+
19
+ var server;
20
+
21
+ before('prepare bundle', {timeout: 10000}, function(t) {
22
+ var b = browserify();
23
+ b.add(path.join(__dirname, 'fixtures', 'browserify-script.js'));
24
+ b.bundle().pipe(fs.createWriteStream(path.join(__dirname, 'browserify-public', 'browserify-bundle.js'))).once('finish', function() {
25
+ t.end();
26
+ });
27
+ });
28
+
29
+ before('start server', function(t) {
30
+ var file = new Static.Server(path.join(__dirname, 'browserify-public'));
31
+ server = http.createServer(function(req, res) {
32
+ file.serve(req, res);
33
+ });
34
+
35
+ server.listen(8080, t.end.bind(t));
36
+ });
37
+
38
+ test('run bundle', function(t) {
39
+ Browser.localhost('server.com', 8080);
40
+
41
+ var browser = new Browser();
42
+
43
+ browser.on('error', function(err) {
44
+ console.error('BROWSER ERROR: ' + err.stack);
45
+ });
46
+
47
+ browser.visit('/', function() {
48
+ browser.assert.text('#content', 'boop');
49
+ t.end();
50
+ });
51
+ });
52
+
53
+ after('stop server', function(t) {
54
+ server.close(t.end.bind(t));
55
+ });
56
+ }
@@ -622,22 +622,6 @@ test("isDone", function(t) {
622
622
  req.end();
623
623
  });
624
624
 
625
- test("requireDone", function(t) {
626
- var scope = nock('http://www.google.com')
627
- .get('/', false, { requireDone: false })
628
- .reply(200, "Hello World!");
629
-
630
- t.ok(scope.isDone(), "done when a requireDone is set to false");
631
-
632
- scope.get('/', false, { requireDone: true})
633
- .reply(200, "Hello World!");
634
-
635
- t.notOk(scope.isDone(), "not done when a requireDone is explicitly set to true");
636
-
637
- nock.cleanAll()
638
- t.end();
639
- });
640
-
641
625
  test("request headers exposed", function(t) {
642
626
 
643
627
  var scope = nock('http://www.headdy.com')
@@ -724,7 +708,7 @@ test("reply headers as function work", function(t) {
724
708
  path: '/'
725
709
  }, function (res) {
726
710
  t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
727
- t.equivalent(res.rawHeaders, ['x-my-headers', 'boo!']); // 67
711
+ t.equivalent(res.rawHeaders, ['X-My-Headers', 'boo!']); // 67
728
712
  t.end();
729
713
  });
730
714
  });
@@ -745,7 +729,7 @@ test("reply headers as function are evaluated only once per request", function(t
745
729
  path: '/'
746
730
  }, function (res) {
747
731
  t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
748
- t.equivalent(res.rawHeaders, ['x-my-headers', 'boo!']);
732
+ t.equivalent(res.rawHeaders, ['X-My-Headers', 'boo!']);
749
733
  t.equal(counter, 1);
750
734
  t.end();
751
735
  });
@@ -767,14 +751,14 @@ test("reply headers as function are evaluated on each request", function(t) {
767
751
  path: '/'
768
752
  }, function (res) {
769
753
  t.equivalent(res.headers, { 'x-my-headers': '1' });
770
- t.equivalent(res.rawHeaders, ['x-my-headers', '1']);
754
+ t.equivalent(res.rawHeaders, ['X-My-Headers', '1']);
771
755
  t.equal(counter, 1);
772
756
  http.get({
773
757
  host: 'example.com',
774
758
  path: '/'
775
759
  }, function (res) {
776
760
  t.equivalent(res.headers, { 'x-my-headers': '2' });
777
- t.equivalent(res.rawHeaders, ['x-my-headers', '2']);
761
+ t.equivalent(res.rawHeaders, ['X-My-Headers', '2']);
778
762
  t.equal(counter, 2);
779
763
  t.end();
780
764
  });
@@ -2297,6 +2281,129 @@ test('pending mocks works', function(t) {
2297
2281
  });
2298
2282
  });
2299
2283
 
2284
+ test('pending mocks doesn\'t include optional mocks', function(t) {
2285
+ var scope = nock('http://example.com')
2286
+ .get('/nonexistent')
2287
+ .optionally()
2288
+ .reply(200);
2289
+
2290
+ t.deepEqual(nock.pendingMocks(), []);
2291
+ t.end();
2292
+ });
2293
+
2294
+ test('optional mocks are still functional', function(t) {
2295
+ var scope = nock('http://example.com')
2296
+ .get('/abc')
2297
+ .optionally()
2298
+ .reply(200);
2299
+
2300
+ var req = http.get({host: 'example.com', path: '/abc'}, function(res) {
2301
+ t.assert(res.statusCode === 200, "should still mock requests");
2302
+ t.deepEqual(nock.pendingMocks(), []);
2303
+ t.end();
2304
+ });
2305
+ });
2306
+
2307
+ test('isDone is true with optional mocks outstanding', function(t) {
2308
+ var scope = nock('http://example.com')
2309
+ .get('/abc')
2310
+ .optionally()
2311
+ .reply(200);
2312
+
2313
+ t.ok(scope.isDone());
2314
+ t.end();
2315
+ });
2316
+
2317
+ test('optional but persisted mocks persist, but never appear as pending', function(t) {
2318
+ var scope = nock('http://example.com')
2319
+ .get('/123')
2320
+ .optionally()
2321
+ .reply(200)
2322
+ .persist();
2323
+
2324
+ t.deepEqual(nock.pendingMocks(), []);
2325
+ var req = http.get({host: 'example.com', path: '/123'}, function(res) {
2326
+ t.assert(res.statusCode === 200, "should mock first request");
2327
+ t.deepEqual(nock.pendingMocks(), []);
2328
+
2329
+ var req = http.get({host: 'example.com', path: '/123'}, function(res) {
2330
+ t.assert(res.statusCode === 200, "should mock second request");
2331
+ t.deepEqual(nock.pendingMocks(), []);
2332
+ t.end();
2333
+ });
2334
+ });
2335
+ });
2336
+
2337
+ test('optional repeated mocks execute repeatedly, but never appear as pending', function(t) {
2338
+ var scope = nock('http://example.com')
2339
+ .get('/456')
2340
+ .optionally()
2341
+ .times(2)
2342
+ .reply(200);
2343
+
2344
+ t.deepEqual(nock.pendingMocks(), []);
2345
+ var req = http.get({host: 'example.com', path: '/456'}, function(res) {
2346
+ t.assert(res.statusCode === 200, "should mock first request");
2347
+ t.deepEqual(nock.pendingMocks(), []);
2348
+
2349
+ var req = http.get({host: 'example.com', path: '/456'}, function(res) {
2350
+ t.assert(res.statusCode === 200, "should mock second request");
2351
+ t.deepEqual(nock.pendingMocks(), []);
2352
+ t.end();
2353
+ });
2354
+ });
2355
+ });
2356
+
2357
+ test('activeMocks returns optional mocks only before they\'re completed', function(t) {
2358
+ nock.cleanAll();
2359
+ var scope = nock('http://example.com')
2360
+ .get('/optional')
2361
+ .optionally()
2362
+ .reply(200);
2363
+
2364
+ t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/optional"]);
2365
+ var req = http.get({host: 'example.com', path: '/optional'}, function(res) {
2366
+ t.deepEqual(nock.activeMocks(), []);
2367
+ t.end();
2368
+ });
2369
+ });
2370
+
2371
+ test('activeMocks always returns persisted mocks', function(t) {
2372
+ nock.cleanAll();
2373
+ var scope = nock('http://example.com')
2374
+ .get('/persisted')
2375
+ .reply(200)
2376
+ .persist();
2377
+
2378
+ t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/persisted"]);
2379
+ var req = http.get({host: 'example.com', path: '/persisted'}, function(res) {
2380
+ t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/persisted"]);
2381
+ t.end();
2382
+ });
2383
+ });
2384
+
2385
+ test('activeMocks returns incomplete mocks', function(t) {
2386
+ nock.cleanAll();
2387
+ var scope = nock('http://example.com')
2388
+ .get('/incomplete')
2389
+ .reply(200);
2390
+
2391
+ t.deepEqual(nock.activeMocks(), ["GET http://example.com:80/incomplete"]);
2392
+ t.end();
2393
+ });
2394
+
2395
+ test('activeMocks doesn\'t return completed mocks', function(t) {
2396
+ nock.cleanAll();
2397
+ var scope = nock('http://example.com')
2398
+ .get('/complete-me')
2399
+ .reply(200);
2400
+
2401
+ var req = http.get({host: 'example.com', path: '/complete-me'}, function(res) {
2402
+ t.deepEqual(nock.activeMocks(), []);
2403
+ t.end();
2404
+ });
2405
+ });
2406
+
2300
2407
  test('username and password works', function(t) {
2301
2408
  var scope = nock('http://passwordyy.com')
2302
2409
  .get('/')
@@ -2444,6 +2551,28 @@ test('persists interceptors', function(t) {
2444
2551
  }).end();
2445
2552
  });
2446
2553
 
2554
+ test('Persisted interceptors are in pendingMocks initially', function(t) {
2555
+ var scope = nock('http://example.com')
2556
+ .get('/abc')
2557
+ .reply(200, "Persisted reply")
2558
+ .persist();
2559
+
2560
+ t.deepEqual(scope.pendingMocks(), ["GET http://example.com:80/abc"]);
2561
+ t.end();
2562
+ });
2563
+
2564
+ test('Persisted interceptors are not in pendingMocks after the first request', function(t) {
2565
+ var scope = nock('http://example.com')
2566
+ .get('/def')
2567
+ .reply(200, "Persisted reply")
2568
+ .persist();
2569
+
2570
+ http.get('http://example.com/def', function(res) {
2571
+ t.deepEqual(scope.pendingMocks(), []);
2572
+ t.end();
2573
+ });
2574
+ });
2575
+
2447
2576
  test("persist reply with file", function(t) {
2448
2577
  var scope = nock('http://www.filereplier.com')
2449
2578
  .persist()
@@ -3438,35 +3567,37 @@ test('define() works with binary buffers', function(t) {
3438
3567
 
3439
3568
  });
3440
3569
 
3441
- test('issue #163 - Authorization header isn\'t mocked', function(t) {
3442
- nock.enableNetConnect();
3443
- function makeRequest(cb) {
3444
- var r = http.request(
3445
- {
3446
- hostname: 'www.example.com',
3447
- path: '/',
3448
- method: 'GET',
3449
- auth: 'foo:bar'
3450
- },
3451
- function(res) {
3452
- cb(res.req._headers);
3453
- }
3454
- );
3455
- r.end();
3456
- }
3570
+ if (process.versions.node >= '0.11' ) {
3571
+ test('issue #163 - Authorization header isn\'t mocked', function(t) {
3572
+ nock.enableNetConnect();
3573
+ function makeRequest(cb) {
3574
+ var r = http.request(
3575
+ {
3576
+ hostname: 'www.example.com',
3577
+ path: '/',
3578
+ method: 'GET',
3579
+ auth: 'foo:bar'
3580
+ },
3581
+ function(res) {
3582
+ cb(res.req._headers);
3583
+ }
3584
+ );
3585
+ r.end();
3586
+ }
3457
3587
 
3458
- makeRequest(function(headers) {
3459
- var n = nock('http://www.example.com', {
3460
- reqheaders: { 'authorization': 'Basic Zm9vOmJhcg==' }
3461
- }).get('/').reply(200);
3588
+ makeRequest(function(headers) {
3589
+ var n = nock('http://www.example.com', {
3590
+ reqheaders: { 'authorization': 'Basic Zm9vOmJhcg==' }
3591
+ }).get('/').reply(200);
3462
3592
 
3463
- makeRequest(function(nockHeader) {
3464
- n.done();
3465
- t.equivalent(headers, nockHeader);
3466
- t.end();
3593
+ makeRequest(function(nockHeader) {
3594
+ n.done();
3595
+ t.equivalent(headers, nockHeader);
3596
+ t.end();
3597
+ });
3467
3598
  });
3468
3599
  });
3469
- });
3600
+ }
3470
3601
 
3471
3602
  test('define() uses reqheaders', function(t) {
3472
3603
  var nockDef = {
@@ -4096,6 +4227,7 @@ test('request emits socket', function(t) {
4096
4227
 
4097
4228
  var req = http.get('http://gotzsocketz.com');
4098
4229
  req.once('socket', function(socket) {
4230
+ t.equal(this, req);
4099
4231
  t.type(socket, Object);
4100
4232
  t.type(socket.getPeerCertificate(), 'string');
4101
4233
  t.end();
@@ -4325,6 +4457,24 @@ test('remove interceptor removes given interceptor', function(t) {
4325
4457
  });
4326
4458
 
4327
4459
 
4460
+ test('remove interceptor removes interceptor from pending requests', function(t) {
4461
+ var givenInterceptor = nock('http://example.org')
4462
+ .get('/somepath');
4463
+ var scope = givenInterceptor
4464
+ .reply(200, 'hey');
4465
+
4466
+ var mocks = scope.pendingMocks();
4467
+ t.deepEqual(mocks, ['GET http://example.org:80/somepath']);
4468
+
4469
+ var result = nock.removeInterceptor(givenInterceptor);
4470
+ t.ok(result, 'result should be true');
4471
+
4472
+ var mocksAfterRemove = scope.pendingMocks();
4473
+ t.deepEqual(mocksAfterRemove, [ ]);
4474
+ t.end();
4475
+ });
4476
+
4477
+
4328
4478
  test('remove interceptor removes given interceptor for https', function(t) {
4329
4479
  var givenInterceptor = nock('https://example.org')
4330
4480
  .get('/somepath');
@@ -4401,7 +4551,6 @@ test('remove interceptor for not found resource', function(t) {
4401
4551
  });
4402
4552
 
4403
4553
  test('isDone() must consider repeated responses', function(t) {
4404
-
4405
4554
  var scope = nock('http://www.example.com')
4406
4555
  .get('/')
4407
4556
  .times(2)
@@ -4431,7 +4580,6 @@ test('isDone() must consider repeated responses', function(t) {
4431
4580
  t.end();
4432
4581
  });
4433
4582
  });
4434
-
4435
4583
  });
4436
4584
 
4437
4585
  test('you must setup an interceptor for each request', function(t) {