Skip to content
This repository
Browse code

Fix broken params_wrapper_test on 1.8.7 :bomb:

I have to keep in mind that 1.8.7 does *not* preserve the hash order.

Guys, let's move to use 1.9.2 in production!
  • Loading branch information...
commit 31f412fc975033a9293888e8618fc933097d1d35 1 parent 8bf5d68
Prem Sichanugrist authored May 03, 2011
116  actionpack/test/controller/params_wrapper_test.rb
@@ -4,8 +4,13 @@ module Admin; class User; end; end
4 4
 
5 5
 class ParamsWrapperTest < ActionController::TestCase
6 6
   class UsersController < ActionController::Base
7  
-    def test
8  
-      render :json => params.except(:controller, :action)
  7
+    class << self
  8
+      attr_accessor :last_parameters
  9
+    end
  10
+
  11
+    def parse
  12
+      self.class.last_parameters = request.params.except(:controller, :action)
  13
+      head :ok
9 14
     end
10 15
   end
11 16
 
@@ -14,11 +19,15 @@ class Person; end
14 19
 
15 20
   tests UsersController
16 21
 
  22
+  def teardown
  23
+    UsersController.last_parameters = nil
  24
+  end
  25
+
17 26
   def test_derivered_name_from_controller
18 27
     with_default_wrapper_options do
19 28
       @request.env['CONTENT_TYPE'] = 'application/json'
20  
-      post :test, { 'username' => 'sikachu' }
21  
-      assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body
  29
+      post :parse, { 'username' => 'sikachu' }
  30
+      assert_parameters({ 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
22 31
     end
23 32
   end
24 33
 
@@ -27,8 +36,8 @@ def test_specify_wrapper_name
27 36
       UsersController.wrap_parameters :person
28 37
 
29 38
       @request.env['CONTENT_TYPE'] = 'application/json'
30  
-      post :test, { 'username' => 'sikachu' }
31  
-      assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body
  39
+      post :parse, { 'username' => 'sikachu' }
  40
+      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
32 41
     end
33 42
   end
34 43
 
@@ -37,8 +46,8 @@ def test_specify_wrapper_model
37 46
       UsersController.wrap_parameters Person
38 47
 
39 48
       @request.env['CONTENT_TYPE'] = 'application/json'
40  
-      post :test, { 'username' => 'sikachu' }
41  
-      assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body
  49
+      post :parse, { 'username' => 'sikachu' }
  50
+      assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }})
42 51
     end
43 52
   end
44 53
 
@@ -47,8 +56,8 @@ def test_specify_only_option
47 56
       UsersController.wrap_parameters :only => :username
48 57
 
49 58
       @request.env['CONTENT_TYPE'] = 'application/json'
50  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
51  
-      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
  59
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  60
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
52 61
     end
53 62
   end
54 63
 
@@ -57,8 +66,8 @@ def test_specify_except_option
57 66
       UsersController.wrap_parameters :except => :title
58 67
 
59 68
       @request.env['CONTENT_TYPE'] = 'application/json'
60  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
61  
-      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
  69
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  70
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
62 71
     end
63 72
   end
64 73
 
@@ -67,16 +76,16 @@ def test_specify_both_wrapper_name_and_only_option
67 76
       UsersController.wrap_parameters :person, :only => :username
68 77
 
69 78
       @request.env['CONTENT_TYPE'] = 'application/json'
70  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
71  
-      assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body
  79
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  80
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
72 81
     end
73 82
   end
74 83
 
75 84
   def test_not_enabled_format
76 85
     with_default_wrapper_options do
77 86
       @request.env['CONTENT_TYPE'] = 'application/xml'
78  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
79  
-      assert_equal '{"username":"sikachu","title":"Developer"}', @response.body
  87
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  88
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
80 89
     end
81 90
   end
82 91
 
@@ -84,8 +93,8 @@ def test_wrap_parameters_false
84 93
     with_default_wrapper_options do
85 94
       UsersController.wrap_parameters false
86 95
       @request.env['CONTENT_TYPE'] = 'application/json'
87  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
88  
-      assert_equal '{"username":"sikachu","title":"Developer"}', @response.body
  96
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  97
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' })
89 98
     end
90 99
   end
91 100
 
@@ -94,32 +103,32 @@ def test_specify_format
94 103
       UsersController.wrap_parameters :format => :xml
95 104
 
96 105
       @request.env['CONTENT_TYPE'] = 'application/xml'
97  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
98  
-      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu","title":"Developer"}}', @response.body
  106
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  107
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }})
99 108
     end
100 109
   end
101 110
 
102 111
   def test_not_wrap_reserved_parameters
103 112
     with_default_wrapper_options do
104 113
       @request.env['CONTENT_TYPE'] = 'application/json'
105  
-      post :test, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu' }
106  
-      assert_equal '{"authenticity_token":"pwned","_method":"put","utf8":"&#9731;","username":"sikachu","user":{"username":"sikachu"}}', @response.body
  114
+      post :parse, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu' }
  115
+      assert_parameters({ 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
107 116
     end
108 117
   end
109 118
 
110 119
   def test_no_double_wrap_if_key_exists
111 120
     with_default_wrapper_options do
112 121
       @request.env['CONTENT_TYPE'] = 'application/json'
113  
-      post :test, { 'user' => { 'username' => 'sikachu' }}
114  
-      assert_equal '{"user":{"username":"sikachu"}}', @response.body
  122
+      post :parse, { 'user' => { 'username' => 'sikachu' }}
  123
+      assert_parameters({ 'user' => { 'username' => 'sikachu' }})
115 124
     end
116 125
   end
117 126
 
118 127
   def test_nested_params
119 128
     with_default_wrapper_options do
120 129
       @request.env['CONTENT_TYPE'] = 'application/json'
121  
-      post :test, { 'person' => { 'username' => 'sikachu' }}
122  
-      assert_equal '{"person":{"username":"sikachu"},"user":{"person":{"username":"sikachu"}}}', @response.body
  130
+      post :parse, { 'person' => { 'username' => 'sikachu' }}
  131
+      assert_parameters({ 'person' => { 'username' => 'sikachu' }, 'user' => {'person' => { 'username' => 'sikachu' }}})
123 132
     end
124 133
   end
125 134
 
@@ -129,8 +138,8 @@ def test_derived_wrapped_keys_from_matching_model
129 138
 
130 139
     with_default_wrapper_options do
131 140
       @request.env['CONTENT_TYPE'] = 'application/json'
132  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
133  
-      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
  141
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  142
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
134 143
     end
135 144
   end
136 145
 
@@ -142,8 +151,8 @@ def test_derived_wrapped_keys_from_specified_model
142 151
       UsersController.wrap_parameters Person
143 152
 
144 153
       @request.env['CONTENT_TYPE'] = 'application/json'
145  
-      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
146  
-      assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body
  154
+      post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  155
+      assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
147 156
     end
148 157
   end
149 158
 
@@ -153,13 +162,22 @@ def with_default_wrapper_options(&block)
153 162
       @controller.class.inherited(@controller.class)
154 163
       yield
155 164
     end
  165
+
  166
+    def assert_parameters(expected)
  167
+      assert_equal expected, UsersController.last_parameters
  168
+    end
156 169
 end
157 170
 
158 171
 class NamespacedParamsWrapperTest < ActionController::TestCase
159 172
   module Admin
160 173
     class UsersController < ActionController::Base
161  
-      def test
162  
-        render :json => params.except(:controller, :action)
  174
+      class << self
  175
+        attr_accessor :last_parameters
  176
+      end
  177
+
  178
+      def parse
  179
+        self.class.last_parameters = request.params.except(:controller, :action)
  180
+        head :ok
163 181
       end
164 182
     end
165 183
   end
@@ -172,11 +190,15 @@ def self.column_names
172 190
 
173 191
   tests Admin::UsersController
174 192
 
  193
+  def teardown
  194
+    Admin::UsersController.last_parameters = nil
  195
+  end
  196
+
175 197
   def test_derivered_name_from_controller
176 198
     with_default_wrapper_options do
177 199
       @request.env['CONTENT_TYPE'] = 'application/json'
178  
-      post :test, { 'username' => 'sikachu' }
179  
-      assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body
  200
+      post :parse, { 'username' => 'sikachu' }
  201
+      assert_parameters({'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }})
180 202
     end
181 203
   end
182 204
 
@@ -185,18 +207,40 @@ def test_namespace_lookup_from_model
185 207
     begin
186 208
       with_default_wrapper_options do
187 209
         @request.env['CONTENT_TYPE'] = 'application/json'
188  
-        post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
189  
-      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
  210
+        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  211
+        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
190 212
       end
191 213
     ensure
192 214
       Admin.send :remove_const, :User
193 215
     end
194 216
   end
195 217
 
  218
+  def test_heirarchy_namespace_lookup_from_model
  219
+    # Make sure that we cleanup ::Admin::User
  220
+    admin_user_constant = ::Admin::User
  221
+    ::Admin.send :remove_const, :User
  222
+
  223
+    Object.const_set(:User, Class.new(Sample))
  224
+    begin
  225
+      with_default_wrapper_options do
  226
+        @request.env['CONTENT_TYPE'] = 'application/json'
  227
+        post :parse, { 'username' => 'sikachu', 'title' => 'Developer' }
  228
+        assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
  229
+      end
  230
+    ensure
  231
+      Object.send :remove_const, :User
  232
+      ::Admin.const_set(:User, admin_user_constant)
  233
+    end
  234
+  end
  235
+
196 236
   private
197 237
     def with_default_wrapper_options(&block)
198 238
       @controller.class._wrapper_options = {:format => [:json]}
199 239
       @controller.class.inherited(@controller.class)
200 240
       yield
201 241
     end
  242
+
  243
+    def assert_parameters(expected)
  244
+      assert_equal expected, Admin::UsersController.last_parameters
  245
+    end
202 246
 end

0 notes on commit 31f412f

Please sign in to comment.
Something went wrong with that request. Please try again.