Source code

Revision control

Copy as Markdown

Other Tools

/* eslint-env worker */
function ok(a, msg) {
dump("OK: " + !!a + " => " + a + " " + msg + "\n");
postMessage({ type: "status", status: !!a, msg: a + ": " + msg });
}
function is(a, b, msg) {
dump("IS: " + (a === b) + " => " + a + " | " + b + " " + msg + "\n");
postMessage({
type: "status",
status: a === b,
msg: a + " === " + b + ": " + msg,
});
}
// eslint-disable-next-line complexity
onmessage = function () {
let status = false;
try {
if (URL instanceof Object) {
status = true;
}
} catch (e) {}
ok(status, "URL in workers \\o/");
var tests = [
{
base: undefined,
error: false,
origin: "http://www.abc.com",
protocol: "http:",
username: "",
password: "",
host: "www.abc.com",
hostname: "www.abc.com",
port: "",
pathname: "/",
search: "",
hash: "",
},
{
base: undefined,
error: false,
origin: "ftp://www.abc.com",
protocol: "ftp:",
username: "auser",
password: "apw",
host: "www.abc.com",
hostname: "www.abc.com",
port: "",
pathname: "/",
search: "",
hash: "",
},
{
base: undefined,
error: false,
protocol: "http:",
username: "",
password: "",
host: "www.abc.com:90",
hostname: "www.abc.com",
port: "90",
pathname: "/apath/",
search: "",
hash: "",
},
{
base: undefined,
error: false,
origin: "http://www.abc.com",
protocol: "http:",
username: "",
password: "",
host: "www.abc.com",
hostname: "www.abc.com",
port: "",
pathname: "/apath/afile.txt",
search: "",
hash: "#ahash",
},
{
base: undefined,
error: false,
origin: "http://example.com",
protocol: "http:",
username: "",
password: "",
host: "example.com",
hostname: "example.com",
port: "",
pathname: "/",
search: "?test",
hash: "#hash",
},
{
base: undefined,
error: false,
origin: "http://example.com",
protocol: "http:",
username: "",
password: "",
host: "example.com",
hostname: "example.com",
port: "",
pathname: "/",
search: "?test",
hash: "",
},
{
base: undefined,
error: false,
hash: "#question%3f",
},
{
base: undefined,
error: false,
protocol: "https:",
port: "4443",
pathname: "/",
hash: "",
search: "",
},
{
base: undefined,
error: false,
protocol: "http:",
pathname: "/apath/afile.txt",
hash: "#ahash?asearch",
search: "",
},
{
base: undefined,
error: false,
protocol: "http:",
pathname: "/apath/afile.txt",
hash: "#ahash",
search: "?asearch",
},
{
base: undefined,
error: false,
pathname: "/apath/afile.txt",
hash: "#ahash",
search: "",
},
{
base: undefined,
error: false,
protocol: "http:",
username: "auser",
password: "apassword",
host: "www.abc.com:90",
hostname: "www.abc.com",
port: "90",
pathname: "/apath/afile.txt",
hash: "#ahash",
search: "?asearch",
},
{ url: "/foo#bar", base: "www.test.org", error: true },
{ url: "/foo#bar", base: null, error: true },
{ url: "/foo#bar", base: 42, error: true },
{
base: undefined,
error: false,
protocol: "ftp:",
},
{
base: undefined,
error: false,
protocol: "file:",
},
{
base: undefined,
error: false,
protocol: "gopher:",
expectedChangedProtocol: "https:",
},
{
base: undefined,
error: false,
protocol: "ws:",
},
{
base: undefined,
error: false,
protocol: "wss:",
},
{
base: undefined,
error: false,
protocol: "foo:",
expectedChangedProtocol: "https:",
},
];
while (tests.length) {
var test = tests.shift();
var error = false;
var url;
try {
if (test.base) {
url = new URL(test.url, test.base);
} else {
url = new URL(test.url);
}
} catch (e) {
error = true;
}
is(test.error, error, "Error creating URL");
if (test.error) {
continue;
}
if ("href" in test) {
is(url.href, test.href, "href");
}
if ("origin" in test) {
is(url.origin, test.origin, "origin");
}
if ("protocol" in test) {
is(url.protocol, test.protocol, "protocol");
}
if ("username" in test) {
is(url.username, test.username, "username");
}
if ("password" in test) {
is(url.password, test.password, "password");
}
if ("host" in test) {
is(url.host, test.host, "host");
}
if ("hostname" in test) {
is(url.hostname, test.hostname, "hostname");
}
if ("port" in test) {
is(url.port, test.port, "port");
}
if ("pathname" in test) {
is(url.pathname, test.pathname, "pathname");
}
if ("search" in test) {
is(url.search, test.search, "search");
}
if ("hash" in test) {
is(url.hash, test.hash, "hash");
}
ok(url, "Url exists!");
if ("href" in test) {
url.href = test.href;
}
if ("protocol" in test) {
url.protocol = test.protocol;
}
if ("username" in test && test.username) {
url.username = test.username;
}
if ("password" in test && test.password) {
url.password = test.password;
}
if ("host" in test) {
url.host = test.host;
}
if ("hostname" in test) {
url.hostname = test.hostname;
}
if ("port" in test) {
url.port = test.port;
}
if ("pathname" in test) {
url.pathname = test.pathname;
}
if ("search" in test) {
url.search = test.search;
}
if ("hash" in test) {
url.hash = test.hash;
}
if ("href" in test) {
is(url.href, test.href, "href");
}
if ("origin" in test) {
is(url.origin, test.origin, "origin");
}
if ("expectedChangedProtocol" in test) {
is(url.protocol, test.expectedChangedProtocol, "protocol");
} else if ("protocol" in test) {
is(url.protocol, test.protocol, "protocol");
}
if ("username" in test) {
is(url.username, test.username, "username");
}
if ("password" in test) {
is(url.password, test.password, "password");
}
if ("host" in test) {
is(url.host, test.host, "host");
}
if ("hostname" in test) {
is(test.hostname, url.hostname, "hostname");
}
if ("port" in test) {
is(test.port, url.port, "port");
}
if ("pathname" in test) {
is(test.pathname, url.pathname, "pathname");
}
if ("search" in test) {
is(test.search, url.search, "search");
}
if ("hash" in test) {
is(test.hash, url.hash, "hash");
}
if ("href" in test) {
is(test.href, url + "", "stringify works");
}
}
postMessage({ type: "finish" });
};