• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// URI encoding
16
17// This test will not pass on FLOAT32 due to precision issues
18
19function checkEncodeURIParseError (str)
20{
21  try {
22    encodeURI (str);
23    assert (false);
24  } catch(e) {
25    assert(e instanceof URIError);
26  }
27}
28
29assert (encodeURI ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f") ===
30        "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F");
31assert (encodeURI ("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f") ===
32        "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F");
33assert (encodeURI (" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN") ===
34         "%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJKLMN");
35assert (encodeURI ("OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}\x7F") ===
36         "OPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7F");
37
38assert (encodeURIComponent ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f") ===
39        "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F");
40assert (encodeURIComponent ("\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f") ===
41        "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F");
42assert (encodeURIComponent (" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN") ===
43        "%20!%22%23%24%25%26'()*%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMN");
44assert (encodeURIComponent ("OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}\x7F") ===
45        "OPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7F");
46
47assert (encodeURI ("\xe9") == "%C3%A9");
48assert (encodeURI ("\ud7ff") == "%ED%9F%BF");
49assert (encodeURI ("\ue000") == "%EE%80%80");
50assert (encodeURI ("\ud800\udc00") == "%F0%90%80%80");
51assert (encodeURI (String.fromCharCode(0xd800, 0xdc00)) == "%F0%90%80%80");
52
53checkEncodeURIParseError ("\ud800");
54checkEncodeURIParseError ("\udfff");
55
56// URI decoding
57
58function checkDecodeURIParseError (str)
59{
60  try {
61    decodeURI (str);
62    assert (false);
63  } catch(e) {
64    assert(e instanceof URIError);
65  }
66}
67
68assert (decodeURI ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F") ===
69        "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
70assert (decodeURI ("%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F") ===
71        "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f");
72assert (decodeURI ("%20%21%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f") ===
73        " !\"%23%24%%26'()*%2b%2c-.%2f");
74assert (decodeURI ("%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f") ===
75        "0123456789%3a%3b<%3d>%3f");
76assert (decodeURI ("%40%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f") ===
77        "%40ABCDEFGHIJKLMNO");
78assert (decodeURI ("%50%51%52%53%54%55%56%57%58%59%5a%5b%5c%5d%5e%5f") ===
79        "PQRSTUVWXYZ[\\]^_");
80assert (decodeURI ("%60%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f") ===
81        "`abcdefghijklmno");
82assert (decodeURI ("%70%71%72%73%74%75%76%77%78%79%7a%7b%7c%7d%7e") ===
83        "pqrstuvwxyz{|}~");
84
85assert (decodeURIComponent ("%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F") ===
86        "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f");
87assert (decodeURIComponent ("%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F") ===
88        "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f");
89assert (decodeURIComponent ("%20%21%22%23%24%25%26%27%28%29%2a%2b%2c%2d%2e%2f") ===
90        " !\"#$%&'()*+,-./");
91assert (decodeURIComponent ("%30%31%32%33%34%35%36%37%38%39%3a%3b%3c%3d%3e%3f") ===
92        "0123456789:;<=>?");
93assert (decodeURIComponent ("%40%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f") ===
94        "@ABCDEFGHIJKLMNO");
95assert (decodeURIComponent ("%50%51%52%53%54%55%56%57%58%59%5a%5b%5c%5d%5e%5f") ===
96        "PQRSTUVWXYZ[\\]^_");
97assert (decodeURIComponent ("%60%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f") ===
98        "`abcdefghijklmno");
99assert (decodeURIComponent ("%70%71%72%73%74%75%76%77%78%79%7a%7b%7c%7d%7e") ===
100        "pqrstuvwxyz{|}~");
101
102assert (decodeURI ("%6A%6B%6C%6D%6E%6F") === "jklmno");
103assert (decodeURI ("%C3%A9") === "\xe9");
104assert (decodeURI ("%e2%b1%a5") === "\u2c65");
105assert (decodeURI ("%f0%90%90%a8") === "\ud801\udc28");
106
107checkDecodeURIParseError ("13%");
108checkDecodeURIParseError ("%0g");
109checkDecodeURIParseError ("%1G");
110checkDecodeURIParseError ("%a");
111checkDecodeURIParseError ("%c1%81");
112checkDecodeURIParseError ("a%80b");
113checkDecodeURIParseError ("%f4%90%80%80");
114checkDecodeURIParseError ("%ed%a0%80");
115checkDecodeURIParseError ("%ed%b0%80");
116checkDecodeURIParseError ("%fa%81%82%83%84");
117checkDecodeURIParseError ("%fd%81%82%83%84%85");
118
119// Coerce (automatic toString()) tests
120
121assert (decodeURI ([1, 2, 3]) === "1,2,3");
122assert (decodeURI ({ x:1 }) === "[object Object]");
123assert (encodeURI (void 0) === "undefined");
124assert (encodeURI (216.000e1) === "2160");
125
126// Combining surrogate fragments
127
128assert (decodeURI("\ud800\udc00 \ud800 \udc00") === "\ud800\udc00 \ud800 \udc00");
129assert (decodeURI("%f0%90%80%80") === "\ud800\udc00");
130assert (decodeURI("\ud800%f0%90%80%80\ud800") === "\ud800\ud800\udc00\ud800");
131assert (decodeURI("\udc00%f0%90%80%80\udc00") === "\udc00\ud800\udc00\udc00");
132
133checkDecodeURIParseError ("\ud800%ed%b0%80");
134checkDecodeURIParseError ("%ed%a0%80\udc00");
135