nock 12.0.0-beta.1 → 12.0.3
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 +11 -9
- package/lib/back.js +1 -8
- package/lib/common.js +2 -2
- package/lib/intercepted_request_router.js +14 -8
- package/lib/interceptor.js +9 -3
- package/lib/playback_interceptor.js +1 -1
- package/lib/scope.js +5 -5
- package/lib/socket.js +1 -0
- package/package.json +5 -6
- package/types/index.d.ts +0 -1
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
|
|
package/lib/back.js
CHANGED
|
@@ -24,13 +24,6 @@ try {
|
|
|
24
24
|
// do nothing, probably in browser
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
let mkdirp
|
|
28
|
-
try {
|
|
29
|
-
mkdirp = require('mkdirp')
|
|
30
|
-
} catch (err) {
|
|
31
|
-
// do nothing, probably in browser
|
|
32
|
-
}
|
|
33
|
-
|
|
34
27
|
/**
|
|
35
28
|
* nock the current function with the fixture given
|
|
36
29
|
*
|
|
@@ -176,7 +169,7 @@ const record = {
|
|
|
176
169
|
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
|
|
177
170
|
debug('recorder outputs:', outputs)
|
|
178
171
|
|
|
179
|
-
|
|
172
|
+
fs.mkdirSync(path.dirname(fixture), { recursive: true })
|
|
180
173
|
fs.writeFileSync(fixture, outputs)
|
|
181
174
|
}
|
|
182
175
|
},
|
package/lib/common.js
CHANGED
|
@@ -436,8 +436,8 @@ function matchStringOrRegexp(target, pattern) {
|
|
|
436
436
|
function formatQueryValue(key, value, stringFormattingFn) {
|
|
437
437
|
// TODO: Probably refactor code to replace `switch(true)` with `if`/`else`.
|
|
438
438
|
switch (true) {
|
|
439
|
-
case
|
|
440
|
-
case
|
|
439
|
+
case typeof value === 'number': // fall-through
|
|
440
|
+
case typeof value === 'boolean':
|
|
441
441
|
value = value.toString()
|
|
442
442
|
break
|
|
443
443
|
case value === null:
|
|
@@ -142,6 +142,7 @@ class InterceptedRequestRouter {
|
|
|
142
142
|
debug('req.end')
|
|
143
143
|
const { req } = this
|
|
144
144
|
|
|
145
|
+
// handle the different overloaded param signatures
|
|
145
146
|
if (typeof chunk === 'function') {
|
|
146
147
|
callback = chunk
|
|
147
148
|
chunk = null
|
|
@@ -150,19 +151,19 @@ class InterceptedRequestRouter {
|
|
|
150
151
|
encoding = null
|
|
151
152
|
}
|
|
152
153
|
|
|
154
|
+
if (typeof callback === 'function') {
|
|
155
|
+
req.once('finish', callback)
|
|
156
|
+
}
|
|
157
|
+
|
|
153
158
|
if (!req.aborted && !this.playbackStarted) {
|
|
154
|
-
req.write(chunk, encoding
|
|
155
|
-
|
|
156
|
-
callback()
|
|
157
|
-
}
|
|
158
|
-
this.startPlayback()
|
|
159
|
-
req.emit('finish')
|
|
160
|
-
req.emit('end')
|
|
161
|
-
})
|
|
159
|
+
req.write(chunk, encoding)
|
|
160
|
+
this.startPlayback()
|
|
162
161
|
}
|
|
163
162
|
if (req.aborted) {
|
|
164
163
|
this.emitError(new Error('Request aborted'))
|
|
165
164
|
}
|
|
165
|
+
|
|
166
|
+
return req
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
handleFlushHeaders() {
|
|
@@ -271,6 +272,11 @@ class InterceptedRequestRouter {
|
|
|
271
272
|
if (matchedInterceptor) {
|
|
272
273
|
debug('interceptor identified, starting mocking')
|
|
273
274
|
|
|
275
|
+
// wait to emit the finish event until we know for sure an Interceptor is going to playback.
|
|
276
|
+
// otherwise an unmocked request might emit finish twice.
|
|
277
|
+
req.finished = true
|
|
278
|
+
req.emit('finish')
|
|
279
|
+
|
|
274
280
|
playbackInterceptor({
|
|
275
281
|
req,
|
|
276
282
|
socket,
|
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') {
|
|
@@ -72,7 +72,7 @@ function selectDefaultHeaders(existingHeaders, defaultHeaders) {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
|
-
* Play back an
|
|
75
|
+
* Play back an interceptor using the given request and mock response.
|
|
76
76
|
*/
|
|
77
77
|
function playbackInterceptor({
|
|
78
78
|
req,
|
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/lib/socket.js
CHANGED
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "12.0.
|
|
10
|
+
"version": "12.0.3",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -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": "^1.0.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"
|