msw 2.12.8 → 2.12.10

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.
@@ -7,7 +7,7 @@
7
7
  * - Please do NOT modify this file.
8
8
  */
9
9
 
10
- const PACKAGE_VERSION = '2.12.8'
10
+ const PACKAGE_VERSION = '2.12.10'
11
11
  const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'
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.12.8",
3
+ "version": "2.12.10",
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",
@@ -215,7 +215,7 @@
215
215
  "sideEffects": false,
216
216
  "dependencies": {
217
217
  "@inquirer/confirm": "^5.0.0",
218
- "@mswjs/interceptors": "^0.41.0",
218
+ "@mswjs/interceptors": "^0.41.2",
219
219
  "@open-draft/deferred-promise": "^2.2.0",
220
220
  "@types/statuses": "^2.0.6",
221
221
  "cookie": "^1.0.2",
@@ -285,7 +285,7 @@
285
285
  "vitest-environment-miniflare": "^2.14.4",
286
286
  "webpack": "^5.95.0",
287
287
  "webpack-http-server": "^0.5.0",
288
- "msw": "2.12.8"
288
+ "msw": "2.12.10"
289
289
  },
290
290
  "peerDependencies": {
291
291
  "typescript": ">= 4.8.x"
@@ -1,5 +1,5 @@
1
1
  import { Emitter } from 'strict-event-emitter'
2
- import { createRequestId } from '@mswjs/interceptors'
2
+ import { createRequestId, resolveWebSocketUrl } from '@mswjs/interceptors'
3
3
  import type {
4
4
  WebSocketClientConnectionProtocol,
5
5
  WebSocketConnectionData,
@@ -46,7 +46,7 @@ export class WebSocketHandler {
46
46
 
47
47
  protected [kEmitter]: Emitter<WebSocketHandlerEventMap>
48
48
 
49
- constructor(private readonly url: Path) {
49
+ constructor(protected readonly url: Path) {
50
50
  this.id = createRequestId()
51
51
 
52
52
  this[kEmitter] = new Emitter()
@@ -60,6 +60,15 @@ export class WebSocketHandler {
60
60
  }): WebSocketHandlerParsedResult {
61
61
  const clientUrl = new URL(args.url)
62
62
 
63
+ // Resolve the WebSocket handler path:
64
+ // - Plain string URLs resolved as per the specification (via Interceptors).
65
+ // - String URLs starting with a wildcard are preserved (prepending a scheme there will break them).
66
+ // - RegExp paths are preserved.
67
+ const resolvedHandlerUrl =
68
+ this.url instanceof RegExp || this.url.startsWith('*')
69
+ ? this.url
70
+ : this.#resolveWebSocketUrl(this.url, args.resolutionContext?.baseUrl)
71
+
63
72
  /**
64
73
  * @note Remove the Socket.IO path prefix from the WebSocket
65
74
  * client URL. This is an exception to keep the users from
@@ -69,7 +78,7 @@ export class WebSocketHandler {
69
78
 
70
79
  const match = matchRequestUrl(
71
80
  clientUrl,
72
- this.url,
81
+ resolvedHandlerUrl,
73
82
  args.resolutionContext?.baseUrl,
74
83
  )
75
84
 
@@ -138,6 +147,26 @@ export class WebSocketHandler {
138
147
  // This is what the developer adds listeners for.
139
148
  return this[kEmitter].emit('connection', connection)
140
149
  }
150
+
151
+ #resolveWebSocketUrl(url: string, baseUrl?: string): string {
152
+ const resolvedUrl = resolveWebSocketUrl(
153
+ baseUrl
154
+ ? /**
155
+ * @note Resolve against the base URL preemtively because `resolveWebSocketUrl` only
156
+ * resolves against `location.href`, which is missing in Node.js. Base URL allows
157
+ * the handler to accept a relative URL in Node.js.
158
+ */
159
+ new URL(url, baseUrl)
160
+ : url,
161
+ )
162
+
163
+ /**
164
+ * @note Omit the trailing slash.
165
+ * While the browser always produces a trailing slash at the end of a WebSocket URL,
166
+ * having it in as the handler's predicate would mean it is *required* in the actual URL.
167
+ */
168
+ return resolvedUrl.replace(/\/$/, '')
169
+ }
141
170
  }
142
171
 
143
172
  function createStopPropagationListener(handler: WebSocketHandler) {
package/src/core/index.ts CHANGED
@@ -26,6 +26,12 @@ export {
26
26
  type ServerSentEventMessage,
27
27
  } from './sse'
28
28
 
29
+ import type { HttpHandler } from './handlers/HttpHandler'
30
+ import type { GraphQLHandler } from './handlers/GraphQLHandler'
31
+ import type { WebSocketHandler } from './handlers/WebSocketHandler'
32
+
33
+ export type AnyHandler = HttpHandler | GraphQLHandler | WebSocketHandler
34
+
29
35
  /* Utils */
30
36
  export { matchRequestUrl } from './utils/matching/matchRequestUrl'
31
37
  export * from './utils/handleRequest'
@@ -5,71 +5,85 @@ import { coercePath, matchRequestUrl } from './matchRequestUrl'
5
5
 
6
6
  describe('matchRequestUrl', () => {
7
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', {})
8
+ expect(
9
+ matchRequestUrl(
10
+ new URL('https://test.mswjs.io'),
11
+ 'https://test.mswjs.io',
12
+ ),
13
+ ).toEqual({
14
+ matches: true,
15
+ params: {},
16
+ })
14
17
  })
15
18
 
16
19
  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/',
20
+ expect(matchRequestUrl(new URL('https://test.mswjs.io'), '*')).toEqual({
21
+ matches: true,
22
+ params: {
23
+ '0': 'https://test.mswjs.io/',
24
+ },
21
25
  })
22
26
  })
23
27
 
24
28
  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', {})
29
+ expect(
30
+ matchRequestUrl(new URL('https://test.mswjs.io'), /test\.mswjs\.io/),
31
+ ).toEqual({
32
+ matches: true,
33
+ params: {},
34
+ })
31
35
  })
32
36
 
33
37
  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',
38
+ expect(
39
+ matchRequestUrl(
40
+ new URL('https://test.mswjs.io/user/abc-123'),
41
+ 'https://test.mswjs.io/user/:userId',
42
+ ),
43
+ ).toEqual({
44
+ matches: true,
45
+ params: {
46
+ userId: 'abc-123',
47
+ },
41
48
  })
42
49
  })
43
50
 
44
51
  test('decodes path parameters', () => {
45
52
  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
+ expect(
55
+ matchRequestUrl(
56
+ new URL(`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`),
57
+ 'https://test.mswjs.io/reflect-url/:url',
58
+ ),
59
+ ).toEqual({
60
+ matches: true,
61
+ params: {
62
+ url,
63
+ },
53
64
  })
54
65
  })
55
66
 
56
67
  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', {})
68
+ expect(
69
+ matchRequestUrl(new URL('https://test.mswjs.io'), 'https://google.com'),
70
+ ).toEqual({
71
+ matches: false,
72
+ params: {},
73
+ })
63
74
  })
64
75
 
65
76
  test('returns true when matching optional path parameters', () => {
66
- const match = matchRequestUrl(
67
- new URL('https://test.mswjs.io/user'),
68
- 'https://test.mswjs.io/user/:userId?',
69
- )
70
- expect(match).toHaveProperty('matches', true)
71
- expect(match).toHaveProperty('params', {
72
- userId: undefined,
77
+ expect(
78
+ matchRequestUrl(
79
+ new URL('https://test.mswjs.io/user/123'),
80
+ 'https://test.mswjs.io/user/:userId?',
81
+ ),
82
+ ).toEqual({
83
+ matches: true,
84
+ params: {
85
+ userId: '123',
86
+ },
73
87
  })
74
88
  })
75
89