Skip to content

Commit

Permalink
Bug 1633415 [wpt PR 23265] - URL: stripping of tab and newlines in se…
Browse files Browse the repository at this point in the history
…tters, a=testonly

Automatic update from web-platform-tests
URL: stripping of tab and newlines in setters

Closes whatwg/url#419.
--

wpt-commits: 04aec1fa1b1f5cf8833b61682da707b7fd47bef6
wpt-pr: 23265
  • Loading branch information
annevk authored and moz-wptsync-bot committed May 4, 2020
1 parent d65234c commit 3994414
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions testing/web-platform/tests/url/url-setters-stripping.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
function urlString({ scheme = "https",
username = "username",
password = "password",
host = "host",
port = "8000",
pathname = "path",
search = "query",
hash = "fragment" }) {
return `${scheme}://${username}:${password}@${host}:${port}/${pathname}?${search}#${hash}`;
}

function urlRecord(scheme) {
return new URL(urlString({ scheme }));
}

for(const scheme of ["https", "wpt++"]) {
for(let i = 0; i < 0x20; i++) {
const stripped = i === 0x09 || i === 0x0A || i === 0x0D;

// It turns out that user agents are surprisingly similar for these ranges so generate fewer
// tests. If this is changed also change the logic for host below.
if (i !== 0 && i !== 0x1F && !stripped) {
continue;
}

const cpString = String.fromCodePoint(i);
const cpReference = "U+" + i.toString(16).toUpperCase().padStart(4, "0");

test(() => {
const expected = scheme === "https" ? (stripped ? "http" : "https") : (stripped ? "wpt--" : "wpt++");
const url = urlRecord(scheme);
url.protocol = String.fromCodePoint(i) + (scheme === "https" ? "http" : "wpt--");
assert_equals(url.protocol, expected + ":", "property");
assert_equals(url.href, urlString({ scheme: expected }), "href");
}, `Setting protocol with leading ${cpReference} (${scheme}:)`);

test(() => {
const expected = scheme === "https" ? (stripped ? "http" : "https") : (stripped ? "wpt--" : "wpt++");
const url = urlRecord(scheme);
url.protocol = (scheme === "https" ? "http" : "wpt--") + String.fromCodePoint(i);
assert_equals(url.protocol, expected + ":", "property");
assert_equals(url.href, urlString({ scheme: expected }), "href");
}, `Setting protocol with ${cpReference} before inserted colon (${scheme}:)`);

// Cannot test protocol with trailing as the algorithm inserts a colon before proceeding

// These do no stripping
for (const property of ["username", "password"]) {
for (const [type, expected, input] of [
["leading", encodeURIComponent(cpString) + "test", String.fromCodePoint(i) + "test"],
["middle", "te" + encodeURIComponent(cpString) + "st", "te" + String.fromCodePoint(i) + "st"],
["trailing", "test" + encodeURIComponent(cpString), "test" + String.fromCodePoint(i)]
]) {
test(() => {
const url = urlRecord(scheme);
url[property] = input;
assert_equals(url[property], expected, "property");
assert_equals(url.href, urlString({ scheme, [property]: expected }), "href");
}, `Setting ${property} with ${type} ${cpReference} (${scheme}:)`);
}
}

for (const [type, expectedPart, input] of [
["leading", (scheme === "https" ? cpString : encodeURIComponent(cpString)) + "test", String.fromCodePoint(i) + "test"],
["middle", "te" + (scheme === "https" ? cpString : encodeURIComponent(cpString)) + "st", "te" + String.fromCodePoint(i) + "st"],
["trailing", "test" + (scheme === "https" ? cpString : encodeURIComponent(cpString)), "test" + String.fromCodePoint(i)]
]) {
test(() => {
const expected = i === 0x00 ? "host" : stripped ? "test" : expectedPart;
const url = urlRecord(scheme);
url.host = input;
assert_equals(url.host, expected + ":8000", "property");
assert_equals(url.href, urlString({ scheme, host: expected }), "href");
}, `Setting host with ${type} ${cpReference} (${scheme}:)`);

test(() => {
const expected = i === 0x00 ? "host" : stripped ? "test" : expectedPart;
const url = urlRecord(scheme);
url.hostname = input;
assert_equals(url.hostname, expected, "property");
assert_equals(url.href, urlString({ scheme, host: expected }), "href");
}, `Setting hostname with ${type} ${cpReference} (${scheme}:)`);
}

test(() => {
const expected = stripped ? "9000" : "8000";
const url = urlRecord(scheme);
url.port = String.fromCodePoint(i) + "9000";
assert_equals(url.port, expected, "property");
assert_equals(url.href, urlString({ scheme, port: expected }), "href");
}, `Setting port with leading ${cpReference} (${scheme}:)`);

test(() => {
const expected = stripped ? "9000" : "90";
const url = urlRecord(scheme);
url.port = "90" + String.fromCodePoint(i) + "00";
assert_equals(url.port, expected, "property");
assert_equals(url.href, urlString({ scheme, port: expected }), "href");
}, `Setting port with middle ${cpReference} (${scheme}:)`);

test(() => {
const expected = "9000";
const url = urlRecord(scheme);
url.port = "9000" + String.fromCodePoint(i);
assert_equals(url.port, expected, "property");
assert_equals(url.href, urlString({ scheme, port: expected }), "href");
}, `Setting port with trailing ${cpReference} (${scheme}:)`);

for (const [property, separator] of [["pathname", "/"], ["search", "?"], ["hash", "#"]]) {
for (const [type, expectedPart, input] of [
["leading", encodeURIComponent(cpString) + "test", String.fromCodePoint(i) + "test"],
["middle", "te" + encodeURIComponent(cpString) + "st", "te" + String.fromCodePoint(i) + "st"],
["trailing", "test" + encodeURIComponent(cpString), "test" + String.fromCodePoint(i)]
]) {
test(() => {
const expected = stripped ? "test" : expectedPart;
const url = urlRecord(scheme);
url[property] = input;
assert_equals(url[property], separator + expected, "property");
assert_equals(url.href, urlString({ scheme, [property]: expected }), "href");
}, `Setting ${property} with ${type} ${cpReference} (${scheme}:)`);
}
}
}
}

0 comments on commit 3994414

Please sign in to comment.