msw 2.3.0 → 2.3.1

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.
@@ -8,7 +8,7 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.3.0'
11
+ const PACKAGE_VERSION = '2.3.1'
12
12
  const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423'
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.3.0",
3
+ "version": "2.3.1",
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",
@@ -61,6 +61,17 @@ describe('matchRequestUrl', () => {
61
61
  expect(match).toHaveProperty('matches', false)
62
62
  expect(match).toHaveProperty('params', {})
63
63
  })
64
+
65
+ 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,
73
+ })
74
+ })
64
75
  })
65
76
 
66
77
  describe('coercePath', () => {
@@ -43,8 +43,14 @@ test('returns a path pattern string as-is', () => {
43
43
  expect(normalizePath('*/resource/*')).toEqual('*/resource/*')
44
44
  })
45
45
 
46
- test('removeß query parameters and hashes from a path pattern string', () => {
46
+ test('removes query parameters and hashes from a path pattern string', () => {
47
47
  expect(normalizePath(':api/user?query=123#some')).toEqual(
48
48
  'http://localhost/:api/user',
49
49
  )
50
50
  })
51
+
52
+ test('preserves optional path parameters', () => {
53
+ expect(normalizePath('/user/:userId?')).toEqual(
54
+ 'http://localhost/user/:userId?',
55
+ )
56
+ })
@@ -8,6 +8,7 @@ import { getAbsoluteUrl } from '../url/getAbsoluteUrl'
8
8
  * - Removes query parameters and hashes.
9
9
  * - Rebases relative URLs against the "baseUrl" or the current location.
10
10
  * - Preserves relative URLs in Node.js, unless specified otherwise.
11
+ * - Preserves optional path parameters.
11
12
  */
12
13
  export function normalizePath(path: Path, baseUrl?: string): Path {
13
14
  // RegExp paths do not need normalization.
@@ -1,17 +1,22 @@
1
1
  import { cleanUrl } from './cleanUrl'
2
2
 
3
- test('removes query parameters from a URL string', () => {
3
+ it('removes query parameters from a URL string', () => {
4
4
  expect(cleanUrl('/user?id=123')).toEqual('/user')
5
5
  expect(cleanUrl('/user?id=123&id=456')).toEqual('/user')
6
6
  expect(cleanUrl('/user?id=123&role=admin')).toEqual('/user')
7
7
  })
8
8
 
9
- test('removes hashes from a URL string', () => {
9
+ it('removes hashes from a URL string', () => {
10
10
  expect(cleanUrl('/user#hash')).toEqual('/user')
11
11
  expect(cleanUrl('/user#hash-with-dashes')).toEqual('/user')
12
12
  })
13
13
 
14
- test('removes both query parameters and hashes from a URL string', () => {
14
+ it('removes both query parameters and hashes from a URL string', () => {
15
15
  expect(cleanUrl('/user?id=123#some')).toEqual('/user')
16
16
  expect(cleanUrl('/user?id=123&role=admin#some')).toEqual('/user')
17
17
  })
18
+
19
+ it('preserves optional path parameters', () => {
20
+ expect(cleanUrl('/user/:id?')).toEqual('/user/:id?')
21
+ expect(cleanUrl('/user/:id?/:messageId?')).toEqual('/user/:id?/:messageId?')
22
+ })
@@ -5,8 +5,16 @@ export function getSearchParams(path: string) {
5
5
  }
6
6
 
7
7
  /**
8
- * Removes query parameters and hashes from a given URL string.
8
+ * Removes search parameters and the fragment
9
+ * from a given URL string.
9
10
  */
10
11
  export function cleanUrl(path: string): string {
12
+ // If the path ends with an optional path parameter,
13
+ // return it as-is.
14
+ if (path.endsWith('?')) {
15
+ return path
16
+ }
17
+
18
+ // Otherwise, remove the search and fragment from it.
11
19
  return path.replace(REDUNDANT_CHARACTERS_EXP, '')
12
20
  }
@@ -3,16 +3,16 @@
3
3
  */
4
4
  import { getAbsoluteUrl } from './getAbsoluteUrl'
5
5
 
6
- test('returns a given relative URL as-is', () => {
6
+ it('returns a given relative URL as-is', () => {
7
7
  expect(getAbsoluteUrl('/reviews')).toBe('/reviews')
8
8
  })
9
9
 
10
- test('rebases a relative URL against a custom base URL', () => {
10
+ it('rebases a relative URL against a custom base URL', () => {
11
11
  expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
12
12
  'https://api.github.com/user',
13
13
  )
14
14
  })
15
- test('returns a given absolute URL as-is', () => {
15
+ it('returns a given absolute URL as-is', () => {
16
16
  expect(getAbsoluteUrl('https://api.mswjs.io/users')).toBe(
17
17
  'https://api.mswjs.io/users',
18
18
  )
@@ -3,27 +3,27 @@
3
3
  */
4
4
  import { getAbsoluteUrl } from './getAbsoluteUrl'
5
5
 
6
- test('rebases a relative URL against the current "baseURI" (default)', () => {
6
+ it('rebases a relative URL against the current "baseURI" (default)', () => {
7
7
  expect(getAbsoluteUrl('/reviews')).toEqual('http://localhost/reviews')
8
8
  })
9
9
 
10
- test('rebases a relative URL against a custom base URL', () => {
10
+ it('rebases a relative URL against a custom base URL', () => {
11
11
  expect(getAbsoluteUrl('/user', 'https://api.github.com')).toEqual(
12
12
  'https://api.github.com/user',
13
13
  )
14
14
  })
15
15
 
16
- test('returns a given absolute URL as-is', () => {
16
+ it('returns a given absolute URL as-is', () => {
17
17
  expect(getAbsoluteUrl('https://api.mswjs.io/users')).toEqual(
18
18
  'https://api.mswjs.io/users',
19
19
  )
20
20
  })
21
21
 
22
- test('returns an absolute URL given a relative path without a leading slash', () => {
22
+ it('returns an absolute URL given a relative path without a leading slash', () => {
23
23
  expect(getAbsoluteUrl('users')).toEqual('http://localhost/users')
24
24
  })
25
25
 
26
- test('returns a path with a pattern as-is', () => {
26
+ it('returns a path with a pattern as-is', () => {
27
27
  expect(getAbsoluteUrl(':api/user')).toEqual('http://localhost/:api/user')
28
28
  expect(getAbsoluteUrl('*/resource/*')).toEqual('*/resource/*')
29
29
  })
@@ -3,30 +3,30 @@
3
3
  */
4
4
  import { isAbsoluteUrl } from './isAbsoluteUrl'
5
5
 
6
- test('returns true for the "http" scheme', () => {
6
+ it('returns true for the "http" scheme', () => {
7
7
  expect(isAbsoluteUrl('http://www.domain.com')).toEqual(true)
8
8
  })
9
9
 
10
- test('returns true for the "https" scheme', () => {
10
+ it('returns true for the "https" scheme', () => {
11
11
  expect(isAbsoluteUrl('https://www.domain.com')).toEqual(true)
12
12
  })
13
13
 
14
- test('returns true for the "ws" scheme', () => {
14
+ it('returns true for the "ws" scheme', () => {
15
15
  expect(isAbsoluteUrl('ws://www.domain.com')).toEqual(true)
16
16
  })
17
17
 
18
- test('returns true for the "ftp" scheme', () => {
18
+ it('returns true for the "ftp" scheme', () => {
19
19
  expect(isAbsoluteUrl('ftp://www.domain.com')).toEqual(true)
20
20
  })
21
21
 
22
- test('returns true for the custom scheme', () => {
22
+ it('returns true for the custom scheme', () => {
23
23
  expect(isAbsoluteUrl('web+my://www.example.com')).toEqual(true)
24
24
  })
25
25
 
26
- test('returns false for the relative URL', () => {
26
+ it('returns false for the relative URL', () => {
27
27
  expect(isAbsoluteUrl('/test')).toEqual(false)
28
28
  })
29
29
 
30
- test('returns false for the relative URL without a leading slash', () => {
30
+ it('returns false for the relative URL without a leading slash', () => {
31
31
  expect(isAbsoluteUrl('test')).toEqual(false)
32
32
  })