-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathurl-normalization.spec.ts
More file actions
147 lines (123 loc) · 4.77 KB
/
Copy pathurl-normalization.spec.ts
File metadata and controls
147 lines (123 loc) · 4.77 KB
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
142
143
144
145
146
147
import { normalizeHost, normalizeUrl } from '../src/util/url';
import { expect } from "./test-utils";
describe("URL normalization for matching", () => {
describe("host normalization", () => {
it("should bracket IPv6 hosts with non-default HTTP ports", () => {
expect(
normalizeHost('http', '[::1]:8000')
).to.equal('[::1]:8000');
});
it("should bracket IPv6 hosts with non-default HTTPS ports", () => {
expect(
normalizeHost('https', '[::1]:8443')
).to.equal('[::1]:8443');
});
it("should bracket IPv6 hosts when normalizing away the default port", () => {
expect(
normalizeHost('http', '[::1]:80')
).to.equal('[::1]');
expect(
normalizeHost('https', '[::1]:443')
).to.equal('[::1]');
});
it("should preserve existing formatting for IPv4 and domain hosts", () => {
expect(
normalizeHost('http', '127.0.0.1:8000')
).to.equal('127.0.0.1:8000');
expect(
normalizeHost('https', 'example.com:443')
).to.equal('example.com');
});
});
it("should do nothing to fully specified URLs", () => {
expect(
normalizeUrl('https://example.com/abc')
).to.equal('https://example.com/abc');
});
it("should normalize away default ports", () => {
expect(
normalizeUrl('https://example.com:443/abc')
).to.equal('https://example.com/abc');
});
it("should lowercase the authority & protocol", () => {
expect(
normalizeUrl('HTTPS://EXAMPLE.COM/PATH')
).to.equal('https://example.com/PATH');
});
it("should add trailing slashes for empty paths", () => {
expect(
normalizeUrl('https://example.com')
).to.equal('https://example.com/');
});
it("should add trailing slashes for protocol-less empty paths", () => {
expect(
normalizeUrl('example.com')
).to.equal('example.com/');
});
it("should remove all query parameters", () => {
expect(
normalizeUrl('https://example.com/path?a=b')
).to.equal('https://example.com/path');
});
it("should remove empty query strings", () => {
expect(
normalizeUrl('https://example.com/a?')
).to.equal('https://example.com/a');
});
it("should remove hash fragments", () => {
expect(
normalizeUrl('https://example.com/path#abc')
).to.equal('https://example.com/path');
});
it("should not decode encoded chars", () => {
expect(
// ' *$/' - only $ & / officially have a difference between their decoded/encoded versions
normalizeUrl('https://example.com/path%20%2A%24%2F')
).to.equal('https://example.com/path%20%2A%24%2F');
});
it("should encode must-be-encoded chars", () => {
expect(
// ' *$/' - only $ & / officially have a difference between their decoded/encoded versions
normalizeUrl('https://example.com/path more-path/')
).to.equal('https://example.com/path%20more-path/');
});
it("should encode IRI unicode chars", () => {
expect(
normalizeUrl('https://example.com/δ%24')
).to.equal('https://example.com/%CE%B4%24');
});
it("should uppercase percent-encoded hex chars", () => {
expect(
// $/ - only / needs encoding, but encoded $ is semantically
// diferent from decoded, so we do want to preserve it.
normalizeUrl('http://example.com/%2f')
).to.equal('http://example.com/%2F');
});
it("should not break when given invalid weird encodings", () => {
expect(
// $/ - only / needs encoding, but encoded $ is semantically
// diferent from decoded, so we do want to preserve it.
normalizeUrl('https://example.com/%u002A %1 δ ')
).to.equal('https://example.com/%U002A%20%1%20%CE%B4');
});
it("should convert unicode domains to their punycode equivalent", () => {
expect(
normalizeUrl('https://測試.com/')
).to.equal('https://xn--g6w251d.com/');
});
it("should trim trailing dots from domain names", () => {
expect(
normalizeUrl('https://example.com./')
).to.equal('https://example.com/');
});
it("should normalize relative URLs", () => {
expect(
normalizeUrl('/abcδ?d#q')
).to.equal('/abc%CE%B4');
});
it("should normalize absolute protocol-less URLs", () => {
expect(
normalizeUrl('測試.com/abcδ?d#q')
).to.equal('xn--g6w251d.com/abc%CE%B4');
});
});