aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-03-13 18:05:01 +0100
committerYves Senn <yves.senn@gmail.com>2013-03-15 10:13:49 +0100
commit41a14dcd1045f61278237f34f065b87212689376 (patch)
tree0f7aafb34684d6ebf78e30b74f310a57b5a6fec1
parentc1003d99b00b3afb5417b6853a3e2ba92638cbe8 (diff)
downloadrails-41a14dcd1045f61278237f34f065b87212689376.tar.gz
rails-41a14dcd1045f61278237f34f065b87212689376.tar.bz2
rails-41a14dcd1045f61278237f34f065b87212689376.zip
`Http::Headers` directly modifies the passed environment.
The env hash passed to `Http::Headers#new` must be in env format. Also be aware that the passed hash is modified directly. docs and test-cases for setting headers/env in functional tests. Follow up to #9700.
-rw-r--r--actionpack/CHANGELOG.md8
-rw-r--r--actionpack/lib/action_dispatch/http/headers.rb3
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb5
-rw-r--r--actionpack/test/controller/test_case_test.rb22
-rw-r--r--actionpack/test/dispatch/header_test.rb22
-rw-r--r--guides/source/testing.md15
6 files changed, 63 insertions, 12 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index 3eb40a586e..b23d0668d3 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -1,13 +1,17 @@
## Rails 4.0.0 (unreleased) ##
-* `ActionDispatch::IntegrationTest` allows headers and rack env
+* Integration and functional tests allow headers and rack env
variables to be passed when performing requests.
Fixes #6513.
Example:
+ # integration test
get "/success", {}, "HTTP_REFERER" => "http://test.com/",
- "Host" => "http://test.com"
+ "Accepts" => "text/plain, text/html"
+
+ # functional test
+ @request.headers["Accepts"] = "text/plain, text/html"
*Yves Senn*
diff --git a/actionpack/lib/action_dispatch/http/headers.rb b/actionpack/lib/action_dispatch/http/headers.rb
index 1574518a16..2666cd4b0a 100644
--- a/actionpack/lib/action_dispatch/http/headers.rb
+++ b/actionpack/lib/action_dispatch/http/headers.rb
@@ -15,8 +15,7 @@ module ActionDispatch
attr_reader :env
def initialize(env = {})
- @env = {}
- merge!(env)
+ @env = env
end
def [](key)
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index ae1b0b5dea..56c31255f3 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -269,7 +269,6 @@ module ActionDispatch
# Performs the actual request.
def process(method, path, parameters = nil, headers_or_env = nil)
- rack_env = Http::Headers.new(headers_or_env || {}).env
if path =~ %r{://}
location = URI.parse(path)
https! URI::HTTPS === location if location.scheme
@@ -300,10 +299,12 @@ module ActionDispatch
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
"HTTP_ACCEPT" => accept
}
+ # this modifies the passed env directly
+ Http::Headers.new(env).merge!(headers_or_env || {})
session = Rack::Test::Session.new(_mock_session)
- env.merge!(rack_env)
+ env.merge!(env)
# NOTE: rack-test v0.5 doesn't build a default uri correctly
# Make sure requested path is always a full uri
diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb
index df31338f09..9c6d02a62d 100644
--- a/actionpack/test/controller/test_case_test.rb
+++ b/actionpack/test/controller/test_case_test.rb
@@ -57,6 +57,10 @@ class TestCaseTest < ActionController::TestCase
render :text => request.protocol
end
+ def test_headers
+ render text: JSON.dump(request.headers.env)
+ end
+
def test_html_output
render :text => <<HTML
<html>
@@ -626,6 +630,24 @@ XML
assert_equal 2004, page[:year]
end
+ test "set additional HTTP headers" do
+ @request.headers['Referer'] = "http://nohost.com/home"
+ @request.headers['Content-Type'] = "application/rss+xml"
+ get :test_headers
+ parsed_env = JSON.parse(@response.body)
+ assert_equal "http://nohost.com/home", parsed_env["HTTP_REFERER"]
+ assert_equal "application/rss+xml", parsed_env["CONTENT_TYPE"]
+ end
+
+ test "set additional env variables" do
+ @request.headers['HTTP_REFERER'] = "http://example.com/about"
+ @request.headers['CONTENT_TYPE'] = "application/json"
+ get :test_headers
+ parsed_env = JSON.parse(@response.body)
+ assert_equal "http://example.com/about", parsed_env["HTTP_REFERER"]
+ assert_equal "application/json", parsed_env["CONTENT_TYPE"]
+ end
+
def test_id_converted_to_string
get :test_params, :id => 20, :foo => Object.new
assert_kind_of String, @request.path_parameters['id']
diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb
index 3bb3b3db23..9e37b96951 100644
--- a/actionpack/test/dispatch/header_test.rb
+++ b/actionpack/test/dispatch/header_test.rb
@@ -8,15 +8,15 @@ class HeaderTest < ActiveSupport::TestCase
)
end
- test "#new with mixed headers and env" do
+ test "#new does not normalize the data" do
headers = ActionDispatch::Http::Headers.new(
"Content-Type" => "application/json",
"HTTP_REFERER" => "/some/page",
"Host" => "http://test.com")
- assert_equal({"CONTENT_TYPE" => "application/json",
+ assert_equal({"Content-Type" => "application/json",
"HTTP_REFERER" => "/some/page",
- "HTTP_HOST" => "http://test.com"}, headers.env)
+ "Host" => "http://test.com"}, headers.env)
end
test "#env returns the headers as env variables" do
@@ -117,11 +117,21 @@ class HeaderTest < ActiveSupport::TestCase
end
test "symbols are treated as strings" do
- headers = ActionDispatch::Http::Headers.new(:SERVER_NAME => "example.com",
- "HTTP_REFERER" => "/",
- :Host => "test.com")
+ headers = ActionDispatch::Http::Headers.new
+ headers.merge!(:SERVER_NAME => "example.com",
+ "HTTP_REFERER" => "/",
+ :Host => "test.com")
assert_equal "example.com", headers["SERVER_NAME"]
assert_equal "/", headers[:HTTP_REFERER]
assert_equal "test.com", headers["HTTP_HOST"]
end
+
+ test "headers directly modifies the passed environment" do
+ env = {"HTTP_REFERER" => "/"}
+ headers = ActionDispatch::Http::Headers.new(env)
+ headers['Referer'] = "http://example.com/"
+ headers.merge! "CONTENT_TYPE" => "text/plain"
+ assert_equal({"HTTP_REFERER"=>"http://example.com/",
+ "CONTENT_TYPE"=>"text/plain"}, env)
+ end
end
diff --git a/guides/source/testing.md b/guides/source/testing.md
index 40bf2603c4..1937cbf17a 100644
--- a/guides/source/testing.md
+++ b/guides/source/testing.md
@@ -501,6 +501,21 @@ You also have access to three instance variables in your functional tests:
* `@request` - The request
* `@response` - The response
+### Setting Headers and CGI variables
+
+Headers and cgi variables can be set directly on the `@request`
+instance variable:
+
+```ruby
+# setting a HTTP Header
+@request.headers["Accepts"] = "text/plain, text/html"
+get :index # simulate the request with custom header
+
+# setting a CGI variable
+@request.headers["HTTP_REFERER"] = "http://example.com/home"
+post :create # simulate the request with custom env variable
+```
+
### Testing Templates and Layouts
If you want to make sure that the response rendered the correct template and layout, you can use the `assert_template`