msw 2.1.2 → 2.1.4
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 +5 -5
- package/lib/browser/index.js +1757 -27
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +1751 -23
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-2guuZL5R.d.ts → GraphQLHandler-eJejgV5u.d.ts} +1 -1
- package/lib/core/{GraphQLHandler-fe5IYUg3.d.mts → GraphQLHandler-rZ7XkeDz.d.mts} +1 -1
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/{RequestHandler-TRh8Eh4H.d.ts → RequestHandler-CwjkprZE.d.ts} +8 -6
- package/lib/core/{RequestHandler-nfjVJ8Tp.d.mts → RequestHandler-MAVTMsma.d.mts} +8 -6
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/getResponse.d.mts +13 -0
- package/lib/core/getResponse.d.ts +13 -0
- package/lib/core/getResponse.js +34 -0
- package/lib/core/getResponse.js.map +1 -0
- package/lib/core/getResponse.mjs +14 -0
- package/lib/core/getResponse.mjs.map +1 -0
- package/lib/core/graphql.d.mts +2 -2
- package/lib/core/graphql.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs.map +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js.map +1 -1
- package/lib/core/handlers/RequestHandler.mjs.map +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +3 -2
- package/lib/core/index.d.ts +3 -2
- package/lib/core/index.js +2 -0
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +2 -0
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/sharedOptions.d.mts +0 -2
- package/lib/core/sharedOptions.d.ts +0 -2
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/executeHandlers.d.mts +2 -0
- package/lib/core/utils/executeHandlers.d.ts +2 -0
- package/lib/core/utils/{getResponse.js → executeHandlers.js} +6 -6
- package/lib/core/utils/executeHandlers.js.map +1 -0
- package/lib/core/utils/{getResponse.mjs → executeHandlers.mjs} +3 -3
- package/lib/core/utils/executeHandlers.mjs.map +1 -0
- package/lib/core/utils/handleRequest.d.mts +2 -2
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js +3 -3
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs +3 -3
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/randomId.d.mts +3 -0
- package/lib/core/utils/internal/randomId.d.ts +3 -0
- package/lib/core/utils/internal/{uuidv4.js → randomId.js} +6 -6
- package/lib/core/utils/internal/randomId.js.map +1 -0
- package/lib/core/utils/internal/randomId.mjs +7 -0
- package/lib/core/utils/internal/randomId.mjs.map +1 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/request/onUnhandledRequest.d.mts +1 -4
- package/lib/core/utils/request/onUnhandledRequest.d.ts +1 -4
- package/lib/core/utils/request/onUnhandledRequest.js +9 -113
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs +9 -105
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
- package/lib/iife/index.js +29 -216
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +1 -4
- package/src/core/getResponse.test.ts +63 -0
- package/src/core/getResponse.ts +23 -0
- package/src/core/handlers/GraphQLHandler.test.ts +4 -4
- package/src/core/handlers/HttpHandler.test.ts +5 -5
- package/src/core/handlers/HttpHandler.ts +1 -1
- package/src/core/handlers/RequestHandler.ts +1 -1
- package/src/core/index.ts +1 -0
- package/src/core/utils/{getResponse.ts → executeHandlers.ts} +7 -5
- package/src/core/utils/handleRequest.test.ts +14 -14
- package/src/core/utils/handleRequest.ts +5 -5
- package/src/core/utils/internal/{uuidv4.ts → randomId.ts} +1 -1
- package/src/core/utils/request/onUnhandledRequest.test.ts +5 -101
- package/src/core/utils/request/onUnhandledRequest.ts +3 -182
- package/lib/core/utils/getResponse.d.mts +0 -2
- package/lib/core/utils/getResponse.d.ts +0 -2
- package/lib/core/utils/getResponse.js.map +0 -1
- package/lib/core/utils/getResponse.mjs.map +0 -1
- package/lib/core/utils/internal/uuidv4.d.mts +0 -3
- package/lib/core/utils/internal/uuidv4.d.ts +0 -3
- package/lib/core/utils/internal/uuidv4.js.map +0 -1
- package/lib/core/utils/internal/uuidv4.mjs +0 -7
- package/lib/core/utils/internal/uuidv4.mjs.map +0 -1
package/lib/mockServiceWorker.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"main": "./lib/core/index.js",
|
|
6
6
|
"module": "./lib/core/index.mjs",
|
|
@@ -93,13 +93,11 @@
|
|
|
93
93
|
"sideEffects": false,
|
|
94
94
|
"dependencies": {
|
|
95
95
|
"@bundled-es-modules/cookie": "^2.0.0",
|
|
96
|
-
"@bundled-es-modules/js-levenshtein": "^2.0.1",
|
|
97
96
|
"@bundled-es-modules/statuses": "^1.0.1",
|
|
98
97
|
"@mswjs/cookies": "^1.1.0",
|
|
99
98
|
"@mswjs/interceptors": "^0.25.14",
|
|
100
99
|
"@open-draft/until": "^2.1.0",
|
|
101
100
|
"@types/cookie": "^0.6.0",
|
|
102
|
-
"@types/js-levenshtein": "^1.1.3",
|
|
103
101
|
"@types/statuses": "^2.0.4",
|
|
104
102
|
"chalk": "^4.1.2",
|
|
105
103
|
"chokidar": "^3.4.2",
|
|
@@ -107,7 +105,6 @@
|
|
|
107
105
|
"headers-polyfill": "^4.0.2",
|
|
108
106
|
"inquirer": "^8.2.0",
|
|
109
107
|
"is-node-process": "^1.2.0",
|
|
110
|
-
"js-levenshtein": "^1.1.6",
|
|
111
108
|
"outvariant": "^1.4.2",
|
|
112
109
|
"path-to-regexp": "^6.2.0",
|
|
113
110
|
"strict-event-emitter": "^0.5.1",
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { http } from './http'
|
|
5
|
+
import { getResponse } from './getResponse'
|
|
6
|
+
|
|
7
|
+
it('returns undefined given empty headers array', async () => {
|
|
8
|
+
expect(
|
|
9
|
+
await getResponse([], new Request('http://localhost/')),
|
|
10
|
+
).toBeUndefined()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('returns undefined given no matching handlers', async () => {
|
|
14
|
+
expect(
|
|
15
|
+
await getResponse(
|
|
16
|
+
[http.get('/product', () => void 0)],
|
|
17
|
+
new Request('http://localhost/user'),
|
|
18
|
+
),
|
|
19
|
+
).toBeUndefined()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('returns undefined given a matching handler that returned no response', async () => {
|
|
23
|
+
expect(
|
|
24
|
+
await getResponse(
|
|
25
|
+
[http.get('*/user', () => void 0)],
|
|
26
|
+
new Request('http://localhost/user'),
|
|
27
|
+
),
|
|
28
|
+
).toBeUndefined()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('returns undefined given a matching handler that returned explicit undefined', async () => {
|
|
32
|
+
expect(
|
|
33
|
+
await getResponse(
|
|
34
|
+
[http.get('*/user', () => undefined)],
|
|
35
|
+
new Request('http://localhost/user'),
|
|
36
|
+
),
|
|
37
|
+
).toBeUndefined()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('returns the response returned from a matching handler', async () => {
|
|
41
|
+
const response = await getResponse(
|
|
42
|
+
[http.get('*/user', () => Response.json({ name: 'John' }))],
|
|
43
|
+
new Request('http://localhost/user'),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
expect(response?.status).toBe(200)
|
|
47
|
+
expect(response?.headers.get('Content-Type')).toBe('application/json')
|
|
48
|
+
expect(await response?.json()).toEqual({ name: 'John' })
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('returns the response from the first matching handler if multiple match', async () => {
|
|
52
|
+
const response = await getResponse(
|
|
53
|
+
[
|
|
54
|
+
http.get('*/user', () => Response.json({ name: 'John' })),
|
|
55
|
+
http.get('*/user', () => Response.json({ name: 'Kate' })),
|
|
56
|
+
],
|
|
57
|
+
new Request('http://localhost/user'),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
expect(response?.status).toBe(200)
|
|
61
|
+
expect(response?.headers.get('Content-Type')).toBe('application/json')
|
|
62
|
+
expect(await response?.json()).toEqual({ name: 'John' })
|
|
63
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RequestHandler } from './handlers/RequestHandler'
|
|
2
|
+
import { executeHandlers } from './utils/executeHandlers'
|
|
3
|
+
import { randomId } from './utils/internal/randomId'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Finds a response for the given request instance
|
|
7
|
+
* in the array of request handlers.
|
|
8
|
+
* @param handlers The array of request handlers.
|
|
9
|
+
* @param request The `Request` instance.
|
|
10
|
+
* @returns {Response} A mocked response, if any.
|
|
11
|
+
*/
|
|
12
|
+
export const getResponse = async (
|
|
13
|
+
handlers: Array<RequestHandler>,
|
|
14
|
+
request: Request,
|
|
15
|
+
): Promise<Response | undefined> => {
|
|
16
|
+
const result = await executeHandlers({
|
|
17
|
+
request,
|
|
18
|
+
requestId: randomId(),
|
|
19
|
+
handlers,
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return result?.response
|
|
23
|
+
}
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
GraphQLResolverExtras,
|
|
10
10
|
isDocumentNode,
|
|
11
11
|
} from './GraphQLHandler'
|
|
12
|
-
import {
|
|
12
|
+
import { randomId } from '../utils/internal/randomId'
|
|
13
13
|
import { HttpResponse } from '../HttpResponse'
|
|
14
14
|
import { ResponseResolver } from './RequestHandler'
|
|
15
15
|
|
|
@@ -737,7 +737,7 @@ describe('run', () => {
|
|
|
737
737
|
userId: 'abc-123',
|
|
738
738
|
},
|
|
739
739
|
})
|
|
740
|
-
const requestId =
|
|
740
|
+
const requestId = randomId()
|
|
741
741
|
const result = await handler.run({ request, requestId })
|
|
742
742
|
|
|
743
743
|
expect(result!.handler).toEqual(handler)
|
|
@@ -779,7 +779,7 @@ describe('run', () => {
|
|
|
779
779
|
const request = createPostGraphQLRequest({
|
|
780
780
|
query: LOGIN,
|
|
781
781
|
})
|
|
782
|
-
const requestId =
|
|
782
|
+
const requestId = randomId()
|
|
783
783
|
const result = await handler.run({ request, requestId })
|
|
784
784
|
|
|
785
785
|
expect(result).toBeNull()
|
|
@@ -827,7 +827,7 @@ describe('request', () => {
|
|
|
827
827
|
`,
|
|
828
828
|
})
|
|
829
829
|
|
|
830
|
-
const requestId =
|
|
830
|
+
const requestId = randomId()
|
|
831
831
|
await handler.run({ request, requestId })
|
|
832
832
|
|
|
833
833
|
expect(matchAllResolver).toHaveBeenCalledTimes(1)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @vitest-environment jsdom
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { randomId } from '../utils/internal/randomId'
|
|
5
5
|
import { HttpHandler, HttpRequestResolverExtras } from './HttpHandler'
|
|
6
6
|
import { HttpResponse } from '..'
|
|
7
7
|
import { ResponseResolver } from './RequestHandler'
|
|
@@ -152,7 +152,7 @@ describe('run', () => {
|
|
|
152
152
|
test('returns a mocked response given a matching request', async () => {
|
|
153
153
|
const handler = new HttpHandler('GET', '/user/:userId', resolver)
|
|
154
154
|
const request = new Request(new URL('/user/abc-123', location.href))
|
|
155
|
-
const requestId =
|
|
155
|
+
const requestId = randomId()
|
|
156
156
|
const result = await handler.run({ request, requestId })
|
|
157
157
|
|
|
158
158
|
expect(result!.handler).toEqual(handler)
|
|
@@ -176,7 +176,7 @@ describe('run', () => {
|
|
|
176
176
|
const handler = new HttpHandler('POST', '/login', resolver)
|
|
177
177
|
const result = await handler.run({
|
|
178
178
|
request: new Request(new URL('/users', location.href)),
|
|
179
|
-
requestId:
|
|
179
|
+
requestId: randomId(),
|
|
180
180
|
})
|
|
181
181
|
|
|
182
182
|
expect(result).toBeNull()
|
|
@@ -186,7 +186,7 @@ describe('run', () => {
|
|
|
186
186
|
const handler = new HttpHandler('GET', '/users', resolver)
|
|
187
187
|
const result = await handler.run({
|
|
188
188
|
request: new Request(new URL('/users', location.href)),
|
|
189
|
-
requestId:
|
|
189
|
+
requestId: randomId(),
|
|
190
190
|
})
|
|
191
191
|
|
|
192
192
|
expect(result?.parsedResult?.match?.params).toEqual({})
|
|
@@ -207,7 +207,7 @@ describe('run', () => {
|
|
|
207
207
|
const run = async () => {
|
|
208
208
|
const result = await handler.run({
|
|
209
209
|
request: new Request(new URL('/users', location.href)),
|
|
210
|
-
requestId:
|
|
210
|
+
requestId: randomId(),
|
|
211
211
|
})
|
|
212
212
|
return result?.response?.text()
|
|
213
213
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ResponseResolutionContext } from '../utils/
|
|
1
|
+
import { ResponseResolutionContext } from '../utils/executeHandlers'
|
|
2
2
|
import { devUtils } from '../utils/internal/devUtils'
|
|
3
3
|
import { isStringEqual } from '../utils/internal/isStringEqual'
|
|
4
4
|
import { getStatusCodeColor } from '../utils/logging/getStatusCodeColor'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { invariant } from 'outvariant'
|
|
2
2
|
import { getCallFrame } from '../utils/internal/getCallFrame'
|
|
3
3
|
import { isIterable } from '../utils/internal/isIterable'
|
|
4
|
-
import type { ResponseResolutionContext } from '../utils/
|
|
4
|
+
import type { ResponseResolutionContext } from '../utils/executeHandlers'
|
|
5
5
|
import type { MaybePromise } from '../typeUtils'
|
|
6
6
|
import { StrictRequest, StrictResponse } from '..//HttpResponse'
|
|
7
7
|
|
package/src/core/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ export { GraphQLHandler } from './handlers/GraphQLHandler'
|
|
|
12
12
|
/* Utils */
|
|
13
13
|
export { matchRequestUrl } from './utils/matching/matchRequestUrl'
|
|
14
14
|
export * from './utils/handleRequest'
|
|
15
|
+
export { getResponse } from './getResponse'
|
|
15
16
|
export { cleanUrl } from './utils/url/cleanUrl'
|
|
16
17
|
|
|
17
18
|
/**
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
RequestHandlerExecutionResult,
|
|
4
4
|
} from '../handlers/RequestHandler'
|
|
5
5
|
|
|
6
|
-
export interface
|
|
6
|
+
export interface HandlersExecutionResult {
|
|
7
7
|
handler: RequestHandler
|
|
8
8
|
parsedResult?: any
|
|
9
9
|
response?: Response
|
|
@@ -14,9 +14,11 @@ export interface ResponseResolutionContext {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* Executes the list of request handlers against the given request.
|
|
18
|
+
* Returns the execution result object containing any matching request
|
|
19
|
+
* handler and any mocked response it returned.
|
|
18
20
|
*/
|
|
19
|
-
export const
|
|
21
|
+
export const executeHandlers = async <Handlers extends Array<RequestHandler>>({
|
|
20
22
|
request,
|
|
21
23
|
requestId,
|
|
22
24
|
handlers,
|
|
@@ -24,9 +26,9 @@ export const getResponse = async <Handler extends Array<RequestHandler>>({
|
|
|
24
26
|
}: {
|
|
25
27
|
request: Request
|
|
26
28
|
requestId: string
|
|
27
|
-
handlers:
|
|
29
|
+
handlers: Handlers
|
|
28
30
|
resolutionContext?: ResponseResolutionContext
|
|
29
|
-
}): Promise<
|
|
31
|
+
}): Promise<HandlersExecutionResult | null> => {
|
|
30
32
|
let matchingHandler: RequestHandler | null = null
|
|
31
33
|
let result: RequestHandlerExecutionResult<any> | null = null
|
|
32
34
|
|
|
@@ -7,7 +7,7 @@ import { RequestHandler } from '../handlers/RequestHandler'
|
|
|
7
7
|
import { http } from '../http'
|
|
8
8
|
import { handleRequest, HandleRequestOptions } from './handleRequest'
|
|
9
9
|
import { RequiredDeep } from '../typeUtils'
|
|
10
|
-
import {
|
|
10
|
+
import { randomId } from './internal/randomId'
|
|
11
11
|
import { HttpResponse } from '../HttpResponse'
|
|
12
12
|
import { passthrough } from '../passthrough'
|
|
13
13
|
|
|
@@ -51,7 +51,7 @@ afterEach(() => {
|
|
|
51
51
|
test('returns undefined for a request with the "x-msw-intention" header equal to "bypass"', async () => {
|
|
52
52
|
const { emitter, events } = setup()
|
|
53
53
|
|
|
54
|
-
const requestId =
|
|
54
|
+
const requestId = randomId()
|
|
55
55
|
const request = new Request(new URL('http://localhost/user'), {
|
|
56
56
|
headers: new Headers({
|
|
57
57
|
'x-msw-intention': 'bypass',
|
|
@@ -97,7 +97,7 @@ test('does not bypass a request with "x-msw-intention" header set to arbitrary v
|
|
|
97
97
|
|
|
98
98
|
const result = await handleRequest(
|
|
99
99
|
request,
|
|
100
|
-
|
|
100
|
+
randomId(),
|
|
101
101
|
handlers,
|
|
102
102
|
options,
|
|
103
103
|
emitter,
|
|
@@ -112,7 +112,7 @@ test('does not bypass a request with "x-msw-intention" header set to arbitrary v
|
|
|
112
112
|
test('reports request as unhandled when it has no matching request handlers', async () => {
|
|
113
113
|
const { emitter, events } = setup()
|
|
114
114
|
|
|
115
|
-
const requestId =
|
|
115
|
+
const requestId = randomId()
|
|
116
116
|
const request = new Request(new URL('http://localhost/user'))
|
|
117
117
|
const handlers: Array<RequestHandler> = []
|
|
118
118
|
|
|
@@ -145,7 +145,7 @@ test('reports request as unhandled when it has no matching request handlers', as
|
|
|
145
145
|
test('returns undefined on a request handler that returns no response', async () => {
|
|
146
146
|
const { emitter, events } = setup()
|
|
147
147
|
|
|
148
|
-
const requestId =
|
|
148
|
+
const requestId = randomId()
|
|
149
149
|
const request = new Request(new URL('http://localhost/user'))
|
|
150
150
|
const handlers: Array<RequestHandler> = [
|
|
151
151
|
http.get('/user', () => {
|
|
@@ -184,7 +184,7 @@ test('returns undefined on a request handler that returns no response', async ()
|
|
|
184
184
|
test('returns the mocked response for a request with a matching request handler', async () => {
|
|
185
185
|
const { emitter, events } = setup()
|
|
186
186
|
|
|
187
|
-
const requestId =
|
|
187
|
+
const requestId = randomId()
|
|
188
188
|
const request = new Request(new URL('http://localhost/user'))
|
|
189
189
|
const mockedResponse = HttpResponse.json({ firstName: 'John' })
|
|
190
190
|
const handlers: Array<RequestHandler> = [
|
|
@@ -242,7 +242,7 @@ test('returns the mocked response for a request with a matching request handler'
|
|
|
242
242
|
test('returns a transformed response if the "transformResponse" option is provided', async () => {
|
|
243
243
|
const { emitter, events } = setup()
|
|
244
244
|
|
|
245
|
-
const requestId =
|
|
245
|
+
const requestId = randomId()
|
|
246
246
|
const request = new Request(new URL('http://localhost/user'))
|
|
247
247
|
const mockedResponse = HttpResponse.json({ firstName: 'John' })
|
|
248
248
|
const handlers: Array<RequestHandler> = [
|
|
@@ -325,7 +325,7 @@ test('returns a transformed response if the "transformResponse" option is provid
|
|
|
325
325
|
it('returns undefined without warning on a passthrough request', async () => {
|
|
326
326
|
const { emitter, events } = setup()
|
|
327
327
|
|
|
328
|
-
const requestId =
|
|
328
|
+
const requestId = randomId()
|
|
329
329
|
const request = new Request(new URL('http://localhost/user'))
|
|
330
330
|
const handlers: Array<RequestHandler> = [
|
|
331
331
|
http.get('/user', () => {
|
|
@@ -358,7 +358,7 @@ it('returns undefined without warning on a passthrough request', async () => {
|
|
|
358
358
|
it('calls the handler with the requestId', async () => {
|
|
359
359
|
const { emitter } = setup()
|
|
360
360
|
|
|
361
|
-
const requestId =
|
|
361
|
+
const requestId = randomId()
|
|
362
362
|
const request = new Request(new URL('http://localhost/user'))
|
|
363
363
|
const handlerFn = vi.fn()
|
|
364
364
|
const handlers: Array<RequestHandler> = [http.get('/user', handlerFn)]
|
|
@@ -390,7 +390,7 @@ it('marks the first matching one-time handler as used', async () => {
|
|
|
390
390
|
})
|
|
391
391
|
const handlers: Array<RequestHandler> = [oneTimeHandler, anotherHandler]
|
|
392
392
|
|
|
393
|
-
const requestId =
|
|
393
|
+
const requestId = randomId()
|
|
394
394
|
const request = new Request('http://localhost/resource')
|
|
395
395
|
const firstResult = await handleRequest(
|
|
396
396
|
request,
|
|
@@ -438,7 +438,7 @@ it('does not mark non-matching one-time handlers as used', async () => {
|
|
|
438
438
|
)
|
|
439
439
|
const handlers: Array<RequestHandler> = [oneTimeHandler, anotherHandler]
|
|
440
440
|
|
|
441
|
-
const requestId =
|
|
441
|
+
const requestId = randomId()
|
|
442
442
|
const firstResult = await handleRequest(
|
|
443
443
|
new Request('http://localhost/another'),
|
|
444
444
|
requestId,
|
|
@@ -481,7 +481,7 @@ it('handles parallel requests with one-time handlers', async () => {
|
|
|
481
481
|
})
|
|
482
482
|
const handlers: Array<RequestHandler> = [oneTimeHandler, anotherHandler]
|
|
483
483
|
|
|
484
|
-
const requestId =
|
|
484
|
+
const requestId = randomId()
|
|
485
485
|
const request = new Request('http://localhost/resource')
|
|
486
486
|
const firstResultPromise = handleRequest(
|
|
487
487
|
request,
|
|
@@ -526,7 +526,7 @@ describe('[Private] - resolutionContext - used for extensions', () => {
|
|
|
526
526
|
|
|
527
527
|
const handlers: Array<RequestHandler> = [handler]
|
|
528
528
|
|
|
529
|
-
const requestId =
|
|
529
|
+
const requestId = randomId()
|
|
530
530
|
const request = new Request(new URL('/resource', baseUrl))
|
|
531
531
|
const response = await handleRequest(
|
|
532
532
|
request,
|
|
@@ -555,7 +555,7 @@ describe('[Private] - resolutionContext - used for extensions', () => {
|
|
|
555
555
|
|
|
556
556
|
const handlers: Array<RequestHandler> = [handler]
|
|
557
557
|
|
|
558
|
-
const requestId =
|
|
558
|
+
const requestId = randomId()
|
|
559
559
|
const request = new Request(
|
|
560
560
|
new URL('/resource', `http://not-the-base-url.com`),
|
|
561
561
|
)
|
|
@@ -3,7 +3,7 @@ import { Emitter } from 'strict-event-emitter'
|
|
|
3
3
|
import { RequestHandler } from '../handlers/RequestHandler'
|
|
4
4
|
import { LifeCycleEventsMap, SharedOptions } from '../sharedOptions'
|
|
5
5
|
import { RequiredDeep } from '../typeUtils'
|
|
6
|
-
import {
|
|
6
|
+
import { HandlersExecutionResult, executeHandlers } from './executeHandlers'
|
|
7
7
|
import { onUnhandledRequest } from './request/onUnhandledRequest'
|
|
8
8
|
import { readResponseCookies } from './request/readResponseCookies'
|
|
9
9
|
|
|
@@ -38,7 +38,7 @@ export interface HandleRequestOptions {
|
|
|
38
38
|
*/
|
|
39
39
|
onMockedResponse?(
|
|
40
40
|
response: Response,
|
|
41
|
-
handler: RequiredDeep<
|
|
41
|
+
handler: RequiredDeep<HandlersExecutionResult>,
|
|
42
42
|
): void
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -61,7 +61,7 @@ export async function handleRequest(
|
|
|
61
61
|
|
|
62
62
|
// Resolve a mocked response from the list of request handlers.
|
|
63
63
|
const lookupResult = await until(() => {
|
|
64
|
-
return
|
|
64
|
+
return executeHandlers({
|
|
65
65
|
request,
|
|
66
66
|
requestId,
|
|
67
67
|
handlers,
|
|
@@ -82,7 +82,7 @@ export async function handleRequest(
|
|
|
82
82
|
// If the handler lookup returned nothing, no request handler was found
|
|
83
83
|
// matching this request. Report the request as unhandled.
|
|
84
84
|
if (!lookupResult.data) {
|
|
85
|
-
await onUnhandledRequest(request,
|
|
85
|
+
await onUnhandledRequest(request, options.onUnhandledRequest)
|
|
86
86
|
emitter.emit('request:unhandled', { request, requestId })
|
|
87
87
|
emitter.emit('request:end', { request, requestId })
|
|
88
88
|
handleRequestOptions?.onPassthroughResponse?.(request)
|
|
@@ -116,7 +116,7 @@ export async function handleRequest(
|
|
|
116
116
|
emitter.emit('request:match', { request, requestId })
|
|
117
117
|
|
|
118
118
|
const requiredLookupResult =
|
|
119
|
-
lookupResult.data as RequiredDeep<
|
|
119
|
+
lookupResult.data as RequiredDeep<HandlersExecutionResult>
|
|
120
120
|
|
|
121
121
|
const transformedResponse =
|
|
122
122
|
handleRequestOptions?.transformResponse?.(response) ||
|
|
@@ -5,10 +5,6 @@ import {
|
|
|
5
5
|
onUnhandledRequest,
|
|
6
6
|
UnhandledRequestCallback,
|
|
7
7
|
} from './onUnhandledRequest'
|
|
8
|
-
import { HttpHandler, HttpMethods } from '../../handlers/HttpHandler'
|
|
9
|
-
import { ResponseResolver } from '../../handlers/RequestHandler'
|
|
10
|
-
|
|
11
|
-
const resolver: ResponseResolver = () => void 0
|
|
12
8
|
|
|
13
9
|
const fixtures = {
|
|
14
10
|
warningWithoutSuggestions: `\
|
|
@@ -24,18 +20,6 @@ Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
|
24
20
|
|
|
25
21
|
• GET /api
|
|
26
22
|
|
|
27
|
-
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
28
|
-
Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
29
|
-
|
|
30
|
-
warningWithSuggestions: (suggestions: string) => `\
|
|
31
|
-
[MSW] Warning: intercepted a request without a matching request handler:
|
|
32
|
-
|
|
33
|
-
• GET /api
|
|
34
|
-
|
|
35
|
-
Did you mean to request one of the following resources instead?
|
|
36
|
-
|
|
37
|
-
${suggestions}
|
|
38
|
-
|
|
39
23
|
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
40
24
|
Read more: https://mswjs.io/docs/getting-started/mocks`,
|
|
41
25
|
}
|
|
@@ -52,7 +36,6 @@ afterEach(() => {
|
|
|
52
36
|
test('supports the "bypass" request strategy', async () => {
|
|
53
37
|
await onUnhandledRequest(
|
|
54
38
|
new Request(new URL('http://localhost/api')),
|
|
55
|
-
[],
|
|
56
39
|
'bypass',
|
|
57
40
|
)
|
|
58
41
|
|
|
@@ -61,22 +44,14 @@ test('supports the "bypass" request strategy', async () => {
|
|
|
61
44
|
})
|
|
62
45
|
|
|
63
46
|
test('supports the "warn" request strategy', async () => {
|
|
64
|
-
await onUnhandledRequest(
|
|
65
|
-
new Request(new URL('http://localhost/api')),
|
|
66
|
-
[],
|
|
67
|
-
'warn',
|
|
68
|
-
)
|
|
47
|
+
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')
|
|
69
48
|
|
|
70
49
|
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
71
50
|
})
|
|
72
51
|
|
|
73
52
|
test('supports the "error" request strategy', async () => {
|
|
74
53
|
await expect(
|
|
75
|
-
onUnhandledRequest(
|
|
76
|
-
new Request(new URL('http://localhost/api')),
|
|
77
|
-
[],
|
|
78
|
-
'error',
|
|
79
|
-
),
|
|
54
|
+
onUnhandledRequest(new Request(new URL('http://localhost/api')), 'error'),
|
|
80
55
|
).rejects.toThrow(
|
|
81
56
|
'[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
|
|
82
57
|
)
|
|
@@ -89,7 +64,7 @@ test('supports a custom callback function', async () => {
|
|
|
89
64
|
console.warn(`callback: ${request.method} ${request.url}`)
|
|
90
65
|
})
|
|
91
66
|
const request = new Request(new URL('/user', 'http://localhost:3000'))
|
|
92
|
-
await onUnhandledRequest(request,
|
|
67
|
+
await onUnhandledRequest(request, callback)
|
|
93
68
|
|
|
94
69
|
expect(callback).toHaveBeenCalledTimes(1)
|
|
95
70
|
expect(callback).toHaveBeenCalledWith(request, {
|
|
@@ -111,7 +86,7 @@ test('supports calling default strategies from the custom callback function', as
|
|
|
111
86
|
},
|
|
112
87
|
)
|
|
113
88
|
const request = new Request(new URL('http://localhost/api'))
|
|
114
|
-
await expect(onUnhandledRequest(request,
|
|
89
|
+
await expect(onUnhandledRequest(request, callback)).rejects.toThrow(
|
|
115
90
|
`[MSW] Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.`,
|
|
116
91
|
)
|
|
117
92
|
|
|
@@ -126,86 +101,15 @@ test('supports calling default strategies from the custom callback function', as
|
|
|
126
101
|
})
|
|
127
102
|
|
|
128
103
|
test('does not print any suggestions given no handlers to suggest', async () => {
|
|
129
|
-
await onUnhandledRequest(
|
|
130
|
-
new Request(new URL('http://localhost/api')),
|
|
131
|
-
[],
|
|
132
|
-
'warn',
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
test('does not print any suggestions given no handlers are similar', async () => {
|
|
139
|
-
await onUnhandledRequest(
|
|
140
|
-
new Request(new URL('http://localhost/api')),
|
|
141
|
-
[
|
|
142
|
-
// None of the defined request handlers match the actual request URL
|
|
143
|
-
// to be used as suggestions.
|
|
144
|
-
new HttpHandler(HttpMethods.GET, 'https://api.github.com', resolver),
|
|
145
|
-
new HttpHandler(HttpMethods.GET, 'https://api.stripe.com', resolver),
|
|
146
|
-
],
|
|
147
|
-
'warn',
|
|
148
|
-
)
|
|
104
|
+
await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn')
|
|
149
105
|
|
|
150
106
|
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
151
107
|
})
|
|
152
108
|
|
|
153
|
-
test('respects RegExp as a request handler method', async () => {
|
|
154
|
-
await onUnhandledRequest(
|
|
155
|
-
new Request(new URL('http://localhost/api')),
|
|
156
|
-
[new HttpHandler(/^GE/, 'http://localhost/api', resolver)],
|
|
157
|
-
'warn',
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions)
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
test('sorts the suggestions by relevance', async () => {
|
|
164
|
-
await onUnhandledRequest(
|
|
165
|
-
new Request(new URL('http://localhost/api')),
|
|
166
|
-
[
|
|
167
|
-
new HttpHandler(HttpMethods.GET, '/', resolver),
|
|
168
|
-
new HttpHandler(HttpMethods.GET, 'https://api.example.com/api', resolver),
|
|
169
|
-
new HttpHandler(HttpMethods.POST, '/api', resolver),
|
|
170
|
-
],
|
|
171
|
-
'warn',
|
|
172
|
-
)
|
|
173
|
-
|
|
174
|
-
expect(console.warn).toHaveBeenCalledWith(
|
|
175
|
-
fixtures.warningWithSuggestions(`\
|
|
176
|
-
• POST /api
|
|
177
|
-
• GET /`),
|
|
178
|
-
)
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
test('does not print more than 4 suggestions', async () => {
|
|
182
|
-
await onUnhandledRequest(
|
|
183
|
-
new Request(new URL('http://localhost/api')),
|
|
184
|
-
[
|
|
185
|
-
new HttpHandler(HttpMethods.GET, '/ap', resolver),
|
|
186
|
-
new HttpHandler(HttpMethods.GET, '/api', resolver),
|
|
187
|
-
new HttpHandler(HttpMethods.GET, '/api-1', resolver),
|
|
188
|
-
new HttpHandler(HttpMethods.GET, '/api-2', resolver),
|
|
189
|
-
new HttpHandler(HttpMethods.GET, '/api-3', resolver),
|
|
190
|
-
new HttpHandler(HttpMethods.GET, '/api-4', resolver),
|
|
191
|
-
],
|
|
192
|
-
'warn',
|
|
193
|
-
)
|
|
194
|
-
|
|
195
|
-
expect(console.warn).toHaveBeenCalledWith(
|
|
196
|
-
fixtures.warningWithSuggestions(`\
|
|
197
|
-
• GET /api
|
|
198
|
-
• GET /ap
|
|
199
|
-
• GET /api-1
|
|
200
|
-
• GET /api-2`),
|
|
201
|
-
)
|
|
202
|
-
})
|
|
203
|
-
|
|
204
109
|
test('throws an exception given unknown request strategy', async () => {
|
|
205
110
|
await expect(
|
|
206
111
|
onUnhandledRequest(
|
|
207
112
|
new Request(new URL('http://localhost/api')),
|
|
208
|
-
[],
|
|
209
113
|
// @ts-expect-error Intentional unknown strategy.
|
|
210
114
|
'invalid-strategy',
|
|
211
115
|
),
|