msw 2.7.4 → 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.
- package/lib/core/handlers/WebSocketHandler.js +4 -2
- package/lib/core/handlers/WebSocketHandler.js.map +1 -1
- package/lib/core/handlers/WebSocketHandler.mjs +4 -2
- package/lib/core/handlers/WebSocketHandler.mjs.map +1 -1
- package/lib/iife/index.js +4 -2
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +1 -1
- package/src/core/handlers/WebSocketHandler.test.ts +160 -0
- package/src/core/handlers/WebSocketHandler.ts +11 -2
package/lib/mockServiceWorker.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - Please do NOT serve this file on production.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
const PACKAGE_VERSION = '2.7.
|
|
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
|
@@ -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
|
|
51
|
-
const
|
|
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,
|