/
utilities.test.ts
141 lines (127 loc) Β· 4.82 KB
/
utilities.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { describe, expect, test } from 'vitest'
import {
hasProtocol,
isRelative,
parsePath,
stringifyParsedURL,
withHttp,
withHttps,
withoutProtocol,
withProtocol
} from '../src'
describe('hasProtocol', () => {
const tests = [
{ input: '//', out: [false, false] },
{ input: '///', out: [false, false] },
{ input: '//test.com', out: [true, false] },
{ input: 'https://', out: [true, true] },
{ input: 'https://test.com', out: [true, true] },
{ input: '/test', out: [false, false] },
{ input: 'file:///home/user', out: [true, true] },
{ input: 'tel:', out: [true, true] },
{ input: 'tel:123456', out: [true, true] },
{ input: 'mailto:support@example.com', out: [true, true] }
]
for (const t of tests) {
test(t.input.toString(), () => {
const [withAcceptRelative, withoutAcceptRelative] = t.out
expect(hasProtocol(t.input, true)).toBe(withAcceptRelative)
expect(hasProtocol(t.input)).toBe(withoutAcceptRelative)
})
}
})
describe('isRelative', () => {
const tests = [
{ input: '/', out: false },
{ input: './/', out: true },
{ input: '../test', out: true },
{ input: 'https://', out: false }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(isRelative(t.input)).toBe(t.out)
})
}
})
describe('stringifyParsedURL', () => {
const tests = [
{ input: '.#hash', out: '.#hash' },
{ input: '.?foo=123', out: '.?foo=123' },
{ input: './?foo=123#hash', out: './?foo=123#hash' },
{ input: '/test?query=123#hash', out: '/test?query=123#hash' },
{ input: 'test?query=123#hash', out: 'test?query=123#hash' },
{ input: '/%c', out: '/%c' },
{ input: '/%', out: '/%' },
{ input: 'http://foo.com/test?query=123#hash', out: 'http://foo.com/test?query=123#hash' },
{ input: 'http://localhost:3000', out: 'http://localhost:3000' },
{ input: 'http://my_email%40gmail.com:password@www.my_site.com', out: 'http://my_email%40gmail.com:password@www.my_site.com' },
{ input: '/test?query=123,123#hash, test', out: '/test?query=123,123#hash, test' }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(stringifyParsedURL(parsePath(t.input))).toBe(t.out)
})
}
})
describe('withHttp', () => {
const tests = [
{ input: 'https://example.com', out: 'http://example.com' },
{ input: 'ftp://example.com/test?foo', out: 'http://example.com/test?foo' },
{ input: 'https://foo.com/test?query=123#hash', out: 'http://foo.com/test?query=123#hash' },
{ input: 'file:///home/user', out: 'http:///home/user' },
{ input: 'foo.bar.com', out: 'http://foo.bar.com' }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(withHttp(t.input)).toBe(t.out)
})
}
})
describe('withHttps', () => {
const tests = [
{ input: 'http://example.com', out: 'https://example.com' },
{ input: 'ftp://example.com/test?foo', out: 'https://example.com/test?foo' },
{ input: 'http://foo.com/test?query=123#hash', out: 'https://foo.com/test?query=123#hash' },
{ input: 'file:///home/user', out: 'https:///home/user' },
{ input: 'foo.bar.com', out: 'https://foo.bar.com' }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(withHttps(t.input)).toBe(t.out)
})
}
})
describe('withProtocol', () => {
const tests = [
{ input: 'http://example.com', protocol: 'https://', out: 'https://example.com' },
{ input: 'https://example.com', protocol: 'http://', out: 'http://example.com' },
{ input: 'ftp://example.com/test?foo', protocol: 'http://', out: 'http://example.com/test?foo' },
{ input: 'http://foo.com/test?query=123#hash', protocol: 'ftp://', out: 'ftp://foo.com/test?query=123#hash' },
{ input: 'file:///home/user', protocol: 'https://', out: 'https:///home/user' },
{ input: 'tel:1234567890', protocol: 'skype:', out: 'skype:1234567890' },
{ input: 'tel://+1234567890', protocol: 'callto://', out: 'callto://+1234567890' }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(withProtocol(t.input, t.protocol)).toBe(t.out)
})
}
})
describe('withoutProtocol', () => {
const tests = [
{ input: 'http://example.com', out: 'example.com' },
{ input: 'https://example.com', out: 'example.com' },
{ input: 'ftp://example.com/test?foo', out: 'example.com/test?foo' },
{ input: 'http://foo.com/test?query=123#hash', out: 'foo.com/test?query=123#hash' },
{ input: 'file:///home/user', out: '/home/user' },
{ input: 'tel:1234567890', out: '1234567890' },
{ input: 'mailto:support@example.com', out: 'support@example.com' },
{ input: 'skype:1234567890', out: '1234567890' },
{ input: 'callto://+1234567890', out: '+1234567890' }
]
for (const t of tests) {
test(t.input.toString(), () => {
expect(withoutProtocol(t.input)).toBe(t.out)
})
}
})