Skip to content
This repository
Browse code

Merge pull request #9716 from senny/rework_headers_functional_tests

Rework headers functional tests
  • Loading branch information...
commit ddb9040b433b00aa0206442aa718eee12bceb517 2 parents c1003d9 + e8598a5
Andrew White authored March 15, 2013
8  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,13 +1,17 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
3  
-*   `ActionDispatch::IntegrationTest` allows headers and rack env
  3
+*   Integration and functional tests allow headers and rack env
4 4
     variables to be passed when performing requests.
5 5
     Fixes #6513.
6 6
 
7 7
     Example:
8 8
 
  9
+        # integration test
9 10
         get "/success", {}, "HTTP_REFERER" => "http://test.com/",
10  
-                            "Host" => "http://test.com"
  11
+                            "Accepts" => "text/plain, text/html"
  12
+
  13
+        # functional test
  14
+        @request.headers["Accepts"] = "text/plain, text/html"
11 15
 
12 16
     *Yves Senn*
13 17
 
34  actionpack/lib/action_controller/test_case.rb
@@ -451,37 +451,57 @@ def prepare_controller_class(new_class)
451 451
 
452 452
       end
453 453
 
454  
-      # Executes a request simulating GET HTTP method and set/volley the response
  454
+      # Simulate a GET request with the given parameters.
  455
+      #
  456
+      # - +action+: The controller action to call
  457
+      # - +parameters+: The HTTP parameters that you want to pass. This may
  458
+      #   be +nil+,
  459
+      #   a Hash, or a String that is appropriately encoded
  460
+      #   (<tt>application/x-www-form-urlencoded</tt> or
  461
+      #   <tt>multipart/form-data</tt>).
  462
+      # - +session+: A Hash of parameters to store in the session. This my be +nil+.
  463
+      # - +flash+: A Hash of parameters to store in the flash. This my be +nil+.
  464
+      #
  465
+      # You can also simulate POST, PATCH, PUT, DELETE, and HEAD requests with
  466
+      # +#post+, +#patch+, +#put+, +#delete+, and +#head+.
  467
+      # Note that the request method is not verified. The different methods are
  468
+      # available to make the tests more expressive.
455 469
       def get(action, *args)
456 470
         process(action, "GET", *args)
457 471
       end
458 472
 
459  
-      # Executes a request simulating POST HTTP method and set/volley the response
  473
+      # Simulate a POST request with the given parameters and set/volley the response.
  474
+      # See +#get+ for more details.
460 475
       def post(action, *args)
461 476
         process(action, "POST", *args)
462 477
       end
463 478
 
464  
-      # Executes a request simulating PATCH HTTP method and set/volley the response
  479
+      # Simulate a PATCH request with the given parameters and set/volley the response.
  480
+      # See +#get+ for more details.
465 481
       def patch(action, *args)
466 482
         process(action, "PATCH", *args)
467 483
       end
468 484
 
469  
-      # Executes a request simulating PUT HTTP method and set/volley the response
  485
+      # Simulate a PUT request with the given parameters and set/volley the response.
  486
+      # See +#get+ for more details.
470 487
       def put(action, *args)
471 488
         process(action, "PUT", *args)
472 489
       end
473 490
 
474  
-      # Executes a request simulating DELETE HTTP method and set/volley the response
  491
+      # Simulate a DELETE request with the given parameters and set/volley the response.
  492
+      # See +#get+ for more details.
475 493
       def delete(action, *args)
476 494
         process(action, "DELETE", *args)
477 495
       end
478 496
 
479  
-      # Executes a request simulating HEAD HTTP method and set/volley the response
  497
+      # Simulate a HEAD request with the given parameters and set/volley the response.
  498
+      # See +#get+ for more details.
480 499
       def head(action, *args)
481 500
         process(action, "HEAD", *args)
482 501
       end
483 502
 
484  
-      # Executes a request simulating OPTIONS HTTP method and set/volley the response
  503
+      # Simulate a OPTIONS request with the given parameters and set/volley the response.
  504
+      # See +#get+ for more details.
485 505
       def options(action, *args)
486 506
         process(action, "OPTIONS", *args)
487 507
       end
3  actionpack/lib/action_dispatch/http/headers.rb
@@ -15,8 +15,7 @@ class Headers
15 15
       attr_reader :env
16 16
 
17 17
       def initialize(env = {})
18  
-        @env = {}
19  
-        merge!(env)
  18
+        @env = env
20 19
       end
21 20
 
22 21
       def [](key)
5  actionpack/lib/action_dispatch/testing/integration.rb
@@ -269,7 +269,6 @@ def _mock_session
269 269
 
270 270
         # Performs the actual request.
271 271
         def process(method, path, parameters = nil, headers_or_env = nil)
272  
-          rack_env = Http::Headers.new(headers_or_env || {}).env
273 272
           if path =~ %r{://}
274 273
             location = URI.parse(path)
275 274
             https! URI::HTTPS === location if location.scheme
@@ -300,10 +299,12 @@ def process(method, path, parameters = nil, headers_or_env = nil)
300 299
             "CONTENT_TYPE"   => "application/x-www-form-urlencoded",
301 300
             "HTTP_ACCEPT"    => accept
302 301
           }
  302
+          # this modifies the passed env directly
  303
+          Http::Headers.new(env).merge!(headers_or_env || {})
303 304
 
304 305
           session = Rack::Test::Session.new(_mock_session)
305 306
 
306  
-          env.merge!(rack_env)
  307
+          env.merge!(env)
307 308
 
308 309
           # NOTE: rack-test v0.5 doesn't build a default uri correctly
309 310
           # Make sure requested path is always a full uri
22  actionpack/test/controller/test_case_test.rb
@@ -57,6 +57,10 @@ def test_protocol
57 57
       render :text => request.protocol
58 58
     end
59 59
 
  60
+    def test_headers
  61
+      render text: JSON.dump(request.headers.env)
  62
+    end
  63
+
60 64
     def test_html_output
61 65
       render :text => <<HTML
62 66
 <html>
@@ -626,6 +630,24 @@ def test_params_passing_doesnt_modify_in_place
626 630
     assert_equal 2004, page[:year]
627 631
   end
628 632
 
  633
+  test "set additional HTTP headers" do
  634
+    @request.headers['Referer'] = "http://nohost.com/home"
  635
+    @request.headers['Content-Type'] = "application/rss+xml"
  636
+    get :test_headers
  637
+    parsed_env = JSON.parse(@response.body)
  638
+    assert_equal "http://nohost.com/home", parsed_env["HTTP_REFERER"]
  639
+    assert_equal "application/rss+xml", parsed_env["CONTENT_TYPE"]
  640
+  end
  641
+
  642
+  test "set additional env variables" do
  643
+    @request.headers['HTTP_REFERER'] = "http://example.com/about"
  644
+    @request.headers['CONTENT_TYPE'] = "application/json"
  645
+    get :test_headers
  646
+    parsed_env = JSON.parse(@response.body)
  647
+    assert_equal "http://example.com/about", parsed_env["HTTP_REFERER"]
  648
+    assert_equal "application/json", parsed_env["CONTENT_TYPE"]
  649
+  end
  650
+
629 651
   def test_id_converted_to_string
630 652
     get :test_params, :id => 20, :foo => Object.new
631 653
     assert_kind_of String, @request.path_parameters['id']
22  actionpack/test/dispatch/header_test.rb
@@ -8,15 +8,15 @@ class HeaderTest < ActiveSupport::TestCase
8 8
     )
9 9
   end
10 10
 
11  
-  test "#new with mixed headers and env" do
  11
+  test "#new does not normalize the data" do
12 12
     headers = ActionDispatch::Http::Headers.new(
13 13
       "Content-Type" => "application/json",
14 14
       "HTTP_REFERER" => "/some/page",
15 15
       "Host" => "http://test.com")
16 16
 
17  
-    assert_equal({"CONTENT_TYPE" => "application/json",
  17
+    assert_equal({"Content-Type" => "application/json",
18 18
                   "HTTP_REFERER" => "/some/page",
19  
-                  "HTTP_HOST" => "http://test.com"}, headers.env)
  19
+                  "Host" => "http://test.com"}, headers.env)
20 20
   end
21 21
 
22 22
   test "#env returns the headers as env variables" do
@@ -117,11 +117,21 @@ class HeaderTest < ActiveSupport::TestCase
117 117
   end
118 118
 
119 119
   test "symbols are treated as strings" do
120  
-    headers = ActionDispatch::Http::Headers.new(:SERVER_NAME => "example.com",
121  
-                                                "HTTP_REFERER" => "/",
122  
-                                                :Host => "test.com")
  120
+    headers = ActionDispatch::Http::Headers.new
  121
+    headers.merge!(:SERVER_NAME => "example.com",
  122
+                   "HTTP_REFERER" => "/",
  123
+                   :Host => "test.com")
123 124
     assert_equal "example.com", headers["SERVER_NAME"]
124 125
     assert_equal "/", headers[:HTTP_REFERER]
125 126
     assert_equal "test.com", headers["HTTP_HOST"]
126 127
   end
  128
+
  129
+  test "headers directly modifies the passed environment" do
  130
+    env = {"HTTP_REFERER" => "/"}
  131
+    headers = ActionDispatch::Http::Headers.new(env)
  132
+    headers['Referer'] = "http://example.com/"
  133
+    headers.merge! "CONTENT_TYPE" => "text/plain"
  134
+    assert_equal({"HTTP_REFERER"=>"http://example.com/",
  135
+                  "CONTENT_TYPE"=>"text/plain"}, env)
  136
+  end
127 137
 end
15  guides/source/testing.md
Source Rendered
@@ -501,6 +501,21 @@ You also have access to three instance variables in your functional tests:
501 501
 * `@request` - The request
502 502
 * `@response` - The response
503 503
 
  504
+### Setting Headers and CGI variables
  505
+
  506
+Headers and cgi variables can be set directly on the `@request`
  507
+instance variable:
  508
+
  509
+```ruby
  510
+# setting a HTTP Header
  511
+@request.headers["Accepts"] = "text/plain, text/html"
  512
+get :index # simulate the request with custom header
  513
+
  514
+# setting a CGI variable
  515
+@request.headers["HTTP_REFERER"] = "http://example.com/home"
  516
+post :create # simulate the request with custom env variable
  517
+```
  518
+
504 519
 ### Testing Templates and Layouts
505 520
 
506 521
 If you want to make sure that the response rendered the correct template and layout, you can use the `assert_template`

0 notes on commit ddb9040

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