msw 2.10.2 → 2.10.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/lib/core/{HttpResponse-I457nh8V.d.mts → HttpResponse-C7FhBLaS.d.mts} +1 -1
- package/lib/core/{HttpResponse-CCdkF1fJ.d.ts → HttpResponse-DWu36LsY.d.ts} +1 -1
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/getResponse.d.mts +3 -2
- package/lib/core/getResponse.d.ts +3 -2
- package/lib/core/getResponse.js +3 -2
- package/lib/core/getResponse.js.map +1 -1
- package/lib/core/getResponse.mjs +6 -3
- package/lib/core/getResponse.mjs.map +1 -1
- package/lib/core/graphql.d.mts +1 -1
- package/lib/core/graphql.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/HttpHandler.js +1 -1
- package/lib/core/handlers/HttpHandler.js.map +1 -1
- package/lib/core/handlers/HttpHandler.mjs +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/WebSocketHandler.d.mts +6 -2
- package/lib/core/handlers/WebSocketHandler.d.ts +6 -2
- package/lib/core/handlers/WebSocketHandler.js +8 -3
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +8 -3
- package/lib/core/handlers/WebSocketHandler.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 +1 -1
- package/lib/core/index.d.ts +1 -1
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- 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 +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/handleRequest.d.mts +1 -1
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
- 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/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
- package/lib/iife/index.js +12 -6
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +2 -2
- package/src/core/getResponse.test.ts +28 -17
- package/src/core/getResponse.ts +7 -1
- package/src/core/handlers/HttpHandler.ts +1 -1
- package/src/core/handlers/WebSocketHandler.test.ts +16 -0
- package/src/core/handlers/WebSocketHandler.ts +15 -2
package/lib/mockServiceWorker.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* - Please do NOT modify this file.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
const PACKAGE_VERSION = '2.10.
|
|
10
|
+
const PACKAGE_VERSION = '2.10.4'
|
|
11
11
|
const INTEGRITY_CHECKSUM = 'f5825c521429caf22a4dd13b66e243af'
|
|
12
12
|
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
|
13
13
|
const activeClientIds = new Set()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "2.10.
|
|
3
|
+
"version": "2.10.4",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./lib/core/index.js",
|
|
@@ -280,7 +280,7 @@
|
|
|
280
280
|
"vitest-environment-miniflare": "^2.14.4",
|
|
281
281
|
"webpack": "^5.95.0",
|
|
282
282
|
"webpack-http-server": "^0.5.0",
|
|
283
|
-
"msw": "2.10.
|
|
283
|
+
"msw": "2.10.4"
|
|
284
284
|
},
|
|
285
285
|
"peerDependencies": {
|
|
286
286
|
"typescript": ">= 4.8.x"
|
|
@@ -1,40 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
* @vitest-environment node
|
|
3
|
-
*/
|
|
1
|
+
// @vitest-environment node
|
|
4
2
|
import { http } from './http'
|
|
5
3
|
import { getResponse } from './getResponse'
|
|
6
4
|
|
|
7
5
|
it('returns undefined given empty headers array', async () => {
|
|
8
|
-
expect(
|
|
9
|
-
|
|
10
|
-
).toBeUndefined()
|
|
6
|
+
await expect(
|
|
7
|
+
getResponse([], new Request('http://localhost/')),
|
|
8
|
+
).resolves.toBeUndefined()
|
|
11
9
|
})
|
|
12
10
|
|
|
13
11
|
it('returns undefined given no matching handlers', async () => {
|
|
14
|
-
expect(
|
|
15
|
-
|
|
12
|
+
await expect(
|
|
13
|
+
getResponse(
|
|
16
14
|
[http.get('/product', () => void 0)],
|
|
17
15
|
new Request('http://localhost/user'),
|
|
18
16
|
),
|
|
19
|
-
).toBeUndefined()
|
|
17
|
+
).resolves.toBeUndefined()
|
|
20
18
|
})
|
|
21
19
|
|
|
22
20
|
it('returns undefined given a matching handler that returned no response', async () => {
|
|
23
|
-
expect(
|
|
24
|
-
|
|
21
|
+
await expect(
|
|
22
|
+
getResponse(
|
|
25
23
|
[http.get('*/user', () => void 0)],
|
|
26
24
|
new Request('http://localhost/user'),
|
|
27
25
|
),
|
|
28
|
-
).toBeUndefined()
|
|
26
|
+
).resolves.toBeUndefined()
|
|
29
27
|
})
|
|
30
28
|
|
|
31
29
|
it('returns undefined given a matching handler that returned explicit undefined', async () => {
|
|
32
|
-
expect(
|
|
33
|
-
|
|
30
|
+
await expect(
|
|
31
|
+
getResponse(
|
|
34
32
|
[http.get('*/user', () => undefined)],
|
|
35
33
|
new Request('http://localhost/user'),
|
|
36
34
|
),
|
|
37
|
-
).toBeUndefined()
|
|
35
|
+
).resolves.toBeUndefined()
|
|
38
36
|
})
|
|
39
37
|
|
|
40
38
|
it('returns the response returned from a matching handler', async () => {
|
|
@@ -45,7 +43,7 @@ it('returns the response returned from a matching handler', async () => {
|
|
|
45
43
|
|
|
46
44
|
expect(response?.status).toBe(200)
|
|
47
45
|
expect(response?.headers.get('Content-Type')).toBe('application/json')
|
|
48
|
-
expect(
|
|
46
|
+
await expect(response?.json()).resolves.toEqual({ name: 'John' })
|
|
49
47
|
})
|
|
50
48
|
|
|
51
49
|
it('returns the response from the first matching handler if multiple match', async () => {
|
|
@@ -59,5 +57,18 @@ it('returns the response from the first matching handler if multiple match', asy
|
|
|
59
57
|
|
|
60
58
|
expect(response?.status).toBe(200)
|
|
61
59
|
expect(response?.headers.get('Content-Type')).toBe('application/json')
|
|
62
|
-
expect(
|
|
60
|
+
await expect(response?.json()).resolves.toEqual({ name: 'John' })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('supports custom base url', async () => {
|
|
64
|
+
const response = await getResponse(
|
|
65
|
+
[http.get('/resource', () => new Response('hello world'))],
|
|
66
|
+
new Request('https://localhost:3000/resource'),
|
|
67
|
+
{
|
|
68
|
+
baseUrl: 'https://localhost:3000/',
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
expect(response?.status).toBe(200)
|
|
73
|
+
await expect(response?.text()).resolves.toBe('hello world')
|
|
63
74
|
})
|
package/src/core/getResponse.ts
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
1
|
import { createRequestId } from '@mswjs/interceptors'
|
|
2
2
|
import type { RequestHandler } from './handlers/RequestHandler'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
executeHandlers,
|
|
5
|
+
type ResponseResolutionContext,
|
|
6
|
+
} from './utils/executeHandlers'
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Finds a response for the given request instance
|
|
7
10
|
* in the array of request handlers.
|
|
8
11
|
* @param handlers The array of request handlers.
|
|
9
12
|
* @param request The `Request` instance.
|
|
13
|
+
* @param resolutionContext Request resolution options.
|
|
10
14
|
* @returns {Response} A mocked response, if any.
|
|
11
15
|
*/
|
|
12
16
|
export const getResponse = async (
|
|
13
17
|
handlers: Array<RequestHandler>,
|
|
14
18
|
request: Request,
|
|
19
|
+
resolutionContext?: ResponseResolutionContext,
|
|
15
20
|
): Promise<Response | undefined> => {
|
|
16
21
|
const result = await executeHandlers({
|
|
17
22
|
request,
|
|
18
23
|
requestId: createRequestId(),
|
|
19
24
|
handlers,
|
|
25
|
+
resolutionContext,
|
|
20
26
|
})
|
|
21
27
|
|
|
22
28
|
return result?.response
|
|
@@ -102,7 +102,7 @@ export class HttpHandler extends RequestHandler<
|
|
|
102
102
|
})
|
|
103
103
|
|
|
104
104
|
devUtils.warn(
|
|
105
|
-
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters using "new URL(request.url).searchParams" instead. Learn more: https://mswjs.io/docs/http/intercepting-
|
|
105
|
+
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters using "new URL(request.url).searchParams" instead. Learn more: https://mswjs.io/docs/http/intercepting-requests#querysearch-parameters`,
|
|
106
106
|
)
|
|
107
107
|
}
|
|
108
108
|
|
|
@@ -108,4 +108,20 @@ describe('parse', () => {
|
|
|
108
108
|
},
|
|
109
109
|
})
|
|
110
110
|
})
|
|
111
|
+
|
|
112
|
+
it('supports a custom resolution context (base url)', () => {
|
|
113
|
+
expect(
|
|
114
|
+
new WebSocketHandler('/api/ws').parse({
|
|
115
|
+
url: new URL('ws://localhost:3000/api/ws'),
|
|
116
|
+
resolutionContext: {
|
|
117
|
+
baseUrl: 'ws://localhost:3000/',
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
).toEqual({
|
|
121
|
+
match: {
|
|
122
|
+
matches: true,
|
|
123
|
+
params: {},
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
})
|
|
111
127
|
})
|
|
@@ -29,6 +29,10 @@ export interface WebSocketHandlerConnection {
|
|
|
29
29
|
params: PathParams
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
export interface WebSocketResolutionContext {
|
|
33
|
+
baseUrl?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
32
36
|
export const kEmitter = Symbol('kEmitter')
|
|
33
37
|
export const kSender = Symbol('kSender')
|
|
34
38
|
const kStopPropagationPatched = Symbol('kStopPropagationPatched')
|
|
@@ -50,7 +54,10 @@ export class WebSocketHandler {
|
|
|
50
54
|
this.__kind = 'EventHandler'
|
|
51
55
|
}
|
|
52
56
|
|
|
53
|
-
public parse(args: {
|
|
57
|
+
public parse(args: {
|
|
58
|
+
url: URL
|
|
59
|
+
resolutionContext?: WebSocketResolutionContext
|
|
60
|
+
}): WebSocketHandlerParsedResult {
|
|
54
61
|
const clientUrl = new URL(args.url)
|
|
55
62
|
|
|
56
63
|
/**
|
|
@@ -60,7 +67,11 @@ export class WebSocketHandler {
|
|
|
60
67
|
*/
|
|
61
68
|
clientUrl.pathname = clientUrl.pathname.replace(/^\/socket.io\//, '/')
|
|
62
69
|
|
|
63
|
-
const match = matchRequestUrl(
|
|
70
|
+
const match = matchRequestUrl(
|
|
71
|
+
clientUrl,
|
|
72
|
+
this.url,
|
|
73
|
+
args.resolutionContext?.baseUrl,
|
|
74
|
+
)
|
|
64
75
|
|
|
65
76
|
return {
|
|
66
77
|
match,
|
|
@@ -76,9 +87,11 @@ export class WebSocketHandler {
|
|
|
76
87
|
|
|
77
88
|
public async run(
|
|
78
89
|
connection: Omit<WebSocketHandlerConnection, 'params'>,
|
|
90
|
+
resolutionContext?: WebSocketResolutionContext,
|
|
79
91
|
): Promise<boolean> {
|
|
80
92
|
const parsedResult = this.parse({
|
|
81
93
|
url: connection.client.url,
|
|
94
|
+
resolutionContext,
|
|
82
95
|
})
|
|
83
96
|
|
|
84
97
|
if (!this.predicate({ url: connection.client.url, parsedResult })) {
|