OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf8"> |
| 5 <link rel="help" href="https://url.spec.whatwg.org/#dom-urlsearchparams-set"> |
| 6 <script src="../../resources/testharness.js"></script> |
| 7 <script src="../../resources/testharnessreport.js"></script> |
| 8 <script src="resources/testharness-extras.js"></script> |
| 9 <script> |
| 10 test(function() { |
| 11 var params = new URLSearchParams(); |
| 12 params.append('a', 'b c'); |
| 13 assert_equals(params + '', 'a=b+c'); |
| 14 params.delete('a'); |
| 15 params.append('a b', 'c'); |
| 16 assert_equals(params + '', 'a+b=c'); |
| 17 }, 'Serialize space'); |
| 18 |
| 19 test(function() { |
| 20 var params = new URLSearchParams(); |
| 21 params.append('a', ''); |
| 22 assert_equals(params + '', 'a='); |
| 23 params.append('a', ''); |
| 24 assert_equals(params + '', 'a=&a='); |
| 25 params.append('', 'b'); |
| 26 assert_equals(params + '', 'a=&a=&=b'); |
| 27 params.append('', ''); |
| 28 assert_equals(params + '', 'a=&a=&=b&='); |
| 29 params.append('', ''); |
| 30 assert_equals(params + '', 'a=&a=&=b&=&='); |
| 31 }, 'Serialize empty value'); |
| 32 |
| 33 test(function() { |
| 34 var params = new URLSearchParams(); |
| 35 params.append('', 'b'); |
| 36 assert_equals(params + '', '=b'); |
| 37 params.append('', 'b'); |
| 38 assert_equals(params + '', '=b&=b'); |
| 39 }, 'Serialize empty name'); |
| 40 |
| 41 test(function() { |
| 42 var params = new URLSearchParams(); |
| 43 params.append('', ''); |
| 44 assert_equals(params + '', '='); |
| 45 params.append('', ''); |
| 46 assert_equals(params + '', '=&='); |
| 47 }, 'Serialize empty name and value'); |
| 48 |
| 49 test(function() { |
| 50 var params = new URLSearchParams(); |
| 51 params.append('a', 'b+c'); |
| 52 assert_equals(params + '', 'a=b%2Bc'); |
| 53 params.delete('a'); |
| 54 params.append('a+b', 'c'); |
| 55 assert_equals(params + '', 'a%2Bb=c'); |
| 56 }, 'Serialize +'); |
| 57 |
| 58 test(function() { |
| 59 var params = new URLSearchParams(); |
| 60 params.append('=', 'a'); |
| 61 assert_equals(params + '', '%3D=a'); |
| 62 params.append('b', '='); |
| 63 assert_equals(params + '', '%3D=a&b=%3D'); |
| 64 }, 'Serialize ='); |
| 65 |
| 66 test(function() { |
| 67 var params = new URLSearchParams(); |
| 68 params.append('&', 'a'); |
| 69 assert_equals(params + '', '%26=a'); |
| 70 params.append('b', '&'); |
| 71 assert_equals(params + '', '%26=a&b=%26'); |
| 72 }, 'Serialize &'); |
| 73 |
| 74 function intToHex(i) { |
| 75 return (i<0x10 ? '0':'') + i.toString(16).toUpperCase(); |
| 76 } |
| 77 |
| 78 function urlEncodedSerialize(n) { |
| 79 if (n === 0x20) { |
| 80 return "\x2B"; |
| 81 } |
| 82 |
| 83 if (n === 0x2A || n === 0x2D || n === 0x2E || |
| 84 (0x30 <= n && n <= 0x39) || (0x41 <= n && n <= 0x5A) || |
| 85 n === 0x5F || (0x61 <= n && n <= 0x7A)) { |
| 86 return String.fromCharCode(n); |
| 87 } |
| 88 var bytes = (new TextEncoder()).encode(String.fromCharCode(n)); |
| 89 var ret = ""; |
| 90 for (var i = 0; i < bytes.length; i++) { |
| 91 ret += "%" + intToHex(bytes[i]); |
| 92 } |
| 93 return ret; |
| 94 } |
| 95 |
| 96 for (var i = 0x00; i < 0xFF; i++) { |
| 97 // Not all bytes can appear in valid UTF-8, so some bytes aren't covered. |
| 98 // TODO(mkwst): We fail to properly encode a few bytes: https://crbug.com/55
7063 |
| 99 test(function() { |
| 100 var params = new URLSearchParams(); |
| 101 params.append('' + i, String.fromCodePoint(i)); |
| 102 assert_equals(params + '', '' + i + '=' + urlEncodedSerialize(i)); |
| 103 }, "Serialize U+00" + intToHex(i) + " -> '" + String.fromCodePoint(i) + "'")
; |
| 104 } |
| 105 |
| 106 test(function() { |
| 107 var params = new URLSearchParams(); |
| 108 params.append('a', 'b%c'); |
| 109 assert_equals(params + '', 'a=b%25c'); |
| 110 params.delete('a'); |
| 111 params.append('a%b', 'c'); |
| 112 assert_equals(params + '', 'a%25b=c'); |
| 113 }, 'Serialize %'); |
| 114 |
| 115 test(function() { |
| 116 var params = new URLSearchParams(); |
| 117 params.append('a', 'b\0c'); |
| 118 assert_equals(params + '', 'a=b%00c'); |
| 119 params.delete('a'); |
| 120 params.append('a\0b', 'c'); |
| 121 assert_equals(params + '', 'a%00b=c'); |
| 122 }, 'Serialize \\0'); |
| 123 |
| 124 test(function() { |
| 125 var params = new URLSearchParams(); |
| 126 params.append('a', 'b\uD83D\uDCA9c'); |
| 127 assert_equals(params + '', 'a=b%F0%9F%92%A9c'); |
| 128 params.delete('a'); |
| 129 params.append('a\uD83D\uDCA9b', 'c'); |
| 130 assert_equals(params + '', 'a%F0%9F%92%A9b=c'); |
| 131 }, 'Serialize \uD83D\uDCA9'); // Unicode Character 'PILE OF POO' (U+1F4A9) |
| 132 |
| 133 test(function() { |
| 134 var params; |
| 135 params = new URLSearchParams('a=b&c=d&&e&&'); |
| 136 assert_equals(params.toString(), 'a=b&c=d&e='); |
| 137 params = new URLSearchParams('a = b &a=b&c=d%20'); |
| 138 assert_equals(params.toString(), 'a+=+b+&a=b&c=d+'); |
| 139 // The lone '=' _does_ survive the roundtrip. |
| 140 params = new URLSearchParams('a=&a=b'); |
| 141 assert_equals(params.toString(), 'a=&a=b'); |
| 142 }, 'URLSearchParams.toString'); |
| 143 </script> |
| 144 </head> |
| 145 </html> |
| 146 |
OLD | NEW |