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
- if (typeof callback === 'function') {
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 intercepto using the given request and mock response.
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
@@ -13,6 +13,7 @@ module.exports = class Socket extends EventEmitter {
13
13
  }
14
14
 
15
15
  this.bufferSize = 0
16
+ this.writableLength = 0
16
17
  this.writable = true
17
18
  this.readable = true
18
19
  this.pending = false
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "12.0.2",
10
+ "version": "12.0.3",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",