nock 13.1.1 β 13.2.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 +46 -4
- package/lib/back.js +56 -0
- package/lib/common.js +7 -5
- package/lib/intercepted_request_router.js +3 -4
- package/lib/interceptor.js +7 -3
- package/package.json +7 -7
- package/types/index.d.ts +1 -1
package/README.md
CHANGED
|
@@ -1323,6 +1323,18 @@ const interceptor = nock('http://example.org').get('somePath')
|
|
|
1323
1323
|
nock.removeInterceptor(interceptor)
|
|
1324
1324
|
```
|
|
1325
1325
|
|
|
1326
|
+
**Note** `.reply(...)` method returns Scope, not Interceptor, and so it is not a valid argument for `nock.removeInterceptor`. So if your method chain ends with `.reply` to be used with `nock.removeInterceptor` the chain need to be break in between:
|
|
1327
|
+
|
|
1328
|
+
```js
|
|
1329
|
+
// this will NOT work
|
|
1330
|
+
const interceptor = nock('http://example.org').get('somePath').reply(200, 'OK')
|
|
1331
|
+
nock.removeInterceptor(interceptor)
|
|
1332
|
+
// this is how it should be
|
|
1333
|
+
const interceptor = nock('http://example.org').get('somePath')
|
|
1334
|
+
interceptor.reply(200, 'OK')
|
|
1335
|
+
nock.removeInterceptor(interceptor)
|
|
1336
|
+
```
|
|
1337
|
+
|
|
1326
1338
|
## Events
|
|
1327
1339
|
|
|
1328
1340
|
A scope emits the following events:
|
|
@@ -1402,6 +1414,14 @@ return nockBack('promisedFixture.json').then(({ nockDone, context }) => {
|
|
|
1402
1414
|
})
|
|
1403
1415
|
```
|
|
1404
1416
|
|
|
1417
|
+
Or, with async/await:
|
|
1418
|
+
|
|
1419
|
+
```js
|
|
1420
|
+
const { nockDone, context } = await nockBack('promisedFixture.json')
|
|
1421
|
+
// your test code
|
|
1422
|
+
nockDone()
|
|
1423
|
+
```
|
|
1424
|
+
|
|
1405
1425
|
#### Options
|
|
1406
1426
|
|
|
1407
1427
|
As an optional second parameter you can pass the following options
|
|
@@ -1451,6 +1471,8 @@ To set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BA
|
|
|
1451
1471
|
|
|
1452
1472
|
- record: use recorded nocks, record new nocks
|
|
1453
1473
|
|
|
1474
|
+
- update: remove recorded nocks, record nocks
|
|
1475
|
+
|
|
1454
1476
|
- lockdown: use recorded nocks, disables all http calls even when not nocked, doesn't record
|
|
1455
1477
|
|
|
1456
1478
|
## Common issues
|
|
@@ -1565,10 +1587,30 @@ By participating in this project you agree to abide by its terms.
|
|
|
1565
1587
|
Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):
|
|
1566
1588
|
|
|
1567
1589
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
1568
|
-
<!-- prettier-ignore -->
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1590
|
+
<!-- prettier-ignore-start -->
|
|
1591
|
+
<!-- markdownlint-disable -->
|
|
1592
|
+
<table>
|
|
1593
|
+
<tr>
|
|
1594
|
+
<td align="center"><a href="http://pgte.me"><img src="https://avatars1.githubusercontent.com/u/47910?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Pedro Teixeira</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=pgte" title="Code">π»</a> <a href="#maintenance-pgte" title="Maintenance">π§</a></td>
|
|
1595
|
+
<td align="center"><a href="https://github.com/n30n0v"><img src="https://avatars3.githubusercontent.com/u/10771967?v=4?s=100" width="100px;" alt=""/><br /><sub><b>n30n0v</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=n30n0v" title="Code">π»</a></td>
|
|
1596
|
+
<td align="center"><a href="https://burntfen.com"><img src="https://avatars3.githubusercontent.com/u/910753?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Richard Littauer</b></sub></a><br /><a href="#maintenance-RichardLitt" title="Maintenance">π§</a> <a href="https://github.com/nock/nock/commits?author=RichardLitt" title="Code">π»</a> <a href="#blog-RichardLitt" title="Blogposts">π</a></td>
|
|
1597
|
+
<td align="center"><a href="http://ianwsperber.com"><img src="https://avatars1.githubusercontent.com/u/3731165?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ian Walker-Sperber</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=ianwsperber" title="Code">π»</a></td>
|
|
1598
|
+
<td align="center"><a href="http://ilovacha.com"><img src="https://avatars2.githubusercontent.com/u/1505203?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Erceg</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=ierceg" title="Code">π»</a> <a href="#maintenance-ierceg" title="Maintenance">π§</a></td>
|
|
1599
|
+
<td align="center"><a href="https://twitter.com/paulmelnikow"><img src="https://avatars2.githubusercontent.com/u/1487036?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Paul Melnikow</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=paulmelnikow" title="Code">π»</a> <a href="#maintenance-paulmelnikow" title="Maintenance">π§</a></td>
|
|
1600
|
+
<td align="center"><a href="https://twitter.com/gr2m"><img src="https://avatars3.githubusercontent.com/u/39992?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gregor Martynus</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=gr2m" title="Code">π»</a> <a href="#maintenance-gr2m" title="Maintenance">π§</a> <a href="#business-gr2m" title="Business development">πΌ</a> <a href="#financial-gr2m" title="Financial">π΅</a> <a href="#blog-gr2m" title="Blogposts">π</a></td>
|
|
1601
|
+
</tr>
|
|
1602
|
+
<tr>
|
|
1603
|
+
<td align="center"><a href="https://gitlab.com/hutson"><img src="https://avatars1.githubusercontent.com/u/6701030?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hutson Betts</b></sub></a><br /><a href="#financial-hutson" title="Financial">π΅</a></td>
|
|
1604
|
+
<td align="center"><a href="http://lilja.io"><img src="https://avatars2.githubusercontent.com/u/6105119?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonas Lilja</b></sub></a><br /><a href="#financial-jlilja" title="Financial">π΅</a> <a href="https://github.com/nock/nock/commits?author=jlilja" title="Code">π»</a></td>
|
|
1605
|
+
<td align="center"><a href="https://github.com/benrki"><img src="https://avatars0.githubusercontent.com/u/4446950?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Benjamin Ki</b></sub></a><br /><a href="#financial-benrki" title="Financial">π΅</a></td>
|
|
1606
|
+
<td align="center"><a href="http://chadf.ca"><img src="https://avatars2.githubusercontent.com/u/3250463?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Chad Fawcett</b></sub></a><br /><a href="#financial-chadfawcett" title="Financial">π΅</a></td>
|
|
1607
|
+
<td align="center"><a href="http://www.laurencemyers.com.au"><img src="https://avatars.githubusercontent.com/u/6336048?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Laurence Dougal Myers</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=laurence-myers" title="Code">π»</a></td>
|
|
1608
|
+
<td align="center"><a href="https://github.com/Beretta1979"><img src="https://avatars.githubusercontent.com/u/10073962?v=4?s=100" width="100px;" alt=""/><br /><sub><b>SΓ©bastien Van Bruaene</b></sub></a><br /><a href="https://github.com/nock/nock/commits?author=Beretta1979" title="Code">π»</a> <a href="https://github.com/nock/nock/commits?author=Beretta1979" title="Tests">β οΈ</a></td>
|
|
1609
|
+
</tr>
|
|
1610
|
+
</table>
|
|
1611
|
+
|
|
1612
|
+
<!-- markdownlint-restore -->
|
|
1613
|
+
<!-- prettier-ignore-end -->
|
|
1572
1614
|
|
|
1573
1615
|
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
1574
1616
|
|
package/lib/back.js
CHANGED
|
@@ -175,6 +175,47 @@ const record = {
|
|
|
175
175
|
},
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
const update = {
|
|
179
|
+
setup: function () {
|
|
180
|
+
recorder.restore()
|
|
181
|
+
recorder.clear()
|
|
182
|
+
cleanAll()
|
|
183
|
+
activate()
|
|
184
|
+
disableNetConnect()
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
start: function (fixture, options) {
|
|
188
|
+
if (!fs) {
|
|
189
|
+
throw new Error('no fs')
|
|
190
|
+
}
|
|
191
|
+
const context = removeFixture(fixture)
|
|
192
|
+
recorder.record({
|
|
193
|
+
dont_print: true,
|
|
194
|
+
output_objects: true,
|
|
195
|
+
...options.recorder,
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
context.isRecording = true
|
|
199
|
+
|
|
200
|
+
return context
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
finish: function (fixture, options, context) {
|
|
204
|
+
let outputs = recorder.outputs()
|
|
205
|
+
|
|
206
|
+
if (typeof options.afterRecord === 'function') {
|
|
207
|
+
outputs = options.afterRecord(outputs)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
outputs =
|
|
211
|
+
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
|
|
212
|
+
debug('recorder outputs:', outputs)
|
|
213
|
+
|
|
214
|
+
fs.mkdirSync(path.dirname(fixture), { recursive: true })
|
|
215
|
+
fs.writeFileSync(fixture, outputs)
|
|
216
|
+
},
|
|
217
|
+
}
|
|
218
|
+
|
|
178
219
|
const lockdown = {
|
|
179
220
|
setup: function () {
|
|
180
221
|
recorder.restore()
|
|
@@ -215,6 +256,19 @@ function load(fixture, options) {
|
|
|
215
256
|
return context
|
|
216
257
|
}
|
|
217
258
|
|
|
259
|
+
function removeFixture(fixture, options) {
|
|
260
|
+
const context = {
|
|
261
|
+
scopes: [],
|
|
262
|
+
assertScopesFinished: function () {},
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (fixture && fixtureExists(fixture)) {
|
|
266
|
+
fs.rmSync ? fs.rmSync(fixture) : fs.unlinkSync(fixture)
|
|
267
|
+
}
|
|
268
|
+
context.isLoaded = false
|
|
269
|
+
return context
|
|
270
|
+
}
|
|
271
|
+
|
|
218
272
|
function applyHook(scopes, fn) {
|
|
219
273
|
if (!fn) {
|
|
220
274
|
return
|
|
@@ -258,6 +312,8 @@ const Modes = {
|
|
|
258
312
|
|
|
259
313
|
record, // use recorded nocks, record new nocks
|
|
260
314
|
|
|
315
|
+
update, // allow http calls, record all nocks, don't use recorded nocks
|
|
316
|
+
|
|
261
317
|
lockdown, // use recorded nocks, disables all http calls even when not nocked, doesnt record
|
|
262
318
|
}
|
|
263
319
|
|
package/lib/common.js
CHANGED
|
@@ -651,11 +651,13 @@ const timeouts = []
|
|
|
651
651
|
const intervals = []
|
|
652
652
|
const immediates = []
|
|
653
653
|
|
|
654
|
-
const wrapTimer =
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
654
|
+
const wrapTimer =
|
|
655
|
+
(timer, ids) =>
|
|
656
|
+
(...args) => {
|
|
657
|
+
const id = timer(...args)
|
|
658
|
+
ids.push(id)
|
|
659
|
+
return id
|
|
660
|
+
}
|
|
659
661
|
|
|
660
662
|
const setTimeout = wrapTimer(timers.setTimeout, timeouts)
|
|
661
663
|
const setInterval = wrapTimer(timers.setInterval, intervals)
|
|
@@ -153,7 +153,7 @@ class InterceptedRequestRouter {
|
|
|
153
153
|
return false
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
if (!buffer
|
|
156
|
+
if (!buffer) {
|
|
157
157
|
return true
|
|
158
158
|
}
|
|
159
159
|
|
|
@@ -279,9 +279,8 @@ class InterceptedRequestRouter {
|
|
|
279
279
|
const requestBodyBuffer = Buffer.concat(this.requestBodyBuffers)
|
|
280
280
|
// When request body is a binary buffer we internally use in its hexadecimal
|
|
281
281
|
// representation.
|
|
282
|
-
const requestBodyIsUtf8Representable =
|
|
283
|
-
requestBodyBuffer
|
|
284
|
-
)
|
|
282
|
+
const requestBodyIsUtf8Representable =
|
|
283
|
+
common.isUtf8Representable(requestBodyBuffer)
|
|
285
284
|
const requestBodyString = requestBodyBuffer.toString(
|
|
286
285
|
requestBodyIsUtf8Representable ? 'utf8' : 'hex'
|
|
287
286
|
)
|
package/lib/interceptor.js
CHANGED
|
@@ -442,13 +442,17 @@ module.exports = class Interceptor {
|
|
|
442
442
|
markConsumed() {
|
|
443
443
|
this.interceptionCounter++
|
|
444
444
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
445
|
+
if (
|
|
446
|
+
(this.scope.shouldPersist() || this.counter > 0) &&
|
|
447
|
+
this.interceptionCounter > 1 &&
|
|
448
|
+
this.filePath
|
|
449
|
+
) {
|
|
448
450
|
this.body = fs.createReadStream(this.filePath)
|
|
449
451
|
this.body.pause()
|
|
450
452
|
}
|
|
451
453
|
|
|
454
|
+
remove(this)
|
|
455
|
+
|
|
452
456
|
if (!this.scope.shouldPersist() && this.counter < 1) {
|
|
453
457
|
this.scope.remove(this._key, this)
|
|
454
458
|
}
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "13.
|
|
10
|
+
"version": "13.2.0",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"propagate": "^2.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@sinonjs/fake-timers": "^
|
|
31
|
+
"@sinonjs/fake-timers": "^8.1.0",
|
|
32
32
|
"assert-rejects": "^1.0.0",
|
|
33
33
|
"chai": "^4.1.2",
|
|
34
34
|
"dirty-chai": "^2.0.1",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"eslint-config-prettier": "^8.1.0",
|
|
38
38
|
"eslint-config-standard": "^16.0.2",
|
|
39
39
|
"eslint-plugin-import": "^2.16.0",
|
|
40
|
-
"eslint-plugin-mocha": "^
|
|
40
|
+
"eslint-plugin-mocha": "^9.0.0",
|
|
41
41
|
"eslint-plugin-node": "^11.0.0",
|
|
42
42
|
"eslint-plugin-promise": "^4.1.1",
|
|
43
43
|
"eslint-plugin-standard": "^5.0.0",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"mocha": "^8.0.1",
|
|
47
47
|
"npm-run-all": "^4.1.5",
|
|
48
48
|
"nyc": "^15.0.0",
|
|
49
|
-
"prettier": "2.
|
|
49
|
+
"prettier": "2.4.1",
|
|
50
50
|
"proxyquire": "^2.1.0",
|
|
51
51
|
"rimraf": "^3.0.0",
|
|
52
52
|
"semantic-release": "^17.0.2",
|
|
53
|
-
"sinon": "^
|
|
54
|
-
"sinon-chai": "^3.
|
|
53
|
+
"sinon": "^12.0.1",
|
|
54
|
+
"sinon-chai": "^3.7.0",
|
|
55
55
|
"typescript": "^4.2.2"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"lint:js": "eslint --cache --cache-location './.cache/eslint' '**/*.js'",
|
|
62
62
|
"lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
|
|
63
63
|
"lint:ts": "dtslint types",
|
|
64
|
-
"test": "nyc mocha tests",
|
|
64
|
+
"test": "nyc --reporter=lcov --reporter=text mocha tests",
|
|
65
65
|
"test:coverage": "open coverage/lcov-report/index.html"
|
|
66
66
|
},
|
|
67
67
|
"license": "MIT",
|
package/types/index.d.ts
CHANGED