/
matchRequestUrl.test.ts
111 lines (101 loc) · 3.54 KB
/
matchRequestUrl.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* @jest-environment jsdom
*/
import { coercePath, matchRequestUrl } from './matchRequestUrl'
describe('matchRequestUrl', () => {
test('returns true when matches against an exact URL', () => {
const match = matchRequestUrl(
new URL('https://test.mswjs.io'),
'https://test.mswjs.io',
)
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {})
})
test('returns true when matched against a wildcard', () => {
const match = matchRequestUrl(new URL('https://test.mswjs.io'), '*')
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {
'0': 'https://test.mswjs.io/',
})
})
test('returns true when matched against a RegExp', () => {
const match = matchRequestUrl(
new URL('https://test.mswjs.io'),
/test\.mswjs\.io/,
)
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {})
})
test('returns path parameters when matched', () => {
const match = matchRequestUrl(
new URL('https://test.mswjs.io/user/abc-123'),
'https://test.mswjs.io/user/:userId',
)
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {
userId: 'abc-123',
})
})
test('decodes path parameters', () => {
const url = 'http://example.com:5001/example'
const match = matchRequestUrl(
new URL(`https://test.mswjs.io/reflect-url/${encodeURIComponent(url)}`),
'https://test.mswjs.io/reflect-url/:url',
)
expect(match).toHaveProperty('matches', true)
expect(match).toHaveProperty('params', {
url,
})
})
test('returns false when does not match against the request URL', () => {
const match = matchRequestUrl(
new URL('https://test.mswjs.io'),
'https://google.com',
)
expect(match).toHaveProperty('matches', false)
expect(match).toHaveProperty('params', {})
})
})
describe('coercePath', () => {
test('replaces wildcard with an unnnamed capturing group', () => {
expect(coercePath('*')).toEqual('(.*)')
expect(coercePath('**')).toEqual('(.*)')
expect(coercePath('/us*')).toEqual('/us(.*)')
expect(coercePath('/user/*')).toEqual('/user/(.*)')
expect(coercePath('https://example.com/user/*')).toEqual(
'https\\://example.com/user/(.*)',
)
expect(coercePath('https://example.com/us*')).toEqual(
'https\\://example.com/us(.*)',
)
})
test('preserves path parameter modifiers', () => {
expect(coercePath(':name*')).toEqual(':name*')
expect(coercePath('/foo/:name*')).toEqual('/foo/:name*')
expect(coercePath('/foo/**:name*')).toEqual('/foo/(.*):name*')
expect(coercePath('**/foo/*/:name*')).toEqual('(.*)/foo/(.*)/:name*')
expect(coercePath('/foo/:first/bar/:second*/*')).toEqual(
'/foo/:first/bar/:second*/(.*)',
)
})
test('escapes the colon in protocol', () => {
expect(coercePath('https://example.com')).toEqual('https\\://example.com')
expect(coercePath('https://example.com/:userId')).toEqual(
'https\\://example.com/:userId',
)
expect(coercePath('http://localhost:3000')).toEqual(
'http\\://localhost\\:3000',
)
})
test('escapes the colon in port', () => {
expect(coercePath('https://example.com:1234')).toEqual(
'https\\://example.com\\:1234',
)
expect(coercePath('https://example.com:1234/:userId')).toEqual(
'https\\://example.com\\:1234/:userId',
)
expect(coercePath('https://example.com:1234/:5678')).toEqual(
'https\\://example.com\\:1234/:5678',
)
})
})