Skip to content

Instantly share code, notes, and snippets.

@mtodd
Created January 27, 2009 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtodd/53568 to your computer and use it in GitHub Desktop.
Save mtodd/53568 to your computer and use it in GitHub Desktop.
# Rack Nested Params Parser Spec
Rack::Utils.parse_query("x[y][z]=10").
should.equal "x" => {"y" => {"z" => "10"}}
Rack::Utils.parse_query("x[y][z][]=10").
should.equal "x" => {"y" => {"z" => ["10"]}}
Rack::Utils.parse_query("x[y][z][]=10&x[y][z][]=5").
should.equal "x" => {"y" => {"z" => ["10", "5"]}}
Rack::Utils.parse_query("x[y][][z]=10").
should.equal "x" => {"y" => [{"z" => "10"}]}
Rack::Utils.parse_query("x[y][][z]=10&x[y][][w]=10").
should.equal "x" => {"y" => [{"z" => "10", "w" => "10"}]}
Rack::Utils.parse_query("x[y][][v][w]=10").
should.equal "x" => {"y" => [{"v" => {"w" => "10"}}]}
Rack::Utils.parse_query("x[y][][z]=10&x[y][][v][w]=10").
should.equal "x" => {"y" => [{"z" => "10", "v" => {"w" => "10"}}]}
Rack::Utils.parse_query("x[y][][z]=10&x[y][][z]=20").
should.equal "x" => {"y" => [{"z" => "10"}, {"z" => "20"}]}
Rack::Utils.parse_query("x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b").
should.equal "x" => {"y" => [{"z" => "10", "w" => "a"}, {"z" => "20", "w" => "b"}]}
Rack::Utils.parse_query("foo=bar&baz=").
should.equal "foo" => "bar", "baz" => ""
Rack::Utils.parse_query("foo=bar").
should.equal "foo" => "bar"
Rack::Utils.parse_query("foo=bar&foo=baz").
should.equal "foo" => ["bar", "baz"]
Rack::Utils.parse_query("foo[]=bar").
should.equal "foo" => ["bar"]
Rack::Utils.parse_query("foo[]=bar&foo[]=baz").
should.equal "foo" => ["bar", "baz"]
Rack::Utils.parse_query("foo=bar&baz[]=1&baz[]=2&baz[]=3").
should.equal "foo" => "bar", "baz" => ["1", "2", "3"]
Rack::Utils.parse_query("foo[]=bar&baz[]=1&baz[]=2&baz[]=3").
should.equal "foo" => ["bar"], "baz" => ["1", "2", "3"]
Rack::Utils.parse_query("foo[bar]=1&foo[]=1").should.raise TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment