2013-03-13 06:30:45 -04:00
|
|
|
require "abstract_unit"
|
2009-01-28 23:50:46 -05:00
|
|
|
|
|
|
|
class HeaderTest < ActiveSupport::TestCase
|
2013-03-13 06:30:45 -04:00
|
|
|
setup do
|
2009-01-28 23:50:46 -05:00
|
|
|
@headers = ActionDispatch::Http::Headers.new(
|
2013-03-13 06:14:49 -04:00
|
|
|
"CONTENT_TYPE" => "text/plain",
|
|
|
|
"HTTP_REFERER" => "/some/page"
|
2009-01-28 23:50:46 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-03-13 13:05:01 -04:00
|
|
|
test "#new does not normalize the data" do
|
2013-03-13 07:25:32 -04:00
|
|
|
headers = ActionDispatch::Http::Headers.new(
|
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"HTTP_REFERER" => "/some/page",
|
|
|
|
"Host" => "http://test.com")
|
|
|
|
|
2013-03-13 13:05:01 -04:00
|
|
|
assert_equal({"Content-Type" => "application/json",
|
2013-03-13 07:25:32 -04:00
|
|
|
"HTTP_REFERER" => "/some/page",
|
2013-03-13 13:05:01 -04:00
|
|
|
"Host" => "http://test.com"}, headers.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
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
|
2012-10-18 18:20:30 -04:00
|
|
|
headers = []
|
|
|
|
@headers.each { |pair| headers << pair }
|
2013-03-13 06:14:49 -04:00
|
|
|
assert_equal [["CONTENT_TYPE", "text/plain"],
|
|
|
|
["HTTP_REFERER", "/some/page"]], headers
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
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
|
|
|
|
|
2013-03-13 11:25:28 -04:00
|
|
|
test "headers can contain numbers" do
|
|
|
|
@headers["Content-MD5"] = "Q2hlY2sgSW50ZWdyaXR5IQ=="
|
|
|
|
|
|
|
|
assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["Content-MD5"]
|
|
|
|
assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["HTTP_CONTENT_MD5"]
|
|
|
|
end
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
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"]
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
test "key?" do
|
|
|
|
assert @headers.key?("CONTENT_TYPE")
|
|
|
|
assert @headers.include?("CONTENT_TYPE")
|
2014-05-07 13:01:34 -04:00
|
|
|
assert @headers.key?("Content-Type")
|
|
|
|
assert @headers.include?("Content-Type")
|
2012-10-18 18:20:30 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
test "fetch with block" do
|
|
|
|
assert_equal "omg", @headers.fetch("notthere") { "omg" }
|
2012-10-18 18:22:58 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 06:14:49 -04:00
|
|
|
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
|
2009-01-28 23:50:46 -05:00
|
|
|
assert_equal "text/plain", @headers["Content-Type"]
|
|
|
|
assert_equal "text/plain", @headers["content-type"]
|
|
|
|
assert_equal "text/plain", @headers["CONTENT_TYPE"]
|
|
|
|
end
|
2012-05-02 14:14:40 -04:00
|
|
|
|
|
|
|
test "fetch" do
|
|
|
|
assert_equal "text/plain", @headers.fetch("content-type", nil)
|
2013-03-13 06:30:45 -04:00
|
|
|
assert_equal "not found", @headers.fetch("not-found", "not found")
|
2012-05-02 14:14:40 -04:00
|
|
|
end
|
2013-03-13 07:25:32 -04:00
|
|
|
|
|
|
|
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
|
2013-03-13 11:25:28 -04:00
|
|
|
|
|
|
|
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
|
2013-03-13 13:05:01 -04:00
|
|
|
headers = ActionDispatch::Http::Headers.new
|
|
|
|
headers.merge!(:SERVER_NAME => "example.com",
|
|
|
|
"HTTP_REFERER" => "/",
|
|
|
|
:Host => "test.com")
|
2013-03-13 11:25:28 -04:00
|
|
|
assert_equal "example.com", headers["SERVER_NAME"]
|
|
|
|
assert_equal "/", headers[:HTTP_REFERER]
|
|
|
|
assert_equal "test.com", headers["HTTP_HOST"]
|
|
|
|
end
|
2013-03-13 13:05:01 -04:00
|
|
|
|
|
|
|
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
|
2009-01-28 23:50:46 -05:00
|
|
|
end
|