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

Side by Side Diff: qunit/tests/url.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Addressed some more feedback Created Jan. 18, 2017, 11:44 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 | « qunit/tests/prefs.js ('k') | qunit/tests/versionComparator.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 19
19 (function()
20 { 20 {
21 var url = require("url"); 21 let {getDecodedHostname,
22 var getDecodedHostname = url.getDecodedHostname; 22 extractHostFromFrame,
23 var extractHostFromFrame = url.extractHostFromFrame; 23 stringifyURL,
24 var stringifyURL = url.stringifyURL; 24 isThirdParty} = require("url");
25 var isThirdParty = url.isThirdParty;
26 25
27 module("URL/host tools"); 26 module("URL/host tools");
28 27
29 test("Extracting hostname from URL", function() 28 test("Extracting hostname from URL", () =>
30 { 29 {
31 function testURLHostname(url, expectedHostname, message) 30 function testURLHostname(url, expectedHostname, message)
32 { 31 {
33 equal(getDecodedHostname(new URL(url)), expectedHostname, message); 32 equal(getDecodedHostname(new URL(url)), expectedHostname, message);
34 } 33 }
35 34
36 testURLHostname("http://example.com/foo", "example.com", "with path"); 35 testURLHostname("http://example.com/foo", "example.com", "with path");
37 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); 36 testURLHostname("http://example.com/?foo=bar", "example.com", "with query");
38 testURLHostname("http://example.com/#top", "example.com", "with hash"); 37 testURLHostname("http://example.com/#top", "example.com", "with hash");
39 testURLHostname("http://example.com:8080/", "example.com", "with port"); 38 testURLHostname("http://example.com:8080/", "example.com", "with port");
40 testURLHostname("http://user:password@example.com/", "example.com", "with au th credentials"); 39 testURLHostname("http://user:password@example.com/", "example.com", "with au th credentials");
41 testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", "with punycod e"); 40 testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", "with punycod e");
42 testURLHostname("about:blank", "", "about:blank"); 41 testURLHostname("about:blank", "", "about:blank");
43 testURLHostname("data:text/plain,foo", "", "data: URL"); 42 testURLHostname("data:text/plain,foo", "", "data: URL");
44 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); 43 testURLHostname("ftp://example.com/", "example.com", "ftp: URL");
45 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); 44 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address");
46 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", "[2001:db8:85a3::8 a2e:370:7334]", "IPv6 address"); 45 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", "[2001:db8:85a3::8 a2e:370:7334]", "IPv6 address");
47 }); 46 });
48 47
49 test("Extracting hostname from frame", function() 48 test("Extracting hostname from frame", () =>
50 { 49 {
51 function testFrameHostname(hierarchy, expectedHostname, message) 50 function testFrameHostname(hierarchy, expectedHostname, message)
52 { 51 {
53 var frame = null; 52 let frame = null;
54 53
55 for (var i = 0; i < hierarchy.length; i++) 54 for (let url of hierarchy)
56 frame = {parent: frame, url: new URL(hierarchy[i])}; 55 frame = {parent: frame, url: new URL(url)};
57 56
58 equal(extractHostFromFrame(frame), expectedHostname, message); 57 equal(extractHostFromFrame(frame), expectedHostname, message);
59 } 58 }
60 59
61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); 60 testFrameHostname(["http://example.com/"], "example.com", "single frame");
62 testFrameHostname(["http://example.com/", "http://example.org/"], "example.o rg", "with parent frame"); 61 testFrameHostname(["http://example.com/", "http://example.org/"], "example.o rg", "with parent frame");
63 testFrameHostname(["http://example.com/", "data:text/plain,foo"], "example.c om", "data: URL, hostname in parent"); 62 testFrameHostname(["http://example.com/", "data:text/plain,foo"], "example.c om", "data: URL, hostname in parent");
64 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], "ex ample.com", "about:blank, hostname in ancestor"); 63 testFrameHostname(["http://example.com/", "about:blank", "about:blank"], "ex ample.com", "about:blank, hostname in ancestor");
65 testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn ame"); 64 testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn ame");
66 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode"); 65 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode");
67 }); 66 });
68 67
69 test("Stringifying URLs", function() 68 test("Stringifying URLs", () =>
70 { 69 {
71 function testNormalizedURL(url, expectedURL, message) 70 function testNormalizedURL(url, expectedURL, message)
72 { 71 {
73 equal(stringifyURL(new URL(url)), expectedURL, message); 72 equal(stringifyURL(new URL(url)), expectedURL, message);
74 } 73 }
75 74
76 function testPreservedURL(url, message) 75 function testPreservedURL(url, message)
77 { 76 {
78 testNormalizedURL(url, url, message); 77 testNormalizedURL(url, url, message);
79 } 78 }
80 79
81 testPreservedURL("http://example.com/foo", "includes path"); 80 testPreservedURL("http://example.com/foo", "includes path");
82 testPreservedURL("http://example.com/?foo=bar", "includes query"); 81 testPreservedURL("http://example.com/?foo=bar", "includes query");
83 testPreservedURL("http://example.com:8080/", "includes port"); 82 testPreservedURL("http://example.com:8080/", "includes port");
84 testPreservedURL("http://example.com/?", "with empty query string"); 83 testPreservedURL("http://example.com/?", "with empty query string");
85 testNormalizedURL("http://example.com/#top","http://example.com/", "stripped hash"); 84 testNormalizedURL("http://example.com/#top","http://example.com/", "stripped hash");
86 testNormalizedURL("http://example.com/#top?", "http://example.com/", "stripp ed hash with trailing question mark"); 85 testNormalizedURL("http://example.com/#top?", "http://example.com/", "stripp ed hash with trailing question mark");
87 testNormalizedURL("http://xn--f-1gaa.com/","http://f\u00f6\u00f6.com/", "dec oded punycode"); 86 testNormalizedURL("http://xn--f-1gaa.com/","http://f\u00f6\u00f6.com/", "dec oded punycode");
88 testPreservedURL("about:blank", "about:blank"); 87 testPreservedURL("about:blank", "about:blank");
89 testPreservedURL("data:text/plain,foo", "data: URL"); 88 testPreservedURL("data:text/plain,foo", "data: URL");
90 }); 89 });
91 90
92 test("Third-party checks", function() 91 test("Third-party checks", () =>
93 { 92 {
94 function hostnameToURL(hostname) 93 function hostnameToURL(hostname)
95 { 94 {
96 return new URL("http://" + hostname); 95 return new URL("http://" + hostname);
97 } 96 }
98 97
99 function testThirdParty(requestHost, documentHost, expected, message) 98 function testThirdParty(requestHost, documentHost, expected, message)
100 { 99 {
101 equal( 100 equal(
102 isThirdParty( 101 isThirdParty(
(...skipping 24 matching lines...) Expand all
127 testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he xadecimal octet isn't third-party"); 126 testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he xadecimal octet isn't third-party");
128 testThirdParty("1.0xff.1.1", "2.0xff.1.1", true, "different IPv4 address wit h hexadecimal octet is third-party"); 127 testThirdParty("1.0xff.1.1", "2.0xff.1.1", true, "different IPv4 address wit h hexadecimal octet is third-party");
129 testThirdParty("0xff.example.com", "example.com", false, "domain starts like a hexadecimal IPv4 address but isn't one"); 128 testThirdParty("0xff.example.com", "example.com", false, "domain starts like a hexadecimal IPv4 address but isn't one");
130 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7 334]", false, "same IPv6 address isn't third-party"); 129 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7 334]", false, "same IPv6 address isn't third-party");
131 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7 334]", true, "different IPv6 address is third-party"); 130 testThirdParty("[2001:db8:85a3::8a2e:370:7334]", "[5001:db8:85a3::8a2e:370:7 334]", true, "different IPv6 address is third-party");
132 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, "same IPv4-mapped IPv6 address isn't third-party"); 131 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.0.2.128]", false, "same IPv4-mapped IPv6 address isn't third-party");
133 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, "differ ent IPv4-mapped IPv6 address is third-party"); 132 testThirdParty("[::ffff:192.0.2.128]", "[::ffff:192.1.2.128]", true, "differ ent IPv4-mapped IPv6 address is third-party");
134 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, "same IDN isn't third-party"); 133 testThirdParty("xn--f-1gaa.com", "f\u00f6\u00f6.com", false, "same IDN isn't third-party");
135 testThirdParty("example.com..", "example.com....", false, "traling dots are ignored"); 134 testThirdParty("example.com..", "example.com....", false, "traling dots are ignored");
136 }); 135 });
137 })(); 136 }
OLDNEW
« no previous file with comments | « qunit/tests/prefs.js ('k') | qunit/tests/versionComparator.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld