dom/workers/test/urlApi_worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/urlApi_worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,272 @@
     1.4 +function ok(a, msg) {
     1.5 +  dump("OK: " + !!a + "  =>  " + a + " " + msg + "\n");
     1.6 +  postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
     1.7 +}
     1.8 +
     1.9 +function is(a, b, msg) {
    1.10 +  dump("IS: " + (a===b) + "  =>  " + a + " | " + b + " " + msg + "\n");
    1.11 +  postMessage({type: 'status', status: a === b, msg: a + " === " + b + ": " + msg });
    1.12 +}
    1.13 +
    1.14 +onmessage = function() {
    1.15 +  status = false;
    1.16 +  try {
    1.17 +    if ((URL instanceof Object)) {
    1.18 +      status = true;
    1.19 +    }
    1.20 +  } catch(e) {
    1.21 +  }
    1.22 +
    1.23 +  var tests = [
    1.24 +    { url: 'http://www.abc.com',
    1.25 +      base: undefined,
    1.26 +      error: false,
    1.27 +      href: 'http://www.abc.com/',
    1.28 +      origin: 'http://www.abc.com',
    1.29 +      protocol: 'http:',
    1.30 +      username: '',
    1.31 +      password: '',
    1.32 +      host: 'www.abc.com',
    1.33 +      hostname: 'www.abc.com',
    1.34 +      port: '',
    1.35 +      pathname: '/',
    1.36 +      search: '',
    1.37 +      hash: ''
    1.38 +    },
    1.39 +    { url: 'ftp://auser:apw@www.abc.com',
    1.40 +      base: undefined,
    1.41 +      error: false,
    1.42 +      href: 'ftp://auser:apw@www.abc.com/',
    1.43 +      origin: 'ftp://www.abc.com',
    1.44 +      protocol: 'ftp:',
    1.45 +      username: 'auser',
    1.46 +      password: 'apw',
    1.47 +      host: 'www.abc.com',
    1.48 +      hostname: 'www.abc.com',
    1.49 +      port: '',
    1.50 +      pathname: '/',
    1.51 +      search: '',
    1.52 +      hash: ''
    1.53 +    },
    1.54 +    { url: 'http://www.abc.com:90/apath/',
    1.55 +      base: undefined,
    1.56 +      error: false,
    1.57 +      href: 'http://www.abc.com:90/apath/',
    1.58 +      origin: 'http://www.abc.com:90',
    1.59 +      protocol: 'http:',
    1.60 +      username: '',
    1.61 +      password: '',
    1.62 +      host: 'www.abc.com:90',
    1.63 +      hostname: 'www.abc.com',
    1.64 +      port: '90',
    1.65 +      pathname: '/apath/',
    1.66 +      search: '',
    1.67 +      hash: ''
    1.68 +    },
    1.69 +    { url: 'http://www.abc.com/apath/afile.txt#ahash',
    1.70 +      base: undefined,
    1.71 +      error: false,
    1.72 +      href: 'http://www.abc.com/apath/afile.txt#ahash',
    1.73 +      origin: 'http://www.abc.com',
    1.74 +      protocol: 'http:',
    1.75 +      username: '',
    1.76 +      password: '',
    1.77 +      host: 'www.abc.com',
    1.78 +      hostname: 'www.abc.com',
    1.79 +      port: '',
    1.80 +      pathname: '/apath/afile.txt',
    1.81 +      search: '',
    1.82 +      hash: '#ahash'
    1.83 +    },
    1.84 +    { url: 'http://example.com/?test#hash',
    1.85 +      base: undefined,
    1.86 +      error: false,
    1.87 +      href: 'http://example.com/?test#hash',
    1.88 +      origin: 'http://example.com',
    1.89 +      protocol: 'http:',
    1.90 +      username: '',
    1.91 +      password: '',
    1.92 +      host: 'example.com',
    1.93 +      hostname: 'example.com',
    1.94 +      port: '',
    1.95 +      pathname: '/',
    1.96 +      search: '?test',
    1.97 +      hash: '#hash'
    1.98 +    },
    1.99 +    { url: 'http://example.com/?test',
   1.100 +      base: undefined,
   1.101 +      error: false,
   1.102 +      href: 'http://example.com/?test',
   1.103 +      origin: 'http://example.com',
   1.104 +      protocol: 'http:',
   1.105 +      username: '',
   1.106 +      password: '',
   1.107 +      host: 'example.com',
   1.108 +      hostname: 'example.com',
   1.109 +      port: '',
   1.110 +      pathname: '/',
   1.111 +      search: '?test',
   1.112 +      hash: ''
   1.113 +    },
   1.114 +    { url: 'http://example.com/carrot#question%3f',
   1.115 +      base: undefined,
   1.116 +      error: false,
   1.117 +      hash: '#question?'
   1.118 +    },
   1.119 +    { url: 'https://example.com:4443?',
   1.120 +      base: undefined,
   1.121 +      error: false,
   1.122 +      protocol: 'https:',
   1.123 +      port: '4443',
   1.124 +      pathname: '/',
   1.125 +      hash: '',
   1.126 +      search: ''
   1.127 +    },
   1.128 +    { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
   1.129 +      base: undefined,
   1.130 +      error: false,
   1.131 +      href: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
   1.132 +      protocol: 'http:',
   1.133 +      pathname: '/apath/afile.txt',
   1.134 +      hash: '#ahash?asearch',
   1.135 +      search: ''
   1.136 +    },
   1.137 +    { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
   1.138 +      base: undefined,
   1.139 +      error: false,
   1.140 +      href: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
   1.141 +      protocol: 'http:',
   1.142 +      pathname: '/apath/afile.txt',
   1.143 +      hash: '#ahash',
   1.144 +      search: '?asearch'
   1.145 +    },
   1.146 +    { url: 'http://abc.com/apath/afile.txt?#ahash',
   1.147 +      base: undefined,
   1.148 +      error: false,
   1.149 +      pathname: '/apath/afile.txt',
   1.150 +      hash: '#ahash',
   1.151 +      search: ''
   1.152 +    },
   1.153 +    { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash',
   1.154 +      base: undefined,
   1.155 +      error: false,
   1.156 +      protocol: 'http:',
   1.157 +      username: 'auser',
   1.158 +      password: 'apassword',
   1.159 +      host: 'www.abc.com:90',
   1.160 +      hostname: 'www.abc.com',
   1.161 +      port: '90',
   1.162 +      pathname: '/apath/afile.txt',
   1.163 +      hash: '#ahash',
   1.164 +      search: '?asearch',
   1.165 +      origin: 'http://www.abc.com:90'
   1.166 +    },
   1.167 +
   1.168 +    { url: '/foo#bar',
   1.169 +      base: 'www.test.org',
   1.170 +      error: true,
   1.171 +    },
   1.172 +    { url: '/foo#bar',
   1.173 +      base: null,
   1.174 +      error: true,
   1.175 +    },
   1.176 +    { url: '/foo#bar',
   1.177 +      base: 42,
   1.178 +      error: true,
   1.179 +    },
   1.180 +    { url: 'ftp://ftp.something.net',
   1.181 +      base: undefined,
   1.182 +      error: false,
   1.183 +      protocol: 'ftp:',
   1.184 +    },
   1.185 +    { url: 'file:///tmp/file',
   1.186 +      base: undefined,
   1.187 +      error: false,
   1.188 +      protocol: 'file:',
   1.189 +    },
   1.190 +    { url: 'gopher://gopher.something.net',
   1.191 +      base: undefined,
   1.192 +      error: false,
   1.193 +      protocol: 'gopher:',
   1.194 +    },
   1.195 +    { url: 'ws://ws.something.net',
   1.196 +      base: undefined,
   1.197 +      error: false,
   1.198 +      protocol: 'ws:',
   1.199 +    },
   1.200 +    { url: 'wss://ws.something.net',
   1.201 +      base: undefined,
   1.202 +      error: false,
   1.203 +      protocol: 'wss:',
   1.204 +    },
   1.205 +    { url: 'foo://foo.something.net',
   1.206 +      base: undefined,
   1.207 +      error: false,
   1.208 +      protocol: 'foo:',
   1.209 +    },
   1.210 +  ];
   1.211 +
   1.212 +  while(tests.length) {
   1.213 +    var test = tests.shift();
   1.214 +
   1.215 +    var error = false;
   1.216 +    var url;
   1.217 +    try {
   1.218 +      if (test.base) {
   1.219 +        url = new URL(test.url, test.base);
   1.220 +      } else {
   1.221 +        url = new URL(test.url);
   1.222 +      }
   1.223 +    } catch(e) {
   1.224 +      error = true;
   1.225 +    }
   1.226 +
   1.227 +    is(test.error, error, "Error creating URL");
   1.228 +    if (test.error) {
   1.229 +      continue;
   1.230 +    }
   1.231 +
   1.232 +    if ('href' in test) is(url.href, test.href, "href");
   1.233 +    if ('origin' in test) is(url.origin, test.origin, "origin");
   1.234 +    if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
   1.235 +    if ('username' in test) is(url.username, test.username, "username");
   1.236 +    if ('password' in test) is(url.password, test.password, "password");
   1.237 +    if ('host' in test) is(url.host, test.host, "host");
   1.238 +    if ('hostname' in test) is(url.hostname, test.hostname, "hostname");
   1.239 +    if ('port' in test) is(url.port, test.port, "port");
   1.240 +    if ('pathname' in test) is(url.pathname, test.pathname, "pathname");
   1.241 +    if ('search' in test) is(url.search, test.search, "search");
   1.242 +    if ('hash' in test) is(url.hash, test.hash, "hash");
   1.243 +
   1.244 +    url = new URL('https://www.example.net/what#foo?bar');
   1.245 +    ok(url, "Url exists!");
   1.246 +
   1.247 +    if ('href' in test) url.href = test.href;
   1.248 +    if ('protocol' in test) url.protocol = test.protocol;
   1.249 +    if ('username' in test && test.username) url.username = test.username;
   1.250 +    if ('password' in test && test.password) url.password = test.password;
   1.251 +    if ('host' in test) url.host = test.host;
   1.252 +    if ('hostname' in test) url.hostname = test.hostname;
   1.253 +    if ('port' in test) url.port = test.port;
   1.254 +    if ('pathname' in test) url.pathname = test.pathname;
   1.255 +    if ('search' in test) url.search = test.search;
   1.256 +    if ('hash' in test) url.hash = test.hash;
   1.257 +
   1.258 +    if ('href' in test) is(url.href, test.href, "href");
   1.259 +    if ('origin' in test) is(url.origin, test.origin, "origin");
   1.260 +    if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
   1.261 +    if ('username' in test) is(url.username, test.username, "username");
   1.262 +    if ('password' in test) is(url.password, test.password, "password");
   1.263 +    if ('host' in test) is(url.host, test.host, "host");
   1.264 +    if ('hostname' in test) is(test.hostname, url.hostname, "hostname");
   1.265 +    if ('port' in test) is(test.port, url.port, "port");
   1.266 +    if ('pathname' in test) is(test.pathname, url.pathname, "pathname");
   1.267 +    if ('search' in test) is(test.search, url.search, "search");
   1.268 +    if ('hash' in test) is(test.hash, url.hash, "hash");
   1.269 +
   1.270 +    if ('href' in test) is (test.href, url + '', 'stringify works');
   1.271 +  }
   1.272 +
   1.273 +  postMessage({type: 'finish' });
   1.274 +}
   1.275 +

mercurial