2009-01-09 14:12:39 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class QueryStringParsingTest < ActionDispatch::IntegrationTest
|
2009-01-09 13:52:59 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_query_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_query_parameters = request.query_parameters
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
TestController.last_query_parameters = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string" do
|
|
|
|
assert_parses(
|
|
|
|
{"action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
|
|
|
|
"action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deep query string" do
|
|
|
|
assert_parses(
|
|
|
|
{'x' => {'y' => {'z' => '10'}}},
|
|
|
|
"x[y][z]=10"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deep query string with array" do
|
|
|
|
assert_parses({'x' => {'y' => {'z' => ['10']}}}, 'x[y][z][]=10')
|
|
|
|
assert_parses({'x' => {'y' => {'z' => ['10', '5']}}}, 'x[y][z][]=10&x[y][z][]=5')
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deep query string with array of hash" do
|
|
|
|
assert_parses({'x' => {'y' => [{'z' => '10'}]}}, 'x[y][][z]=10')
|
|
|
|
assert_parses({'x' => {'y' => [{'z' => '10', 'w' => '10'}]}}, 'x[y][][z]=10&x[y][][w]=10')
|
|
|
|
assert_parses({'x' => {'y' => [{'z' => '10', 'v' => {'w' => '10'}}]}}, 'x[y][][z]=10&x[y][][v][w]=10')
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deep query string with array of hashes with one pair" do
|
|
|
|
assert_parses({'x' => {'y' => [{'z' => '10'}, {'z' => '20'}]}}, 'x[y][][z]=10&x[y][][z]=20')
|
|
|
|
end
|
|
|
|
|
|
|
|
test "deep query string with array of hashes with multiple pairs" do
|
|
|
|
assert_parses(
|
|
|
|
{'x' => {'y' => [{'z' => '10', 'w' => 'a'}, {'z' => '20', 'w' => 'b'}]}},
|
|
|
|
'x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string with nil" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "full_name" => ''},
|
|
|
|
"action=create_customer&full_name="
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string with array" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "selected" => ["1", "2", "3"]},
|
|
|
|
"action=create_customer&selected[]=1&selected[]=2&selected[]=3"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string with amps" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "name" => "Don't & Does"},
|
|
|
|
"action=create_customer&name=Don%27t+%26+Does"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string with many equal" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "full_name" => "abc=def=ghi"},
|
|
|
|
"action=create_customer&full_name=abc=def=ghi"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string without equal" do
|
2012-05-30 18:13:03 -04:00
|
|
|
assert_parses({"action" => nil}, "action")
|
|
|
|
assert_parses({"action" => {"foo" => nil}}, "action[foo]")
|
|
|
|
assert_parses({"action" => {"foo" => { "bar" => nil }}}, "action[foo][bar]")
|
2013-01-08 17:26:04 -05:00
|
|
|
assert_parses({"action" => {"foo" => { "bar" => nil }}}, "action[foo][bar][]")
|
|
|
|
assert_parses({"action" => {"foo" => nil }}, "action[foo][]")
|
2012-05-30 18:13:03 -04:00
|
|
|
assert_parses({"action"=>{"foo"=>[{"bar"=>nil}]}}, "action[foo][][bar]")
|
2009-01-09 13:52:59 -05:00
|
|
|
end
|
|
|
|
|
2012-06-10 23:44:54 -04:00
|
|
|
def test_array_parses_without_nil
|
|
|
|
assert_parses({"action" => ['1']}, "action[]=1&action[]")
|
|
|
|
end
|
|
|
|
|
2009-01-09 13:52:59 -05:00
|
|
|
test "query string with empty key" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
|
|
|
|
"action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "query string with many ampersands" do
|
|
|
|
assert_parses(
|
|
|
|
{ "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
|
|
|
|
"&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unbalanced query string with array" do
|
|
|
|
assert_parses(
|
|
|
|
{'location' => ["1", "2"], 'age_group' => ["2"]},
|
|
|
|
"location[]=1&location[]=2&age_group[]=2"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-05-20 05:04:12 -04:00
|
|
|
test "ambiguous query string returns a bad request" do
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
get ':action', :to => ::QueryStringParsingTest::TestController
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/parse", nil, "QUERY_STRING" => "foo[]=bar&foo[4]=bar"
|
|
|
|
assert_response :bad_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-09 13:52:59 -05:00
|
|
|
private
|
|
|
|
def assert_parses(expected, actual)
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':action', :to => ::QueryStringParsingTest::TestController
|
2009-01-09 13:52:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
get "/parse", actual
|
|
|
|
assert_response :ok
|
2009-11-04 18:25:15 -05:00
|
|
|
assert_equal(expected, ::QueryStringParsingTest::TestController.last_query_parameters)
|
2009-01-09 13:52:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|