Left: | ||
Right: |
OLD | NEW |
---|---|
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 url = require("url"); |
Sebastian Noack
2017/01/16 15:35:57
I suppose we could better use destructuring here n
kzar
2017/01/17 07:42:50
Done.
| |
22 var getDecodedHostname = url.getDecodedHostname; | 22 let getDecodedHostname = url.getDecodedHostname; |
23 var extractHostFromFrame = url.extractHostFromFrame; | 23 let extractHostFromFrame = url.extractHostFromFrame; |
24 var stringifyURL = url.stringifyURL; | 24 let stringifyURL = url.stringifyURL; |
25 var isThirdParty = url.isThirdParty; | 25 let isThirdParty = url.isThirdParty; |
26 | 26 |
27 module("URL/host tools"); | 27 module("URL/host tools"); |
28 | 28 |
29 test("Extracting hostname from URL", function() | 29 test("Extracting hostname from URL", () => |
30 { | 30 { |
31 function testURLHostname(url, expectedHostname, message) | 31 function testURLHostname(url, expectedHostname, message) |
32 { | 32 { |
33 equal(getDecodedHostname(new URL(url)), expectedHostname, message); | 33 equal(getDecodedHostname(new URL(url)), expectedHostname, message); |
34 } | 34 } |
35 | 35 |
36 testURLHostname("http://example.com/foo", "example.com", "with path"); | 36 testURLHostname("http://example.com/foo", "example.com", "with path"); |
37 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); | 37 testURLHostname("http://example.com/?foo=bar", "example.com", "with query"); |
38 testURLHostname("http://example.com/#top", "example.com", "with hash"); | 38 testURLHostname("http://example.com/#top", "example.com", "with hash"); |
39 testURLHostname("http://example.com:8080/", "example.com", "with port"); | 39 testURLHostname("http://example.com:8080/", "example.com", "with port"); |
40 testURLHostname("http://user:password@example.com/", "example.com", "with au th credentials"); | 40 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"); | 41 testURLHostname("http://xn--f-1gaa.com/", "f\u00f6\u00f6.com", "with punycod e"); |
42 testURLHostname("about:blank", "", "about:blank"); | 42 testURLHostname("about:blank", "", "about:blank"); |
43 testURLHostname("data:text/plain,foo", "", "data: URL"); | 43 testURLHostname("data:text/plain,foo", "", "data: URL"); |
44 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); | 44 testURLHostname("ftp://example.com/", "example.com", "ftp: URL"); |
45 testURLHostname("http://1.2.3.4:8000/", "1.2.3.4", "IPv4 address"); | 45 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"); | 46 testURLHostname("http://[2001:db8:85a3::8a2e:370:7334]/", "[2001:db8:85a3::8 a2e:370:7334]", "IPv6 address"); |
47 }); | 47 }); |
48 | 48 |
49 test("Extracting hostname from frame", function() | 49 test("Extracting hostname from frame", () => |
50 { | 50 { |
51 function testFrameHostname(hierarchy, expectedHostname, message) | 51 function testFrameHostname(hierarchy, expectedHostname, message) |
52 { | 52 { |
53 var frame = null; | 53 let frame = null; |
54 | 54 |
55 for (var i = 0; i < hierarchy.length; i++) | 55 for (let url of hierarchy) |
56 frame = {parent: frame, url: new URL(hierarchy[i])}; | 56 frame = {parent: frame, url: new URL(url)}; |
57 | 57 |
58 equal(extractHostFromFrame(frame), expectedHostname, message); | 58 equal(extractHostFromFrame(frame), expectedHostname, message); |
59 } | 59 } |
60 | 60 |
61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); | 61 testFrameHostname(["http://example.com/"], "example.com", "single frame"); |
62 testFrameHostname(["http://example.com/", "http://example.org/"], "example.o rg", "with parent frame"); | 62 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"); | 63 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"); | 64 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"); | 65 testFrameHostname(["about:blank", "about:blank"], "", "about:blank, no hostn ame"); |
66 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode"); | 66 testFrameHostname(["http://xn--f-1gaa.com/"], "f\u00f6\u00f6.com", "with pun ycode"); |
67 }); | 67 }); |
68 | 68 |
69 test("Stringifying URLs", function() | 69 test("Stringifying URLs", () => |
70 { | 70 { |
71 function testNormalizedURL(url, expectedURL, message) | 71 function testNormalizedURL(url, expectedURL, message) |
72 { | 72 { |
73 equal(stringifyURL(new URL(url)), expectedURL, message); | 73 equal(stringifyURL(new URL(url)), expectedURL, message); |
74 } | 74 } |
75 | 75 |
76 function testPreservedURL(url, message) | 76 function testPreservedURL(url, message) |
77 { | 77 { |
78 testNormalizedURL(url, url, message); | 78 testNormalizedURL(url, url, message); |
79 } | 79 } |
80 | 80 |
81 testPreservedURL("http://example.com/foo", "includes path"); | 81 testPreservedURL("http://example.com/foo", "includes path"); |
82 testPreservedURL("http://example.com/?foo=bar", "includes query"); | 82 testPreservedURL("http://example.com/?foo=bar", "includes query"); |
83 testPreservedURL("http://example.com:8080/", "includes port"); | 83 testPreservedURL("http://example.com:8080/", "includes port"); |
84 testPreservedURL("http://example.com/?", "with empty query string"); | 84 testPreservedURL("http://example.com/?", "with empty query string"); |
85 testNormalizedURL("http://example.com/#top","http://example.com/", "stripped hash"); | 85 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"); | 86 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"); | 87 testNormalizedURL("http://xn--f-1gaa.com/","http://f\u00f6\u00f6.com/", "dec oded punycode"); |
88 testPreservedURL("about:blank", "about:blank"); | 88 testPreservedURL("about:blank", "about:blank"); |
89 testPreservedURL("data:text/plain,foo", "data: URL"); | 89 testPreservedURL("data:text/plain,foo", "data: URL"); |
90 }); | 90 }); |
91 | 91 |
92 test("Third-party checks", function() | 92 test("Third-party checks", () => |
93 { | 93 { |
94 function hostnameToURL(hostname) | 94 function hostnameToURL(hostname) |
95 { | 95 { |
96 return new URL("http://" + hostname); | 96 return new URL("http://" + hostname); |
97 } | 97 } |
98 | 98 |
99 function testThirdParty(requestHost, documentHost, expected, message) | 99 function testThirdParty(requestHost, documentHost, expected, message) |
100 { | 100 { |
101 equal( | 101 equal( |
102 isThirdParty( | 102 isThirdParty( |
(...skipping 24 matching lines...) Expand all Loading... | |
127 testThirdParty("1.0xff.3.4", "1.0xff.3.4", false, "same IPv4 address with he xadecimal octet isn't third-party"); | 127 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"); | 128 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"); | 129 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"); | 130 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"); | 131 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"); | 132 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"); | 133 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"); | 134 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"); | 135 testThirdParty("example.com..", "example.com....", false, "traling dots are ignored"); |
136 }); | 136 }); |
137 })(); | 137 } |
OLD | NEW |