dom/base/test/test_url.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     2 <!DOCTYPE HTML>
     3 <html>
     4 <head>
     5   <meta charset="utf-8">
     6   <title>Test URL API</title>
     7   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     8   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     9 </head>
    10 <body>
    11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887364">Mozilla Bug 887364</a>
    12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=991471">Mozilla Bug 991471</a>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=996055">Mozilla Bug 996055</a>
    14 <p id="display"></p>
    15 <div id="content" style="display: none">
    16   <iframe name="x" id="x"></iframe>
    17   <iframe name="y" id="y"></iframe>
    18 </div>
    19 <pre id="test">
    20 </pre>
    21   <script type="application/javascript">
    23   /** Test for Bug 887364 **/
    24   ok("URL" in window, "window.URL exists");
    26   var tests = [
    27     { url: 'http://www.abc.com',
    28       base: undefined,
    29       error: false,
    30       href: 'http://www.abc.com/',
    31       origin: 'http://www.abc.com',
    32       protocol: 'http:',
    33       username: '',
    34       password: '',
    35       host: 'www.abc.com',
    36       hostname: 'www.abc.com',
    37       port: '',
    38       pathname: '/',
    39       search: '',
    40       hash: ''
    41     },
    42     { url: 'ftp://auser:apw@www.abc.com',
    43       base: undefined,
    44       error: false,
    45       href: 'ftp://auser:apw@www.abc.com/',
    46       origin: 'ftp://www.abc.com',
    47       protocol: 'ftp:',
    48       username: 'auser',
    49       password: 'apw',
    50       host: 'www.abc.com',
    51       hostname: 'www.abc.com',
    52       port: '',
    53       pathname: '/',
    54       search: '',
    55       hash: ''
    56     },
    57     { url: 'http://www.abc.com:90/apath/',
    58       base: undefined,
    59       error: false,
    60       href: 'http://www.abc.com:90/apath/',
    61       origin: 'http://www.abc.com:90',
    62       protocol: 'http:',
    63       username: '',
    64       password: '',
    65       host: 'www.abc.com:90',
    66       hostname: 'www.abc.com',
    67       port: '90',
    68       pathname: '/apath/',
    69       search: '',
    70       hash: ''
    71     },
    72     { url: 'http://www.abc.com/apath/afile.txt#ahash',
    73       base: undefined,
    74       error: false,
    75       href: 'http://www.abc.com/apath/afile.txt#ahash',
    76       origin: 'http://www.abc.com',
    77       protocol: 'http:',
    78       username: '',
    79       password: '',
    80       host: 'www.abc.com',
    81       hostname: 'www.abc.com',
    82       port: '',
    83       pathname: '/apath/afile.txt',
    84       search: '',
    85       hash: '#ahash'
    86     },
    87     { url: 'http://example.com/?test#hash',
    88       base: undefined,
    89       error: false,
    90       href: 'http://example.com/?test#hash',
    91       origin: 'http://example.com',
    92       protocol: 'http:',
    93       username: '',
    94       password: '',
    95       host: 'example.com',
    96       hostname: 'example.com',
    97       port: '',
    98       pathname: '/',
    99       search: '?test',
   100       hash: '#hash'
   101     },
   102     { url: 'http://example.com/?test',
   103       base: undefined,
   104       error: false,
   105       href: 'http://example.com/?test',
   106       origin: 'http://example.com',
   107       protocol: 'http:',
   108       username: '',
   109       password: '',
   110       host: 'example.com',
   111       hostname: 'example.com',
   112       port: '',
   113       pathname: '/',
   114       search: '?test',
   115       hash: ''
   116     },
   117     { url: 'http://example.com/carrot#question%3f',
   118       base: undefined,
   119       error: false,
   120       hash: '#question?'
   121     },
   122     { url: 'https://example.com:4443?',
   123       base: undefined,
   124       error: false,
   125       protocol: 'https:',
   126       port: '4443',
   127       pathname: '/',
   128       hash: '',
   129       search: ''
   130     },
   131     { url: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
   132       base: undefined,
   133       error: false,
   134       href: 'http://www.abc.com/apath/afile.txt#ahash?asearch',
   135       protocol: 'http:',
   136       pathname: '/apath/afile.txt',
   137       hash: '#ahash?asearch',
   138       search: ''
   139     },
   140     { url: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
   141       base: undefined,
   142       error: false,
   143       href: 'http://www.abc.com/apath/afile.txt?asearch#ahash',
   144       protocol: 'http:',
   145       pathname: '/apath/afile.txt',
   146       hash: '#ahash',
   147       search: '?asearch'
   148     },
   149     { url: 'http://abc.com/apath/afile.txt?#ahash',
   150       base: undefined,
   151       error: false,
   152       pathname: '/apath/afile.txt',
   153       hash: '#ahash',
   154       search: ''
   155     },
   156     { url: 'http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash',
   157       base: undefined,
   158       error: false,
   159       protocol: 'http:',
   160       username: 'auser',
   161       password: 'apassword',
   162       host: 'www.abc.com:90',
   163       hostname: 'www.abc.com',
   164       port: '90',
   165       pathname: '/apath/afile.txt',
   166       hash: '#ahash',
   167       search: '?asearch',
   168       origin: 'http://www.abc.com:90'
   169     },
   171     { url: '/foo#bar',
   172       base: 'www.test.org',
   173       error: true,
   174     },
   175     { url: '/foo#bar',
   176       base: null,
   177       error: true,
   178     },
   179     { url: '/foo#bar',
   180       base: 42,
   181       error: true,
   182     },
   183     { url: 'ftp://ftp.something.net',
   184       base: undefined,
   185       error: false,
   186       protocol: 'ftp:',
   187     },
   188     { url: 'file:///tmp/file',
   189       base: undefined,
   190       error: false,
   191       protocol: 'file:',
   192     },
   193     { url: 'gopher://gopher.something.net',
   194       base: undefined,
   195       error: false,
   196       protocol: 'gopher:',
   197     },
   198     { url: 'ws://ws.something.net',
   199       base: undefined,
   200       error: false,
   201       protocol: 'ws:',
   202     },
   203     { url: 'wss://ws.something.net',
   204       base: undefined,
   205       error: false,
   206       protocol: 'wss:',
   207     },
   208     { url: 'foo://foo.something.net',
   209       base: undefined,
   210       error: false,
   211       protocol: 'foo:',
   212     },
   213   ];
   215   while(tests.length) {
   216     var test = tests.shift();
   218     var error = false;
   219     var url;
   220     try {
   221       if (test.base) {
   222         url = new URL(test.url, test.base);
   223       } else {
   224         url = new URL(test.url);
   225       }
   226     } catch(e) {
   227       error = true;
   228     }
   230     is(test.error, error, "Error creating URL");
   231     if (test.error) {
   232       continue;
   233     }
   235     if ('href' in test) is(url.href, test.href, "href");
   236     if ('origin' in test) is(url.origin, test.origin, "origin");
   237     if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
   238     if ('username' in test) is(url.username, test.username, "username");
   239     if ('password' in test) is(url.password, test.password, "password");
   240     if ('host' in test) is(url.host, test.host, "host");
   241     if ('hostname' in test) is(url.hostname, test.hostname, "hostname");
   242     if ('port' in test) is(url.port, test.port, "port");
   243     if ('pathname' in test) is(url.pathname, test.pathname, "pathname");
   244     if ('search' in test) is(url.search, test.search, "search");
   245     if ('hash' in test) is(url.hash, test.hash, "hash");
   247     url = new URL('https://www.example.net/what#foo?bar');
   248     ok(url, "Url exists!");
   250     if ('href' in test) url.href = test.href;
   251     if ('protocol' in test) url.protocol = test.protocol;
   252     if ('username' in test && test.username) url.username = test.username;
   253     if ('password' in test && test.password) url.password = test.password;
   254     if ('host' in test) url.host = test.host;
   255     if ('hostname' in test) url.hostname = test.hostname;
   256     if ('port' in test) url.port = test.port;
   257     if ('pathname' in test) url.pathname = test.pathname;
   258     if ('search' in test) url.search = test.search;
   259     if ('hash' in test) url.hash = test.hash;
   261     if ('href' in test) is(url.href, test.href, "href");
   262     if ('origin' in test) is(url.origin, test.origin, "origin");
   263     if ('protocol' in test) is(url.protocol, test.protocol, "protocol");
   264     if ('username' in test) is(url.username, test.username, "username");
   265     if ('password' in test) is(url.password, test.password, "password");
   266     if ('host' in test) is(url.host, test.host, "host");
   267     if ('hostname' in test) is(test.hostname, url.hostname, "hostname");
   268     if ('port' in test) is(test.port, url.port, "port");
   269     if ('pathname' in test) is(test.pathname, url.pathname, "pathname");
   270     if ('search' in test) is(test.search, url.search, "search");
   271     if ('hash' in test) is(test.hash, url.hash, "hash");
   273     if ('href' in test) is (test.href, url + '', 'stringify works');
   274   }
   276   </script>
   278   <script>
   279     /** Test for Bug 991471 **/
   280     var url = new URL("http://localhost/");
   281     url.hostname = "";
   282     url.username = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt";
   283     url.hostname = "www.mozilla.org";
   284     url.username = "";
   285     url.hostname = "www.mozilla.org";
   286     is(url.href, "http://www.mozilla.org/", "No parsing error with empty host");
   287   </script>
   289   <script>
   290     /** Test for Bug 996055 **/
   291     var url = new URL("http://localhost/");
   292     url.hostname = "";
   293     is(url.href, "http://localhost/", "Empty hostname is ignored");
   294   </script>
   295 </body>
   296 </html>

mercurial