nock 13.0.1 → 13.0.5

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.
@@ -574,7 +574,7 @@ const scope = nock('http://www.headdy.com')
574
574
 
575
575
  #### Including Content-Length Header Automatically
576
576
 
577
- When using `scope.reply()` to set a response body manually, you can have the
577
+ When using `interceptor.reply()` to set a response body manually, you can have the
578
578
  `Content-Length` header calculated automatically.
579
579
 
580
580
  ```js
@@ -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
@@ -548,8 +548,15 @@ function urlToOptions(url) {
548
548
  * - The expected data can use regexp to compare values
549
549
  * - JSON path notation and nested objects are considered equal
550
550
  */
551
- const dataEqual = (expected, actual) =>
552
- deepEqual(expand(expected), expand(actual))
551
+ const dataEqual = (expected, actual) => {
552
+ if (isPlainObject(expected)) {
553
+ expected = expand(expected)
554
+ }
555
+ if (isPlainObject(actual)) {
556
+ actual = expand(actual)
557
+ }
558
+ return deepEqual(expected, actual)
559
+ }
553
560
 
554
561
  /**
555
562
  * Converts flat objects whose keys use JSON path notation to nested objects.
@@ -573,17 +580,20 @@ function deepEqual(expected, actual) {
573
580
  return expected.test(actual)
574
581
  }
575
582
 
576
- if (Array.isArray(expected) || isPlainObject(expected)) {
577
- if (actual === undefined) {
583
+ if (Array.isArray(expected) && Array.isArray(actual)) {
584
+ if (expected.length !== actual.length) {
578
585
  return false
579
586
  }
580
587
 
581
- const expKeys = Object.keys(expected)
582
- if (expKeys.length !== Object.keys(actual).length) {
583
- return false
584
- }
588
+ return expected.every((expVal, idx) => deepEqual(expVal, actual[idx]))
589
+ }
590
+
591
+ if (isPlainObject(expected) && isPlainObject(actual)) {
592
+ const allKeys = Array.from(
593
+ new Set(Object.keys(expected).concat(Object.keys(actual)))
594
+ )
585
595
 
586
- return expKeys.every(key => deepEqual(expected[key], actual[key]))
596
+ return allKeys.every(key => deepEqual(expected[key], actual[key]))
587
597
  }
588
598
 
589
599
  return expected === actual
@@ -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
 
@@ -295,6 +297,8 @@ class InterceptedRequestRouter {
295
297
  'interceptor identified, starting mocking'
296
298
  )
297
299
 
300
+ matchedInterceptor.markConsumed()
301
+
298
302
  // wait to emit the finish event until we know for sure an Interceptor is going to playback.
299
303
  // otherwise an unmocked request might emit finish twice.
300
304
  req.emit('finish')
@@ -124,8 +124,6 @@ function playbackInterceptor({
124
124
  const { logger } = interceptor.scope
125
125
 
126
126
  function start() {
127
- interceptor.markConsumed()
128
- interceptor.req = req
129
127
  req.headers = req.getHeaders()
130
128
 
131
129
  interceptor.scope.emit('request', req, interceptor, requestBodyString)
@@ -151,6 +149,13 @@ function playbackInterceptor({
151
149
  response.rawHeaders = [...interceptor.rawHeaders]
152
150
  logger('response.rawHeaders:', response.rawHeaders)
153
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
+
154
159
  if (interceptor.replyFunction) {
155
160
  const parsedRequestBody = parseJSONRequestBody(req, requestBodyString)
156
161
 
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.1",
10
+ "version": "13.0.5",
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": "^6.2.0",
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": {