@@ -11,7 +11,7 @@ const {
1111/* eslint-disable */
1212var params ; // Strict mode fix for WPT.
1313/* WPT Refs:
14- https://github.com/w3c/web-platform-tests/blob/e94c604916 /url/urlsearchparams-constructor.html
14+ https://github.com/w3c/web-platform-tests/blob/54c3502d7b /url/urlsearchparams-constructor.html
1515 License: http://www.w3.org/Consortium/Legal/2008/04-testsuite-copyright.html
1616*/
1717test ( function ( ) {
@@ -87,6 +87,17 @@ test(function() {
8787 assert_equals ( params . get ( 'a b' ) , 'c' ) ;
8888} , 'Parse +' ) ;
8989
90+ test ( function ( ) {
91+ const testValue = '+15555555555' ;
92+ const params = new URLSearchParams ( ) ;
93+ params . set ( 'query' , testValue ) ;
94+ var newParams = new URLSearchParams ( params . toString ( ) ) ;
95+
96+ assert_equals ( params . toString ( ) , 'query=%2B15555555555' ) ;
97+ assert_equals ( params . get ( 'query' ) , testValue ) ;
98+ assert_equals ( newParams . get ( 'query' ) , testValue ) ;
99+ } , 'Parse encoded +' ) ;
100+
90101test ( function ( ) {
91102 var params = new URLSearchParams ( 'a=b c' ) ;
92103 assert_equals ( params . get ( 'a' ) , 'b c' ) ;
@@ -156,7 +167,8 @@ test(function() {
156167[
157168 { "input" : { "+" : "%C2" } , "output" : [ [ "+" , "%C2" ] ] , "name" : "object with +" } ,
158169 { "input" : { c : "x" , a : "?" } , "output" : [ [ "c" , "x" ] , [ "a" , "?" ] ] , "name" : "object with two keys" } ,
159- { "input" : [ [ "c" , "x" ] , [ "a" , "?" ] ] , "output" : [ [ "c" , "x" ] , [ "a" , "?" ] ] , "name" : "array with two keys" }
170+ { "input" : [ [ "c" , "x" ] , [ "a" , "?" ] ] , "output" : [ [ "c" , "x" ] , [ "a" , "?" ] ] , "name" : "array with two keys" } ,
171+ { "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" }
160172] . forEach ( ( val ) => {
161173 test ( ( ) => {
162174 let params = new URLSearchParams ( val . input ) ,
@@ -179,12 +191,12 @@ test(() => {
179191/* eslint-enable */
180192
181193// Tests below are not from WPT.
182- {
183- // assert.throws (() => {
184- // new URLSearchParams( {
185- // toString() { throw new TypeError('Illegal invocation'); }
186- // });
187- // }, TypeError );
194+ function makeIterableFunc ( array ) {
195+ return Object . assign ( ( ) => { } , {
196+ [ Symbol . iterator ] ( ) {
197+ return array [ Symbol . iterator ] ( ) ;
198+ }
199+ } ) ;
188200}
189201
190202{
@@ -200,17 +212,25 @@ test(() => {
200212 } ) ;
201213
202214 let params ;
203- // URLSearchParams constructor, undefined and null as argument
204215 params = new URLSearchParams ( undefined ) ;
205216 assert . strictEqual ( params . toString ( ) , '' ) ;
206217 params = new URLSearchParams ( null ) ;
207218 assert . strictEqual ( params . toString ( ) , '' ) ;
219+ params = new URLSearchParams (
220+ makeIterableFunc ( [ [ 'key' , 'val' ] , [ 'key2' , 'val2' ] ] )
221+ ) ;
222+ assert . strictEqual ( params . toString ( ) , 'key=val&key2=val2' ) ;
223+ params = new URLSearchParams (
224+ makeIterableFunc ( [ [ 'key' , 'val' ] , [ 'key2' , 'val2' ] ] . map ( makeIterableFunc ) )
225+ ) ;
226+ assert . strictEqual ( params . toString ( ) , 'key=val&key2=val2' ) ;
208227 assert . throws ( ( ) => new URLSearchParams ( [ [ 1 ] ] ) , tupleError ) ;
209228 assert . throws ( ( ) => new URLSearchParams ( [ [ 1 , 2 , 3 ] ] ) , tupleError ) ;
210229 assert . throws ( ( ) => new URLSearchParams ( { [ Symbol . iterator ] : 42 } ) ,
211230 iterableError ) ;
212231 assert . throws ( ( ) => new URLSearchParams ( [ { } ] ) , tupleError ) ;
213232 assert . throws ( ( ) => new URLSearchParams ( [ 'a' ] ) , tupleError ) ;
233+ assert . throws ( ( ) => new URLSearchParams ( [ null ] ) , tupleError ) ;
214234 assert . throws ( ( ) => new URLSearchParams ( [ { [ Symbol . iterator ] : 42 } ] ) ,
215235 tupleError ) ;
216236}
@@ -221,15 +241,14 @@ test(() => {
221241 valueOf ( ) { throw new Error ( 'valueOf' ) ; }
222242 } ;
223243 const sym = Symbol ( ) ;
224-
225- assert . throws ( ( ) => new URLSearchParams ( { a : obj } ) , / ^ E r r o r : t o S t r i n g $ / ) ;
226- assert . throws ( ( ) => new URLSearchParams ( [ [ 'a' , obj ] ] ) , / ^ E r r o r : t o S t r i n g $ / ) ;
227- assert . throws ( ( ) => new URLSearchParams ( sym ) ,
228- / ^ T y p e E r r o r : C a n n o t c o n v e r t a S y m b o l v a l u e t o a s t r i n g $ / ) ;
229- assert . throws ( ( ) => new URLSearchParams ( { a : sym } ) ,
230- / ^ T y p e E r r o r : C a n n o t c o n v e r t a S y m b o l v a l u e t o a s t r i n g $ / ) ;
231- assert . throws ( ( ) => new URLSearchParams ( [ [ sym , 'a' ] ] ) ,
232- / ^ T y p e E r r o r : C a n n o t c o n v e r t a S y m b o l v a l u e t o a s t r i n g $ / ) ;
233- assert . throws ( ( ) => new URLSearchParams ( [ [ 'a' , sym ] ] ) ,
234- / ^ T y p e E r r o r : C a n n o t c o n v e r t a S y m b o l v a l u e t o a s t r i n g $ / ) ;
244+ const toStringError = / ^ E r r o r : t o S t r i n g $ / ;
245+ const symbolError = / ^ T y p e E r r o r : C a n n o t c o n v e r t a S y m b o l v a l u e t o a s t r i n g $ / ;
246+
247+ assert . throws ( ( ) => new URLSearchParams ( { a : obj } ) , toStringError ) ;
248+ assert . throws ( ( ) => new URLSearchParams ( [ [ 'a' , obj ] ] ) , toStringError ) ;
249+ assert . throws ( ( ) => new URLSearchParams ( sym ) , symbolError ) ;
250+ assert . throws ( ( ) => new URLSearchParams ( { [ sym ] : 'a' } ) , symbolError ) ;
251+ assert . throws ( ( ) => new URLSearchParams ( { a : sym } ) , symbolError ) ;
252+ assert . throws ( ( ) => new URLSearchParams ( [ [ sym , 'a' ] ] ) , symbolError ) ;
253+ assert . throws ( ( ) => new URLSearchParams ( [ [ 'a' , sym ] ] ) , symbolError ) ;
235254}
0 commit comments