nock 12.0.2 → 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.
|
@@ -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,
|
|
@@ -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/socket.js
CHANGED