Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / test / controller / request / query_string_parsing_test.rb
1 require 'abstract_unit'
2
3 class QueryStringParsingTest < ActionController::IntegrationTest
4 class TestController < ActionController::Base
5 class << self
6 attr_accessor :last_query_parameters
7 end
8
9 def parse
10 self.class.last_query_parameters = request.query_parameters
11 head :ok
12 end
13 end
14
15 def teardown
16 TestController.last_query_parameters = nil
17 end
18
19 test "query string" do
20 assert_parses(
21 {"action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
22 "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
23 )
24 end
25
26 test "deep query string" do
27 assert_parses(
28 {'x' => {'y' => {'z' => '10'}}},
29 "x[y][z]=10"
30 )
31 end
32
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')
36 end
37
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')
42 end
43
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')
46 end
47
48 test "deep query string with array of hashes with multiple pairs" do
49 assert_parses(
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'
52 )
53 end
54
55 test "query string with nil" do
56 assert_parses(
57 { "action" => "create_customer", "full_name" => ''},
58 "action=create_customer&full_name="
59 )
60 end
61
62 test "query string with array" do
63 assert_parses(
64 { "action" => "create_customer", "selected" => ["1", "2", "3"]},
65 "action=create_customer&selected[]=1&selected[]=2&selected[]=3"
66 )
67 end
68
69 test "query string with amps" do
70 assert_parses(
71 { "action" => "create_customer", "name" => "Don't & Does"},
72 "action=create_customer&name=Don%27t+%26+Does"
73 )
74 end
75
76 test "query string with many equal" do
77 assert_parses(
78 { "action" => "create_customer", "full_name" => "abc=def=ghi"},
79 "action=create_customer&full_name=abc=def=ghi"
80 )
81 end
82
83 test "query string without equal" do
84 assert_parses({ "action" => nil }, "action")
85 end
86
87 test "query string with empty key" do
88 assert_parses(
89 { "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
90 "action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
91 )
92 end
93
94 test "query string with many ampersands" do
95 assert_parses(
96 { "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
97 "&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
98 )
99 end
100
101 test "unbalanced query string with array" do
102 assert_parses(
103 {'location' => ["1", "2"], 'age_group' => ["2"]},
104 "location[]=1&location[]=2&age_group[]=2"
105 )
106 end
107
108 private
109 def assert_parses(expected, actual)
110 with_routing do |set|
111 set.draw do |map|
112 map.connect ':action', :controller => "query_string_parsing_test/test"
113 end
114
115 get "/parse", actual
116 assert_response :ok
117 assert_equal(expected, TestController.last_query_parameters)
118 end
119 end
120 end