nock 13.0.3 → 13.0.7

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
@@ -86,7 +86,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
86
86
  - [Usage](#usage-1)
87
87
  - [Options](#options-1)
88
88
  - [Example](#example)
89
- - [Modes](#modes)
89
+ - [Modes](#modes)
90
90
  - [Common issues](#common-issues)
91
91
  - [Axios](#axios)
92
92
  - [Memory issues with Jest](#memory-issues-with-jest)
@@ -376,7 +376,7 @@ const scope = nock('http://www.google.com')
376
376
  In Nock 11.x it was possible to invoke `.reply()` with a status code and a
377
377
  function that returns an array containing a status code and body. (The status
378
378
  code from the array would take precedence over the one passed directly to
379
- reply.) This is no longer allowed. In 12.x, either call `.reply()` with a
379
+ reply.) This is no longer allowed. In Nock 12 and later, either call `.reply()` with a
380
380
  status code and a function that returns the body, or call it with a single
381
381
  argument: a function that returns an array containing both the status code and
382
382
  body.
@@ -634,7 +634,6 @@ You are able to specify a non-standard port like this:
634
634
 
635
635
  ```js
636
636
  const scope = nock('http://my.server.com:8081')
637
- ...
638
637
  ```
639
638
 
640
639
  ### Repeat response n times
@@ -1064,7 +1063,7 @@ You can bypass Nock completely by setting the `NOCK_OFF` environment variable to
1064
1063
 
1065
1064
  This way you can have your tests hit the real servers just by switching on this environment variable.
1066
1065
 
1067
- ```js
1066
+ ```shell script
1068
1067
  $ NOCK_OFF=true node my_test.js
1069
1068
  ```
1070
1069
 
@@ -1304,10 +1303,10 @@ nock.removeInterceptor({
1304
1303
 
1305
1304
  ```js
1306
1305
  nock.removeInterceptor({
1307
- hostname : 'localhost',
1308
- path : '/login'
1309
- method: 'POST'
1310
- proto : 'https'
1306
+ hostname: 'localhost',
1307
+ path: '/login',
1308
+ method: 'POST',
1309
+ proto: 'https',
1311
1310
  })
1312
1311
  ```
1313
1312
 
@@ -1389,11 +1388,9 @@ nockBack('zomboFixture.json', nockDone => {
1389
1388
  If your tests are using promises then use `nockBack` like this:
1390
1389
 
1391
1390
  ```js
1392
- return nockBack('promisedFixture.json')
1393
- .then(({ nockDone, context }) => {
1394
- // do your tests returning a promise and chain it with
1395
- // `.then(nockDone)`
1396
- })
1391
+ return nockBack('promisedFixture.json').then(({ nockDone, context }) => {
1392
+ // do your tests returning a promise and chain it with
1393
+ // `.then(nockDone)`
1397
1394
  })
1398
1395
  ```
1399
1396
 
@@ -1411,7 +1408,7 @@ As an optional second parameter you can pass the following options
1411
1408
  ```js
1412
1409
  function prepareScope(scope) {
1413
1410
  scope.filteringRequestBody = (body, aRecordedBody) => {
1414
- if (typeof(body) !== 'string' || typeof(aRecordedBody) !== 'string') {
1411
+ if (typeof body !== 'string' || typeof aRecordedBody !== 'string') {
1415
1412
  return body
1416
1413
  }
1417
1414
 
@@ -1428,15 +1425,15 @@ function prepareScope(scope) {
1428
1425
  }
1429
1426
  }
1430
1427
 
1431
- nockBack('zomboFixture.json', { before: prepareScope }, nockDone => {
1432
- request.get('http://zombo.com', function(err, res, body) {
1428
+ nockBack('exampleFixture.json', { before: prepareScope }, nockDone => {
1429
+ request.get('http://example.com', function (err, res, body) {
1433
1430
  // do your tests
1434
1431
  nockDone()
1435
- }
1436
- }
1432
+ })
1433
+ })
1437
1434
  ```
1438
1435
 
1439
- #### Modes
1436
+ ### Modes
1440
1437
 
1441
1438
  To set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BACK_MODE` environment variable set before loading nock. If the mode needs to be changed programmatically, the following is valid: `nockBack.setMode(nockBack.currentMode)`
1442
1439
 
package/lib/common.js CHANGED
@@ -416,9 +416,12 @@ function matchStringOrRegexp(target, pattern) {
416
416
  const targetStr =
417
417
  target === undefined || target === null ? '' : String(target)
418
418
 
419
- return pattern instanceof RegExp
420
- ? pattern.test(targetStr)
421
- : targetStr === String(pattern)
419
+ if (pattern instanceof RegExp) {
420
+ // if the regexp happens to have a global flag, we want to ensure we test the entire target
421
+ pattern.lastIndex = 0
422
+ return pattern.test(targetStr)
423
+ }
424
+ return targetStr === String(pattern)
422
425
  }
423
426
 
424
427
  /**
@@ -195,7 +195,9 @@ class InterceptedRequestRouter {
195
195
  req.once('finish', callback)
196
196
  }
197
197
 
198
- req.write(chunk, encoding)
198
+ if (chunk) {
199
+ req.write(chunk, encoding)
200
+ }
199
201
  req.finished = true
200
202
  this.maybeStartPlayback()
201
203
 
@@ -296,7 +298,6 @@ class InterceptedRequestRouter {
296
298
  )
297
299
 
298
300
  matchedInterceptor.markConsumed()
299
- matchedInterceptor.req = req
300
301
 
301
302
  // wait to emit the finish event until we know for sure an Interceptor is going to playback.
302
303
  // otherwise an unmocked request might emit finish twice.
@@ -149,6 +149,13 @@ function playbackInterceptor({
149
149
  response.rawHeaders = [...interceptor.rawHeaders]
150
150
  logger('response.rawHeaders:', response.rawHeaders)
151
151
 
152
+ // TODO: MAJOR: Don't tack the request onto the interceptor.
153
+ // The only reason we do this is so that it's available inside reply functions.
154
+ // It would be better to pass the request as an argument to the functions instead.
155
+ // Not adding the req as a third arg now because it should first be decided if (path, body, req)
156
+ // is the signature we want to go with going forward.
157
+ interceptor.req = req
158
+
152
159
  if (interceptor.replyFunction) {
153
160
  const parsedRequestBody = parseJSONRequestBody(req, requestBodyString)
154
161
 
package/lib/recorder.js CHANGED
@@ -110,6 +110,10 @@ function generateRequestAndResponse({
110
110
  const queryStr = req.path.slice(queryIndex + 1)
111
111
  queryObj = querystring.parse(queryStr)
112
112
  }
113
+
114
+ // Escape any single quotes in the path as the output uses them
115
+ path = path.replace(/'/g, `\\'`)
116
+
113
117
  // Always encode the query parameters when recording.
114
118
  const encodedQueryObj = {}
115
119
  for (const key in queryObj) {
package/lib/scope.js CHANGED
@@ -331,7 +331,7 @@ function define(nockDefs) {
331
331
  let response
332
332
  if (!nockDef.response) {
333
333
  response = ''
334
- // TODO: Rename `responseIsBinary` to `reponseIsUtf8Representable`.
334
+ // TODO: Rename `responseIsBinary` to `responseIsUtf8Representable`.
335
335
  } else if (nockDef.responseIsBinary) {
336
336
  response = Buffer.from(nockDef.response, 'hex')
337
337
  } else {
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.0.3",
10
+ "version": "13.0.7",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",
@@ -32,15 +32,16 @@
32
32
  "assert-rejects": "^1.0.0",
33
33
  "chai": "^4.1.2",
34
34
  "dirty-chai": "^2.0.1",
35
- "dtslint": "^3.0.0",
35
+ "dtslint": "^4.0.4",
36
36
  "eslint": "^7.3.1",
37
37
  "eslint-config-prettier": "^6.0.0",
38
38
  "eslint-config-standard": "^14.0.0",
39
39
  "eslint-plugin-import": "^2.16.0",
40
- "eslint-plugin-mocha": "^7.0.1",
40
+ "eslint-plugin-mocha": "^8.0.0",
41
41
  "eslint-plugin-node": "^11.0.0",
42
42
  "eslint-plugin-promise": "^4.1.1",
43
43
  "eslint-plugin-standard": "^4.0.0",
44
+ "form-data": "^3.0.0",
44
45
  "got": "^11.3.0",
45
46
  "mocha": "^8.0.1",
46
47
  "npm-run-all": "^4.1.5",
@@ -52,7 +53,7 @@
52
53
  "semantic-release": "^17.0.2",
53
54
  "sinon": "^9.0.0",
54
55
  "sinon-chai": "^3.3.0",
55
- "superagent": "^5.0.2",
56
+ "superagent": "^6.1.0",
56
57
  "tap": "14.6.1"
57
58
  },
58
59
  "scripts": {