-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathurl.test.ts
91 lines (85 loc) · 3.54 KB
/
url.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
import {
getNumberOfUrlSegments,
getSanitizedUrlString,
parseUrl,
stripUrlQueryAndFragment,
} from '../../src/utils-hoist/url';
describe('stripQueryStringAndFragment', () => {
const urlString = 'http://dogs.are.great:1231/yay/';
const queryString = '?furry=yes&funny=very';
const fragment = '#adoptnotbuy';
it('strips query string from url', () => {
const urlWithQueryString = `${urlString}${queryString}`;
expect(stripUrlQueryAndFragment(urlWithQueryString)).toBe(urlString);
});
it('strips fragment from url', () => {
const urlWithFragment = `${urlString}${fragment}`;
expect(stripUrlQueryAndFragment(urlWithFragment)).toBe(urlString);
});
it('strips query string and fragment from url', () => {
const urlWithQueryStringAndFragment = `${urlString}${queryString}${fragment}`;
expect(stripUrlQueryAndFragment(urlWithQueryStringAndFragment)).toBe(urlString);
});
});
describe('getNumberOfUrlSegments', () => {
test.each([
['regular path', '/projects/123/views/234', 4],
['single param parameterized path', '/users/:id/details', 3],
['multi param parameterized path', '/stores/:storeId/products/:productId', 4],
['regex path', String(/\/api\/post[0-9]/), 2],
])('%s', (_: string, input, output) => {
// eslint-disable-next-line deprecation/deprecation
expect(getNumberOfUrlSegments(input)).toEqual(output);
});
});
describe('getSanitizedUrlString', () => {
it.each([
['regular url', 'https://somedomain.com', 'https://somedomain.com'],
['regular url with a path', 'https://somedomain.com/path/to/happiness', 'https://somedomain.com/path/to/happiness'],
[
'url with standard http port 80',
'http://somedomain.com:80/path/to/happiness',
'http://somedomain.com/path/to/happiness',
],
[
'url with standard https port 443',
'https://somedomain.com:443/path/to/happiness',
'https://somedomain.com/path/to/happiness',
],
[
'url with non-standard port',
'https://somedomain.com:4200/path/to/happiness',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with query params',
'https://somedomain.com:4200/path/to/happiness?auhtToken=abc123¶m2=bar',
'https://somedomain.com:4200/path/to/happiness',
],
[
'url with a fragment',
'https://somedomain.com/path/to/happiness#somewildfragment123',
'https://somedomain.com/path/to/happiness',
],
[
'url with a fragment and query params',
'https://somedomain.com/path/to/happiness#somewildfragment123?auhtToken=abc123¶m2=bar',
'https://somedomain.com/path/to/happiness',
],
[
'url with authorization',
'https://username:[email protected]',
'https://[filtered]:[filtered]@somedomain.com',
],
['same-origin url', '/api/v4/users?id=123', '/api/v4/users'],
['url without a protocol', 'example.com', 'example.com'],
['url without a protocol with a path', 'example.com/sub/path?id=123', 'example.com/sub/path'],
['url with port 8080', 'http://172.31.12.144:8080/test', 'http://172.31.12.144:8080/test'],
['url with port 4433', 'http://172.31.12.144:4433/test', 'http://172.31.12.144:4433/test'],
['url with port 443', 'http://172.31.12.144:443/test', 'http://172.31.12.144/test'],
['url with IP and port 80', 'http://172.31.12.144:80/test', 'http://172.31.12.144/test'],
])('returns a sanitized URL for a %s', (_, rawUrl: string, sanitizedURL: string) => {
const urlObject = parseUrl(rawUrl);
expect(getSanitizedUrlString(urlObject)).toEqual(sanitizedURL);
});
});