nock 13.0.8 → 13.1.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.
package/README.md CHANGED
@@ -640,6 +640,8 @@ const scope = nock('http://my.server.com:8081')
640
640
 
641
641
  You are able to specify the number of times to repeat the same response.
642
642
 
643
+ **NOTE:** When request times is more than the number you specified, you will get an error before cleaning this interceptor.
644
+
643
645
  ```js
644
646
  nock('http://zombo.com').get('/').times(4).reply(200, 'Ok')
645
647
 
@@ -647,7 +649,15 @@ http.get('http://zombo.com/') // respond body "Ok"
647
649
  http.get('http://zombo.com/') // respond body "Ok"
648
650
  http.get('http://zombo.com/') // respond body "Ok"
649
651
  http.get('http://zombo.com/') // respond body "Ok"
650
- http.get('http://zombo.com/') // respond with zombo.com result
652
+
653
+ // This code will get an error with message:
654
+ // Nock: No match for request
655
+ http.get('http://zombo.com/')
656
+
657
+ // clean your interceptor
658
+ nock.cleanAll()
659
+
660
+ http.get('http://zombo.com/') // real respond with zombo.com result
651
661
  ```
652
662
 
653
663
  Sugar syntax
@@ -1213,13 +1223,12 @@ nocks.forEach(function (nock) {
1213
1223
  const recordedBodyResult = /timestamp:([0-9]+)/.exec(aRecordedBody)
1214
1224
  if (recordedBodyResult) {
1215
1225
  const recordedTimestamp = recordedBodyResult[1]
1216
- return body.replace(/(timestamp):([0-9]+)/g, function (
1217
- match,
1218
- key,
1219
- value
1220
- ) {
1221
- return key + ':' + recordedTimestamp
1222
- })
1226
+ return body.replace(
1227
+ /(timestamp):([0-9]+)/g,
1228
+ function (match, key, value) {
1229
+ return key + ':' + recordedTimestamp
1230
+ }
1231
+ )
1223
1232
  } else {
1224
1233
  return body
1225
1234
  }
package/lib/common.js CHANGED
@@ -673,6 +673,34 @@ function removeAllTimers() {
673
673
  clearTimer(clearImmediate, immediates)
674
674
  }
675
675
 
676
+ /**
677
+ * Check if the Client Request has been cancelled.
678
+ *
679
+ * Until Node 14 is the minimum, we need to look at both flags to see if the request has been cancelled.
680
+ * The two flags have the same purpose, but the Node maintainers are migrating from `abort(ed)` to
681
+ * `destroy(ed)` terminology, to be more consistent with `stream.Writable`.
682
+ * In Node 14.x+, Calling `abort()` will set both `aborted` and `destroyed` to true, however,
683
+ * calling `destroy()` will only set `destroyed` to true.
684
+ * Falling back on checking if the socket is destroyed to cover the case of Node <14.x where
685
+ * `destroy()` is called, but `destroyed` is undefined.
686
+ *
687
+ * Node Client Request history:
688
+ * - `request.abort()`: Added in: v0.3.8, Deprecated since: v14.1.0, v13.14.0
689
+ * - `request.aborted`: Added in: v0.11.14, Became a boolean instead of a timestamp: v11.0.0, Not deprecated (yet)
690
+ * - `request.destroy()`: Added in: v0.3.0
691
+ * - `request.destroyed`: Added in: v14.1.0, v13.14.0
692
+ *
693
+ * @param {ClientRequest} req
694
+ * @returns {boolean}
695
+ */
696
+ function isRequestDestroyed(req) {
697
+ return !!(
698
+ req.destroyed === true ||
699
+ req.aborted ||
700
+ (req.socket && req.socket.destroyed)
701
+ )
702
+ }
703
+
676
704
  module.exports = {
677
705
  contentEncoding,
678
706
  dataEqual,
@@ -686,6 +714,7 @@ module.exports = {
686
714
  isContentEncoded,
687
715
  isJSONContent,
688
716
  isPlainObject,
717
+ isRequestDestroyed,
689
718
  isStream,
690
719
  isUtf8Representable,
691
720
  mapValue,
@@ -103,8 +103,7 @@ class InterceptedRequestRouter {
103
103
  connectSocket() {
104
104
  const { req, socket } = this
105
105
 
106
- // Until Node 14 is the minimum, we need to look at both flags to see if the request has been cancelled.
107
- if (req.destroyed || req.aborted) {
106
+ if (common.isRequestDestroyed(req)) {
108
107
  return
109
108
  }
110
109
 
@@ -250,8 +249,7 @@ class InterceptedRequestRouter {
250
249
  return
251
250
  }
252
251
 
253
- // Until Node 14 is the minimum, we need to look at both flags to see if the request has been cancelled.
254
- if (!req.destroyed && !req.aborted && !playbackStarted) {
252
+ if (!common.isRequestDestroyed(req) && !playbackStarted) {
255
253
  this.startPlayback()
256
254
  }
257
255
  }
@@ -32,8 +32,9 @@ module.exports = class Interceptor {
32
32
  // When enabled filteringScope ignores the passed URL entirely so we skip validation.
33
33
 
34
34
  if (
35
- !scope.scopeOptions.filteringScope &&
36
35
  uriIsStr &&
36
+ !scope.scopeOptions.filteringScope &&
37
+ !scope.basePathname &&
37
38
  !uri.startsWith('/') &&
38
39
  !uri.startsWith('*')
39
40
  ) {
@@ -293,7 +293,7 @@ function playbackInterceptor({
293
293
  const { delayBodyInMs, delayConnectionInMs } = interceptor
294
294
 
295
295
  function respond() {
296
- if (req.aborted) {
296
+ if (common.isRequestDestroyed(req)) {
297
297
  return
298
298
  }
299
299
 
@@ -318,7 +318,7 @@ function playbackInterceptor({
318
318
  // correct events are emitted first ('socket', 'finish') and any aborts in the in the queue or
319
319
  // called during a 'finish' listener can be called.
320
320
  common.setImmediate(() => {
321
- if (!req.aborted) {
321
+ if (!common.isRequestDestroyed(req)) {
322
322
  start()
323
323
  }
324
324
  })
package/lib/recorder.js CHANGED
@@ -309,8 +309,6 @@ function record(recOptions) {
309
309
 
310
310
  if (callback) {
311
311
  callback(res, options, callback)
312
- } else {
313
- res.resume()
314
312
  }
315
313
 
316
314
  debug('finished setting up intercepting')
package/lib/socket.js CHANGED
@@ -92,6 +92,7 @@ module.exports = class Socket extends EventEmitter {
92
92
  debug('socket destroy')
93
93
  this.destroyed = true
94
94
  this.readable = this.writable = false
95
+ this.readableEnded = this.writableFinished = true
95
96
 
96
97
  process.nextTick(() => {
97
98
  if (err) {
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.0.8",
10
+ "version": "13.1.0",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -28,33 +28,31 @@
28
28
  "propagate": "^2.0.0"
29
29
  },
30
30
  "devDependencies": {
31
- "@sinonjs/fake-timers": "^6.0.0",
31
+ "@sinonjs/fake-timers": "^7.0.2",
32
32
  "assert-rejects": "^1.0.0",
33
33
  "chai": "^4.1.2",
34
34
  "dirty-chai": "^2.0.1",
35
35
  "dtslint": "^4.0.4",
36
36
  "eslint": "^7.3.1",
37
- "eslint-config-prettier": "^6.0.0",
38
- "eslint-config-standard": "^14.0.0",
37
+ "eslint-config-prettier": "^8.1.0",
38
+ "eslint-config-standard": "^16.0.2",
39
39
  "eslint-plugin-import": "^2.16.0",
40
40
  "eslint-plugin-mocha": "^8.0.0",
41
41
  "eslint-plugin-node": "^11.0.0",
42
42
  "eslint-plugin-promise": "^4.1.1",
43
- "eslint-plugin-standard": "^4.0.0",
44
- "form-data": "^3.0.0",
43
+ "eslint-plugin-standard": "^5.0.0",
44
+ "form-data": "^4.0.0",
45
45
  "got": "^11.3.0",
46
46
  "mocha": "^8.0.1",
47
47
  "npm-run-all": "^4.1.5",
48
48
  "nyc": "^15.0.0",
49
- "prettier": "2.0.5",
49
+ "prettier": "2.2.1",
50
50
  "proxyquire": "^2.1.0",
51
- "request": "^2.83.0",
52
51
  "rimraf": "^3.0.0",
53
52
  "semantic-release": "^17.0.2",
54
- "sinon": "^9.0.0",
53
+ "sinon": "^10.0.0",
55
54
  "sinon-chai": "^3.3.0",
56
- "superagent": "^6.1.0",
57
- "tap": "14.6.1"
55
+ "typescript": "^4.2.2"
58
56
  },
59
57
  "scripts": {
60
58
  "format:fix": "prettier --write '**/*.{js,json,md,ts,yml,yaml}'",
@@ -63,19 +61,8 @@
63
61
  "lint:js": "eslint --cache --cache-location './.cache/eslint' '**/*.js'",
64
62
  "lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
65
63
  "lint:ts": "dtslint types",
66
- "test": "run-s test:mocha test:tap",
67
- "test:coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html",
68
- "test:mocha": "nyc mocha $(grep -lr '^\\s*it(' tests)",
69
- "test:tap": "tap --100 --coverage --coverage-report=text ./tests/test_*.js"
70
- },
71
- "nyc": {
72
- "reporter": [
73
- "lcov",
74
- "text-summary"
75
- ],
76
- "exclude": [
77
- "tests/"
78
- ]
64
+ "test": "nyc mocha tests",
65
+ "test:coverage": "open coverage/lcov-report/index.html"
79
66
  },
80
67
  "license": "MIT",
81
68
  "files": [