nock 13.5.1 → 14.0.0-beta.1

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/lib/common.js CHANGED
@@ -735,6 +735,24 @@ const expand = input => {
735
735
  return result
736
736
  }
737
737
 
738
+ /**
739
+ * @param {Request} request
740
+ */
741
+ function convertFetchRequestToOptions(request) {
742
+ const url = new URL(request.url)
743
+ const options = {
744
+ ...urlToOptions(url),
745
+ method: request.method,
746
+ host: url.hostname,
747
+ port: url.port || (url.protocol === 'https:' ? 443 : 80),
748
+ path: url.pathname + url.search,
749
+ proto: url.protocol.slice(0, -1),
750
+ headers: Object.fromEntries(request.headers.entries()),
751
+ }
752
+
753
+ return options
754
+ }
755
+
738
756
  module.exports = {
739
757
  contentEncoding,
740
758
  dataEqual,
@@ -765,4 +783,5 @@ module.exports = {
765
783
  setInterval,
766
784
  setTimeout,
767
785
  stringifyRequest,
786
+ convertFetchRequestToClientRequest: convertFetchRequestToOptions,
768
787
  }
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ const { headersArrayToObject } = require('./common')
4
+
5
+ /**
6
+ * Creates a Fetch API `Response` instance from the given
7
+ * `http.IncomingMessage` instance.
8
+ * Inspired by: https://github.com/mswjs/interceptors/blob/04152ed914f8041272b6e92ed374216b8177e1b2/src/interceptors/ClientRequest/utils/createResponse.ts#L8
9
+ */
10
+
11
+ /**
12
+ * Response status codes for responses that cannot have body.
13
+ * @see https://fetch.spec.whatwg.org/#statuses
14
+ */
15
+ const responseStatusCodesWithoutBody = [204, 205, 304]
16
+
17
+ /**
18
+ * @param {IncomingMessage} message
19
+ */
20
+ function createResponse(message) {
21
+ const responseBodyOrNull = responseStatusCodesWithoutBody.includes(
22
+ message.statusCode,
23
+ )
24
+ ? null
25
+ : new ReadableStream({
26
+ start(controller) {
27
+ message.on('data', chunk => controller.enqueue(chunk))
28
+ message.on('end', () => controller.close())
29
+
30
+ /**
31
+ * @todo Should also listen to the "error" on the message
32
+ * and forward it to the controller. Otherwise the stream
33
+ * will pend indefinitely.
34
+ */
35
+ },
36
+ })
37
+
38
+ return new Response(responseBodyOrNull, {
39
+ status: message.statusCode,
40
+ statusText: message.statusMessage,
41
+ headers: headersArrayToObject(message.rawHeaders),
42
+ })
43
+ }
44
+
45
+ module.exports = { createResponse }
package/lib/intercept.js CHANGED
@@ -10,6 +10,7 @@ const { inherits } = require('util')
10
10
  const http = require('http')
11
11
  const debug = require('debug')('nock.intercept')
12
12
  const globalEmitter = require('./global_emitter')
13
+ const { createResponse } = require('./create_response')
13
14
 
14
15
  /**
15
16
  * @name NetConnectNotAllowedError
@@ -302,7 +303,9 @@ function overrideClientRequest() {
302
303
 
303
304
  // Fallback to original ClientRequest if nock is off or the net connection is enabled.
304
305
  if (isOff() || isEnabledForNetConnect(options)) {
305
- originalClientRequest.apply(this, arguments)
306
+ if (options.isFetchRequest === undefined) {
307
+ originalClientRequest.apply(this, arguments)
308
+ }
306
309
  } else {
307
310
  common.setImmediate(
308
311
  function () {
@@ -434,6 +437,28 @@ function activate() {
434
437
  }
435
438
  })
436
439
 
440
+ const originalFetch = global.fetch
441
+ global.fetch = async (input, init = undefined) => {
442
+ const request = new Request(input, init)
443
+ const options = common.convertFetchRequestToClientRequest(request)
444
+ options.isFetchRequest = true
445
+ const body = await request.arrayBuffer()
446
+ const clientRequest = new http.ClientRequest(options)
447
+
448
+ // If mock found
449
+ if (clientRequest.interceptors) {
450
+ return new Promise((resolve, reject) => {
451
+ clientRequest.on('response', response => {
452
+ resolve(createResponse(response))
453
+ })
454
+ clientRequest.on('error', reject)
455
+ clientRequest.end(body)
456
+ })
457
+ } else {
458
+ return originalFetch(input, init)
459
+ }
460
+ }
461
+
437
462
  overrideClientRequest()
438
463
  }
439
464
 
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "testing",
8
8
  "isolation"
9
9
  ],
10
- "version": "13.5.1",
10
+ "version": "14.0.0-beta.1",
11
11
  "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
12
12
  "repository": {
13
13
  "type": "git",