1test(function() { 2 var params = new URLSearchParams(); 3 assert_equals(params + '', ''); 4 params = new URLSearchParams(''); 5 assert_equals(params + '', ''); 6 params = new URLSearchParams('a=b'); 7 assert_equals(params + '', 'a=b'); 8 params = new URLSearchParams(params); 9 assert_equals(params + '', 'a=b'); 10}, 'Basic URLSearchParams construction'); 11 12test(function() { 13 var params = new URLSearchParams() 14 assert_equals(params.toString(), "") 15}, "URLSearchParams constructor, no arguments") 16 17test(function () { 18 var params = new URLSearchParams("?a=b") 19 assert_equals(params.toString(), "a=b") 20}, 'URLSearchParams constructor, remove leading "?"') 21 22test(() => { 23 var params = new URLSearchParams(DOMException); 24 assert_equals(params.toString(), "INDEX_SIZE_ERR=1&DOMSTRING_SIZE_ERR=2&HIERARCHY_REQUEST_ERR=3&WRONG_DOCUMENT_ERR=4&INVALID_CHARACTER_ERR=5&NO_DATA_ALLOWED_ERR=6&NO_MODIFICATION_ALLOWED_ERR=7&NOT_FOUND_ERR=8&NOT_SUPPORTED_ERR=9&INUSE_ATTRIBUTE_ERR=10&INVALID_STATE_ERR=11&SYNTAX_ERR=12&INVALID_MODIFICATION_ERR=13&NAMESPACE_ERR=14&INVALID_ACCESS_ERR=15&VALIDATION_ERR=16&TYPE_MISMATCH_ERR=17&SECURITY_ERR=18&NETWORK_ERR=19&ABORT_ERR=20&URL_MISMATCH_ERR=21"A_EXCEEDED_ERR=22&TIMEOUT_ERR=23&INVALID_NODE_TYPE_ERR=24&DATA_CLONE_ERR=25") 25 assert_throws(new TypeError(), () => new URLSearchParams(DOMException.prototype), 26 "Constructing a URLSearchParams from DOMException.prototype should throw due to branding checks"); 27}, "URLSearchParams constructor, DOMException as argument") 28 29test(() => { 30 var params = new URLSearchParams(''); 31 assert_true(params != null, 'constructor returned non-null value.'); 32 assert_equals(params.__proto__, URLSearchParams.prototype, 'expected URLSearchParams.prototype as prototype.'); 33}, "URLSearchParams constructor, empty string as argument") 34 35test(() => { 36 var params = new URLSearchParams({}); 37 assert_equals(params + '', ""); 38}, 'URLSearchParams constructor, {} as argument'); 39 40test(function() { 41 var params = new URLSearchParams('a=b'); 42 assert_true(params != null, 'constructor returned non-null value.'); 43 assert_true(params.has('a'), 'Search params object has name "a"'); 44 assert_false(params.has('b'), 'Search params object has not got name "b"'); 45 46 params = new URLSearchParams('a=b&c'); 47 assert_true(params != null, 'constructor returned non-null value.'); 48 assert_true(params.has('a'), 'Search params object has name "a"'); 49 assert_true(params.has('c'), 'Search params object has name "c"'); 50 51 params = new URLSearchParams('&a&&& &&&&&a+b=& c&m%c3%b8%c3%b8'); 52 assert_true(params != null, 'constructor returned non-null value.'); 53 assert_true(params.has('a'), 'Search params object has name "a"'); 54 assert_true(params.has('a b'), 'Search params object has name "a b"'); 55 assert_true(params.has(' '), 'Search params object has name " "'); 56 assert_false(params.has('c'), 'Search params object did not have the name "c"'); 57 assert_true(params.has(' c'), 'Search params object has name " c"'); 58 assert_true(params.has('møø'), 'Search params object has name "møø"'); 59}, 'URLSearchParams constructor, string.'); 60 61test(function() { 62 var seed = new URLSearchParams('a=b&c=d'); 63 var params = new URLSearchParams(seed); 64 assert_true(params != null, 'constructor returned non-null value.'); 65 assert_equals(params.get('a'), 'b'); 66 assert_equals(params.get('c'), 'd'); 67 assert_false(params.has('d')); 68 // The name-value pairs are copied when created; later updates 69 // should not be observable. 70 seed.append('e', 'f'); 71 assert_false(params.has('e')); 72 params.append('g', 'h'); 73 assert_false(seed.has('g')); 74}, 'URLSearchParams constructor, object.'); 75 76test(function() { 77 var params = new URLSearchParams('a=b+c'); 78 assert_equals(params.get('a'), 'b c'); 79 params = new URLSearchParams('a+b=c'); 80 assert_equals(params.get('a b'), 'c'); 81}, 'Parse +'); 82 83test(function() { 84 const testValue = '+15555555555'; 85 const params = new URLSearchParams(); 86 params.set('query', testValue); 87 var newParams = new URLSearchParams(params.toString()); 88 89 assert_equals(params.toString(), 'query=%2B15555555555'); 90 assert_equals(params.get('query'), testValue); 91 assert_equals(newParams.get('query'), testValue); 92}, 'Parse encoded +'); 93 94test(function() { 95 var params = new URLSearchParams('a=b c'); 96 assert_equals(params.get('a'), 'b c'); 97 params = new URLSearchParams('a b=c'); 98 assert_equals(params.get('a b'), 'c'); 99}, 'Parse space'); 100 101test(function() { 102 var params = new URLSearchParams('a=b%20c'); 103 assert_equals(params.get('a'), 'b c'); 104 params = new URLSearchParams('a%20b=c'); 105 assert_equals(params.get('a b'), 'c'); 106}, 'Parse %20'); 107 108test(function() { 109 var params = new URLSearchParams('a=b\0c'); 110 assert_equals(params.get('a'), 'b\0c'); 111 params = new URLSearchParams('a\0b=c'); 112 assert_equals(params.get('a\0b'), 'c'); 113}, 'Parse \\0'); 114 115test(function() { 116 var params = new URLSearchParams('a=b%00c'); 117 assert_equals(params.get('a'), 'b\0c'); 118 params = new URLSearchParams('a%00b=c'); 119 assert_equals(params.get('a\0b'), 'c'); 120}, 'Parse %00'); 121 122test(function() { 123 var params = new URLSearchParams('a=b\u2384'); 124 assert_equals(params.get('a'), 'b\u2384'); 125 params = new URLSearchParams('a\u2384b=c'); 126 assert_equals(params.get('a\u2384b'), 'c'); 127}, 'Parse \u2384'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384) 128 129test(function() { 130 var params = new URLSearchParams('a=b%e2%8e%84'); 131 assert_equals(params.get('a'), 'b\u2384'); 132 params = new URLSearchParams('a%e2%8e%84b=c'); 133 assert_equals(params.get('a\u2384b'), 'c'); 134}, 'Parse %e2%8e%84'); // Unicode Character 'COMPOSITION SYMBOL' (U+2384) 135 136test(function() { 137 var params = new URLSearchParams('a=b\uD83D\uDCA9c'); 138 assert_equals(params.get('a'), 'b\uD83D\uDCA9c'); 139 params = new URLSearchParams('a\uD83D\uDCA9b=c'); 140 assert_equals(params.get('a\uD83D\uDCA9b'), 'c'); 141}, 'Parse \uD83D\uDCA9'); // Unicode Character 'PILE OF POO' (U+1F4A9) 142 143test(function() { 144 var params = new URLSearchParams('a=b%f0%9f%92%a9c'); 145 assert_equals(params.get('a'), 'b\uD83D\uDCA9c'); 146 params = new URLSearchParams('a%f0%9f%92%a9b=c'); 147 assert_equals(params.get('a\uD83D\uDCA9b'), 'c'); 148}, 'Parse %f0%9f%92%a9'); // Unicode Character 'PILE OF POO' (U+1F4A9) 149 150test(function() { 151 var params = new URLSearchParams([]); 152 assert_true(params != null, 'constructor returned non-null value.'); 153 params = new URLSearchParams([['a', 'b'], ['c', 'd']]); 154 assert_equals(params.get("a"), "b"); 155 assert_equals(params.get("c"), "d"); 156 assert_throws(new TypeError(), function() { new URLSearchParams([[1]]); }); 157 assert_throws(new TypeError(), function() { new URLSearchParams([[1,2,3]]); }); 158}, "Constructor with sequence of sequences of strings"); 159 160[ 161 { "input": {"+": "%C2"}, "output": [["+", "%C2"]], "name": "object with +" }, 162 { "input": {c: "x", a: "?"}, "output": [["c", "x"], ["a", "?"]], "name": "object with two keys" }, 163 { "input": [["c", "x"], ["a", "?"]], "output": [["c", "x"], ["a", "?"]], "name": "array with two keys" }, 164 { "input": {"a\0b": "42", "c\uD83D": "23", "d\u1234": "foo"}, "output": [["a\0b", "42"], ["c\uFFFD", "23"], ["d\u1234", "foo"]], "name": "object with NULL, non-ASCII, and surrogate keys" } 165].forEach((val) => { 166 test(() => { 167 let params = new URLSearchParams(val.input), 168 i = 0 169 for (let param of params) { 170 assert_array_equals(param, val.output[i]) 171 i++ 172 } 173 }, "Construct with " + val.name) 174}) 175 176test(() => { 177 var params = new URLSearchParams() 178 params[Symbol.iterator] = function *() { 179 yield ["a", "b"] 180 } 181 let params2 = new URLSearchParams(params) 182 assert_equals(params2.get("a"), "b") 183}, "Custom [Symbol.iterator]") 184