Skip to content
This repository
Browse code

Change integration test helpers to accept Rack environment instead of…

… just HTTP Headers.

  Before :  get '/path', {}, 'Accept' => 'text/javascript'
  After  :  get '/path', {}, 'HTTP_ACCEPT' => 'text/javascript'
  • Loading branch information...
commit 4a6f4b92ad2f48dc7906d223fe4708d36624bd50 1 parent 0bdef30
Pratik authored May 19, 2009
10  actionpack/lib/action_controller/testing/integration.rb
@@ -62,8 +62,8 @@ def head(path, parameters = nil, headers = nil)
62 62
       # with 'HTTP_' if not already.
63 63
       def xml_http_request(request_method, path, parameters = nil, headers = nil)
64 64
         headers ||= {}
65  
-        headers['X-Requested-With'] = 'XMLHttpRequest'
66  
-        headers['Accept'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
  65
+        headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
  66
+        headers['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
67 67
         process(request_method, path, parameters, headers)
68 68
       end
69 69
       alias xhr :xml_http_request
@@ -228,7 +228,7 @@ def url_for(options)
228 228
 
229 229
       private
230 230
         # Performs the actual request.
231  
-        def process(method, path, parameters = nil, headers = nil)
  231
+        def process(method, path, parameters = nil, rack_environment = nil)
232 232
           if path =~ %r{://}
233 233
             location = URI.parse(path)
234 234
             https! URI::HTTPS === location if location.scheme
@@ -265,9 +265,7 @@ def process(method, path, parameters = nil, headers = nil)
265 265
           }
266 266
           env = Rack::MockRequest.env_for(path, opts)
267 267
 
268  
-          (headers || {}).each do |key, value|
269  
-            key = key.to_s.upcase.gsub(/-/, "_")
270  
-            key = "HTTP_#{key}" unless env.has_key?(key) || key =~ /^HTTP_/
  268
+          (rack_environment || {}).each do |key, value|
271 269
             env[key] = value
272 270
           end
273 271
 
24  actionpack/test/controller/integration_test.rb
@@ -123,8 +123,8 @@ def test_head
123 123
   def test_xml_http_request_get
124 124
     path = "/index"; params = "blah"; headers = {:location => 'blah'}
125 125
     headers_after_xhr = headers.merge(
126  
-      "X-Requested-With" => "XMLHttpRequest",
127  
-      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*"
  126
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  127
+      "HTTP_ACCEPT"           => "text/javascript, text/html, application/xml, text/xml, */*"
128 128
     )
129 129
     @session.expects(:process).with(:get,path,params,headers_after_xhr)
130 130
     @session.xml_http_request(:get,path,params,headers)
@@ -133,8 +133,8 @@ def test_xml_http_request_get
133 133
   def test_xml_http_request_post
134 134
     path = "/index"; params = "blah"; headers = {:location => 'blah'}
135 135
     headers_after_xhr = headers.merge(
136  
-      "X-Requested-With" => "XMLHttpRequest",
137  
-      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*"
  136
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  137
+      "HTTP_ACCEPT"           => "text/javascript, text/html, application/xml, text/xml, */*"
138 138
     )
139 139
     @session.expects(:process).with(:post,path,params,headers_after_xhr)
140 140
     @session.xml_http_request(:post,path,params,headers)
@@ -143,8 +143,8 @@ def test_xml_http_request_post
143 143
   def test_xml_http_request_put
144 144
     path = "/index"; params = "blah"; headers = {:location => 'blah'}
145 145
     headers_after_xhr = headers.merge(
146  
-      "X-Requested-With" => "XMLHttpRequest",
147  
-      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*"
  146
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  147
+      "HTTP_ACCEPT"           => "text/javascript, text/html, application/xml, text/xml, */*"
148 148
     )
149 149
     @session.expects(:process).with(:put,path,params,headers_after_xhr)
150 150
     @session.xml_http_request(:put,path,params,headers)
@@ -153,8 +153,8 @@ def test_xml_http_request_put
153 153
   def test_xml_http_request_delete
154 154
     path = "/index"; params = "blah"; headers = {:location => 'blah'}
155 155
     headers_after_xhr = headers.merge(
156  
-      "X-Requested-With" => "XMLHttpRequest",
157  
-      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*"
  156
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  157
+      "HTTP_ACCEPT"           => "text/javascript, text/html, application/xml, text/xml, */*"
158 158
     )
159 159
     @session.expects(:process).with(:delete,path,params,headers_after_xhr)
160 160
     @session.xml_http_request(:delete,path,params,headers)
@@ -163,17 +163,17 @@ def test_xml_http_request_delete
163 163
   def test_xml_http_request_head
164 164
     path = "/index"; params = "blah"; headers = {:location => 'blah'}
165 165
     headers_after_xhr = headers.merge(
166  
-      "X-Requested-With" => "XMLHttpRequest",
167  
-      "Accept"           => "text/javascript, text/html, application/xml, text/xml, */*"
  166
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest",
  167
+      "HTTP_ACCEPT"           => "text/javascript, text/html, application/xml, text/xml, */*"
168 168
     )
169 169
     @session.expects(:process).with(:head,path,params,headers_after_xhr)
170 170
     @session.xml_http_request(:head,path,params,headers)
171 171
   end
172 172
 
173 173
   def test_xml_http_request_override_accept
174  
-    path = "/index"; params = "blah"; headers = {:location => 'blah', "Accept" => "application/xml"}
  174
+    path = "/index"; params = "blah"; headers = {:location => 'blah', "HTTP_ACCEPT" => "application/xml"}
175 175
     headers_after_xhr = headers.merge(
176  
-      "X-Requested-With" => "XMLHttpRequest"
  176
+      "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest"
177 177
     )
178 178
     @session.expects(:process).with(:post,path,params,headers_after_xhr)
179 179
     @session.xml_http_request(:post,path,params,headers)

1 note on commit 4a6f4b9

Adam Williams

I was working with some integration testing in Rails 3.1.1, invoking the post method on ActionDispatch::Integration::RequestHelpers, when I found that my headers were never making it to the endpoint under test. The method is declared as def post(path, parameters = nil, headers = nil), and the documentation for get declares:

+headers+: Additional HTTP headers to pass, as a Hash. The keys will
automatically be upcased, with the prefix 'HTTP_' added if needed.

I've traced the problem all the way back to this commit. That is, it's a problem only in that the documentation is not correct, and the headers are in fact the rack_environment in the actual process method.

Am I mistaken?

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