msw 2.7.3 → 2.7.5

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.
Files changed (31) hide show
  1. package/README.md +45 -80
  2. package/lib/core/{GraphQLHandler-D4_CxZQj.d.mts → GraphQLHandler-BNMGdWQe.d.mts} +1 -0
  3. package/lib/core/{GraphQLHandler-Pox7fIFM.d.ts → GraphQLHandler-D1CSV926.d.ts} +1 -0
  4. package/lib/core/graphql.d.mts +1 -1
  5. package/lib/core/graphql.d.ts +1 -1
  6. package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
  7. package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
  8. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  9. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  10. package/lib/core/handlers/WebSocketHandler.js +4 -2
  11. package/lib/core/handlers/WebSocketHandler.js.map +1 -1
  12. package/lib/core/handlers/WebSocketHandler.mjs +4 -2
  13. package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
  14. package/lib/core/index.d.mts +1 -1
  15. package/lib/core/index.d.ts +1 -1
  16. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
  17. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
  18. package/lib/core/utils/url/getAbsoluteUrl.js +1 -1
  19. package/lib/core/utils/url/getAbsoluteUrl.js.map +1 -1
  20. package/lib/core/utils/url/getAbsoluteUrl.mjs +1 -1
  21. package/lib/core/utils/url/getAbsoluteUrl.mjs.map +1 -1
  22. package/lib/iife/index.js +5 -3
  23. package/lib/iife/index.js.map +1 -1
  24. package/lib/mockServiceWorker.js +1 -1
  25. package/package.json +1 -1
  26. package/src/browser/utils/getAbsoluteWorkerUrl.test.ts +1 -3
  27. package/src/core/handlers/GraphQLHandler.ts +1 -0
  28. package/src/core/handlers/WebSocketHandler.test.ts +160 -0
  29. package/src/core/handlers/WebSocketHandler.ts +11 -2
  30. package/src/core/utils/url/getAbsoluteUrl.test.ts +29 -11
  31. package/src/core/utils/url/getAbsoluteUrl.ts +1 -2
@@ -8,7 +8,7 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.7.3'
11
+ const PACKAGE_VERSION = '2.7.5'
12
12
  const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f'
13
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
14
14
  const activeClientIds = new Set()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.7.3",
3
+ "version": "2.7.5",
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",
@@ -1,6 +1,4 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
1
+ // @vitest-environment jsdom
4
2
  import { getAbsoluteWorkerUrl } from './getAbsoluteWorkerUrl'
5
3
 
6
4
  const rawLocation = window.location
@@ -72,6 +72,7 @@ export type GraphQLResponseBody<BodyType extends DefaultBodyType> =
72
72
  | {
73
73
  data?: BodyType | null
74
74
  errors?: readonly Partial<GraphQLError>[] | null
75
+ extensions?: Record<string, any>
75
76
  }
76
77
  | null
77
78
  | undefined
@@ -0,0 +1,160 @@
1
+ import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
2
+ import { WebSocketHandler } from './WebSocketHandler'
3
+
4
+ describe('parse', () => {
5
+ it('matches an exact url', () => {
6
+ expect(
7
+ new WebSocketHandler('ws://localhost:3000').parse({
8
+ event: new MessageEvent('connection', {
9
+ data: {
10
+ client: {
11
+ url: new URL('ws://localhost:3000'),
12
+ },
13
+ } as WebSocketConnectionData,
14
+ }),
15
+ }),
16
+ ).toEqual({
17
+ match: {
18
+ matches: true,
19
+ params: {},
20
+ },
21
+ })
22
+ })
23
+
24
+ it('ignores trailing slash', () => {
25
+ expect(
26
+ new WebSocketHandler('ws://localhost:3000').parse({
27
+ event: new MessageEvent('connection', {
28
+ data: {
29
+ client: {
30
+ url: new URL('ws://localhost:3000/'),
31
+ },
32
+ } as WebSocketConnectionData,
33
+ }),
34
+ }),
35
+ ).toEqual({
36
+ match: {
37
+ matches: true,
38
+ params: {},
39
+ },
40
+ })
41
+
42
+ expect(
43
+ new WebSocketHandler('ws://localhost:3000/').parse({
44
+ event: new MessageEvent('connection', {
45
+ data: {
46
+ client: {
47
+ url: new URL('ws://localhost:3000'),
48
+ },
49
+ } as WebSocketConnectionData,
50
+ }),
51
+ }),
52
+ ).toEqual({
53
+ match: {
54
+ matches: true,
55
+ params: {},
56
+ },
57
+ })
58
+ })
59
+
60
+ it('supports path parameters', () => {
61
+ expect(
62
+ new WebSocketHandler('ws://localhost:3000/:serviceName').parse({
63
+ event: new MessageEvent('connection', {
64
+ data: {
65
+ client: {
66
+ url: new URL('ws://localhost:3000/auth'),
67
+ },
68
+ } as WebSocketConnectionData,
69
+ }),
70
+ }),
71
+ ).toEqual({
72
+ match: {
73
+ matches: true,
74
+ params: {
75
+ serviceName: 'auth',
76
+ },
77
+ },
78
+ })
79
+ })
80
+
81
+ it('ignores "/socket.io/" prefix in the client url', () => {
82
+ expect(
83
+ new WebSocketHandler('ws://localhost:3000').parse({
84
+ event: new MessageEvent('connection', {
85
+ data: {
86
+ client: {
87
+ url: new URL(
88
+ 'ws://localhost:3000/socket.io/?EIO=4&transport=websocket',
89
+ ),
90
+ },
91
+ } as WebSocketConnectionData,
92
+ }),
93
+ }),
94
+ ).toEqual({
95
+ match: {
96
+ matches: true,
97
+ params: {},
98
+ },
99
+ })
100
+
101
+ expect(
102
+ new WebSocketHandler('ws://localhost:3000/non-matching').parse({
103
+ event: new MessageEvent('connection', {
104
+ data: {
105
+ client: {
106
+ url: new URL(
107
+ 'ws://localhost:3000/socket.io/?EIO=4&transport=websocket',
108
+ ),
109
+ },
110
+ } as WebSocketConnectionData,
111
+ }),
112
+ }),
113
+ ).toEqual({
114
+ match: {
115
+ matches: false,
116
+ params: {},
117
+ },
118
+ })
119
+ })
120
+
121
+ it('preserves non-prefix "/socket.io/" path segment', () => {
122
+ /**
123
+ * @note It is highly unlikely but we still shouldn't modify the
124
+ * WebSocket client URL if it contains a user-defined "socket.io" segment.
125
+ */
126
+ expect(
127
+ new WebSocketHandler('ws://localhost:3000/clients/socket.io/123').parse({
128
+ event: new MessageEvent('connection', {
129
+ data: {
130
+ client: {
131
+ url: new URL('ws://localhost:3000/clients/socket.io/123'),
132
+ },
133
+ } as WebSocketConnectionData,
134
+ }),
135
+ }),
136
+ ).toEqual({
137
+ match: {
138
+ matches: true,
139
+ params: {},
140
+ },
141
+ })
142
+
143
+ expect(
144
+ new WebSocketHandler('ws://localhost:3000').parse({
145
+ event: new MessageEvent('connection', {
146
+ data: {
147
+ client: {
148
+ url: new URL('ws://localhost:3000/clients/socket.io/123'),
149
+ },
150
+ } as WebSocketConnectionData,
151
+ }),
152
+ }),
153
+ ).toEqual({
154
+ match: {
155
+ matches: false,
156
+ params: {},
157
+ },
158
+ })
159
+ })
160
+ })
@@ -47,8 +47,17 @@ export class WebSocketHandler {
47
47
  public parse(args: {
48
48
  event: MessageEvent<WebSocketConnectionData>
49
49
  }): WebSocketHandlerParsedResult {
50
- const connection = args.event.data
51
- const match = matchRequestUrl(connection.client.url, this.url)
50
+ const { data: connection } = args.event
51
+ const { url: clientUrl } = connection.client
52
+
53
+ /**
54
+ * @note Remove the Socket.IO path prefix from the WebSocket
55
+ * client URL. This is an exception to keep the users from
56
+ * including the implementation details in their handlers.
57
+ */
58
+ clientUrl.pathname = clientUrl.pathname.replace(/^\/socket.io\//, '/')
59
+
60
+ const match = matchRequestUrl(clientUrl, this.url)
52
61
 
53
62
  return {
54
63
  match,
@@ -1,29 +1,47 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
1
+ // @vitest-environment jsdom
4
2
  import { getAbsoluteUrl } from './getAbsoluteUrl'
5
3
 
6
- it('rebases a relative URL against the current "baseURI" (default)', () => {
7
- expect(getAbsoluteUrl('/reviews')).toEqual('http://localhost/reviews')
4
+ const rawLocation = window.location
5
+
6
+ afterAll(() => {
7
+ Object.defineProperty(window, 'location', {
8
+ value: rawLocation,
9
+ })
10
+ })
11
+
12
+ it('resolves a relative URL against the current location (default)', () => {
13
+ expect(getAbsoluteUrl('/reviews')).toBe('http://localhost/reviews')
14
+ })
15
+
16
+ it('supports relative URLs starting with search parameters', () => {
17
+ Object.defineProperty(window, 'location', {
18
+ value: {
19
+ href: 'http://localhost/nested',
20
+ },
21
+ })
22
+
23
+ expect(getAbsoluteUrl('?resourceId=abc-123')).toBe(
24
+ 'http://localhost/nested?resourceId=abc-123',
25
+ )
8
26
  })
9
27
 
10
- it('rebases a relative URL against a custom base URL', () => {
11
- expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
28
+ it('resolves a relative URL against a custom base URL', () => {
29
+ expect(getAbsoluteUrl('/user', 'https://api.github.com')).toBe(
12
30
  'https://api.github.com/user',
13
31
  )
14
32
  })
15
33
 
16
34
  it('returns a given absolute URL as-is', () => {
17
- expect(getAbsoluteUrl('https://api.mswjs.io/users')).toEqual(
35
+ expect(getAbsoluteUrl('https://api.mswjs.io/users')).toBe(
18
36
  'https://api.mswjs.io/users',
19
37
  )
20
38
  })
21
39
 
22
40
  it('returns an absolute URL given a relative path without a leading slash', () => {
23
- expect(getAbsoluteUrl('users')).toEqual('http://localhost/users')
41
+ expect(getAbsoluteUrl('users')).toBe('http://localhost/users')
24
42
  })
25
43
 
26
44
  it('returns a path with a pattern as-is', () => {
27
- expect(getAbsoluteUrl(':api/user')).toEqual('http://localhost/:api/user')
28
- expect(getAbsoluteUrl('*/resource/*')).toEqual('*/resource/*')
45
+ expect(getAbsoluteUrl(':api/user')).toBe('http://localhost/:api/user')
46
+ expect(getAbsoluteUrl('*/resource/*')).toBe('*/resource/*')
29
47
  })
@@ -16,8 +16,7 @@ export function getAbsoluteUrl(path: string, baseUrl?: string): string {
16
16
 
17
17
  // Resolve a relative request URL against a given custom "baseUrl"
18
18
  // or the document baseURI (in the case of browser/browser-like environments).
19
- const origin =
20
- baseUrl || (typeof document !== 'undefined' && document.baseURI)
19
+ const origin = baseUrl || (typeof location !== 'undefined' && location.href)
21
20
 
22
21
  return origin
23
22
  ? // Encode and decode the path to preserve escaped characters.