• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
4// met:
5//
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.
15//
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.
27
28// Tests of URI encoding and decoding.
29
30assertEquals("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'();/?:@&=+$,#",
31             encodeURI("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.!~*'();/?:@&=+$,#"));
32
33var cc1 = 0x007D;
34var s1 = String.fromCharCode(cc1);
35var cc2 = 0x0000;
36var s2 = String.fromCharCode(cc2);
37var cc3 = 0x0080;
38var s3 = String.fromCharCode(cc3);
39var cc4 = 0x0555;
40var s4 = String.fromCharCode(cc4);
41var cc5 = 0x07FF;
42var s5 = String.fromCharCode(cc5);
43var cc6 = 0x0800;
44var s6 = String.fromCharCode(cc6);
45var cc7 = 0xAEEE;
46var s7 = String.fromCharCode(cc7);
47var cc8_1 = 0xD800;
48var cc8_2 = 0xDC00;
49var s8 = String.fromCharCode(cc8_1)+String.fromCharCode(cc8_2);
50var cc9_1 = 0xDBFF;
51var cc9_2 = 0xDFFF;
52var s9 = String.fromCharCode(cc9_1)+String.fromCharCode(cc9_2);
53var cc10 = 0xE000;
54var s10 = String.fromCharCode(cc10);
55
56assertEquals('%7D', encodeURI(s1));
57assertEquals('%00', encodeURI(s2));
58assertEquals('%C2%80', encodeURI(s3));
59assertEquals('%D5%95', encodeURI(s4));
60assertEquals('%DF%BF', encodeURI(s5));
61assertEquals('%E0%A0%80', encodeURI(s6));
62assertEquals('%EA%BB%AE', encodeURI(s7));
63assertEquals('%F0%90%80%80', encodeURI(s8));
64assertEquals('%F4%8F%BF%BF', encodeURI(s9));
65assertEquals('%EE%80%80', encodeURI(s10));
66
67assertEquals(cc1, decodeURI(encodeURI(s1)).charCodeAt(0));
68assertEquals(cc2, decodeURI(encodeURI(s2)).charCodeAt(0));
69assertEquals(cc3, decodeURI(encodeURI(s3)).charCodeAt(0));
70assertEquals(cc4, decodeURI(encodeURI(s4)).charCodeAt(0));
71assertEquals(cc5, decodeURI(encodeURI(s5)).charCodeAt(0));
72assertEquals(cc6, decodeURI(encodeURI(s6)).charCodeAt(0));
73assertEquals(cc7, decodeURI(encodeURI(s7)).charCodeAt(0));
74assertEquals(cc8_1, decodeURI(encodeURI(s8)).charCodeAt(0));
75assertEquals(cc8_2, decodeURI(encodeURI(s8)).charCodeAt(1));
76assertEquals(cc9_1, decodeURI(encodeURI(s9)).charCodeAt(0));
77assertEquals(cc9_2, decodeURI(encodeURI(s9)).charCodeAt(1));
78assertEquals(cc10, decodeURI(encodeURI(s10)).charCodeAt(0));
79
80assertEquals("", decodeURI(""));
81assertEquals("", encodeURI(""));
82
83function test(string) {
84  assertEquals(string, decodeURI(encodeURI(string)));
85}
86
87test("\u1234\u0123\uabcd");
88test("abcd");
89test("ab<\u1234\u0123");
90test("ab\u1234<\u0123");
91