@@ -2,6 +2,14 @@ import qs from "../lib";
22import { test , assert } from "vitest" ;
33import vm from "node:vm" ;
44
5+ function createWithNoPrototype ( properties ) {
6+ const noProto = Object . create ( null ) ;
7+ properties . forEach ( ( property ) => {
8+ noProto [ property . key ] = property . value ;
9+ } ) ;
10+ return noProto ;
11+ }
12+
513const foreignObject = vm . runInNewContext ( '({"foo": ["bar", "baz"]})' ) ;
614const qsNoMungeTestCases = [
715 [ "" , { } ] ,
@@ -19,15 +27,105 @@ const qsNoMungeTestCases = [
1927 ] ,
2028 [ "trololol=yes&lololo=no" , { trololol : "yes" , lololo : "no" } ] ,
2129] ;
30+ const qsTestCases = [
31+ [
32+ "__proto__=1" ,
33+ "__proto__=1" ,
34+ createWithNoPrototype ( [ { key : "__proto__" , value : "1" } ] ) ,
35+ ] ,
36+ [
37+ "__defineGetter__=asdf" ,
38+ "__defineGetter__=asdf" ,
39+ JSON . parse ( '{"__defineGetter__":"asdf"}' ) ,
40+ ] ,
41+ [
42+ "foo=918854443121279438895193" ,
43+ "foo=918854443121279438895193" ,
44+ { foo : "918854443121279438895193" } ,
45+ ] ,
46+ [ "foo=bar" , "foo=bar" , { foo : "bar" } ] ,
47+ [ "foo=bar&foo=quux" , "foo=bar&foo=quux" , { foo : [ "bar" , "quux" ] } ] ,
48+ [ "foo=1&bar=2" , "foo=1&bar=2" , { foo : "1" , bar : "2" } ] ,
49+ [
50+ "my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F" ,
51+ "my%20weird%20field=q1!2%22'w%245%267%2Fz8)%3F" ,
52+ { "my weird field" : "q1!2\"'w$5&7/z8)?" } ,
53+ ] ,
54+ [ "foo%3Dbaz=bar" , "foo%3Dbaz=bar" , { "foo=baz" : "bar" } ] ,
55+ [ "foo=baz=bar" , "foo=baz%3Dbar" , { foo : "baz=bar" } ] ,
56+ [
57+ "str=foo&arr=1&arr=2&arr=3&somenull=&undef=" ,
58+ "str=foo&arr=1&arr=2&arr=3&somenull=&undef=" ,
59+ {
60+ str : "foo" ,
61+ arr : [ "1" , "2" , "3" ] ,
62+ somenull : "" ,
63+ undef : "" ,
64+ } ,
65+ ] ,
66+ [ " foo = bar " , "%20foo%20=%20bar%20" , { " foo " : " bar " } ] ,
67+ [ "foo=%zx" , "foo=%25zx" , { foo : "%zx" } ] ,
68+ [ "foo=%EF%BF%BD" , "foo=%EF%BF%BD" , { foo : "\ufffd" } ] ,
69+ // See: https://github.com/joyent/node/issues/1707
70+ [
71+ "hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz" ,
72+ "hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz" ,
73+ {
74+ hasOwnProperty : "x" ,
75+ toString : "foo" ,
76+ valueOf : "bar" ,
77+ __defineGetter__ : "baz" ,
78+ } ,
79+ ] ,
80+ // See: https://github.com/joyent/node/issues/3058
81+ [ "foo&bar=baz" , "foo=&bar=baz" , { foo : "" , bar : "baz" } ] ,
82+ [ "a=b&c&d=e" , "a=b&c=&d=e" , { a : "b" , c : "" , d : "e" } ] ,
83+ [ "a=b&c=&d=e" , "a=b&c=&d=e" , { a : "b" , c : "" , d : "e" } ] ,
84+ [ "a=b&=c&d=e" , "a=b&=c&d=e" , { a : "b" , "" : "c" , d : "e" } ] ,
85+ [ "a=b&=&c=d" , "a=b&=&c=d" , { a : "b" , "" : "" , c : "d" } ] ,
86+ [ "&&foo=bar&&" , "foo=bar" , { foo : "bar" } ] ,
87+ [ "&" , "" , { } ] ,
88+ [ "&&&&" , "" , { } ] ,
89+ [ "&=&" , "=" , { "" : "" } ] ,
90+ [ "&=&=" , "=&=" , { "" : [ "" , "" ] } ] ,
91+ [ "=" , "=" , { "" : "" } ] ,
92+ [ "+" , "%20=" , { " " : "" } ] ,
93+ [ "+=" , "%20=" , { " " : "" } ] ,
94+ [ "+&" , "%20=" , { " " : "" } ] ,
95+ [ "=+" , "=%20" , { "" : " " } ] ,
96+ [ "+=&" , "%20=" , { " " : "" } ] ,
97+ [ "a&&b" , "a=&b=" , { a : "" , b : "" } ] ,
98+ [ "a=a&&b=b" , "a=a&b=b" , { a : "a" , b : "b" } ] ,
99+ [ "&a" , "a=" , { a : "" } ] ,
100+ [ "&=" , "=" , { "" : "" } ] ,
101+ [ "a&a&" , "a=&a=" , { a : [ "" , "" ] } ] ,
102+ [ "a&a&a&" , "a=&a=&a=" , { a : [ "" , "" , "" ] } ] ,
103+ [ "a&a&a&a&" , "a=&a=&a=&a=" , { a : [ "" , "" , "" , "" ] } ] ,
104+ [ "a=&a=value&a=" , "a=&a=value&a=" , { a : [ "" , "value" , "" ] } ] ,
105+ [ "foo+bar=baz+quux" , "foo%20bar=baz%20quux" , { "foo bar" : "baz quux" } ] ,
106+ [ "+foo=+bar" , "%20foo=%20bar" , { " foo" : " bar" } ] ,
107+ [ "a+" , "a%20=" , { "a " : "" } ] ,
108+ [ "=a+" , "=a%20" , { "" : "a " } ] ,
109+ [ "a+&" , "a%20=" , { "a " : "" } ] ,
110+ [ "=a+&" , "=a%20" , { "" : "a " } ] ,
111+ [ "%20+" , "%20%20=" , { " " : "" } ] ,
112+ [ "=%20+" , "=%20%20" , { "" : " " } ] ,
113+ [ "%20+&" , "%20%20=" , { " " : "" } ] ,
114+ [ "=%20+&" , "=%20%20" , { "" : " " } ] ,
115+ [ null , "" , { } ] ,
116+ [ undefined , "" , { } ] ,
117+ ] ;
22118
23119test ( "should parse the basics" , ( ) => {
24120 qsNoMungeTestCases . forEach ( ( t ) => {
25121 assert . deepEqual ( qs . parse ( t [ 0 ] ) , t [ 1 ] ) ;
26122 } ) ;
27123} ) ;
28124
29- test ( "should throw error on invalid type" , ( ) => {
30- assert . throws ( ( ) => qs . parse ( 5 ) , "Invalid Input" ) ;
125+ test ( "should succeed on node.js tests" , ( ) => {
126+ qsTestCases . forEach ( ( t ) => {
127+ assert . deepEqual ( qs . parse ( t [ 0 ] ) , t [ 2 ] , t [ 0 ] ) ;
128+ } ) ;
31129} ) ;
32130
33131test ( "handles & on first/last character" , ( ) => {
0 commit comments