From 8945be464feb8c9ec8c4e7be52e5195f17a1ef5e Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 13 Mar 2013 11:14:49 +0100 Subject: Http::Headers respects headers that are not prefixed with HTTP_ --- actionpack/test/dispatch/header_test.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index 42432510c3..d3fc83ca12 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -3,14 +3,16 @@ require 'abstract_unit' class HeaderTest < ActiveSupport::TestCase def setup @headers = ActionDispatch::Http::Headers.new( - "HTTP_CONTENT_TYPE" => "text/plain" + "CONTENT_TYPE" => "text/plain", + "HTTP_REFERER" => "/some/page" ) end def test_each headers = [] @headers.each { |pair| headers << pair } - assert_equal [["HTTP_CONTENT_TYPE", "text/plain"]], headers + assert_equal [["CONTENT_TYPE", "text/plain"], + ["HTTP_REFERER", "/some/page"]], headers end def test_setter @@ -19,19 +21,24 @@ class HeaderTest < ActiveSupport::TestCase end def test_key? - assert @headers.key?('HTTP_CONTENT_TYPE') - assert @headers.include?('HTTP_CONTENT_TYPE') + assert @headers.key?('CONTENT_TYPE') + assert @headers.include?('CONTENT_TYPE') end def test_fetch_with_block assert_equal 'omg', @headers.fetch('notthere') { 'omg' } end - test "content type" do + test "accessing http header" do + assert_equal "/some/page", @headers["Referer"] + assert_equal "/some/page", @headers["referer"] + assert_equal "/some/page", @headers["HTTP_REFERER"] + end + + test "accessing special header" do assert_equal "text/plain", @headers["Content-Type"] assert_equal "text/plain", @headers["content-type"] assert_equal "text/plain", @headers["CONTENT_TYPE"] - assert_equal "text/plain", @headers["HTTP_CONTENT_TYPE"] end test "fetch" do -- cgit v1.2.3 From e2a5de2bb200773943e605cdddb9b18bbfa77e13 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 13 Mar 2013 11:30:45 +0100 Subject: refactor, `Http::Headers` stores headers in env notation Also: cleanup, use consistent syntax for `Http::Header` and test. --- actionpack/test/dispatch/header_test.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index d3fc83ca12..9f92c3791e 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -1,32 +1,41 @@ -require 'abstract_unit' +require "abstract_unit" class HeaderTest < ActiveSupport::TestCase - def setup + setup do @headers = ActionDispatch::Http::Headers.new( "CONTENT_TYPE" => "text/plain", "HTTP_REFERER" => "/some/page" ) end - def test_each + test "each" do headers = [] @headers.each { |pair| headers << pair } assert_equal [["CONTENT_TYPE", "text/plain"], ["HTTP_REFERER", "/some/page"]], headers end - def test_setter - @headers['foo'] = "bar" - assert_equal "bar", @headers['foo'] + test "set new headers" do + @headers["Host"] = "127.0.0.1" + + assert_equal "127.0.0.1", @headers["Host"] + assert_equal "127.0.0.1", @headers["HTTP_HOST"] + end + + test "set new env variables" do + @headers["HTTP_HOST"] = "127.0.0.1" + + assert_equal "127.0.0.1", @headers["Host"] + assert_equal "127.0.0.1", @headers["HTTP_HOST"] end - def test_key? - assert @headers.key?('CONTENT_TYPE') - assert @headers.include?('CONTENT_TYPE') + test "key?" do + assert @headers.key?("CONTENT_TYPE") + assert @headers.include?("CONTENT_TYPE") end - def test_fetch_with_block - assert_equal 'omg', @headers.fetch('notthere') { 'omg' } + test "fetch with block" do + assert_equal "omg", @headers.fetch("notthere") { "omg" } end test "accessing http header" do @@ -43,6 +52,6 @@ class HeaderTest < ActiveSupport::TestCase test "fetch" do assert_equal "text/plain", @headers.fetch("content-type", nil) - assert_equal "not found", @headers.fetch('not-found', 'not found') + assert_equal "not found", @headers.fetch("not-found", "not found") end end -- cgit v1.2.3 From 9af59b2468e4ad6c3c2ca89f90968fdcaa417aba Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 13 Mar 2013 12:25:32 +0100 Subject: allow headers and env to be passed in `IntegrationTest`. Closes #6513. --- actionpack/test/controller/integration_test.rb | 15 +++++++++ actionpack/test/dispatch/header_test.rb | 45 +++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 72b882539c..c3bdf74d93 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -573,6 +573,21 @@ class MetalIntegrationTest < ActionDispatch::IntegrationTest def test_generate_url_without_controller assert_equal 'http://www.example.com/foo', url_for(:controller => "foo") end + + def test_pass_headers + get "/success", {}, "Referer" => "http://www.example.com/foo", "Host" => "http://nohost.com" + + assert_equal "http://nohost.com", @request.env["HTTP_HOST"] + assert_equal "http://www.example.com/foo", @request.env["HTTP_REFERER"] + end + + def test_pass_env + get "/success", {}, "HTTP_REFERER" => "http://test.com/", "HTTP_HOST" => "http://test.com" + + assert_equal "http://test.com", @request.env["HTTP_HOST"] + assert_equal "http://test.com/", @request.env["HTTP_REFERER"] + end + end class ApplicationIntegrationTest < ActionDispatch::IntegrationTest diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index 9f92c3791e..0bfc18b4ed 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -8,7 +8,23 @@ class HeaderTest < ActiveSupport::TestCase ) end - test "each" do + test "#new with mixed headers and env" do + headers = ActionDispatch::Http::Headers.new( + "Content-Type" => "application/json", + "HTTP_REFERER" => "/some/page", + "Host" => "http://test.com") + + assert_equal({"CONTENT_TYPE" => "application/json", + "HTTP_REFERER" => "/some/page", + "HTTP_HOST" => "http://test.com"}, headers.env) + end + + test "#env returns the headers as env variables" do + assert_equal({"CONTENT_TYPE" => "text/plain", + "HTTP_REFERER" => "/some/page"}, @headers.env) + end + + test "#each iterates through the env variables" do headers = [] @headers.each { |pair| headers << pair } assert_equal [["CONTENT_TYPE", "text/plain"], @@ -54,4 +70,31 @@ class HeaderTest < ActiveSupport::TestCase assert_equal "text/plain", @headers.fetch("content-type", nil) assert_equal "not found", @headers.fetch("not-found", "not found") end + + test "#merge! headers with mutation" do + @headers.merge!("Host" => "http://example.test", + "Content-Type" => "text/html") + assert_equal({"HTTP_HOST" => "http://example.test", + "CONTENT_TYPE" => "text/html", + "HTTP_REFERER" => "/some/page"}, @headers.env) + end + + test "#merge! env with mutation" do + @headers.merge!("HTTP_HOST" => "http://first.com", + "CONTENT_TYPE" => "text/html") + assert_equal({"HTTP_HOST" => "http://first.com", + "CONTENT_TYPE" => "text/html", + "HTTP_REFERER" => "/some/page"}, @headers.env) + end + + test "merge without mutation" do + combined = @headers.merge("HTTP_HOST" => "http://example.com", + "CONTENT_TYPE" => "text/html") + assert_equal({"HTTP_HOST" => "http://example.com", + "CONTENT_TYPE" => "text/html", + "HTTP_REFERER" => "/some/page"}, combined.env) + + assert_equal({"CONTENT_TYPE" => "text/plain", + "HTTP_REFERER" => "/some/page"}, @headers.env) + end end -- cgit v1.2.3 From a709246d1739c44e04b00412e7a4ec09c1500fc3 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 13 Mar 2013 16:25:28 +0100 Subject: `Http::Headers` respects dotted env vars, symbols, headers with numbers. --- actionpack/test/dispatch/header_test.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index 0bfc18b4ed..3bb3b3db23 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -38,6 +38,13 @@ class HeaderTest < ActiveSupport::TestCase assert_equal "127.0.0.1", @headers["HTTP_HOST"] end + test "headers can contain numbers" do + @headers["Content-MD5"] = "Q2hlY2sgSW50ZWdyaXR5IQ==" + + assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["Content-MD5"] + assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["HTTP_CONTENT_MD5"] + end + test "set new env variables" do @headers["HTTP_HOST"] = "127.0.0.1" @@ -97,4 +104,24 @@ class HeaderTest < ActiveSupport::TestCase assert_equal({"CONTENT_TYPE" => "text/plain", "HTTP_REFERER" => "/some/page"}, @headers.env) end + + test "env variables with . are not modified" do + headers = ActionDispatch::Http::Headers.new + headers.merge! "rack.input" => "", + "rack.request.cookie_hash" => "", + "action_dispatch.logger" => "" + + assert_equal(["action_dispatch.logger", + "rack.input", + "rack.request.cookie_hash"], headers.env.keys.sort) + end + + test "symbols are treated as strings" do + headers = ActionDispatch::Http::Headers.new(: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 end -- cgit v1.2.3