Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/url.js

Issue 30013574: Issue 7296 - Implement lightweight URL parsing (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 21, 2019, 11:20 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 // Only starting NodeJS 10 that URL is in the global space. 20 // Only starting NodeJS 10 that URL is in the global space.
21 const {URL} = require("url"); 21 const {URL} = require("url");
22 const {createSandbox} = require("./_common"); 22 const {createSandbox} = require("./_common");
23 23
24 const publicSuffixes = require("../data/publicSuffixList.json"); 24 const publicSuffixes = require("../data/publicSuffixList.json");
25 25
26 let parseURL = null;
26 let normalizeHostname = null; 27 let normalizeHostname = null;
27 let domainSuffixes = null; 28 let domainSuffixes = null;
28 let isThirdParty = null; 29 let isThirdParty = null;
29 let getBaseDomain = null; 30 let getBaseDomain = null;
30 31
31 exports.setUp = function(callback) 32 exports.setUp = function(callback)
32 { 33 {
33 let sandboxedRequire = createSandbox({ 34 let sandboxedRequire = createSandbox({
34 extraExports: { 35 extraExports: {
35 domain: ["getBaseDomain"] 36 domain: ["getBaseDomain"]
36 } 37 }
37 }); 38 });
38 ( 39 (
39 {normalizeHostname, domainSuffixes, isThirdParty, 40 {parseURL, normalizeHostname, domainSuffixes, isThirdParty,
40 getBaseDomain} = sandboxedRequire("../lib/url") 41 getBaseDomain} = sandboxedRequire("../lib/url")
41 ); 42 );
42 43
43 callback(); 44 callback();
44 }; 45 };
45 46
46 function hostnameToURL(hostname) 47 function hostnameToURL(hostname)
47 { 48 {
48 return new URL("http://" + hostname); 49 return new URL("http://" + hostname);
49 } 50 }
50 51
52 function testURLParsing(test, url)
53 {
54 let {protocol, hostname} = parseURL(url);
55
56 // We need to ensure only that our implementation matches that of the URL
57 // object.
58 let urlObject = new URL(url);
59
60 test.equal(protocol, urlObject.protocol);
61 test.equal(hostname, urlObject.hostname);
62 }
63
51 function testThirdParty(test, requestHostname, documentHostname, expected, 64 function testThirdParty(test, requestHostname, documentHostname, expected,
52 message) 65 message)
53 { 66 {
54 test.equal( 67 test.equal(
55 isThirdParty( 68 isThirdParty(
56 hostnameToURL(requestHostname), 69 hostnameToURL(requestHostname),
57 70
58 // Chrome's URL object normalizes IP addresses. So some test 71 // Chrome's URL object normalizes IP addresses. So some test
59 // will fail if we don't normalize the document host as well. 72 // will fail if we don't normalize the document host as well.
60 hostnameToURL(documentHostname).hostname 73 hostnameToURL(documentHostname).hostname
61 ), 74 ),
62 expected, 75 expected,
63 message 76 message
64 ); 77 );
65 } 78 }
66 79
80 exports.testParseURL = function(test)
81 {
82 testURLParsing(test, "https://example.com");
83 testURLParsing(test, "https://example.com/");
84 testURLParsing(test, "https://example.com/foo");
85 testURLParsing(test, "https://example.com/foo/bar");
86 testURLParsing(
87 test,
88 "https://example.com/foo/bar?https://random.com/foo/bar"
89 );
90
91 testURLParsing(test, "https://example.com:8080");
92 testURLParsing(test, "https://example.com:8080/");
93 testURLParsing(test, "https://example.com:8080/foo");
94 testURLParsing(test, "https://example.com:8080/foo/bar");
95 testURLParsing(
96 test,
97 "https://example.com:8080/foo/bar?https://random.com/foo/bar"
98 );
99
100 testURLParsing(test, "http://localhost");
101 testURLParsing(test, "http://localhost/");
102 testURLParsing(test, "http://localhost/foo");
103 testURLParsing(test, "http://localhost/foo/bar");
104 testURLParsing(
105 test,
106 "http://localhost/foo/bar?https://random.com/foo/bar"
107 );
108
109 testURLParsing(test, "http://192.168.1.1");
110 testURLParsing(test, "http://192.168.1.1/");
111 testURLParsing(test, "http://192.168.1.1/foo");
112 testURLParsing(test, "http://192.168.1.1/foo/bar");
113 testURLParsing(
114 test,
115 "http://192.168.1.1/foo/bar?https://random.com/foo/bar"
116 );
117
118 testURLParsing(test, "https://user@example.com");
119 testURLParsing(test, "https://user@example.com/");
120 testURLParsing(test, "https://user@example.com/foo");
121 testURLParsing(test, "https://user@example.com/foo/bar");
122 testURLParsing(
123 test,
124 "https://user@example.com/foo/bar?https://random.com/foo/bar"
125 );
126
127 testURLParsing(test, "https://user@example.com:8080");
128 testURLParsing(test, "https://user@example.com:8080/");
129 testURLParsing(test, "https://user@example.com:8080/foo");
130 testURLParsing(test, "https://user@example.com:8080/foo/bar");
131 testURLParsing(
132 test,
133 "https://user@example.com:8080/foo/bar?https://random.com/foo/bar"
134 );
135
136 testURLParsing(test, "https://user:pass@example.com");
137 testURLParsing(test, "https://user:pass@example.com/");
138 testURLParsing(test, "https://user:pass@example.com/foo");
139 testURLParsing(test, "https://user:pass@example.com/foo/bar");
140 testURLParsing(
141 test,
142 "https://user:pass@example.com/foo/bar?https://random.com/foo/bar"
143 );
144
145 testURLParsing(test, "https://user:pass@example.com:8080");
146 testURLParsing(test, "https://user:pass@example.com:8080/");
147 testURLParsing(test, "https://user:pass@example.com:8080/foo");
148 testURLParsing(test, "https://user:pass@example.com:8080/foo/bar");
149 testURLParsing(
150 test,
151 "https://user:pass@example.com:8080/foo/bar?https://random.com/foo/bar"
152 );
153
154 testURLParsing(test, "https://us@er:pa@ss@example.com");
155 testURLParsing(test, "https://us@er:pa@ss@example.com/");
156 testURLParsing(test, "https://us@er:pa@ss@example.com/foo");
157 testURLParsing(test, "https://us@er:pa@ss@example.com/foo/bar");
158 testURLParsing(
159 test,
160 "https://us@er:pa@ss@example.com/foo/bar?https://random.com/foo/bar"
161 );
162
163 testURLParsing(test, "https://us@er:pa@ss@example.com:8080");
164 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/");
165 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo");
166 testURLParsing(test, "https://us@er:pa@ss@example.com:8080/foo/bar");
167 testURLParsing(
168 test,
169 "https://us@er:pa@ss@example.com:8080/foo/bar?https://random.com/foo/bar"
170 );
171
172 testURLParsing(test, "ftp://user:pass@example.com:21");
173 testURLParsing(test, "ftp://user:pass@example.com:21/");
174 testURLParsing(test, "ftp://user:pass@example.com:21/foo");
175 testURLParsing(test, "ftp://user:pass@example.com:21/foo/bar");
176
177 testURLParsing(test, "about:blank");
178 testURLParsing(test, "chrome://extensions");
179 testURLParsing(
180 test,
181 "chrome-extension://bhignfpcigccnlfapldlodmhlidjaion/options.html"
182 );
183 testURLParsing(test, "mailto:john.doe@mail.example.com");
184
185 testURLParsing(test, "news:newsgroup");
186 testURLParsing(test, "news:message-id");
187 testURLParsing(test, "nntp://example.com:119/newsgroup");
188 testURLParsing(test, "nntp://example.com:119/message-id");
189
190 testURLParsing(test, "data:,");
191 testURLParsing(
192 test,
193 "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh"
194 );
195 testURLParsing(
196 test,
197 "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678"
198 );
199
200 testURLParsing(test, "javascript:");
201 testURLParsing(test, "javascript:alert();");
202 testURLParsing(test, "javascript:foo/bar/");
203 testURLParsing(test, "javascript://foo/bar/");
204
205 testURLParsing(test, "file:///dev/random");
206
207 test.done();
208 };
209
67 exports.testNormalizeHostname = function(test) 210 exports.testNormalizeHostname = function(test)
68 { 211 {
69 test.equal(normalizeHostname("example.com"), "example.com"); 212 test.equal(normalizeHostname("example.com"), "example.com");
70 test.equal(normalizeHostname("example.com."), "example.com"); 213 test.equal(normalizeHostname("example.com."), "example.com");
71 test.equal(normalizeHostname("example.com.."), "example.com"); 214 test.equal(normalizeHostname("example.com.."), "example.com");
72 test.equal(normalizeHostname("example.com..."), "example.com"); 215 test.equal(normalizeHostname("example.com..."), "example.com");
73 216
74 test.equal(normalizeHostname("Example.com"), "example.com"); 217 test.equal(normalizeHostname("Example.com"), "example.com");
75 test.equal(normalizeHostname("ExaMple.Com"), "example.com"); 218 test.equal(normalizeHostname("ExaMple.Com"), "example.com");
76 test.equal(normalizeHostname("ExaMple.Com.."), "example.com"); 219 test.equal(normalizeHostname("ExaMple.Com.."), "example.com");
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 "us-east-1.amazonaws.com" 431 "us-east-1.amazonaws.com"
289 ); 432 );
290 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com"); 433 test.equal(getBaseDomain("example.amazonaws.com"), "amazonaws.com");
291 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com"); 434 test.equal(getBaseDomain("amazonaws.com"), "amazonaws.com");
292 435
293 // Edge case. 436 // Edge case.
294 test.equal(getBaseDomain(""), ""); 437 test.equal(getBaseDomain(""), "");
295 438
296 test.done(); 439 test.done();
297 }; 440 };
OLDNEW
« no previous file with comments | « lib/url.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld