1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Tests of URI encoding and decoding.
30 assertEquals("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'();/?:@&=+$,#",
31 encodeURI("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'();/?:@&=+$,#"));
34 var s1 = String.fromCharCode(cc1);
36 var s2 = String.fromCharCode(cc2);
38 var s3 = String.fromCharCode(cc3);
40 var s4 = String.fromCharCode(cc4);
42 var s5 = String.fromCharCode(cc5);
44 var s6 = String.fromCharCode(cc6);
46 var s7 = String.fromCharCode(cc7);
49 var s8 = String.fromCharCode(cc8_1)+String.fromCharCode(cc8_2);
52 var s9 = String.fromCharCode(cc9_1)+String.fromCharCode(cc9_2);
54 var s10 = String.fromCharCode(cc10);
56 assertEquals('%7D', encodeURI(s1));
57 assertEquals('%00', encodeURI(s2));
58 assertEquals('%C2%80', encodeURI(s3));
59 assertEquals('%D5%95', encodeURI(s4));
60 assertEquals('%DF%BF', encodeURI(s5));
61 assertEquals('%E0%A0%80', encodeURI(s6));
62 assertEquals('%EA%BB%AE', encodeURI(s7));
63 assertEquals('%F0%90%80%80', encodeURI(s8));
64 assertEquals('%F4%8F%BF%BF', encodeURI(s9));
65 assertEquals('%EE%80%80', encodeURI(s10));
67 assertEquals(cc1, decodeURI(encodeURI(s1)).charCodeAt(0));
68 assertEquals(cc2, decodeURI(encodeURI(s2)).charCodeAt(0));
69 assertEquals(cc3, decodeURI(encodeURI(s3)).charCodeAt(0));
70 assertEquals(cc4, decodeURI(encodeURI(s4)).charCodeAt(0));
71 assertEquals(cc5, decodeURI(encodeURI(s5)).charCodeAt(0));
72 assertEquals(cc6, decodeURI(encodeURI(s6)).charCodeAt(0));
73 assertEquals(cc7, decodeURI(encodeURI(s7)).charCodeAt(0));
74 assertEquals(cc8_1, decodeURI(encodeURI(s8)).charCodeAt(0));
75 assertEquals(cc8_2, decodeURI(encodeURI(s8)).charCodeAt(1));
76 assertEquals(cc9_1, decodeURI(encodeURI(s9)).charCodeAt(0));
77 assertEquals(cc9_2, decodeURI(encodeURI(s9)).charCodeAt(1));
78 assertEquals(cc10, decodeURI(encodeURI(s10)).charCodeAt(0));
80 assertEquals("", decodeURI(""));
81 assertEquals("", encodeURI(""));
83 function test(string) {
84 assertEquals(string, decodeURI(encodeURI(string)));
87 test("\u1234\u0123\uabcd");
89 test("ab<\u1234\u0123");
90 test("ab\u1234<\u0123");