msw 2.0.12 → 2.0.13
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/browser/index.d.mts +104 -0
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/index.js +1 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/index.mjs +1 -0
- package/lib/browser/index.mjs.map +1 -0
- package/lib/core/{GraphQLHandler-da09c680.d.ts → GraphQLHandler-SHlRCcy4.d.ts} +2 -2
- package/lib/core/GraphQLHandler-zlmUDnN6.d.mts +97 -0
- package/lib/core/HttpResponse.d.mts +2 -0
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js +1 -0
- package/lib/core/HttpResponse.js.map +1 -0
- package/lib/core/HttpResponse.mjs +1 -0
- package/lib/core/HttpResponse.mjs.map +1 -0
- package/lib/core/{RequestHandler-25f9cfd1.d.ts → RequestHandler-rBDJQrEf.d.ts} +1 -1
- package/lib/core/RequestHandler-vxZdj6Tw.d.mts +179 -0
- package/lib/core/SetupApi.d.mts +26 -0
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/SetupApi.js +1 -0
- package/lib/core/SetupApi.js.map +1 -0
- package/lib/core/SetupApi.mjs +1 -0
- package/lib/core/SetupApi.mjs.map +1 -0
- package/lib/core/bypass.d.mts +16 -0
- package/lib/core/bypass.d.ts +1 -1
- package/lib/core/bypass.js +1 -0
- package/lib/core/bypass.js.map +1 -0
- package/lib/core/bypass.mjs +1 -0
- package/lib/core/bypass.mjs.map +1 -0
- package/lib/core/delay.d.mts +18 -0
- package/lib/core/delay.d.ts +1 -1
- package/lib/core/delay.js +1 -0
- package/lib/core/delay.js.map +1 -0
- package/lib/core/delay.mjs +1 -0
- package/lib/core/delay.mjs.map +1 -0
- package/lib/core/graphql.d.mts +109 -0
- package/lib/core/graphql.d.ts +3 -3
- package/lib/core/graphql.js +1 -0
- package/lib/core/graphql.js.map +1 -0
- package/lib/core/graphql.mjs +1 -0
- package/lib/core/graphql.mjs.map +1 -0
- package/lib/core/handlers/GraphQLHandler.d.mts +5 -0
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.js +3 -2
- package/lib/core/handlers/GraphQLHandler.js.map +1 -0
- package/lib/core/handlers/GraphQLHandler.mjs +3 -2
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -0
- package/lib/core/handlers/HttpHandler.d.mts +62 -0
- package/lib/core/handlers/HttpHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.js +1 -0
- package/lib/core/handlers/HttpHandler.js.map +1 -0
- package/lib/core/handlers/HttpHandler.mjs +1 -0
- package/lib/core/handlers/HttpHandler.mjs.map +1 -0
- package/lib/core/handlers/RequestHandler.d.mts +2 -0
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js +3 -2
- package/lib/core/handlers/RequestHandler.js.map +1 -0
- package/lib/core/handlers/RequestHandler.mjs +3 -2
- package/lib/core/handlers/RequestHandler.mjs.map +1 -0
- package/lib/core/http.d.mts +26 -0
- package/lib/core/http.d.ts +1 -1
- package/lib/core/http.js +1 -0
- package/lib/core/http.js.map +1 -0
- package/lib/core/http.mjs +1 -0
- package/lib/core/http.mjs.map +1 -0
- package/lib/core/index.d.mts +18 -0
- package/lib/core/index.d.ts +2 -2
- package/lib/core/index.js +1 -0
- package/lib/core/index.js.map +1 -0
- package/lib/core/index.mjs +1 -0
- package/lib/core/index.mjs.map +1 -0
- package/lib/core/passthrough.d.mts +17 -0
- package/lib/core/passthrough.js +1 -0
- package/lib/core/passthrough.js.map +1 -0
- package/lib/core/passthrough.mjs +1 -0
- package/lib/core/passthrough.mjs.map +1 -0
- package/lib/core/sharedOptions.d.mts +66 -0
- package/lib/core/sharedOptions.d.ts +2 -2
- package/lib/core/sharedOptions.js +1 -0
- package/lib/core/sharedOptions.js.map +1 -0
- package/lib/core/sharedOptions.mjs +1 -0
- package/lib/core/sharedOptions.mjs.map +1 -0
- package/lib/core/typeUtils.d.mts +7 -0
- package/lib/core/typeUtils.d.ts +1 -1
- package/lib/core/typeUtils.js +1 -0
- package/lib/core/typeUtils.js.map +1 -0
- package/lib/core/typeUtils.mjs +1 -0
- package/lib/core/typeUtils.mjs.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.d.mts +12 -0
- package/lib/core/utils/HttpResponse/decorators.d.ts +2 -2
- package/lib/core/utils/HttpResponse/decorators.js +1 -0
- package/lib/core/utils/HttpResponse/decorators.js.map +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs +1 -0
- package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -0
- package/lib/core/utils/getResponse.d.mts +2 -0
- package/lib/core/utils/getResponse.d.ts +1 -1
- package/lib/core/utils/getResponse.js +1 -0
- package/lib/core/utils/getResponse.js.map +1 -0
- package/lib/core/utils/getResponse.mjs +1 -0
- package/lib/core/utils/getResponse.mjs.map +1 -0
- package/lib/core/utils/handleRequest.d.mts +37 -0
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js +1 -0
- package/lib/core/utils/handleRequest.js.map +1 -0
- package/lib/core/utils/handleRequest.mjs +1 -0
- package/lib/core/utils/handleRequest.mjs.map +1 -0
- package/lib/core/utils/internal/Disposable.d.mts +7 -0
- package/lib/core/utils/internal/Disposable.d.ts +1 -1
- package/lib/core/utils/internal/Disposable.js +1 -0
- package/lib/core/utils/internal/Disposable.js.map +1 -0
- package/lib/core/utils/internal/Disposable.mjs +1 -0
- package/lib/core/utils/internal/Disposable.mjs.map +1 -0
- package/lib/core/utils/internal/checkGlobals.d.mts +3 -0
- package/lib/core/utils/internal/checkGlobals.js +1 -0
- package/lib/core/utils/internal/checkGlobals.js.map +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs +1 -0
- package/lib/core/utils/internal/checkGlobals.mjs.map +1 -0
- package/lib/core/utils/internal/devUtils.d.mts +19 -0
- package/lib/core/utils/internal/devUtils.js +1 -0
- package/lib/core/utils/internal/devUtils.js.map +1 -0
- package/lib/core/utils/internal/devUtils.mjs +1 -0
- package/lib/core/utils/internal/devUtils.mjs.map +1 -0
- package/lib/core/utils/internal/getCallFrame.d.mts +6 -0
- package/lib/core/utils/internal/getCallFrame.js +1 -0
- package/lib/core/utils/internal/getCallFrame.js.map +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs +1 -0
- package/lib/core/utils/internal/getCallFrame.mjs.map +1 -0
- package/lib/core/utils/internal/isIterable.d.mts +6 -0
- package/lib/core/utils/internal/isIterable.js +1 -0
- package/lib/core/utils/internal/isIterable.js.map +1 -0
- package/lib/core/utils/internal/isIterable.mjs +1 -0
- package/lib/core/utils/internal/isIterable.mjs.map +1 -0
- package/lib/core/utils/internal/isObject.d.mts +6 -0
- package/lib/core/utils/internal/isObject.js +1 -0
- package/lib/core/utils/internal/isObject.js.map +1 -0
- package/lib/core/utils/internal/isObject.mjs +1 -0
- package/lib/core/utils/internal/isObject.mjs.map +1 -0
- package/lib/core/utils/internal/isStringEqual.d.mts +6 -0
- package/lib/core/utils/internal/isStringEqual.js +1 -0
- package/lib/core/utils/internal/isStringEqual.js.map +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs +1 -0
- package/lib/core/utils/internal/isStringEqual.mjs.map +1 -0
- package/lib/core/utils/internal/jsonParse.d.mts +7 -0
- package/lib/core/utils/internal/jsonParse.js +1 -0
- package/lib/core/utils/internal/jsonParse.js.map +1 -0
- package/lib/core/utils/internal/jsonParse.mjs +1 -0
- package/lib/core/utils/internal/jsonParse.mjs.map +1 -0
- package/lib/core/utils/internal/mergeRight.d.mts +7 -0
- package/lib/core/utils/internal/mergeRight.js +1 -0
- package/lib/core/utils/internal/mergeRight.js.map +1 -0
- package/lib/core/utils/internal/mergeRight.mjs +1 -0
- package/lib/core/utils/internal/mergeRight.mjs.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +5 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.js +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.d.mts +10 -0
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.js +1 -0
- package/lib/core/utils/internal/parseMultipartData.js.map +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs +1 -0
- package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -0
- package/lib/core/utils/internal/pipeEvents.d.mts +8 -0
- package/lib/core/utils/internal/pipeEvents.js +1 -0
- package/lib/core/utils/internal/pipeEvents.js.map +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs +1 -0
- package/lib/core/utils/internal/pipeEvents.mjs.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +8 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.js +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.js.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.d.mts +6 -0
- package/lib/core/utils/internal/toReadonlyArray.js +1 -0
- package/lib/core/utils/internal/toReadonlyArray.js.map +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs +1 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs.map +1 -0
- package/lib/core/utils/internal/tryCatch.d.mts +3 -0
- package/lib/core/utils/internal/tryCatch.js +1 -0
- package/lib/core/utils/internal/tryCatch.js.map +1 -0
- package/lib/core/utils/internal/tryCatch.mjs +1 -0
- package/lib/core/utils/internal/tryCatch.mjs.map +1 -0
- package/lib/core/utils/internal/uuidv4.d.mts +3 -0
- package/lib/core/utils/internal/uuidv4.js +1 -0
- package/lib/core/utils/internal/uuidv4.js.map +1 -0
- package/lib/core/utils/internal/uuidv4.mjs +1 -0
- package/lib/core/utils/internal/uuidv4.mjs.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.d.mts +11 -0
- package/lib/core/utils/logging/getStatusCodeColor.js +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.js.map +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs +1 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs.map +1 -0
- package/lib/core/utils/logging/getTimestamp.d.mts +6 -0
- package/lib/core/utils/logging/getTimestamp.js +1 -0
- package/lib/core/utils/logging/getTimestamp.js.map +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs +1 -0
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -0
- package/lib/core/utils/logging/serializeRequest.d.mts +12 -0
- package/lib/core/utils/logging/serializeRequest.d.ts +1 -1
- package/lib/core/utils/logging/serializeRequest.js +1 -0
- package/lib/core/utils/logging/serializeRequest.js.map +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs +1 -0
- package/lib/core/utils/logging/serializeRequest.mjs.map +1 -0
- package/lib/core/utils/logging/serializeResponse.d.mts +9 -0
- package/lib/core/utils/logging/serializeResponse.d.ts +1 -1
- package/lib/core/utils/logging/serializeResponse.js +1 -0
- package/lib/core/utils/logging/serializeResponse.js.map +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs +1 -0
- package/lib/core/utils/logging/serializeResponse.mjs.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.d.mts +19 -0
- package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -1
- package/lib/core/utils/matching/matchRequestUrl.js +1 -0
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs +1 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -0
- package/lib/core/utils/matching/normalizePath.d.mts +12 -0
- package/lib/core/utils/matching/normalizePath.js +1 -0
- package/lib/core/utils/matching/normalizePath.js.map +1 -0
- package/lib/core/utils/matching/normalizePath.mjs +1 -0
- package/lib/core/utils/matching/normalizePath.mjs.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +7 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js.map +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs +1 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +1 -0
- package/lib/core/utils/request/getRequestCookies.d.mts +8 -0
- package/lib/core/utils/request/getRequestCookies.js +1 -0
- package/lib/core/utils/request/getRequestCookies.js.map +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs +1 -0
- package/lib/core/utils/request/getRequestCookies.mjs.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.d.mts +12 -0
- package/lib/core/utils/request/onUnhandledRequest.d.ts +2 -2
- package/lib/core/utils/request/onUnhandledRequest.js +1 -0
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs +1 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -0
- package/lib/core/utils/request/readResponseCookies.d.mts +3 -0
- package/lib/core/utils/request/readResponseCookies.js +1 -0
- package/lib/core/utils/request/readResponseCookies.js.map +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs +1 -0
- package/lib/core/utils/request/readResponseCookies.mjs.map +1 -0
- package/lib/core/utils/toResponseInit.d.mts +3 -0
- package/lib/core/utils/toResponseInit.js +1 -0
- package/lib/core/utils/toResponseInit.js.map +1 -0
- package/lib/core/utils/toResponseInit.mjs +1 -0
- package/lib/core/utils/toResponseInit.mjs.map +1 -0
- package/lib/core/utils/url/cleanUrl.d.mts +7 -0
- package/lib/core/utils/url/cleanUrl.js +1 -0
- package/lib/core/utils/url/cleanUrl.js.map +1 -0
- package/lib/core/utils/url/cleanUrl.mjs +1 -0
- package/lib/core/utils/url/cleanUrl.mjs.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/getAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.d.mts +6 -0
- package/lib/core/utils/url/isAbsoluteUrl.js +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.js.map +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs +1 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs.map +1 -0
- package/lib/iife/index.js +20 -19
- package/lib/iife/index.js.map +1 -0
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +75 -0
- package/lib/native/index.js +1 -0
- package/lib/native/index.js.map +1 -0
- package/lib/native/index.mjs +1 -0
- package/lib/native/index.mjs.map +1 -0
- package/lib/node/index.d.mts +75 -0
- package/lib/node/index.d.ts +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/index.mjs +1 -0
- package/lib/node/index.mjs.map +1 -0
- package/package.json +5 -4
- package/src/browser/global.browser.d.ts +1 -0
- package/src/browser/index.ts +3 -0
- package/src/browser/setupWorker/glossary.ts +259 -0
- package/src/browser/setupWorker/setupWorker.node.test.ts +10 -0
- package/src/browser/setupWorker/setupWorker.ts +199 -0
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +67 -0
- package/src/browser/setupWorker/start/createFallbackStart.ts +21 -0
- package/src/browser/setupWorker/start/createRequestListener.ts +127 -0
- package/src/browser/setupWorker/start/createResponseListener.ts +58 -0
- package/src/browser/setupWorker/start/createStartHandler.ts +143 -0
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +32 -0
- package/src/browser/setupWorker/start/utils/enableMocking.ts +32 -0
- package/src/browser/setupWorker/start/utils/getWorkerByRegistration.ts +25 -0
- package/src/browser/setupWorker/start/utils/getWorkerInstance.ts +94 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.test.ts +59 -0
- package/src/browser/setupWorker/start/utils/prepareStartHandler.ts +44 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.test.ts +84 -0
- package/src/browser/setupWorker/start/utils/printStartMessage.ts +40 -0
- package/src/browser/setupWorker/start/utils/validateWorkerScope.ts +18 -0
- package/src/browser/setupWorker/stop/createFallbackStop.ts +11 -0
- package/src/browser/setupWorker/stop/createStop.ts +29 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.test.ts +26 -0
- package/src/browser/setupWorker/stop/utils/printStopMessage.ts +12 -0
- package/src/browser/tsconfig.browser.build.json +6 -0
- package/src/browser/tsconfig.browser.json +9 -0
- package/src/browser/utils/deferNetworkRequestsUntil.test.ts +48 -0
- package/src/browser/utils/deferNetworkRequestsUntil.ts +29 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +31 -0
- package/src/browser/utils/getAbsoluteWorkerUrl.ts +7 -0
- package/src/browser/utils/parseWorkerRequest.ts +15 -0
- package/src/browser/utils/pruneGetRequestBody.test.ts +53 -0
- package/src/browser/utils/pruneGetRequestBody.ts +21 -0
- package/src/browser/utils/requestIntegrityCheck.ts +23 -0
- package/src/browser/utils/supportsReadableStreamTransfer.ts +17 -0
- package/src/core/HttpResponse.test.ts +200 -0
- package/src/core/HttpResponse.ts +134 -0
- package/src/core/SetupApi.ts +95 -0
- package/src/core/bypass.test.ts +47 -0
- package/src/core/bypass.ts +36 -0
- package/src/core/delay.ts +70 -0
- package/src/core/graphql.test.ts +11 -0
- package/src/core/graphql.ts +138 -0
- package/src/core/handlers/GraphQLHandler.test.ts +820 -0
- package/src/core/handlers/GraphQLHandler.ts +263 -0
- package/src/core/handlers/HttpHandler.test.ts +218 -0
- package/src/core/handlers/HttpHandler.ts +169 -0
- package/src/core/handlers/RequestHandler.ts +338 -0
- package/src/core/http.test.ts +15 -0
- package/src/core/http.ts +51 -0
- package/src/core/index.ts +56 -0
- package/src/core/passthrough.test.ts +13 -0
- package/src/core/passthrough.ts +23 -0
- package/src/core/sharedOptions.ts +66 -0
- package/src/core/typeUtils.ts +20 -0
- package/src/core/utils/HttpResponse/decorators.ts +56 -0
- package/src/core/utils/getResponse.ts +55 -0
- package/src/core/utils/handleRequest.test.ts +554 -0
- package/src/core/utils/handleRequest.ts +132 -0
- package/src/core/utils/internal/Disposable.ts +9 -0
- package/src/core/utils/internal/checkGlobals.ts +17 -0
- package/src/core/utils/internal/devUtils.ts +31 -0
- package/src/core/utils/internal/getCallFrame.test.ts +154 -0
- package/src/core/utils/internal/getCallFrame.ts +35 -0
- package/src/core/utils/internal/isIterable.test.ts +23 -0
- package/src/core/utils/internal/isIterable.ts +12 -0
- package/src/core/utils/internal/isObject.test.ts +20 -0
- package/src/core/utils/internal/isObject.ts +6 -0
- package/src/core/utils/internal/isStringEqual.test.ts +45 -0
- package/src/core/utils/internal/isStringEqual.ts +6 -0
- package/src/core/utils/internal/jsonParse.test.ts +13 -0
- package/src/core/utils/internal/jsonParse.ts +13 -0
- package/src/core/utils/internal/mergeRight.test.ts +43 -0
- package/src/core/utils/internal/mergeRight.ts +27 -0
- package/src/core/utils/internal/parseGraphQLRequest.test.ts +99 -0
- package/src/core/utils/internal/parseGraphQLRequest.ts +205 -0
- package/src/core/utils/internal/parseMultipartData.test.ts +76 -0
- package/src/core/utils/internal/parseMultipartData.ts +104 -0
- package/src/core/utils/internal/pipeEvents.test.ts +14 -0
- package/src/core/utils/internal/pipeEvents.ts +25 -0
- package/src/core/utils/internal/requestHandlerUtils.ts +21 -0
- package/src/core/utils/internal/toReadonlyArray.test.ts +30 -0
- package/src/core/utils/internal/toReadonlyArray.ts +8 -0
- package/src/core/utils/internal/tryCatch.test.ts +29 -0
- package/src/core/utils/internal/tryCatch.ts +11 -0
- package/src/core/utils/internal/uuidv4.ts +3 -0
- package/src/core/utils/logging/getStatusCodeColor.test.ts +22 -0
- package/src/core/utils/logging/getStatusCodeColor.ts +20 -0
- package/src/core/utils/logging/getTimestamp.test.ts +18 -0
- package/src/core/utils/logging/getTimestamp.ts +12 -0
- package/src/core/utils/logging/serializeRequest.test.ts +23 -0
- package/src/core/utils/logging/serializeRequest.ts +23 -0
- package/src/core/utils/logging/serializeResponse.test.ts +77 -0
- package/src/core/utils/logging/serializeResponse.ts +31 -0
- package/src/core/utils/matching/matchRequestUrl.test.ts +114 -0
- package/src/core/utils/matching/matchRequestUrl.ts +73 -0
- package/src/core/utils/matching/normalizePath.node.test.ts +44 -0
- package/src/core/utils/matching/normalizePath.test.ts +50 -0
- package/src/core/utils/matching/normalizePath.ts +21 -0
- package/src/core/utils/request/getPublicUrlFromRequest.test.ts +26 -0
- package/src/core/utils/request/getPublicUrlFromRequest.ts +15 -0
- package/src/core/utils/request/getRequestCookies.node.test.ts +29 -0
- package/src/core/utils/request/getRequestCookies.test.ts +64 -0
- package/src/core/utils/request/getRequestCookies.ts +75 -0
- package/src/core/utils/request/onUnhandledRequest.test.ts +215 -0
- package/src/core/utils/request/onUnhandledRequest.ts +247 -0
- package/src/core/utils/request/readResponseCookies.ts +9 -0
- package/src/core/utils/toResponseInit.ts +7 -0
- package/src/core/utils/url/cleanUrl.test.ts +17 -0
- package/src/core/utils/url/cleanUrl.ts +12 -0
- package/src/core/utils/url/getAbsoluteUrl.node.test.ts +19 -0
- package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -0
- package/src/core/utils/url/getAbsoluteUrl.ts +26 -0
- package/src/core/utils/url/isAbsoluteUrl.test.ts +32 -0
- package/src/core/utils/url/isAbsoluteUrl.ts +6 -0
- package/src/iife/index.ts +2 -0
- package/src/mockServiceWorker.js +287 -0
- package/src/native/index.ts +17 -0
- package/src/node/SetupServerApi.ts +113 -0
- package/src/node/glossary.ts +62 -0
- package/src/node/index.ts +3 -0
- package/src/node/setupServer.ts +21 -0
- package/src/node/utils/isNodeExceptionLike.ts +14 -0
- package/src/tsconfig.core.build.json +6 -0
- package/src/tsconfig.node.build.json +6 -0
- package/src/tsconfig.node.json +8 -0
- package/src/tsconfig.src.json +15 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { encodeBuffer } from '@mswjs/interceptors'
|
|
5
|
+
import { serializeResponse } from './serializeResponse'
|
|
6
|
+
|
|
7
|
+
it('serializes response without body', async () => {
|
|
8
|
+
const result = await serializeResponse(new Response(null))
|
|
9
|
+
|
|
10
|
+
expect(result.status).toBe(200)
|
|
11
|
+
expect(result.statusText).toBe('OK')
|
|
12
|
+
expect(result.headers).toEqual({})
|
|
13
|
+
expect(result.body).toBe('')
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('serializes a plain text response', async () => {
|
|
17
|
+
const result = await serializeResponse(
|
|
18
|
+
new Response('hello world', {
|
|
19
|
+
status: 201,
|
|
20
|
+
statusText: 'Created',
|
|
21
|
+
headers: {
|
|
22
|
+
'Content-Type': 'text/plain',
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
expect(result.status).toBe(201)
|
|
28
|
+
expect(result.statusText).toBe('Created')
|
|
29
|
+
expect(result.headers).toEqual({
|
|
30
|
+
'content-type': 'text/plain',
|
|
31
|
+
})
|
|
32
|
+
expect(result.body).toBe('hello world')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('serializes a JSON response', async () => {
|
|
36
|
+
const response = new Response(JSON.stringify({ users: ['John'] }), {
|
|
37
|
+
headers: {
|
|
38
|
+
'Content-Type': 'application/json',
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
const result = await serializeResponse(response)
|
|
42
|
+
|
|
43
|
+
expect(result.headers).toEqual({
|
|
44
|
+
'content-type': 'application/json',
|
|
45
|
+
})
|
|
46
|
+
expect(result.body).toBe(JSON.stringify({ users: ['John'] }))
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('serializes a ArrayBuffer response', async () => {
|
|
50
|
+
const data = encodeBuffer('hello world')
|
|
51
|
+
const response = new Response(data)
|
|
52
|
+
const result = await serializeResponse(response)
|
|
53
|
+
|
|
54
|
+
expect(result.body).toBe('hello world')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('serializes a Blob response', async () => {
|
|
58
|
+
const response = new Response(new Blob(['hello world']))
|
|
59
|
+
const result = await serializeResponse(response)
|
|
60
|
+
|
|
61
|
+
expect(result.body).toBe('hello world')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('serializes a FormData response', async () => {
|
|
65
|
+
const data = new FormData()
|
|
66
|
+
data.set('firstName', 'Alice')
|
|
67
|
+
data.set('age', '32')
|
|
68
|
+
const response = new Response(data)
|
|
69
|
+
const result = await serializeResponse(response)
|
|
70
|
+
|
|
71
|
+
expect(result.body).toContain(
|
|
72
|
+
`Content-Disposition: form-data; name="firstName"\r\n\r\nAlice`,
|
|
73
|
+
)
|
|
74
|
+
expect(result.body).toContain(
|
|
75
|
+
`Content-Disposition: form-data; name="age"\r\n\r\n32`,
|
|
76
|
+
)
|
|
77
|
+
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import statuses from '@bundled-es-modules/statuses'
|
|
2
|
+
|
|
3
|
+
const { message } = statuses
|
|
4
|
+
|
|
5
|
+
export interface SerializedResponse {
|
|
6
|
+
status: number
|
|
7
|
+
statusText: string
|
|
8
|
+
headers: Record<string, any>
|
|
9
|
+
body: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function serializeResponse(
|
|
13
|
+
response: Response,
|
|
14
|
+
): Promise<SerializedResponse> {
|
|
15
|
+
const responseClone = response.clone()
|
|
16
|
+
const responseText = await responseClone.text()
|
|
17
|
+
|
|
18
|
+
// Normalize the response status and status text when logging
|
|
19
|
+
// since the default Response instance doesn't infer status texts
|
|
20
|
+
// from status codes. This has no effect on the actual response instance.
|
|
21
|
+
const responseStatus = responseClone.status || 200
|
|
22
|
+
const responseStatusText =
|
|
23
|
+
responseClone.statusText || message[responseStatus] || 'OK'
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
status: responseStatus,
|
|
27
|
+
statusText: responseStatusText,
|
|
28
|
+
headers: Object.fromEntries(responseClone.headers.entries()),
|
|
29
|
+
body: responseText,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { coercePath, matchRequestUrl } from './matchRequestUrl'
|
|
5
|
+
|
|
6
|
+
describe('matchRequestUrl', () => {
|
|
7
|
+
test('returns true when matches against an exact URL', () => {
|
|
8
|
+
const match = matchRequestUrl(
|
|
9
|
+
new URL('https://test.mswjs.io'),
|
|
10
|
+
'https://test.mswjs.io',
|
|
11
|
+
)
|
|
12
|
+
expect(match).toHaveProperty('matches', true)
|
|
13
|
+
expect(match).toHaveProperty('params', {})
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('returns true when matched against a wildcard', () => {
|
|
17
|
+
const match = matchRequestUrl(new URL('https://test.mswjs.io'), '*')
|
|
18
|
+
expect(match).toHaveProperty('matches', true)
|
|
19
|
+
expect(match).toHaveProperty('params', {
|
|
20
|
+
'0': 'https://test.mswjs.io/',
|
|
21
|
+
})
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('returns true when matched against a RegExp', () => {
|
|
25
|
+
const match = matchRequestUrl(
|
|
26
|
+
new URL('https://test.mswjs.io'),
|
|
27
|
+
/test\.mswjs\.io/,
|
|
28
|
+
)
|
|
29
|
+
expect(match).toHaveProperty('matches', true)
|
|
30
|
+
expect(match).toHaveProperty('params', {})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('returns path parameters when matched', () => {
|
|
34
|
+
const match = matchRequestUrl(
|
|
35
|
+
new URL('https://test.mswjs.io/user/abc-123'),
|
|
36
|
+
'https://test.mswjs.io/user/:userId',
|
|
37
|
+
)
|
|
38
|
+
expect(match).toHaveProperty('matches', true)
|
|
39
|
+
expect(match).toHaveProperty('params', {
|
|
40
|
+
userId: 'abc-123',
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('decodes path parameters', () => {
|
|
45
|
+
const url = 'http://example.com:5001/example'
|
|
46
|
+
const match = matchRequestUrl(
|
|
47
|
+
new URL(`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`),
|
|
48
|
+
'https://test.mswjs.io/reflect-url/:url',
|
|
49
|
+
)
|
|
50
|
+
expect(match).toHaveProperty('matches', true)
|
|
51
|
+
expect(match).toHaveProperty('params', {
|
|
52
|
+
url,
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('returns false when does not match against the request URL', () => {
|
|
57
|
+
const match = matchRequestUrl(
|
|
58
|
+
new URL('https://test.mswjs.io'),
|
|
59
|
+
'https://google.com',
|
|
60
|
+
)
|
|
61
|
+
expect(match).toHaveProperty('matches', false)
|
|
62
|
+
expect(match).toHaveProperty('params', {})
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('coercePath', () => {
|
|
67
|
+
test('escapes the colon in protocol', () => {
|
|
68
|
+
expect(coercePath('https://example.com')).toEqual('https\\://example.com')
|
|
69
|
+
expect(coercePath('https://example.com/:userId')).toEqual(
|
|
70
|
+
'https\\://example.com/:userId',
|
|
71
|
+
)
|
|
72
|
+
expect(coercePath('http://localhost:3000')).toEqual(
|
|
73
|
+
'http\\://localhost\\:3000',
|
|
74
|
+
)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('escapes the colon before the port number', () => {
|
|
78
|
+
expect(coercePath('localhost:8080')).toEqual('localhost\\:8080')
|
|
79
|
+
expect(coercePath('http://127.0.0.1:8080')).toEqual(
|
|
80
|
+
'http\\://127.0.0.1\\:8080',
|
|
81
|
+
)
|
|
82
|
+
expect(coercePath('https://example.com:1234')).toEqual(
|
|
83
|
+
'https\\://example.com\\:1234',
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
expect(coercePath('localhost:8080/:5678')).toEqual('localhost\\:8080/:5678')
|
|
87
|
+
expect(coercePath('https://example.com:8080/:5678')).toEqual(
|
|
88
|
+
'https\\://example.com\\:8080/:5678',
|
|
89
|
+
)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('replaces wildcard with an unnnamed capturing group', () => {
|
|
93
|
+
expect(coercePath('*')).toEqual('(.*)')
|
|
94
|
+
expect(coercePath('**')).toEqual('(.*)')
|
|
95
|
+
expect(coercePath('/us*')).toEqual('/us(.*)')
|
|
96
|
+
expect(coercePath('/user/*')).toEqual('/user/(.*)')
|
|
97
|
+
expect(coercePath('https://example.com/user/*')).toEqual(
|
|
98
|
+
'https\\://example.com/user/(.*)',
|
|
99
|
+
)
|
|
100
|
+
expect(coercePath('https://example.com/us*')).toEqual(
|
|
101
|
+
'https\\://example.com/us(.*)',
|
|
102
|
+
)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('preserves path parameter modifiers', () => {
|
|
106
|
+
expect(coercePath(':name*')).toEqual(':name*')
|
|
107
|
+
expect(coercePath('/foo/:name*')).toEqual('/foo/:name*')
|
|
108
|
+
expect(coercePath('/foo/**:name*')).toEqual('/foo/(.*):name*')
|
|
109
|
+
expect(coercePath('**/foo/*/:name*')).toEqual('(.*)/foo/(.*)/:name*')
|
|
110
|
+
expect(coercePath('/foo/:first/bar/:second*/*')).toEqual(
|
|
111
|
+
'/foo/:first/bar/:second*/(.*)',
|
|
112
|
+
)
|
|
113
|
+
})
|
|
114
|
+
})
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { match } from 'path-to-regexp'
|
|
2
|
+
import { getCleanUrl } from '@mswjs/interceptors'
|
|
3
|
+
import { normalizePath } from './normalizePath'
|
|
4
|
+
|
|
5
|
+
export type Path = string | RegExp
|
|
6
|
+
export type PathParams<KeyType extends keyof any = string> = {
|
|
7
|
+
[ParamName in KeyType]: string | ReadonlyArray<string>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Match {
|
|
11
|
+
matches: boolean
|
|
12
|
+
params?: PathParams
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Coerce a path supported by MSW into a path
|
|
17
|
+
* supported by "path-to-regexp".
|
|
18
|
+
*/
|
|
19
|
+
export function coercePath(path: string): string {
|
|
20
|
+
return (
|
|
21
|
+
path
|
|
22
|
+
/**
|
|
23
|
+
* Replace wildcards ("*") with unnamed capturing groups
|
|
24
|
+
* because "path-to-regexp" doesn't support wildcards.
|
|
25
|
+
* Ignore path parameter' modifiers (i.e. ":name*").
|
|
26
|
+
*/
|
|
27
|
+
.replace(
|
|
28
|
+
/([:a-zA-Z_-]*)(\*{1,2})+/g,
|
|
29
|
+
(_, parameterName: string | undefined, wildcard: string) => {
|
|
30
|
+
const expression = '(.*)'
|
|
31
|
+
|
|
32
|
+
if (!parameterName) {
|
|
33
|
+
return expression
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return parameterName.startsWith(':')
|
|
37
|
+
? `${parameterName}${wildcard}`
|
|
38
|
+
: `${parameterName}${expression}`
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
/**
|
|
42
|
+
* Escape the port so that "path-to-regexp" can match
|
|
43
|
+
* absolute URLs including port numbers.
|
|
44
|
+
*/
|
|
45
|
+
.replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
|
|
46
|
+
/**
|
|
47
|
+
* Escape the protocol so that "path-to-regexp" could match
|
|
48
|
+
* absolute URL.
|
|
49
|
+
* @see https://github.com/pillarjs/path-to-regexp/issues/259
|
|
50
|
+
*/
|
|
51
|
+
.replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2')
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns the result of matching given request URL against a mask.
|
|
57
|
+
*/
|
|
58
|
+
export function matchRequestUrl(url: URL, path: Path, baseUrl?: string): Match {
|
|
59
|
+
const normalizedPath = normalizePath(path, baseUrl)
|
|
60
|
+
const cleanPath =
|
|
61
|
+
typeof normalizedPath === 'string'
|
|
62
|
+
? coercePath(normalizedPath)
|
|
63
|
+
: normalizedPath
|
|
64
|
+
|
|
65
|
+
const cleanUrl = getCleanUrl(url)
|
|
66
|
+
const result = match(cleanPath, { decode: decodeURIComponent })(cleanUrl)
|
|
67
|
+
const params = (result && (result.params as PathParams)) || {}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
matches: result !== false,
|
|
71
|
+
params,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { normalizePath } from './normalizePath'
|
|
5
|
+
|
|
6
|
+
test('returns RegExp as-is', () => {
|
|
7
|
+
const path = /s/
|
|
8
|
+
expect(normalizePath(path)).toEqual(path)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('returns a clean absolute URL as-is', () => {
|
|
12
|
+
const path = 'https://test.mswjs.io/path'
|
|
13
|
+
expect(normalizePath(path)).toEqual(path)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('returns a relative URL as-is given a string path', () => {
|
|
17
|
+
const path = '/relative/url'
|
|
18
|
+
expect(normalizePath(path)).toEqual(path)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('rebases a relative URL against a custom base URL', () => {
|
|
22
|
+
const path = '/relative/url'
|
|
23
|
+
expect(normalizePath(path, 'https://test.mswjs.io')).toEqual(
|
|
24
|
+
'https://test.mswjs.io/relative/url',
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('removes query parameters and hashes from an absolute URL', () => {
|
|
29
|
+
expect(normalizePath('https://test.mswjs.io/user?query=123')).toEqual(
|
|
30
|
+
'https://test.mswjs.io/user',
|
|
31
|
+
)
|
|
32
|
+
expect(normalizePath('https://test.mswjs.io/user#some')).toEqual(
|
|
33
|
+
'https://test.mswjs.io/user',
|
|
34
|
+
)
|
|
35
|
+
expect(normalizePath('https://test.mswjs.io/user?query=123#some')).toEqual(
|
|
36
|
+
'https://test.mswjs.io/user',
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('removes query parameters and hashes from a relative URL', () => {
|
|
41
|
+
expect(normalizePath('/user?query=123')).toEqual('/user')
|
|
42
|
+
expect(normalizePath('/user#some')).toEqual('/user')
|
|
43
|
+
expect(normalizePath('/user?query=123#some')).toEqual('/user')
|
|
44
|
+
})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { normalizePath } from './normalizePath'
|
|
5
|
+
|
|
6
|
+
test('returns RegExp as-is', () => {
|
|
7
|
+
const path = /\/user\/(.+?)\//
|
|
8
|
+
expect(normalizePath(path)).toEqual(path)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('returns a clean absolute URL as-is', () => {
|
|
12
|
+
const path = 'https://test.mswjs.io/user'
|
|
13
|
+
expect(normalizePath(path)).toEqual(path)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('rebases a relative URL against the current location', () => {
|
|
17
|
+
const path = '/relative/url'
|
|
18
|
+
expect(normalizePath(path)).toBe(`${location.origin}/relative/url`)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('rebases a relative URL against a custom base URL', () => {
|
|
22
|
+
const path = '/relative/url'
|
|
23
|
+
expect(normalizePath(path, 'https://test.mswjs.io')).toEqual(
|
|
24
|
+
'https://test.mswjs.io/relative/url',
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('removes query parameters and hashes from an absolute URL', () => {
|
|
29
|
+
const path = 'https://test.mswjs.io/user?query=123#some'
|
|
30
|
+
expect(normalizePath(path)).toEqual('https://test.mswjs.io/user')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('removes query parameters and hashes from a relative URL', () => {
|
|
34
|
+
expect(normalizePath('/user?query=123')).toEqual(`${location.origin}/user`)
|
|
35
|
+
expect(normalizePath('/user#some')).toEqual(`${location.origin}/user`)
|
|
36
|
+
expect(normalizePath('/user?query=123#some')).toEqual(
|
|
37
|
+
`${location.origin}/user`,
|
|
38
|
+
)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('returns a path pattern string as-is', () => {
|
|
42
|
+
expect(normalizePath(':api/user')).toEqual('http://localhost/:api/user')
|
|
43
|
+
expect(normalizePath('*/resource/*')).toEqual('*/resource/*')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('removeß query parameters and hashes from a path pattern string', () => {
|
|
47
|
+
expect(normalizePath(':api/user?query=123#some')).toEqual(
|
|
48
|
+
'http://localhost/:api/user',
|
|
49
|
+
)
|
|
50
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Path } from './matchRequestUrl'
|
|
2
|
+
import { cleanUrl } from '../url/cleanUrl'
|
|
3
|
+
import { getAbsoluteUrl } from '../url/getAbsoluteUrl'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes a given request handler path:
|
|
7
|
+
* - Preserves RegExp.
|
|
8
|
+
* - Removes query parameters and hashes.
|
|
9
|
+
* - Rebases relative URLs against the "baseUrl" or the current location.
|
|
10
|
+
* - Preserves relative URLs in Node.js, unless specified otherwise.
|
|
11
|
+
*/
|
|
12
|
+
export function normalizePath(path: Path, baseUrl?: string): Path {
|
|
13
|
+
// RegExp paths do not need normalization.
|
|
14
|
+
if (path instanceof RegExp) {
|
|
15
|
+
return path
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const maybeAbsoluteUrl = getAbsoluteUrl(path, baseUrl)
|
|
19
|
+
|
|
20
|
+
return cleanUrl(maybeAbsoluteUrl)
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
|
|
5
|
+
|
|
6
|
+
test('returns an absolute request URL withouth search params', () => {
|
|
7
|
+
expect(
|
|
8
|
+
getPublicUrlFromRequest(new Request(new URL('https://test.mswjs.io/path'))),
|
|
9
|
+
).toBe('https://test.mswjs.io/path')
|
|
10
|
+
|
|
11
|
+
expect(
|
|
12
|
+
getPublicUrlFromRequest(new Request(new URL('http://localhost/path'))),
|
|
13
|
+
).toBe('/path')
|
|
14
|
+
|
|
15
|
+
expect(
|
|
16
|
+
getPublicUrlFromRequest(
|
|
17
|
+
new Request(new URL('http://localhost/path?foo=bar')),
|
|
18
|
+
),
|
|
19
|
+
).toBe('/path')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns a relative URL given the request to the same origin', () => {
|
|
23
|
+
expect(getPublicUrlFromRequest(new Request('http://localhost/user'))).toBe(
|
|
24
|
+
'/user',
|
|
25
|
+
)
|
|
26
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a relative URL if the given request URL is relative to the current origin.
|
|
3
|
+
* Otherwise returns an absolute URL.
|
|
4
|
+
*/
|
|
5
|
+
export function getPublicUrlFromRequest(request: Request): string {
|
|
6
|
+
if (typeof location === 'undefined') {
|
|
7
|
+
return request.url
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const url = new URL(request.url)
|
|
11
|
+
|
|
12
|
+
return url.origin === location.origin
|
|
13
|
+
? url.pathname
|
|
14
|
+
: url.origin + url.pathname
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { getRequestCookies } from './getRequestCookies'
|
|
5
|
+
|
|
6
|
+
const prevLocation = global.location
|
|
7
|
+
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
// Node.js applications may polyfill some browser globals (document, location)
|
|
10
|
+
// when performing Server-Side Rendering of front-end applications.
|
|
11
|
+
global.location = {
|
|
12
|
+
href: 'https://mswjs.io',
|
|
13
|
+
origin: 'https://mswjs.io',
|
|
14
|
+
} as Location
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
afterAll(() => {
|
|
18
|
+
global.location = prevLocation
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('returns empty object when in a node environment with polyfilled location object', () => {
|
|
22
|
+
const cookies = getRequestCookies(
|
|
23
|
+
new Request(new URL('/user', location.href), {
|
|
24
|
+
credentials: 'include',
|
|
25
|
+
}),
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
expect(cookies).toEqual({})
|
|
29
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { getRequestCookies } from './getRequestCookies'
|
|
5
|
+
import { clearCookies } from '../../../../test/support/utils'
|
|
6
|
+
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
// Emulate some `document.cookie` value.
|
|
9
|
+
document.cookie = 'auth-token=abc-123;'
|
|
10
|
+
document.cookie = 'custom-cookie=yes;'
|
|
11
|
+
document.cookie = `encoded-cookie=${encodeURIComponent('测试')};`
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
afterAll(() => {
|
|
15
|
+
clearCookies()
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('returns all document cookies given "include" credentials', () => {
|
|
19
|
+
const cookies = getRequestCookies(
|
|
20
|
+
new Request(new URL('/user', location.origin), {
|
|
21
|
+
credentials: 'include',
|
|
22
|
+
}),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
expect(cookies).toEqual({
|
|
26
|
+
'auth-token': 'abc-123',
|
|
27
|
+
'custom-cookie': 'yes',
|
|
28
|
+
'encoded-cookie': '测试',
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('returns all document cookies given "same-origin" credentials and the same request origin', () => {
|
|
33
|
+
const cookies = getRequestCookies(
|
|
34
|
+
new Request(new URL('/user', location.origin), {
|
|
35
|
+
credentials: 'same-origin',
|
|
36
|
+
}),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
expect(cookies).toEqual({
|
|
40
|
+
'auth-token': 'abc-123',
|
|
41
|
+
'custom-cookie': 'yes',
|
|
42
|
+
'encoded-cookie': '测试',
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('returns an empty object given "same-origin" credentials and a different request origin', () => {
|
|
47
|
+
const cookies = getRequestCookies(
|
|
48
|
+
new Request(new URL('https://test.mswjs.io/user'), {
|
|
49
|
+
credentials: 'same-origin',
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
expect(cookies).toEqual({})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('returns an empty object given "omit" credentials', () => {
|
|
57
|
+
const cookies = getRequestCookies(
|
|
58
|
+
new Request(new URL('/user', location.origin), {
|
|
59
|
+
credentials: 'omit',
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
expect(cookies).toEqual({})
|
|
64
|
+
})
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import cookieUtils from '@bundled-es-modules/cookie'
|
|
2
|
+
import { store } from '@mswjs/cookies'
|
|
3
|
+
|
|
4
|
+
function getAllDocumentCookies() {
|
|
5
|
+
return cookieUtils.parse(document.cookie)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** @todo Rename this to "getDocumentCookies" */
|
|
9
|
+
/**
|
|
10
|
+
* Returns relevant document cookies based on the request `credentials` option.
|
|
11
|
+
*/
|
|
12
|
+
export function getRequestCookies(request: Request): Record<string, string> {
|
|
13
|
+
/**
|
|
14
|
+
* @note No cookies persist on the document in Node.js: no document.
|
|
15
|
+
*/
|
|
16
|
+
if (typeof document === 'undefined' || typeof location === 'undefined') {
|
|
17
|
+
return {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
switch (request.credentials) {
|
|
21
|
+
case 'same-origin': {
|
|
22
|
+
const url = new URL(request.url)
|
|
23
|
+
|
|
24
|
+
// Return document cookies only when requested a resource
|
|
25
|
+
// from the same origin as the current document.
|
|
26
|
+
return location.origin === url.origin ? getAllDocumentCookies() : {}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
case 'include': {
|
|
30
|
+
// Return all document cookies.
|
|
31
|
+
return getAllDocumentCookies()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
default: {
|
|
35
|
+
return {}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getAllRequestCookies(request: Request): Record<string, string> {
|
|
41
|
+
const requestCookiesString = request.headers.get('cookie')
|
|
42
|
+
const cookiesFromHeaders = requestCookiesString
|
|
43
|
+
? cookieUtils.parse(requestCookiesString)
|
|
44
|
+
: {}
|
|
45
|
+
|
|
46
|
+
store.hydrate()
|
|
47
|
+
|
|
48
|
+
const cookiesFromStore = Array.from(store.get(request)?.entries()).reduce<
|
|
49
|
+
Record<string, string>
|
|
50
|
+
>((cookies, [name, { value }]) => {
|
|
51
|
+
return Object.assign(cookies, { [name.trim()]: value })
|
|
52
|
+
}, {})
|
|
53
|
+
|
|
54
|
+
const cookiesFromDocument = getRequestCookies(request)
|
|
55
|
+
|
|
56
|
+
const forwardedCookies = {
|
|
57
|
+
...cookiesFromDocument,
|
|
58
|
+
...cookiesFromStore,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Set the inferred cookies from the cookie store and the document
|
|
62
|
+
// on the request's headers.
|
|
63
|
+
/**
|
|
64
|
+
* @todo Consider making this a separate step so this function
|
|
65
|
+
* is pure-er.
|
|
66
|
+
*/
|
|
67
|
+
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
68
|
+
request.headers.append('cookie', cookieUtils.serialize(name, value))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
...forwardedCookies,
|
|
73
|
+
...cookiesFromHeaders,
|
|
74
|
+
}
|
|
75
|
+
}
|