@mswjs/interceptors 0.41.9 → 0.42.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/README.md +274 -228
- package/lib/browser/{bufferUtils-BiiO6HZv.mjs → buffer-utils-DJj7YzLG.js} +3 -3
- package/lib/browser/buffer-utils-DJj7YzLG.js.map +1 -0
- package/lib/browser/create-request-id-Bk5YX1AM.js +720 -0
- package/lib/browser/create-request-id-Bk5YX1AM.js.map +1 -0
- package/lib/browser/{getRawRequest-B1BqgWG6.mjs → fetch-utils-CUOrwEQf.js} +123 -52
- package/lib/browser/fetch-utils-CUOrwEQf.js.map +1 -0
- package/lib/browser/handle-request-CqVvBhcw.js +167 -0
- package/lib/browser/handle-request-CqVvBhcw.js.map +1 -0
- package/lib/{node/hasConfigurableGlobal-BiTmog1u.mjs → browser/has-configurable-global-Cew-dYqk.js} +5 -7
- package/lib/browser/has-configurable-global-Cew-dYqk.js.map +1 -0
- package/lib/browser/http-CB9rQ7i_.d.ts +105 -0
- package/lib/browser/{index.d.cts → index.d.ts} +33 -38
- package/lib/browser/index.js +130 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/browser/interceptors/WebSocket/index.d.ts +2 -0
- package/lib/browser/interceptors/WebSocket/{index.mjs → index.js} +39 -30
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.d.ts +11 -0
- package/lib/browser/interceptors/XMLHttpRequest/web.js +2 -0
- package/lib/browser/interceptors/fetch/web.d.ts +14 -0
- package/lib/browser/interceptors/fetch/web.js +2 -0
- package/lib/browser/presets/browser.d.ts +11 -0
- package/lib/browser/presets/browser.js +12 -0
- package/lib/browser/presets/browser.js.map +1 -0
- package/lib/browser/{resolveWebSocketUrl-C83-x9iE.mjs → resolve-web-socket-url-CSvNPLGi.js} +3 -3
- package/lib/browser/resolve-web-socket-url-CSvNPLGi.js.map +1 -0
- package/lib/browser/{XMLHttpRequest-DrZAf3w6.mjs → web-BcKSQghf.js} +327 -255
- package/lib/browser/web-BcKSQghf.js.map +1 -0
- package/lib/browser/web-CdcYFjgm.js +248 -0
- package/lib/browser/web-CdcYFjgm.js.map +1 -0
- package/lib/browser/{interceptors/WebSocket/index.d.cts → websocket-CC0nB0md.d.ts} +41 -33
- package/lib/node/batch-interceptor-ByEZVih3.js +119 -0
- package/lib/node/batch-interceptor-ByEZVih3.js.map +1 -0
- package/lib/node/batch-interceptor-CPjFK2td.d.ts +28 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js +187 -0
- package/lib/node/buffer-utils-BvPY1Tc-.js.map +1 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js +568 -0
- package/lib/node/fetch-utils-Dw1PsmtX.js.map +1 -0
- package/lib/node/has-configurable-global-CJCh_g1I.js +22 -0
- package/lib/node/has-configurable-global-CJCh_g1I.js.map +1 -0
- package/lib/node/http-CB9rQ7i_.d.ts +105 -0
- package/lib/{browser/interceptors/WebSocket/index.d.mts → node/index.d.ts} +120 -60
- package/lib/node/{index.mjs → index.js} +8 -11
- package/lib/node/index.js.map +1 -0
- package/lib/node/interceptor-dtJZ_9QT.d.ts +50 -0
- package/lib/node/interceptors/ClientRequest/index.d.ts +14 -0
- package/lib/node/interceptors/ClientRequest/index.js +67 -0
- package/lib/node/interceptors/ClientRequest/index.js.map +1 -0
- package/lib/node/interceptors/XMLHttpRequest/node.d.ts +12 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js +74 -0
- package/lib/node/interceptors/XMLHttpRequest/node.js.map +1 -0
- package/lib/node/interceptors/fetch/node.d.ts +18 -0
- package/lib/node/interceptors/fetch/node.js +83 -0
- package/lib/node/interceptors/fetch/node.js.map +1 -0
- package/lib/node/interceptors/http/index.d.ts +14 -0
- package/lib/node/interceptors/http/index.js +32 -0
- package/lib/node/interceptors/http/index.js.map +1 -0
- package/lib/node/interceptors/net/index.d.ts +203 -0
- package/lib/node/interceptors/net/index.js +2 -0
- package/lib/node/llhttp/constants.cjs +485 -0
- package/lib/node/llhttp/constants.d.cts +957 -0
- package/lib/node/llhttp/llhttp.wasm +0 -0
- package/lib/node/net-DtMnyEeg.js +1388 -0
- package/lib/node/net-DtMnyEeg.js.map +1 -0
- package/lib/node/presets/node.d.ts +12 -0
- package/lib/node/presets/node.js +17 -0
- package/lib/node/presets/node.js.map +1 -0
- package/lib/node/{RemoteHttpInterceptor.d.cts → remote-http-interceptor.d.ts} +12 -11
- package/lib/node/{RemoteHttpInterceptor.mjs → remote-http-interceptor.js} +38 -33
- package/lib/node/remote-http-interceptor.js.map +1 -0
- package/lib/node/source-lP0yyEtA.js +1990 -0
- package/lib/node/source-lP0yyEtA.js.map +1 -0
- package/package.json +79 -118
- package/src/batch-interceptor.test.ts +475 -0
- package/src/batch-interceptor.ts +244 -0
- package/src/{createRequestId.test.ts → create-request-id.test.ts} +1 -2
- package/src/disposable.ts +13 -0
- package/src/events/http.ts +92 -0
- package/src/events/websocket.ts +47 -0
- package/src/glossary.ts +1 -27
- package/src/index.ts +18 -10
- package/src/interceptor.test.ts +279 -0
- package/src/interceptor.ts +139 -0
- package/src/interceptors/ClientRequest/index.test.ts +21 -39
- package/src/interceptors/ClientRequest/index.ts +64 -179
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.test.ts → record-raw-headers.test.ts} +1 -2
- package/src/interceptors/ClientRequest/utils/{recordRawHeaders.ts → record-raw-headers.ts} +29 -2
- package/src/interceptors/WebSocket/index.ts +33 -50
- package/src/interceptors/WebSocket/utils/{bindEvent.test.ts → bind-event.test.ts} +1 -2
- package/src/interceptors/WebSocket/utils/events.test.ts +0 -1
- package/src/interceptors/WebSocket/{WebSocketClassTransport.ts → web-socket-class-transport.ts} +3 -3
- package/src/interceptors/WebSocket/{WebSocketClientConnection.ts → web-socket-client-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketOverride.ts → web-socket-override.ts} +6 -7
- package/src/interceptors/WebSocket/{WebSocketServerConnection.ts → web-socket-server-connection.ts} +4 -4
- package/src/interceptors/WebSocket/{WebSocketTransport.ts → web-socket-transport.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/node.ts +104 -0
- package/src/interceptors/XMLHttpRequest/polyfills/{ProgressEventPolyfill.ts → progress-event-polyfill.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{concateArrayBuffer.test.ts → concat-array-buffer.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/utils/{concatArrayBuffer.ts → concat-array-buffer.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.test.ts → create-event.test.ts} +3 -4
- package/src/interceptors/XMLHttpRequest/utils/{createEvent.ts → create-event.ts} +2 -2
- package/src/interceptors/XMLHttpRequest/utils/{createResponse.ts → create-response.ts} +1 -1
- package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.test.ts → get-body-byte-length.test.ts} +1 -2
- package/src/interceptors/XMLHttpRequest/web.ts +34 -0
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestController.ts → xml-http-request-controller.ts} +363 -204
- package/src/interceptors/XMLHttpRequest/{XMLHttpRequestProxy.ts → xml-http-request-proxy.ts} +28 -25
- package/src/interceptors/fetch/node.ts +103 -0
- package/src/interceptors/fetch/utils/{followRedirect.ts → follow-redirect.ts} +1 -1
- package/src/interceptors/fetch/web.ts +217 -0
- package/src/interceptors/http/forward-events.test.ts +86 -0
- package/src/interceptors/http/forward-events.ts +139 -0
- package/src/interceptors/http/http-parser/index.ts +310 -0
- package/src/interceptors/http/http-parser/llhttp/constants.cjs +485 -0
- package/src/interceptors/http/http-parser/llhttp/constants.d.cts +957 -0
- package/src/interceptors/http/http-parser/llhttp/llhttp.wasm +0 -0
- package/src/interceptors/http/http-parser.ts +138 -0
- package/src/interceptors/http/index.ts +34 -0
- package/src/interceptors/http/source.ts +688 -0
- package/src/interceptors/net/index.ts +368 -0
- package/src/interceptors/net/socket-controller.ts +1476 -0
- package/src/interceptors/net/utils/address-info.ts +54 -0
- package/src/interceptors/net/utils/connection-options-to-url.ts +56 -0
- package/src/interceptors/net/utils/flush-writes.ts +57 -0
- package/src/interceptors/net/utils/get-tls-connect-options.ts +23 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.test.ts +233 -0
- package/src/interceptors/net/utils/normalize-net-connect-args.ts +121 -0
- package/src/interceptors/net/utils/normalize-tls-connect-args.ts +50 -0
- package/src/presets/browser.ts +4 -4
- package/src/presets/node.ts +4 -4
- package/src/{RemoteHttpInterceptor.ts → remote-http-interceptor.ts} +55 -42
- package/src/request-context.ts +40 -0
- package/src/{RequestController.test.ts → request-controller.test.ts} +37 -3
- package/src/{RequestController.ts → request-controller.ts} +33 -11
- package/src/utils/{bufferUtils.test.ts → buffer-utils.test.ts} +1 -2
- package/src/utils/buffer-utils.ts +37 -0
- package/src/utils/{cloneObject.test.ts → clone-object.test.ts} +1 -2
- package/src/utils/{cloneObject.ts → clone-object.ts} +8 -8
- package/src/utils/{createProxy.test.ts → create-proxy.test.ts} +6 -4
- package/src/utils/{createProxy.ts → create-proxy.ts} +1 -1
- package/src/utils/{fetchUtils.test.ts → fetch-utils.test.ts} +1 -1
- package/src/utils/{fetchUtils.ts → fetch-utils.ts} +28 -3
- package/src/utils/{findPropertySource.test.ts → find-property-source.test.ts} +1 -2
- package/src/utils/{getCleanUrl.test.ts → get-clean-url.test.ts} +1 -2
- package/src/utils/{getUrlByRequestOptions.test.ts → get-url-by-request-options.test.ts} +4 -8
- package/src/utils/{getUrlByRequestOptions.ts → get-url-by-request-options.ts} +16 -16
- package/src/utils/{getValueBySymbol.test.ts → get-value-by-symbol.test.ts} +1 -2
- package/src/utils/{handleRequest.ts → handle-request.ts} +61 -41
- package/src/utils/{hasConfigurableGlobal.test.ts → has-configurable-global.test.ts} +1 -2
- package/src/utils/{hasConfigurableGlobal.ts → has-configurable-global.ts} +2 -4
- package/src/utils/{isObject.test.ts → is-object.test.ts} +1 -2
- package/src/utils/logger.test.ts +154 -0
- package/src/utils/logger.ts +154 -0
- package/src/utils/{parseJson.test.ts → parse-json.test.ts} +1 -2
- package/src/utils/{patchesRegistry.test.ts → patches-registry.test.ts} +1 -1
- package/src/utils/{patchesRegistry.ts → patches-registry.ts} +1 -1
- package/src/utils/{responseUtils.ts → response-utils.ts} +25 -2
- package/ClientRequest/package.json +0 -11
- package/RemoteHttpInterceptor/package.json +0 -11
- package/WebSocket/package.json +0 -12
- package/XMLHttpRequest/package.json +0 -12
- package/fetch/package.json +0 -12
- package/lib/browser/Interceptor-Deczogc8.d.cts +0 -65
- package/lib/browser/Interceptor-gqKgs-aF.d.mts +0 -65
- package/lib/browser/XMLHttpRequest-DrZAf3w6.mjs.map +0 -1
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs +0 -751
- package/lib/browser/XMLHttpRequest-gXuzzX1g.cjs.map +0 -1
- package/lib/browser/bufferUtils-BiiO6HZv.mjs.map +0 -1
- package/lib/browser/bufferUtils-Uc0eRItL.cjs +0 -38
- package/lib/browser/bufferUtils-Uc0eRItL.cjs.map +0 -1
- package/lib/browser/createRequestId-DOf8Ktjs.cjs +0 -205
- package/lib/browser/createRequestId-DOf8Ktjs.cjs.map +0 -1
- package/lib/browser/createRequestId-DYCsFHOi.mjs +0 -170
- package/lib/browser/createRequestId-DYCsFHOi.mjs.map +0 -1
- package/lib/browser/fetch-5IMPqr9e.mjs +0 -236
- package/lib/browser/fetch-5IMPqr9e.mjs.map +0 -1
- package/lib/browser/fetch-Bt9rMY9c.cjs +0 -241
- package/lib/browser/fetch-Bt9rMY9c.cjs.map +0 -1
- package/lib/browser/getRawRequest-B1BqgWG6.mjs.map +0 -1
- package/lib/browser/getRawRequest-DdfaiPVH.cjs +0 -376
- package/lib/browser/getRawRequest-DdfaiPVH.cjs.map +0 -1
- package/lib/browser/glossary-1lhZx6Aw.d.cts +0 -69
- package/lib/browser/glossary-nkHWcwvY.d.mts +0 -69
- package/lib/browser/handleRequest-CJim54xG.cjs +0 -191
- package/lib/browser/handleRequest-CJim54xG.cjs.map +0 -1
- package/lib/browser/handleRequest-CPPgsG7s.mjs +0 -180
- package/lib/browser/handleRequest-CPPgsG7s.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs +0 -109
- package/lib/browser/hasConfigurableGlobal-C8zq1MCg.mjs.map +0 -1
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs +0 -126
- package/lib/browser/hasConfigurableGlobal-CS7adEvV.cjs.map +0 -1
- package/lib/browser/index.cjs +0 -70
- package/lib/browser/index.cjs.map +0 -1
- package/lib/browser/index.d.mts +0 -125
- package/lib/browser/index.mjs +0 -56
- package/lib/browser/index.mjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.cjs +0 -618
- package/lib/browser/interceptors/WebSocket/index.cjs.map +0 -1
- package/lib/browser/interceptors/WebSocket/index.mjs.map +0 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.cjs +0 -8
- package/lib/browser/interceptors/XMLHttpRequest/index.d.cts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.d.mts +0 -15
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +0 -8
- package/lib/browser/interceptors/fetch/index.cjs +0 -7
- package/lib/browser/interceptors/fetch/index.d.cts +0 -13
- package/lib/browser/interceptors/fetch/index.d.mts +0 -13
- package/lib/browser/interceptors/fetch/index.mjs +0 -7
- package/lib/browser/presets/browser.cjs +0 -18
- package/lib/browser/presets/browser.cjs.map +0 -1
- package/lib/browser/presets/browser.d.cts +0 -12
- package/lib/browser/presets/browser.d.mts +0 -14
- package/lib/browser/presets/browser.mjs +0 -18
- package/lib/browser/presets/browser.mjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs +0 -31
- package/lib/browser/resolveWebSocketUrl-6K6EgqsA.cjs.map +0 -1
- package/lib/browser/resolveWebSocketUrl-C83-x9iE.mjs.map +0 -1
- package/lib/node/BatchInterceptor-BGhH8z1z.cjs +0 -49
- package/lib/node/BatchInterceptor-BGhH8z1z.cjs.map +0 -1
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs +0 -44
- package/lib/node/BatchInterceptor-C4-RX5rg.mjs.map +0 -1
- package/lib/node/BatchInterceptor-KKF8Q47I.d.cts +0 -26
- package/lib/node/BatchInterceptor-rSr-NH_H.d.mts +0 -26
- package/lib/node/ClientRequest-2loAH7xM.cjs +0 -1058
- package/lib/node/ClientRequest-2loAH7xM.cjs.map +0 -1
- package/lib/node/ClientRequest-DGhBX21V.mjs +0 -1049
- package/lib/node/ClientRequest-DGhBX21V.mjs.map +0 -1
- package/lib/node/Interceptor-BsZ21ue0.d.cts +0 -132
- package/lib/node/Interceptor-soD-WpQO.d.mts +0 -132
- package/lib/node/RemoteHttpInterceptor.cjs +0 -155
- package/lib/node/RemoteHttpInterceptor.cjs.map +0 -1
- package/lib/node/RemoteHttpInterceptor.d.mts +0 -39
- package/lib/node/RemoteHttpInterceptor.mjs.map +0 -1
- package/lib/node/XMLHttpRequest-BtDNZipW.cjs +0 -752
- package/lib/node/XMLHttpRequest-BtDNZipW.cjs.map +0 -1
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs +0 -746
- package/lib/node/XMLHttpRequest-Dw6Wm-UU.mjs.map +0 -1
- package/lib/node/bufferUtils-DxPxwff_.mjs +0 -20
- package/lib/node/bufferUtils-DxPxwff_.mjs.map +0 -1
- package/lib/node/bufferUtils-S5_-2eN4.cjs +0 -38
- package/lib/node/bufferUtils-S5_-2eN4.cjs.map +0 -1
- package/lib/node/chunk-CbDLau6x.cjs +0 -34
- package/lib/node/fetch-CaC3rGEu.mjs +0 -252
- package/lib/node/fetch-CaC3rGEu.mjs.map +0 -1
- package/lib/node/fetch-D2ClLNN-.cjs +0 -259
- package/lib/node/fetch-D2ClLNN-.cjs.map +0 -1
- package/lib/node/fetchUtils-BKJ1XmiO.mjs +0 -480
- package/lib/node/fetchUtils-BKJ1XmiO.mjs.map +0 -1
- package/lib/node/fetchUtils-umV5xXBy.cjs +0 -546
- package/lib/node/fetchUtils-umV5xXBy.cjs.map +0 -1
- package/lib/node/getRawRequest-B6znU6il.cjs +0 -36
- package/lib/node/getRawRequest-B6znU6il.cjs.map +0 -1
- package/lib/node/getRawRequest-C2-1urzA.mjs +0 -24
- package/lib/node/getRawRequest-C2-1urzA.mjs.map +0 -1
- package/lib/node/handleRequest-C-cKYjIh.cjs +0 -222
- package/lib/node/handleRequest-C-cKYjIh.cjs.map +0 -1
- package/lib/node/handleRequest-FIQv5pwH.mjs +0 -192
- package/lib/node/handleRequest-FIQv5pwH.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-BiTmog1u.mjs.map +0 -1
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs +0 -108
- package/lib/node/hasConfigurableGlobal-MjY06_Ok.cjs.map +0 -1
- package/lib/node/index-BJ5E5aW-.d.cts +0 -113
- package/lib/node/index-DfoF45ih.d.mts +0 -113
- package/lib/node/index.cjs +0 -53
- package/lib/node/index.cjs.map +0 -1
- package/lib/node/index.d.cts +0 -104
- package/lib/node/index.d.mts +0 -104
- package/lib/node/index.mjs.map +0 -1
- package/lib/node/interceptors/ClientRequest/index.cjs +0 -6
- package/lib/node/interceptors/ClientRequest/index.d.cts +0 -2
- package/lib/node/interceptors/ClientRequest/index.d.mts +0 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +0 -6
- package/lib/node/interceptors/XMLHttpRequest/index.cjs +0 -7
- package/lib/node/interceptors/XMLHttpRequest/index.d.cts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.d.mts +0 -14
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +0 -7
- package/lib/node/interceptors/fetch/index.cjs +0 -6
- package/lib/node/interceptors/fetch/index.d.cts +0 -12
- package/lib/node/interceptors/fetch/index.d.mts +0 -12
- package/lib/node/interceptors/fetch/index.mjs +0 -6
- package/lib/node/node-DIKcnzhK.cjs +0 -39
- package/lib/node/node-DIKcnzhK.cjs.map +0 -1
- package/lib/node/node-lsdNwZEW.mjs +0 -27
- package/lib/node/node-lsdNwZEW.mjs.map +0 -1
- package/lib/node/presets/node.cjs +0 -23
- package/lib/node/presets/node.cjs.map +0 -1
- package/lib/node/presets/node.d.cts +0 -13
- package/lib/node/presets/node.d.mts +0 -15
- package/lib/node/presets/node.mjs +0 -23
- package/lib/node/presets/node.mjs.map +0 -1
- package/lib/node/utils/node/index.cjs +0 -4
- package/lib/node/utils/node/index.d.cts +0 -16
- package/lib/node/utils/node/index.d.mts +0 -16
- package/lib/node/utils/node/index.mjs +0 -3
- package/presets/browser/package.json +0 -5
- package/presets/node/package.json +0 -11
- package/src/BatchInterceptor.test.ts +0 -255
- package/src/BatchInterceptor.ts +0 -96
- package/src/Interceptor.test.ts +0 -205
- package/src/Interceptor.ts +0 -249
- package/src/getRawRequest.ts +0 -21
- package/src/interceptors/ClientRequest/MockHttpSocket.ts +0 -724
- package/src/interceptors/ClientRequest/agents.ts +0 -110
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.test.ts +0 -54
- package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +0 -45
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.test.ts +0 -427
- package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +0 -268
- package/src/interceptors/ClientRequest/utils/parserUtils.ts +0 -48
- package/src/interceptors/Socket/MockSocket.test.ts +0 -264
- package/src/interceptors/Socket/MockSocket.ts +0 -58
- package/src/interceptors/Socket/utils/baseUrlFromConnectionOptions.ts +0 -26
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.test.ts +0 -52
- package/src/interceptors/Socket/utils/normalizeSocketWriteArgs.ts +0 -33
- package/src/interceptors/XMLHttpRequest/index.ts +0 -40
- package/src/interceptors/fetch/index.ts +0 -203
- package/src/utils/bufferUtils.ts +0 -22
- package/src/utils/canParseUrl.ts +0 -13
- package/src/utils/emitAsync.ts +0 -25
- package/src/utils/nextTick.ts +0 -11
- package/src/utils/node/index.ts +0 -39
- /package/src/{createRequestId.ts → create-request-id.ts} +0 -0
- /package/src/{InterceptorError.ts → interceptor-error.ts} +0 -0
- /package/src/interceptors/WebSocket/utils/{bindEvent.ts → bind-event.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/polyfills/{EventPolyfill.ts → event-polyfill.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{getBodyByteLength.ts → get-body-byte-length.ts} +0 -0
- /package/src/interceptors/XMLHttpRequest/utils/{isDomParserSupportedType.ts → is-dom-parser-supported-type.ts} +0 -0
- /package/src/interceptors/fetch/utils/{createNetworkError.ts → create-network-error.ts} +0 -0
- /package/src/utils/{findPropertySource.ts → find-property-source.ts} +0 -0
- /package/src/utils/{getCleanUrl.ts → get-clean-url.ts} +0 -0
- /package/src/utils/{getValueBySymbol.ts → get-value-by-symbol.ts} +0 -0
- /package/src/utils/{isNodeLikeError.ts → is-node-like-error.ts} +0 -0
- /package/src/utils/{isObject.ts → is-object.ts} +0 -0
- /package/src/utils/{isPropertyAccessible.ts → is-property-accessible.ts} +0 -0
- /package/src/utils/{parseJson.ts → parse-json.ts} +0 -0
- /package/src/utils/{resolveWebSocketUrl.ts → resolve-web-socket-url.ts} +0 -0
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Here's how requests are handled in Node.js:
|
|
3
|
-
*
|
|
4
|
-
* 1. http.ClientRequest instance calls `agent.addRequest(request, options, cb)`.
|
|
5
|
-
* 2. Agent creates a new socket: `agent.createSocket(options, cb)`.
|
|
6
|
-
* 3. Agent creates a new connection: `agent.createConnection(options, cb)`.
|
|
7
|
-
*/
|
|
8
|
-
import net from 'node:net'
|
|
9
|
-
import http from 'node:http'
|
|
10
|
-
import https from 'node:https'
|
|
11
|
-
import {
|
|
12
|
-
MockHttpSocket,
|
|
13
|
-
type MockHttpSocketRequestCallback,
|
|
14
|
-
type MockHttpSocketResponseCallback,
|
|
15
|
-
} from './MockHttpSocket'
|
|
16
|
-
|
|
17
|
-
declare module 'node:http' {
|
|
18
|
-
interface Agent {
|
|
19
|
-
options?: http.AgentOptions
|
|
20
|
-
createConnection(options: any, callback: any): net.Socket
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface MockAgentOptions {
|
|
25
|
-
customAgent?: http.RequestOptions['agent']
|
|
26
|
-
onRequest: MockHttpSocketRequestCallback
|
|
27
|
-
onResponse: MockHttpSocketResponseCallback
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export class MockAgent extends http.Agent {
|
|
31
|
-
private customAgent?: http.RequestOptions['agent']
|
|
32
|
-
private onRequest: MockHttpSocketRequestCallback
|
|
33
|
-
private onResponse: MockHttpSocketResponseCallback
|
|
34
|
-
|
|
35
|
-
constructor(options: MockAgentOptions) {
|
|
36
|
-
super()
|
|
37
|
-
this.customAgent = options.customAgent
|
|
38
|
-
this.onRequest = options.onRequest
|
|
39
|
-
this.onResponse = options.onResponse
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public createConnection(options: any, callback: any): net.Socket {
|
|
43
|
-
const createConnection =
|
|
44
|
-
this.customAgent instanceof http.Agent
|
|
45
|
-
? this.customAgent.createConnection
|
|
46
|
-
: super.createConnection
|
|
47
|
-
|
|
48
|
-
const createConnectionOptions =
|
|
49
|
-
this.customAgent instanceof http.Agent
|
|
50
|
-
? {
|
|
51
|
-
...options,
|
|
52
|
-
...this.customAgent.options,
|
|
53
|
-
}
|
|
54
|
-
: options
|
|
55
|
-
|
|
56
|
-
const socket = new MockHttpSocket({
|
|
57
|
-
connectionOptions: options,
|
|
58
|
-
createConnection: createConnection.bind(
|
|
59
|
-
this.customAgent || this,
|
|
60
|
-
createConnectionOptions,
|
|
61
|
-
callback
|
|
62
|
-
),
|
|
63
|
-
onRequest: this.onRequest.bind(this),
|
|
64
|
-
onResponse: this.onResponse.bind(this),
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
return socket
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export class MockHttpsAgent extends https.Agent {
|
|
72
|
-
private customAgent?: https.RequestOptions['agent']
|
|
73
|
-
private onRequest: MockHttpSocketRequestCallback
|
|
74
|
-
private onResponse: MockHttpSocketResponseCallback
|
|
75
|
-
|
|
76
|
-
constructor(options: MockAgentOptions) {
|
|
77
|
-
super()
|
|
78
|
-
this.customAgent = options.customAgent
|
|
79
|
-
this.onRequest = options.onRequest
|
|
80
|
-
this.onResponse = options.onResponse
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
public createConnection(options: any, callback: any): net.Socket {
|
|
84
|
-
const createConnection =
|
|
85
|
-
this.customAgent instanceof http.Agent
|
|
86
|
-
? this.customAgent.createConnection
|
|
87
|
-
: super.createConnection
|
|
88
|
-
|
|
89
|
-
const createConnectionOptions =
|
|
90
|
-
this.customAgent instanceof http.Agent
|
|
91
|
-
? {
|
|
92
|
-
...options,
|
|
93
|
-
...this.customAgent.options,
|
|
94
|
-
}
|
|
95
|
-
: options
|
|
96
|
-
|
|
97
|
-
const socket = new MockHttpSocket({
|
|
98
|
-
connectionOptions: options,
|
|
99
|
-
createConnection: createConnection.bind(
|
|
100
|
-
this.customAgent || this,
|
|
101
|
-
createConnectionOptions,
|
|
102
|
-
callback
|
|
103
|
-
),
|
|
104
|
-
onRequest: this.onRequest.bind(this),
|
|
105
|
-
onResponse: this.onResponse.bind(this),
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
return socket
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { it, expect } from 'vitest'
|
|
2
|
-
import { IncomingMessage } from 'http'
|
|
3
|
-
import { Socket } from 'net'
|
|
4
|
-
import * as zlib from 'zlib'
|
|
5
|
-
import { getIncomingMessageBody } from './getIncomingMessageBody'
|
|
6
|
-
|
|
7
|
-
it('returns utf8 string given a utf8 response body', async () => {
|
|
8
|
-
const utfBuffer = Buffer.from('one')
|
|
9
|
-
const message = new IncomingMessage(new Socket())
|
|
10
|
-
|
|
11
|
-
const pendingResponseBody = getIncomingMessageBody(message)
|
|
12
|
-
message.emit('data', utfBuffer)
|
|
13
|
-
message.emit('end')
|
|
14
|
-
|
|
15
|
-
expect(await pendingResponseBody).toEqual('one')
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('returns utf8 string given a gzipped response body', async () => {
|
|
19
|
-
const utfBuffer = zlib.gzipSync(Buffer.from('two'))
|
|
20
|
-
const message = new IncomingMessage(new Socket())
|
|
21
|
-
message.headers = {
|
|
22
|
-
'content-encoding': 'gzip',
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const pendingResponseBody = getIncomingMessageBody(message)
|
|
26
|
-
message.emit('data', utfBuffer)
|
|
27
|
-
message.emit('end')
|
|
28
|
-
|
|
29
|
-
expect(await pendingResponseBody).toEqual('two')
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('returns utf8 string given a gzipped response body with incorrect "content-length"', async () => {
|
|
33
|
-
const utfBuffer = zlib.gzipSync(Buffer.from('three'))
|
|
34
|
-
const message = new IncomingMessage(new Socket())
|
|
35
|
-
message.headers = {
|
|
36
|
-
'content-encoding': 'gzip',
|
|
37
|
-
'content-length': '500',
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const pendingResponseBody = getIncomingMessageBody(message)
|
|
41
|
-
message.emit('data', utfBuffer)
|
|
42
|
-
message.emit('end')
|
|
43
|
-
|
|
44
|
-
expect(await pendingResponseBody).toEqual('three')
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('returns empty string given an empty body', async () => {
|
|
48
|
-
const message = new IncomingMessage(new Socket())
|
|
49
|
-
|
|
50
|
-
const pendingResponseBody = getIncomingMessageBody(message)
|
|
51
|
-
message.emit('end')
|
|
52
|
-
|
|
53
|
-
expect(await pendingResponseBody).toEqual('')
|
|
54
|
-
})
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage } from 'http'
|
|
2
|
-
import { PassThrough } from 'stream'
|
|
3
|
-
import * as zlib from 'zlib'
|
|
4
|
-
import { Logger } from '@open-draft/logger'
|
|
5
|
-
|
|
6
|
-
const logger = new Logger('http getIncomingMessageBody')
|
|
7
|
-
|
|
8
|
-
export function getIncomingMessageBody(
|
|
9
|
-
response: IncomingMessage
|
|
10
|
-
): Promise<string> {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
logger.info('cloning the original response...')
|
|
13
|
-
|
|
14
|
-
// Pipe the original response to support non-clone
|
|
15
|
-
// "response" input. No need to clone the response,
|
|
16
|
-
// as we always have access to the full "response" input,
|
|
17
|
-
// either a clone or an original one (in tests).
|
|
18
|
-
const responseClone = response.pipe(new PassThrough())
|
|
19
|
-
const stream =
|
|
20
|
-
response.headers['content-encoding'] === 'gzip'
|
|
21
|
-
? responseClone.pipe(zlib.createGunzip())
|
|
22
|
-
: responseClone
|
|
23
|
-
|
|
24
|
-
const encoding = response.readableEncoding || 'utf8'
|
|
25
|
-
stream.setEncoding(encoding)
|
|
26
|
-
logger.info('using encoding:', encoding)
|
|
27
|
-
|
|
28
|
-
let body = ''
|
|
29
|
-
|
|
30
|
-
stream.on('data', (responseBody) => {
|
|
31
|
-
logger.info('response body read:', responseBody)
|
|
32
|
-
body += responseBody
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
stream.once('end', () => {
|
|
36
|
-
logger.info('response body end')
|
|
37
|
-
resolve(body)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
stream.once('error', (error) => {
|
|
41
|
-
logger.info('error while reading response body:', error)
|
|
42
|
-
reject(error)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
}
|
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
import { it, expect } from 'vitest'
|
|
2
|
-
import { parse } from 'url'
|
|
3
|
-
import { globalAgent as httpGlobalAgent, RequestOptions } from 'http'
|
|
4
|
-
import { Agent as HttpsAgent, globalAgent as httpsGlobalAgent } from 'https'
|
|
5
|
-
import { getUrlByRequestOptions } from '../../../utils/getUrlByRequestOptions'
|
|
6
|
-
import { normalizeClientRequestArgs } from './normalizeClientRequestArgs'
|
|
7
|
-
|
|
8
|
-
it('handles [string, callback] input', () => {
|
|
9
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
10
|
-
'https://mswjs.io/resource',
|
|
11
|
-
function cb() {},
|
|
12
|
-
])
|
|
13
|
-
|
|
14
|
-
// URL string must be converted to a URL instance.
|
|
15
|
-
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
16
|
-
|
|
17
|
-
// Request options must be derived from the URL instance.
|
|
18
|
-
expect(options).toHaveProperty('method', 'GET')
|
|
19
|
-
expect(options).toHaveProperty('protocol', 'https:')
|
|
20
|
-
expect(options).toHaveProperty('hostname', 'mswjs.io')
|
|
21
|
-
expect(options).toHaveProperty('path', '/resource')
|
|
22
|
-
|
|
23
|
-
// Callback must be preserved.
|
|
24
|
-
expect(callback?.name).toEqual('cb')
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('handles [string, RequestOptions, callback] input', () => {
|
|
28
|
-
const initialOptions = {
|
|
29
|
-
headers: {
|
|
30
|
-
'Content-Type': 'text/plain',
|
|
31
|
-
},
|
|
32
|
-
}
|
|
33
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
34
|
-
'https://mswjs.io/resource',
|
|
35
|
-
initialOptions,
|
|
36
|
-
function cb() {},
|
|
37
|
-
])
|
|
38
|
-
|
|
39
|
-
// URL must be created from the string.
|
|
40
|
-
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
41
|
-
|
|
42
|
-
// Request options must be preserved.
|
|
43
|
-
expect(options).toHaveProperty('headers', initialOptions.headers)
|
|
44
|
-
|
|
45
|
-
// Callback must be preserved.
|
|
46
|
-
expect(callback?.name).toEqual('cb')
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('handles [URL, callback] input', () => {
|
|
50
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
51
|
-
new URL('https://mswjs.io/resource'),
|
|
52
|
-
function cb() {},
|
|
53
|
-
])
|
|
54
|
-
|
|
55
|
-
// URL must be preserved.
|
|
56
|
-
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
57
|
-
|
|
58
|
-
// Request options must be derived from the URL instance.
|
|
59
|
-
expect(options.method).toEqual('GET')
|
|
60
|
-
expect(options.protocol).toEqual('https:')
|
|
61
|
-
expect(options.hostname).toEqual('mswjs.io')
|
|
62
|
-
expect(options.path).toEqual('/resource')
|
|
63
|
-
|
|
64
|
-
// Callback must be preserved.
|
|
65
|
-
expect(callback?.name).toEqual('cb')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('handles [Absolute Legacy URL, callback] input', () => {
|
|
69
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
70
|
-
parse('https://cherry:durian@mswjs.io:12345/resource?apple=banana'),
|
|
71
|
-
function cb() {},
|
|
72
|
-
])
|
|
73
|
-
|
|
74
|
-
// URL must be preserved.
|
|
75
|
-
expect(url.toJSON()).toEqual(
|
|
76
|
-
new URL(
|
|
77
|
-
'https://cherry:durian@mswjs.io:12345/resource?apple=banana'
|
|
78
|
-
).toJSON()
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
// Request options must be derived from the URL instance.
|
|
82
|
-
expect(options.method).toEqual('GET')
|
|
83
|
-
expect(options.protocol).toEqual('https:')
|
|
84
|
-
expect(options.hostname).toEqual('mswjs.io')
|
|
85
|
-
expect(options.path).toEqual('/resource?apple=banana')
|
|
86
|
-
expect(options.port).toEqual(12345)
|
|
87
|
-
expect(options.auth).toEqual('cherry:durian')
|
|
88
|
-
|
|
89
|
-
// Callback must be preserved.
|
|
90
|
-
expect(callback?.name).toEqual('cb')
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
it('handles [Relative Legacy URL, RequestOptions without path set, callback] input', () => {
|
|
94
|
-
const [url, options, callback] = normalizeClientRequestArgs('http:', [
|
|
95
|
-
parse('/resource?apple=banana'),
|
|
96
|
-
{ host: 'mswjs.io' },
|
|
97
|
-
function cb() {},
|
|
98
|
-
])
|
|
99
|
-
|
|
100
|
-
// Correct WHATWG URL generated.
|
|
101
|
-
expect(url.toJSON()).toEqual(
|
|
102
|
-
new URL('http://mswjs.io/resource?apple=banana').toJSON()
|
|
103
|
-
)
|
|
104
|
-
|
|
105
|
-
// No path in request options, so legacy url path is copied-in.
|
|
106
|
-
expect(options.protocol).toEqual('http:')
|
|
107
|
-
expect(options.host).toEqual('mswjs.io')
|
|
108
|
-
expect(options.path).toEqual('/resource?apple=banana')
|
|
109
|
-
|
|
110
|
-
// Callback must be preserved.
|
|
111
|
-
expect(callback?.name).toEqual('cb')
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
it('handles [Relative Legacy URL, RequestOptions with path set, callback] input', () => {
|
|
115
|
-
const [url, options, callback] = normalizeClientRequestArgs('http:', [
|
|
116
|
-
parse('/resource?apple=banana'),
|
|
117
|
-
{ host: 'mswjs.io', path: '/other?cherry=durian' },
|
|
118
|
-
function cb() {},
|
|
119
|
-
])
|
|
120
|
-
|
|
121
|
-
// Correct WHATWG URL generated.
|
|
122
|
-
expect(url.toJSON()).toEqual(
|
|
123
|
-
new URL('http://mswjs.io/other?cherry=durian').toJSON()
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
// Path in request options, so that path is preferred.
|
|
127
|
-
expect(options.protocol).toEqual('http:')
|
|
128
|
-
expect(options.host).toEqual('mswjs.io')
|
|
129
|
-
expect(options.path).toEqual('/other?cherry=durian')
|
|
130
|
-
|
|
131
|
-
// Callback must be preserved.
|
|
132
|
-
expect(callback?.name).toEqual('cb')
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
it('handles [Relative Legacy URL, callback] input', () => {
|
|
136
|
-
const [url, options, callback] = normalizeClientRequestArgs('http:', [
|
|
137
|
-
parse('/resource?apple=banana'),
|
|
138
|
-
function cb() {},
|
|
139
|
-
])
|
|
140
|
-
|
|
141
|
-
// Correct WHATWG URL generated.
|
|
142
|
-
expect(url.toJSON()).toMatch(
|
|
143
|
-
getUrlByRequestOptions({ path: '/resource?apple=banana' }).toJSON()
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
// Check path is in options.
|
|
147
|
-
expect(options.protocol).toEqual('http:')
|
|
148
|
-
expect(options.path).toEqual('/resource?apple=banana')
|
|
149
|
-
|
|
150
|
-
// Callback must be preserved.
|
|
151
|
-
expect(callback).toBeTypeOf('function')
|
|
152
|
-
expect(callback?.name).toEqual('cb')
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
it('handles [Relative Legacy URL] input', () => {
|
|
156
|
-
const [url, options, callback] = normalizeClientRequestArgs('http:', [
|
|
157
|
-
parse('/resource?apple=banana'),
|
|
158
|
-
])
|
|
159
|
-
|
|
160
|
-
// Correct WHATWG URL generated.
|
|
161
|
-
expect(url.toJSON()).toMatch(
|
|
162
|
-
getUrlByRequestOptions({ path: '/resource?apple=banana' }).toJSON()
|
|
163
|
-
)
|
|
164
|
-
|
|
165
|
-
// Check path is in options.
|
|
166
|
-
expect(options.protocol).toEqual('http:')
|
|
167
|
-
expect(options.path).toEqual('/resource?apple=banana')
|
|
168
|
-
|
|
169
|
-
// Callback must be preserved.
|
|
170
|
-
expect(callback).toBeUndefined()
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
it('handles [URL, RequestOptions, callback] input', () => {
|
|
174
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
175
|
-
new URL('https://mswjs.io/resource'),
|
|
176
|
-
{
|
|
177
|
-
agent: false,
|
|
178
|
-
headers: {
|
|
179
|
-
'Content-Type': 'text/plain',
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
function cb() {},
|
|
183
|
-
])
|
|
184
|
-
|
|
185
|
-
// URL must be preserved.
|
|
186
|
-
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
187
|
-
|
|
188
|
-
// Options must be preserved.
|
|
189
|
-
// `urlToHttpOptions` from `node:url` generates additional
|
|
190
|
-
// ClientRequest options, some of which are not legally allowed.
|
|
191
|
-
expect(options).toMatchObject<RequestOptions>({
|
|
192
|
-
agent: false,
|
|
193
|
-
_defaultAgent: httpsGlobalAgent,
|
|
194
|
-
protocol: url.protocol,
|
|
195
|
-
method: 'GET',
|
|
196
|
-
headers: {
|
|
197
|
-
'Content-Type': 'text/plain',
|
|
198
|
-
},
|
|
199
|
-
hostname: url.hostname,
|
|
200
|
-
path: url.pathname,
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
// Callback must be preserved.
|
|
204
|
-
expect(callback).toBeTypeOf('function')
|
|
205
|
-
expect(callback?.name).toEqual('cb')
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
it('handles [URL, RequestOptions] where options have custom "hostname"', () => {
|
|
209
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
210
|
-
new URL('http://example.com/path-from-url'),
|
|
211
|
-
{
|
|
212
|
-
hostname: 'host-from-options.com',
|
|
213
|
-
},
|
|
214
|
-
])
|
|
215
|
-
expect(url.href).toBe('http://host-from-options.com/path-from-url')
|
|
216
|
-
expect(options).toMatchObject({
|
|
217
|
-
hostname: 'host-from-options.com',
|
|
218
|
-
path: '/path-from-url',
|
|
219
|
-
})
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
it('handles [URL, RequestOptions] where options contain "host" and "path" and "port"', () => {
|
|
223
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
224
|
-
new URL('http://example.com/path-from-url?a=b&c=d'),
|
|
225
|
-
{
|
|
226
|
-
hostname: 'host-from-options.com',
|
|
227
|
-
path: '/path-from-options',
|
|
228
|
-
port: 1234,
|
|
229
|
-
},
|
|
230
|
-
])
|
|
231
|
-
// Must remove the query string since it's not specified in "options.path"
|
|
232
|
-
expect(url.href).toBe('http://host-from-options.com:1234/path-from-options')
|
|
233
|
-
expect(options).toMatchObject<RequestOptions>({
|
|
234
|
-
hostname: 'host-from-options.com',
|
|
235
|
-
path: '/path-from-options',
|
|
236
|
-
port: 1234,
|
|
237
|
-
})
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
it('handles [URL, RequestOptions] where options contain "path" with query string', () => {
|
|
241
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
242
|
-
new URL('http://example.com/path-from-url?a=b&c=d'),
|
|
243
|
-
{
|
|
244
|
-
path: '/path-from-options?foo=bar&baz=xyz',
|
|
245
|
-
},
|
|
246
|
-
])
|
|
247
|
-
expect(url.href).toBe('http://example.com/path-from-options?foo=bar&baz=xyz')
|
|
248
|
-
expect(options).toMatchObject<RequestOptions>({
|
|
249
|
-
hostname: 'example.com',
|
|
250
|
-
path: '/path-from-options?foo=bar&baz=xyz',
|
|
251
|
-
})
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
it('handles [RequestOptions, callback] input', () => {
|
|
255
|
-
const initialOptions = {
|
|
256
|
-
method: 'POST',
|
|
257
|
-
protocol: 'https:',
|
|
258
|
-
host: 'mswjs.io',
|
|
259
|
-
/**
|
|
260
|
-
* @see https://github.com/mswjs/msw/issues/705
|
|
261
|
-
*/
|
|
262
|
-
origin: 'https://mswjs.io',
|
|
263
|
-
path: '/resource',
|
|
264
|
-
headers: {
|
|
265
|
-
'Content-Type': 'text/plain',
|
|
266
|
-
},
|
|
267
|
-
}
|
|
268
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
269
|
-
initialOptions,
|
|
270
|
-
function cb() {},
|
|
271
|
-
])
|
|
272
|
-
|
|
273
|
-
// URL must be derived from request options.
|
|
274
|
-
expect(url.href).toEqual('https://mswjs.io/resource')
|
|
275
|
-
|
|
276
|
-
// Request options must be preserved.
|
|
277
|
-
expect(options).toMatchObject(initialOptions)
|
|
278
|
-
|
|
279
|
-
// Callback must be preserved.
|
|
280
|
-
expect(callback).toBeTypeOf('function')
|
|
281
|
-
expect(callback?.name).toEqual('cb')
|
|
282
|
-
})
|
|
283
|
-
|
|
284
|
-
it('handles [Empty RequestOptions, callback] input', () => {
|
|
285
|
-
const [_, options, callback] = normalizeClientRequestArgs('https:', [
|
|
286
|
-
{},
|
|
287
|
-
function cb() {},
|
|
288
|
-
])
|
|
289
|
-
|
|
290
|
-
expect(options.protocol).toEqual('https:')
|
|
291
|
-
|
|
292
|
-
// Callback must be preserved
|
|
293
|
-
expect(callback?.name).toEqual('cb')
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* @see https://github.com/mswjs/interceptors/issues/19
|
|
298
|
-
*/
|
|
299
|
-
it('handles [PartialRequestOptions, callback] input', () => {
|
|
300
|
-
const initialOptions = {
|
|
301
|
-
method: 'GET',
|
|
302
|
-
port: '50176',
|
|
303
|
-
path: '/resource',
|
|
304
|
-
host: '127.0.0.1',
|
|
305
|
-
ca: undefined,
|
|
306
|
-
key: undefined,
|
|
307
|
-
pfx: undefined,
|
|
308
|
-
cert: undefined,
|
|
309
|
-
passphrase: undefined,
|
|
310
|
-
agent: false,
|
|
311
|
-
}
|
|
312
|
-
const [url, options, callback] = normalizeClientRequestArgs('https:', [
|
|
313
|
-
initialOptions,
|
|
314
|
-
function cb() {},
|
|
315
|
-
])
|
|
316
|
-
|
|
317
|
-
// URL must be derived from request options.
|
|
318
|
-
expect(url.toJSON()).toEqual(
|
|
319
|
-
new URL('https://127.0.0.1:50176/resource').toJSON()
|
|
320
|
-
)
|
|
321
|
-
|
|
322
|
-
// Request options must be preserved.
|
|
323
|
-
expect(options).toMatchObject(initialOptions)
|
|
324
|
-
|
|
325
|
-
// Options protocol must be inferred from the request issuing module.
|
|
326
|
-
expect(options.protocol).toEqual('https:')
|
|
327
|
-
|
|
328
|
-
// Callback must be preserved.
|
|
329
|
-
expect(callback).toBeTypeOf('function')
|
|
330
|
-
expect(callback?.name).toEqual('cb')
|
|
331
|
-
})
|
|
332
|
-
|
|
333
|
-
it('sets the default Agent for HTTP request', () => {
|
|
334
|
-
const [, options] = normalizeClientRequestArgs('http:', [
|
|
335
|
-
'http://github.com',
|
|
336
|
-
{},
|
|
337
|
-
])
|
|
338
|
-
|
|
339
|
-
expect(options._defaultAgent).toEqual(httpGlobalAgent)
|
|
340
|
-
})
|
|
341
|
-
|
|
342
|
-
it('sets the default Agent for HTTPS request', () => {
|
|
343
|
-
const [, options] = normalizeClientRequestArgs('https:', [
|
|
344
|
-
'https://github.com',
|
|
345
|
-
{},
|
|
346
|
-
])
|
|
347
|
-
|
|
348
|
-
expect(options._defaultAgent).toEqual(httpsGlobalAgent)
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
it('preserves a custom default Agent when set', () => {
|
|
352
|
-
const [, options] = normalizeClientRequestArgs('https:', [
|
|
353
|
-
'https://github.com',
|
|
354
|
-
{
|
|
355
|
-
/**
|
|
356
|
-
* @note Intentionally incorrect Agent for HTTPS request.
|
|
357
|
-
*/
|
|
358
|
-
_defaultAgent: httpGlobalAgent,
|
|
359
|
-
},
|
|
360
|
-
])
|
|
361
|
-
|
|
362
|
-
expect(options._defaultAgent).toEqual(httpGlobalAgent)
|
|
363
|
-
})
|
|
364
|
-
|
|
365
|
-
it('merges URL-based RequestOptions with the custom RequestOptions', () => {
|
|
366
|
-
const [url, options] = normalizeClientRequestArgs('https:', [
|
|
367
|
-
'https://github.com/graphql',
|
|
368
|
-
{
|
|
369
|
-
method: 'GET',
|
|
370
|
-
pfx: 'PFX_KEY',
|
|
371
|
-
},
|
|
372
|
-
])
|
|
373
|
-
|
|
374
|
-
expect(url.href).toEqual('https://github.com/graphql')
|
|
375
|
-
|
|
376
|
-
// Original request options must be preserved.
|
|
377
|
-
expect(options.method).toEqual('GET')
|
|
378
|
-
expect(options.pfx).toEqual('PFX_KEY')
|
|
379
|
-
|
|
380
|
-
// Other options must be inferred from the URL.
|
|
381
|
-
expect(options.protocol).toEqual(url.protocol)
|
|
382
|
-
expect(options.hostname).toEqual(url.hostname)
|
|
383
|
-
expect(options.path).toEqual(url.pathname)
|
|
384
|
-
})
|
|
385
|
-
|
|
386
|
-
it('respects custom "options.path" over URL path', () => {
|
|
387
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
388
|
-
new URL('http://example.com/path-from-url'),
|
|
389
|
-
{
|
|
390
|
-
path: '/path-from-options',
|
|
391
|
-
},
|
|
392
|
-
])
|
|
393
|
-
|
|
394
|
-
expect(url.href).toBe('http://example.com/path-from-options')
|
|
395
|
-
expect(options.protocol).toBe('http:')
|
|
396
|
-
expect(options.hostname).toBe('example.com')
|
|
397
|
-
expect(options.path).toBe('/path-from-options')
|
|
398
|
-
})
|
|
399
|
-
|
|
400
|
-
it('respects custom "options.path" over URL path with query string', () => {
|
|
401
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
402
|
-
new URL('http://example.com/path-from-url?a=b&c=d'),
|
|
403
|
-
{
|
|
404
|
-
path: '/path-from-options',
|
|
405
|
-
},
|
|
406
|
-
])
|
|
407
|
-
|
|
408
|
-
// Must replace both the path and the query string.
|
|
409
|
-
expect(url.href).toBe('http://example.com/path-from-options')
|
|
410
|
-
expect(options.protocol).toBe('http:')
|
|
411
|
-
expect(options.hostname).toBe('example.com')
|
|
412
|
-
expect(options.path).toBe('/path-from-options')
|
|
413
|
-
})
|
|
414
|
-
|
|
415
|
-
it('preserves URL query string', () => {
|
|
416
|
-
const [url, options] = normalizeClientRequestArgs('http:', [
|
|
417
|
-
new URL('http://example.com:8080/resource?a=b&c=d'),
|
|
418
|
-
])
|
|
419
|
-
|
|
420
|
-
expect(url.href).toBe('http://example.com:8080/resource?a=b&c=d')
|
|
421
|
-
expect(options.protocol).toBe('http:')
|
|
422
|
-
// expect(options.host).toBe('example.com:8080')
|
|
423
|
-
expect(options.hostname).toBe('example.com')
|
|
424
|
-
// Query string is a part of the options path.
|
|
425
|
-
expect(options.path).toBe('/resource?a=b&c=d')
|
|
426
|
-
expect(options.port).toBe(8080)
|
|
427
|
-
})
|