msw 3.0.3 → 3.0.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.
@@ -0,0 +1,371 @@
1
+ // @vitest-environment node
2
+ import { isCommonAssetRequest } from './is-common-asset-request'
3
+
4
+ test('treats requests over the "file:" protocol as common assets', () => {
5
+ expect(
6
+ isCommonAssetRequest(new Request('file:///Users/me/app/index.js')),
7
+ ).toBe(true)
8
+ expect(isCommonAssetRequest(new Request('file:///Users/me/app/data'))).toBe(
9
+ true,
10
+ )
11
+ expect(isCommonAssetRequest(new Request('file:///etc/hosts'))).toBe(true)
12
+ })
13
+
14
+ test('does not treat requests over other protocols as common assets by default', () => {
15
+ expect(isCommonAssetRequest(new Request('http://localhost/api/user'))).toBe(
16
+ false,
17
+ )
18
+ expect(isCommonAssetRequest(new Request('https://localhost/api/user'))).toBe(
19
+ false,
20
+ )
21
+ expect(isCommonAssetRequest(new Request('ftp://localhost/api/user'))).toBe(
22
+ false,
23
+ )
24
+ })
25
+
26
+ test('treats requests to Google Fonts as common assets', () => {
27
+ expect(
28
+ isCommonAssetRequest(
29
+ new Request('https://fonts.googleapis.com/css2?family=Inter'),
30
+ ),
31
+ ).toBe(true)
32
+ expect(
33
+ isCommonAssetRequest(new Request('https://fonts.googleapis.com/')),
34
+ ).toBe(true)
35
+ expect(
36
+ isCommonAssetRequest(new Request('http://fonts.googleapis.com/icon')),
37
+ ).toBe(true)
38
+ })
39
+
40
+ test('does not treat requests to other Google hosts as common assets', () => {
41
+ expect(
42
+ isCommonAssetRequest(new Request('https://www.googleapis.com/oauth2/v1')),
43
+ ).toBe(false)
44
+ expect(
45
+ isCommonAssetRequest(new Request('https://fonts.gstatic.com/resource')),
46
+ ).toBe(false)
47
+ })
48
+
49
+ test('treats requests to node modules served over HTTP as common assets', () => {
50
+ expect(
51
+ isCommonAssetRequest(
52
+ new Request('http://localhost:3000/node_modules/react/index.js'),
53
+ ),
54
+ ).toBe(true)
55
+ expect(
56
+ isCommonAssetRequest(
57
+ new Request('http://localhost:3000/node_modules/.vite/deps/react.js'),
58
+ ),
59
+ ).toBe(true)
60
+ expect(
61
+ isCommonAssetRequest(
62
+ new Request('http://localhost:3000/packages/app/node_modules/lodash'),
63
+ ),
64
+ ).toBe(true)
65
+ })
66
+
67
+ test('does not treat node modules in the hostname as common assets', () => {
68
+ expect(
69
+ isCommonAssetRequest(new Request('http://node_modules.example.com/api')),
70
+ ).toBe(false)
71
+ })
72
+
73
+ test('does not treat node modules in the query as common assets', () => {
74
+ expect(
75
+ isCommonAssetRequest(
76
+ new Request('http://localhost/api?path=node_modules/react'),
77
+ ),
78
+ ).toBe(false)
79
+ })
80
+
81
+ test('treats internal Vite requests as common assets', () => {
82
+ expect(
83
+ isCommonAssetRequest(new Request('http://localhost:5173/@vite/client')),
84
+ ).toBe(true)
85
+ expect(
86
+ isCommonAssetRequest(new Request('http://localhost:5173/@vite/env')),
87
+ ).toBe(true)
88
+ expect(
89
+ isCommonAssetRequest(
90
+ new Request('http://localhost:5173/@vite-plugin-react/preamble'),
91
+ ),
92
+ ).toBe(true)
93
+ })
94
+
95
+ test('does not treat Vite mentions in the query as common assets', () => {
96
+ expect(
97
+ isCommonAssetRequest(new Request('http://localhost/api?source=@vite')),
98
+ ).toBe(false)
99
+ })
100
+
101
+ test('treats stylesheet requests as common assets', () => {
102
+ expect(isCommonAssetRequest(new Request('http://localhost/main.css'))).toBe(
103
+ true,
104
+ )
105
+ expect(isCommonAssetRequest(new Request('http://localhost/main.scss'))).toBe(
106
+ true,
107
+ )
108
+ expect(isCommonAssetRequest(new Request('http://localhost/main.less'))).toBe(
109
+ true,
110
+ )
111
+ })
112
+
113
+ test('treats script requests as common assets', () => {
114
+ expect(isCommonAssetRequest(new Request('http://localhost/main.js'))).toBe(
115
+ true,
116
+ )
117
+ expect(isCommonAssetRequest(new Request('http://localhost/main.mjs'))).toBe(
118
+ true,
119
+ )
120
+ expect(isCommonAssetRequest(new Request('http://localhost/main.jsx'))).toBe(
121
+ true,
122
+ )
123
+ expect(isCommonAssetRequest(new Request('http://localhost/main.ts'))).toBe(
124
+ true,
125
+ )
126
+ expect(isCommonAssetRequest(new Request('http://localhost/main.mts'))).toBe(
127
+ true,
128
+ )
129
+ expect(isCommonAssetRequest(new Request('http://localhost/main.tsx'))).toBe(
130
+ true,
131
+ )
132
+ })
133
+
134
+ test('treats HTML document requests as common assets', () => {
135
+ expect(isCommonAssetRequest(new Request('http://localhost/index.html'))).toBe(
136
+ true,
137
+ )
138
+ })
139
+
140
+ test('treats font requests as common assets', () => {
141
+ expect(isCommonAssetRequest(new Request('http://localhost/font.ttf'))).toBe(
142
+ true,
143
+ )
144
+ expect(isCommonAssetRequest(new Request('http://localhost/font.otf'))).toBe(
145
+ true,
146
+ )
147
+ expect(isCommonAssetRequest(new Request('http://localhost/font.woff'))).toBe(
148
+ true,
149
+ )
150
+ expect(isCommonAssetRequest(new Request('http://localhost/font.woff2'))).toBe(
151
+ true,
152
+ )
153
+ expect(isCommonAssetRequest(new Request('http://localhost/font.eot'))).toBe(
154
+ true,
155
+ )
156
+ })
157
+
158
+ test('treats image requests as common assets', () => {
159
+ expect(isCommonAssetRequest(new Request('http://localhost/image.gif'))).toBe(
160
+ true,
161
+ )
162
+ expect(isCommonAssetRequest(new Request('http://localhost/image.jpg'))).toBe(
163
+ true,
164
+ )
165
+ expect(isCommonAssetRequest(new Request('http://localhost/image.jpeg'))).toBe(
166
+ true,
167
+ )
168
+ expect(isCommonAssetRequest(new Request('http://localhost/image.png'))).toBe(
169
+ true,
170
+ )
171
+ expect(isCommonAssetRequest(new Request('http://localhost/image.avif'))).toBe(
172
+ true,
173
+ )
174
+ expect(isCommonAssetRequest(new Request('http://localhost/image.webp'))).toBe(
175
+ true,
176
+ )
177
+ expect(isCommonAssetRequest(new Request('http://localhost/image.svg'))).toBe(
178
+ true,
179
+ )
180
+ })
181
+
182
+ test('treats favicon requests as common assets', () => {
183
+ expect(
184
+ isCommonAssetRequest(new Request('http://localhost/favicon.ico')),
185
+ ).toBe(true)
186
+ })
187
+
188
+ test('treats video requests as common assets', () => {
189
+ expect(isCommonAssetRequest(new Request('http://localhost/video.mp4'))).toBe(
190
+ true,
191
+ )
192
+ expect(isCommonAssetRequest(new Request('http://localhost/video.webm'))).toBe(
193
+ true,
194
+ )
195
+ expect(isCommonAssetRequest(new Request('http://localhost/video.ogg'))).toBe(
196
+ true,
197
+ )
198
+ expect(isCommonAssetRequest(new Request('http://localhost/video.mov'))).toBe(
199
+ true,
200
+ )
201
+ })
202
+
203
+ test('treats audio requests as common assets', () => {
204
+ expect(isCommonAssetRequest(new Request('http://localhost/audio.mp3'))).toBe(
205
+ true,
206
+ )
207
+ expect(isCommonAssetRequest(new Request('http://localhost/audio.wav'))).toBe(
208
+ true,
209
+ )
210
+ expect(isCommonAssetRequest(new Request('http://localhost/audio.flac'))).toBe(
211
+ true,
212
+ )
213
+ expect(isCommonAssetRequest(new Request('http://localhost/audio.aac'))).toBe(
214
+ true,
215
+ )
216
+ })
217
+
218
+ test('treats document requests as common assets', () => {
219
+ expect(isCommonAssetRequest(new Request('http://localhost/file.pdf'))).toBe(
220
+ true,
221
+ )
222
+ expect(isCommonAssetRequest(new Request('http://localhost/file.txt'))).toBe(
223
+ true,
224
+ )
225
+ expect(isCommonAssetRequest(new Request('http://localhost/file.csv'))).toBe(
226
+ true,
227
+ )
228
+ expect(isCommonAssetRequest(new Request('http://localhost/file.json'))).toBe(
229
+ true,
230
+ )
231
+ expect(isCommonAssetRequest(new Request('http://localhost/file.xml'))).toBe(
232
+ true,
233
+ )
234
+ expect(isCommonAssetRequest(new Request('http://localhost/file.md'))).toBe(
235
+ true,
236
+ )
237
+ })
238
+
239
+ test('treats archive requests as common assets', () => {
240
+ expect(
241
+ isCommonAssetRequest(new Request('http://localhost/archive.zip')),
242
+ ).toBe(true)
243
+ expect(
244
+ isCommonAssetRequest(new Request('http://localhost/archive.tar')),
245
+ ).toBe(true)
246
+ expect(isCommonAssetRequest(new Request('http://localhost/archive.gz'))).toBe(
247
+ true,
248
+ )
249
+ expect(
250
+ isCommonAssetRequest(new Request('http://localhost/archive.tar.gz')),
251
+ ).toBe(true)
252
+ expect(
253
+ isCommonAssetRequest(new Request('http://localhost/archive.rar')),
254
+ ).toBe(true)
255
+ expect(isCommonAssetRequest(new Request('http://localhost/archive.7z'))).toBe(
256
+ true,
257
+ )
258
+ })
259
+
260
+ test('matches the asset extension regardless of its case', () => {
261
+ expect(isCommonAssetRequest(new Request('http://localhost/image.PNG'))).toBe(
262
+ true,
263
+ )
264
+ expect(isCommonAssetRequest(new Request('http://localhost/main.Js'))).toBe(
265
+ true,
266
+ )
267
+ expect(isCommonAssetRequest(new Request('http://localhost/file.JSON'))).toBe(
268
+ true,
269
+ )
270
+ })
271
+
272
+ test('treats nested asset paths as common assets', () => {
273
+ expect(
274
+ isCommonAssetRequest(
275
+ new Request('http://localhost/static/assets/images/logo.png'),
276
+ ),
277
+ ).toBe(true)
278
+ expect(
279
+ isCommonAssetRequest(new Request('https://cdn.example.com/v1/bundle.js')),
280
+ ).toBe(true)
281
+ })
282
+
283
+ test('ignores the query string when matching the asset extension', () => {
284
+ expect(
285
+ isCommonAssetRequest(new Request('http://localhost/main.js?v=123')),
286
+ ).toBe(true)
287
+ expect(
288
+ isCommonAssetRequest(new Request('http://localhost/image.png?w=100&h=100')),
289
+ ).toBe(true)
290
+ })
291
+
292
+ test('ignores the hash when matching the asset extension', () => {
293
+ expect(
294
+ isCommonAssetRequest(new Request('http://localhost/main.js#section')),
295
+ ).toBe(true)
296
+ })
297
+
298
+ test('does not treat asset-like extensions in the query as common assets', () => {
299
+ expect(
300
+ isCommonAssetRequest(new Request('http://localhost/api?file=image.png')),
301
+ ).toBe(false)
302
+ })
303
+
304
+ test('does not treat asset-like extensions in the hostname as common assets', () => {
305
+ expect(isCommonAssetRequest(new Request('http://example.png/api'))).toBe(
306
+ false,
307
+ )
308
+ })
309
+
310
+ test('does not treat an extension followed by more path as a common asset', () => {
311
+ expect(
312
+ isCommonAssetRequest(new Request('http://localhost/main.js/metadata')),
313
+ ).toBe(false)
314
+ expect(isCommonAssetRequest(new Request('http://localhost/image.png/'))).toBe(
315
+ false,
316
+ )
317
+ })
318
+
319
+ test('does not treat unknown extensions as common assets', () => {
320
+ expect(isCommonAssetRequest(new Request('http://localhost/data.bin'))).toBe(
321
+ false,
322
+ )
323
+ expect(isCommonAssetRequest(new Request('http://localhost/map.wasm'))).toBe(
324
+ false,
325
+ )
326
+ expect(isCommonAssetRequest(new Request('http://localhost/file.docx'))).toBe(
327
+ false,
328
+ )
329
+ })
330
+
331
+ test('does not treat an extension without a leading dot as a common asset', () => {
332
+ expect(isCommonAssetRequest(new Request('http://localhost/png'))).toBe(false)
333
+ expect(isCommonAssetRequest(new Request('http://localhost/api/json'))).toBe(
334
+ false,
335
+ )
336
+ })
337
+
338
+ test('does not treat API requests as common assets', () => {
339
+ expect(isCommonAssetRequest(new Request('http://localhost/api/user'))).toBe(
340
+ false,
341
+ )
342
+ expect(
343
+ isCommonAssetRequest(new Request('https://api.example.com/v1/users/123')),
344
+ ).toBe(false)
345
+ expect(isCommonAssetRequest(new Request('http://localhost/graphql'))).toBe(
346
+ false,
347
+ )
348
+ expect(isCommonAssetRequest(new Request('http://localhost/'))).toBe(false)
349
+ })
350
+
351
+ test('does not treat API requests with dotted segments as common assets', () => {
352
+ expect(
353
+ isCommonAssetRequest(new Request('http://localhost/api/v1.2/users')),
354
+ ).toBe(false)
355
+ expect(
356
+ isCommonAssetRequest(new Request('http://localhost/users/john.doe')),
357
+ ).toBe(false)
358
+ })
359
+
360
+ test('ignores the request method when matching common assets', () => {
361
+ expect(
362
+ isCommonAssetRequest(
363
+ new Request('http://localhost/main.js', { method: 'POST' }),
364
+ ),
365
+ ).toBe(true)
366
+ expect(
367
+ isCommonAssetRequest(
368
+ new Request('http://localhost/api/user', { method: 'GET' }),
369
+ ),
370
+ ).toBe(false)
371
+ })
@@ -40,7 +40,7 @@ export function isCommonAssetRequest(request: Request): boolean {
40
40
  }
41
41
 
42
42
  // Ignore common static assets.
43
- return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test(
43
+ return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|ico|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test(
44
44
  url.pathname,
45
45
  )
46
46
  }