nock 11.9.1 → 12.0.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.
- package/README.md +17 -10
- package/index.js +1 -8
- package/lib/back.js +2 -11
- package/lib/common.js +5 -8
- package/lib/intercept.js +6 -3
- package/lib/interceptor.js +9 -3
- package/lib/scope.js +5 -5
- package/package.json +6 -7
- package/types/index.d.ts +3 -2
package/README.md
CHANGED
|
@@ -113,15 +113,17 @@ The latest version of nock supports all currently maintained Node versions, see
|
|
|
113
113
|
|
|
114
114
|
Here is a list of past nock versions with respective node version support
|
|
115
115
|
|
|
116
|
-
| node | nock
|
|
117
|
-
| ---- |
|
|
118
|
-
| 0.10 | up to 8.x
|
|
119
|
-
| 0.11 | up to 8.x
|
|
120
|
-
| 0.12 | up to 8.x
|
|
121
|
-
| 4 | up to 9.x
|
|
122
|
-
| 5 | up to 8.x
|
|
123
|
-
|
|
|
124
|
-
|
|
|
116
|
+
| node | nock |
|
|
117
|
+
| ---- | ---------- |
|
|
118
|
+
| 0.10 | up to 8.x |
|
|
119
|
+
| 0.11 | up to 8.x |
|
|
120
|
+
| 0.12 | up to 8.x |
|
|
121
|
+
| 4 | up to 9.x |
|
|
122
|
+
| 5 | up to 8.x |
|
|
123
|
+
| 6 | up to 10.x |
|
|
124
|
+
| 7 | up to 9.x |
|
|
125
|
+
| 8 | up to 11.x |
|
|
126
|
+
| 9 | up to 9.x |
|
|
125
127
|
|
|
126
128
|
## Usage
|
|
127
129
|
|
|
@@ -1154,7 +1156,7 @@ For enabling any real HTTP requests (the default behavior):
|
|
|
1154
1156
|
nock.enableNetConnect()
|
|
1155
1157
|
```
|
|
1156
1158
|
|
|
1157
|
-
You could allow real HTTP requests for certain host names by providing a string or a regular expression for the hostname:
|
|
1159
|
+
You could allow real HTTP requests for certain host names by providing a string or a regular expression for the hostname, or a function that accepts the hostname and returns true or false:
|
|
1158
1160
|
|
|
1159
1161
|
```js
|
|
1160
1162
|
// Using a string
|
|
@@ -1163,6 +1165,11 @@ nock.enableNetConnect('amazon.com')
|
|
|
1163
1165
|
// Or a RegExp
|
|
1164
1166
|
nock.enableNetConnect(/(amazon|github)\.com/)
|
|
1165
1167
|
|
|
1168
|
+
// Or a Function
|
|
1169
|
+
nock.enableNetConnect(
|
|
1170
|
+
host => host.includes('amazon.com') || host.includes('github.com')
|
|
1171
|
+
)
|
|
1172
|
+
|
|
1166
1173
|
http.get('http://www.amazon.com/')
|
|
1167
1174
|
http.get('http://github.com/')
|
|
1168
1175
|
|
package/index.js
CHANGED
|
@@ -29,14 +29,7 @@ Object.assign(module.exports, {
|
|
|
29
29
|
removeInterceptor,
|
|
30
30
|
disableNetConnect,
|
|
31
31
|
enableNetConnect,
|
|
32
|
-
|
|
33
|
-
// The other global methods do not do this, so it's not clear this was
|
|
34
|
-
// deliberate or is even helpful. This shim is included for backward
|
|
35
|
-
// compatibility and should be replaced with an alias to `removeAll()`.
|
|
36
|
-
cleanAll() {
|
|
37
|
-
removeAll()
|
|
38
|
-
return module.exports
|
|
39
|
-
},
|
|
32
|
+
cleanAll: removeAll,
|
|
40
33
|
abortPendingRequests,
|
|
41
34
|
load,
|
|
42
35
|
loadDefs,
|
package/lib/back.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const assert = require('assert')
|
|
4
|
-
const _ = require('lodash')
|
|
5
4
|
const recorder = require('./recorder')
|
|
6
5
|
const {
|
|
7
6
|
activate,
|
|
@@ -25,13 +24,6 @@ try {
|
|
|
25
24
|
// do nothing, probably in browser
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
let mkdirp
|
|
29
|
-
try {
|
|
30
|
-
mkdirp = require('mkdirp')
|
|
31
|
-
} catch (err) {
|
|
32
|
-
// do nothing, probably in browser
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
/**
|
|
36
28
|
* nock the current function with the fixture given
|
|
37
29
|
*
|
|
@@ -61,8 +53,7 @@ function Back(fixtureName, options, nockedFn) {
|
|
|
61
53
|
)
|
|
62
54
|
}
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
if (!_.isString(fixtureName)) {
|
|
56
|
+
if (typeof fixtureName !== 'string') {
|
|
66
57
|
throw new Error('Parameter fixtureName must be a string')
|
|
67
58
|
}
|
|
68
59
|
|
|
@@ -178,7 +169,7 @@ const record = {
|
|
|
178
169
|
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
|
|
179
170
|
debug('recorder outputs:', outputs)
|
|
180
171
|
|
|
181
|
-
|
|
172
|
+
fs.mkdirSync(path.dirname(fixture), { recursive: true })
|
|
182
173
|
fs.writeFileSync(fixture, outputs)
|
|
183
174
|
}
|
|
184
175
|
},
|
package/lib/common.js
CHANGED
|
@@ -174,8 +174,7 @@ function stringifyRequest(options, body) {
|
|
|
174
174
|
|
|
175
175
|
function isContentEncoded(headers) {
|
|
176
176
|
const contentEncoding = headers['content-encoding']
|
|
177
|
-
|
|
178
|
-
return _.isString(contentEncoding) && contentEncoding !== ''
|
|
177
|
+
return typeof contentEncoding === 'string' && contentEncoding !== ''
|
|
179
178
|
}
|
|
180
179
|
|
|
181
180
|
function contentEncoding(headers, encoder) {
|
|
@@ -364,8 +363,7 @@ function deleteHeadersField(headers, fieldNameToDelete) {
|
|
|
364
363
|
throw Error('headers must be an object')
|
|
365
364
|
}
|
|
366
365
|
|
|
367
|
-
|
|
368
|
-
if (!_.isString(fieldNameToDelete)) {
|
|
366
|
+
if (typeof fieldNameToDelete !== 'string') {
|
|
369
367
|
throw Error('field name must be a string')
|
|
370
368
|
}
|
|
371
369
|
|
|
@@ -438,16 +436,15 @@ function matchStringOrRegexp(target, pattern) {
|
|
|
438
436
|
function formatQueryValue(key, value, stringFormattingFn) {
|
|
439
437
|
// TODO: Probably refactor code to replace `switch(true)` with `if`/`else`.
|
|
440
438
|
switch (true) {
|
|
441
|
-
case
|
|
442
|
-
case
|
|
439
|
+
case typeof value === 'number': // fall-through
|
|
440
|
+
case typeof value === 'boolean':
|
|
443
441
|
value = value.toString()
|
|
444
442
|
break
|
|
445
443
|
case value === null:
|
|
446
444
|
case value === undefined:
|
|
447
445
|
value = ''
|
|
448
446
|
break
|
|
449
|
-
|
|
450
|
-
case _.isString(value):
|
|
447
|
+
case typeof value === 'string':
|
|
451
448
|
if (stringFormattingFn) {
|
|
452
449
|
value = stringFormattingFn(value)
|
|
453
450
|
}
|
package/lib/intercept.js
CHANGED
|
@@ -8,7 +8,6 @@ const { InterceptedRequestRouter } = require('./intercepted_request_router')
|
|
|
8
8
|
const common = require('./common')
|
|
9
9
|
const { inherits } = require('util')
|
|
10
10
|
const http = require('http')
|
|
11
|
-
const _ = require('lodash')
|
|
12
11
|
const debug = require('debug')('nock.intercept')
|
|
13
12
|
const globalEmitter = require('./global_emitter')
|
|
14
13
|
|
|
@@ -50,13 +49,17 @@ let allowNetConnect
|
|
|
50
49
|
* @example
|
|
51
50
|
* // Enables real requests for url that matches google and amazon
|
|
52
51
|
* nock.enableNetConnect(/(google|amazon)/);
|
|
52
|
+
* @example
|
|
53
|
+
* // Enables real requests for url that includes google
|
|
54
|
+
* nock.enableNetConnect(host => host.includes('google'));
|
|
53
55
|
*/
|
|
54
56
|
function enableNetConnect(matcher) {
|
|
55
|
-
|
|
56
|
-
if (_.isString(matcher)) {
|
|
57
|
+
if (typeof matcher === 'string') {
|
|
57
58
|
allowNetConnect = new RegExp(matcher)
|
|
58
59
|
} else if (matcher instanceof RegExp) {
|
|
59
60
|
allowNetConnect = matcher
|
|
61
|
+
} else if (typeof matcher === 'function') {
|
|
62
|
+
allowNetConnect = { test: matcher }
|
|
60
63
|
} else {
|
|
61
64
|
allowNetConnect = /.*/
|
|
62
65
|
}
|
package/lib/interceptor.js
CHANGED
|
@@ -102,7 +102,10 @@ module.exports = class Interceptor {
|
|
|
102
102
|
replyWithError(errorMessage) {
|
|
103
103
|
this.errorMessage = errorMessage
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
this.options = {
|
|
106
|
+
...this.scope.scopeOptions,
|
|
107
|
+
...this.options,
|
|
108
|
+
}
|
|
106
109
|
|
|
107
110
|
this.scope.add(this._key, this)
|
|
108
111
|
return this.scope
|
|
@@ -132,7 +135,10 @@ module.exports = class Interceptor {
|
|
|
132
135
|
}
|
|
133
136
|
}
|
|
134
137
|
|
|
135
|
-
|
|
138
|
+
this.options = {
|
|
139
|
+
...this.scope.scopeOptions,
|
|
140
|
+
...this.options,
|
|
141
|
+
}
|
|
136
142
|
|
|
137
143
|
this.rawHeaders = common.headersInputToRawArray(rawHeaders)
|
|
138
144
|
|
|
@@ -557,7 +563,7 @@ module.exports = class Interceptor {
|
|
|
557
563
|
delay(opts) {
|
|
558
564
|
let headDelay
|
|
559
565
|
let bodyDelay
|
|
560
|
-
if (
|
|
566
|
+
if (typeof opts === 'number') {
|
|
561
567
|
headDelay = opts
|
|
562
568
|
bodyDelay = 0
|
|
563
569
|
} else if (typeof opts === 'object') {
|
package/lib/scope.js
CHANGED
|
@@ -7,7 +7,6 @@ const { addInterceptor, isOn } = require('./intercept')
|
|
|
7
7
|
const common = require('./common')
|
|
8
8
|
const assert = require('assert')
|
|
9
9
|
const url = require('url')
|
|
10
|
-
const _ = require('lodash')
|
|
11
10
|
const debug = require('debug')('nock.scope')
|
|
12
11
|
const { EventEmitter } = require('events')
|
|
13
12
|
const util = require('util')
|
|
@@ -184,7 +183,7 @@ class Scope extends EventEmitter {
|
|
|
184
183
|
}
|
|
185
184
|
return candidate.replace(filteringArguments[0], filteringArguments[1])
|
|
186
185
|
}
|
|
187
|
-
} else if (
|
|
186
|
+
} else if (typeof arguments[0] === 'function') {
|
|
188
187
|
return arguments[0]
|
|
189
188
|
}
|
|
190
189
|
}
|
|
@@ -356,9 +355,10 @@ function define(nockDefs) {
|
|
|
356
355
|
} else if (nockDef.responseIsBinary) {
|
|
357
356
|
response = Buffer.from(nockDef.response, 'hex')
|
|
358
357
|
} else {
|
|
359
|
-
response =
|
|
360
|
-
|
|
361
|
-
|
|
358
|
+
response =
|
|
359
|
+
typeof nockDef.response === 'string'
|
|
360
|
+
? tryJsonParse(nockDef.response)
|
|
361
|
+
: nockDef.response
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
const scope = new Scope(nscope, options)
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "
|
|
10
|
+
"version": "12.0.2",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "http://github.com/nock/nock/issues"
|
|
18
18
|
},
|
|
19
19
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
20
|
+
"node": ">= 10.13"
|
|
21
21
|
},
|
|
22
22
|
"main": "./index.js",
|
|
23
23
|
"types": "types",
|
|
@@ -25,14 +25,13 @@
|
|
|
25
25
|
"debug": "^4.1.0",
|
|
26
26
|
"json-stringify-safe": "^5.0.1",
|
|
27
27
|
"lodash": "^4.17.13",
|
|
28
|
-
"mkdirp": "^0.5.0",
|
|
29
28
|
"propagate": "^2.0.0"
|
|
30
29
|
},
|
|
31
30
|
"devDependencies": {
|
|
32
31
|
"assert-rejects": "^1.0.0",
|
|
33
32
|
"chai": "^4.1.2",
|
|
34
33
|
"dirty-chai": "^2.0.1",
|
|
35
|
-
"dtslint": "^
|
|
34
|
+
"dtslint": "^3.0.0",
|
|
36
35
|
"eslint": "^6.0.0",
|
|
37
36
|
"eslint-config-prettier": "^6.0.0",
|
|
38
37
|
"eslint-config-standard": "^14.0.0",
|
|
@@ -41,7 +40,7 @@
|
|
|
41
40
|
"eslint-plugin-node": "^11.0.0",
|
|
42
41
|
"eslint-plugin-promise": "^4.1.1",
|
|
43
42
|
"eslint-plugin-standard": "^4.0.0",
|
|
44
|
-
"got": "^
|
|
43
|
+
"got": "^10.5.2",
|
|
45
44
|
"@sinonjs/fake-timers": "^6.0.0",
|
|
46
45
|
"mocha": "^7.0.1",
|
|
47
46
|
"npm-run-all": "^4.1.5",
|
|
@@ -51,7 +50,7 @@
|
|
|
51
50
|
"request": "^2.83.0",
|
|
52
51
|
"rimraf": "^3.0.0",
|
|
53
52
|
"semantic-release": "^17.0.2",
|
|
54
|
-
"sinon": "^
|
|
53
|
+
"sinon": "^9.0.0",
|
|
55
54
|
"sinon-chai": "^3.3.0",
|
|
56
55
|
"superagent": "^5.0.2",
|
|
57
56
|
"tap": "14.6.1"
|
|
@@ -64,7 +63,7 @@
|
|
|
64
63
|
"lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
|
|
65
64
|
"lint:ts": "dtslint types",
|
|
66
65
|
"semantic-release": "semantic-release",
|
|
67
|
-
"test": "run-
|
|
66
|
+
"test": "run-s test:mocha test:tap",
|
|
68
67
|
"test:coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html",
|
|
69
68
|
"test:mocha": "nyc mocha $(grep -lr '^\\s*it(' tests)",
|
|
70
69
|
"test:tap": "tap --100 --coverage --coverage-report=text ./tests/test_*.js"
|
package/types/index.d.ts
CHANGED
|
@@ -21,7 +21,9 @@ declare namespace nock {
|
|
|
21
21
|
function activeMocks(): string[]
|
|
22
22
|
function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean
|
|
23
23
|
function disableNetConnect(): void
|
|
24
|
-
function enableNetConnect(
|
|
24
|
+
function enableNetConnect(
|
|
25
|
+
matcher?: string | RegExp | ((host: string) => boolean)
|
|
26
|
+
): void
|
|
25
27
|
function load(path: string): Scope[]
|
|
26
28
|
function loadDefs(path: string): Definition[]
|
|
27
29
|
function define(defs: Definition[]): Scope[]
|
|
@@ -125,7 +127,6 @@ declare namespace nock {
|
|
|
125
127
|
|
|
126
128
|
done(): void
|
|
127
129
|
isDone(): boolean
|
|
128
|
-
restore(): void
|
|
129
130
|
pendingMocks(): string[]
|
|
130
131
|
activeMocks(): string[]
|
|
131
132
|
}
|