1 require 'abstract_unit'
3 class QueryStringParsingTest
< ActionController
::IntegrationTest
4 class TestController
< ActionController
::Base
6 attr_accessor
:last_query_parameters
10 self.class.last_query_parameters
= request
.query_parameters
16 TestController
.last_query_parameters
= nil
19 test
"query string" do
21 {"action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
22 "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
26 test
"deep query string" do
28 {'x' => {'y' => {'z' => '10'}}},
33 test
"deep query string with array" do
34 assert_parses({'x' => {'y' => {'z' => ['10']}}}, 'x[y][z][]=10')
35 assert_parses({'x' => {'y' => {'z' => ['10', '5']}}}, 'x[y][z][]=10&x[y][z][]=5')
38 test
"deep query string with array of hash" do
39 assert_parses({'x' => {'y' => [{'z' => '10'}]}}, 'x[y][][z]=10')
40 assert_parses({'x' => {'y' => [{'z' => '10', 'w' => '10'}]}}, 'x[y][][z]=10&x[y][][w]=10')
41 assert_parses({'x' => {'y' => [{'z' => '10', 'v' => {'w' => '10'}}]}}, 'x[y][][z]=10&x[y][][v][w]=10')
44 test
"deep query string with array of hashes with one pair" do
45 assert_parses({'x' => {'y' => [{'z' => '10'}, {'z' => '20'}]}}, 'x[y][][z]=10&x[y][][z]=20')
48 test
"deep query string with array of hashes with multiple pairs" do
50 {'x' => {'y' => [{'z' => '10', 'w' => 'a'}, {'z' => '20', 'w' => 'b'}]}},
51 'x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b'
55 test
"query string with nil" do
57 { "action" => "create_customer", "full_name" => ''},
58 "action=create_customer&full_name="
62 test
"query string with array" do
64 { "action" => "create_customer", "selected" => ["1", "2", "3"]},
65 "action=create_customer&selected[]=1&selected[]=2&selected[]=3"
69 test
"query string with amps" do
71 { "action" => "create_customer", "name" => "Don't & Does"},
72 "action=create_customer&name=Don%27t+%26+Does"
76 test
"query string with many equal" do
78 { "action" => "create_customer", "full_name" => "abc=def=ghi"},
79 "action=create_customer&full_name=abc=def=ghi"
83 test
"query string without equal" do
84 assert_parses({ "action" => nil }, "action")
87 test
"query string with empty key" do
89 { "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
90 "action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
94 test
"query string with many ampersands" do
96 { "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
97 "&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
101 test
"unbalanced query string with array" do
103 {'location' => ["1", "2"], 'age_group' => ["2"]},
104 "location[]=1&location[]=2&age_group[]=2"
109 def assert_parses(expected
, actual
)
110 with_routing
do |set
|
112 map
.connect
':action', :controller => "query_string_parsing_test/test"
117 assert_equal(expected
, TestController
.last_query_parameters
)