2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
require "abstract_unit"
|
2009-01-28 23:50:46 -05:00
|
|
|
|
|
|
|
class HeaderTest < ActiveSupport::TestCase
|
2015-08-20 18:52:53 -04:00
|
|
|
def make_headers(hash)
|
2015-08-20 18:57:15 -04:00
|
|
|
ActionDispatch::Http::Headers.new ActionDispatch::Request.new hash
|
2015-08-20 18:52:53 -04:00
|
|
|
end
|
|
|
|
|
2013-03-13 06:30:45 -04:00
|
|
|
setup do
|
2015-08-20 18:52:53 -04:00
|
|
|
@headers = make_headers(
|
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
|
2015-08-20 18:52:53 -04:00
|
|
|
headers = make_headers(
|
2013-03-13 07:25:32 -04:00
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"HTTP_REFERER" => "/some/page",
|
|
|
|
"Host" => "http://test.com")
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "Content-Type" => "application/json",
|
2013-03-13 07:25:32 -04:00
|
|
|
"HTTP_REFERER" => "/some/page",
|
2016-08-16 03:30:11 -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
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "CONTENT_TYPE" => "text/plain",
|
|
|
|
"HTTP_REFERER" => "/some/page" }, @headers.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
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
|
|
|
|
|
2015-10-03 22:14:37 -04:00
|
|
|
test "add to multivalued headers" do
|
|
|
|
# Sets header when not present
|
2016-08-06 12:54:50 -04:00
|
|
|
@headers.add "Foo", "1"
|
|
|
|
assert_equal "1", @headers["Foo"]
|
2015-10-03 22:14:37 -04:00
|
|
|
|
|
|
|
# Ignores nil values
|
2016-08-06 12:54:50 -04:00
|
|
|
@headers.add "Foo", nil
|
|
|
|
assert_equal "1", @headers["Foo"]
|
2015-10-03 22:14:37 -04:00
|
|
|
|
|
|
|
# Converts value to string
|
2016-08-06 12:54:50 -04:00
|
|
|
@headers.add "Foo", 1
|
|
|
|
assert_equal "1,1", @headers["Foo"]
|
2015-10-03 22:14:37 -04:00
|
|
|
|
|
|
|
# Case-insensitive
|
2016-08-06 12:54:50 -04:00
|
|
|
@headers.add "fOo", 2
|
|
|
|
assert_equal "1,1,2", @headers["foO"]
|
2015-10-03 22:14:37 -04:00
|
|
|
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")
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @headers, "CONTENT_TYPE"
|
2014-05-07 13:01:34 -04:00
|
|
|
assert @headers.key?("Content-Type")
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes @headers, "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")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "HTTP_HOST" => "http://example.test",
|
2013-03-13 07:25:32 -04:00
|
|
|
"CONTENT_TYPE" => "text/html",
|
2016-08-16 03:30:11 -04:00
|
|
|
"HTTP_REFERER" => "/some/page" }, @headers.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "#merge! env with mutation" do
|
|
|
|
@headers.merge!("HTTP_HOST" => "http://first.com",
|
|
|
|
"CONTENT_TYPE" => "text/html")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "HTTP_HOST" => "http://first.com",
|
2013-03-13 07:25:32 -04:00
|
|
|
"CONTENT_TYPE" => "text/html",
|
2016-08-16 03:30:11 -04:00
|
|
|
"HTTP_REFERER" => "/some/page" }, @headers.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "merge without mutation" do
|
|
|
|
combined = @headers.merge("HTTP_HOST" => "http://example.com",
|
|
|
|
"CONTENT_TYPE" => "text/html")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "HTTP_HOST" => "http://example.com",
|
2013-03-13 07:25:32 -04:00
|
|
|
"CONTENT_TYPE" => "text/html",
|
2016-08-16 03:30:11 -04:00
|
|
|
"HTTP_REFERER" => "/some/page" }, combined.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "CONTENT_TYPE" => "text/plain",
|
|
|
|
"HTTP_REFERER" => "/some/page" }, @headers.env)
|
2013-03-13 07:25:32 -04:00
|
|
|
end
|
2013-03-13 11:25:28 -04:00
|
|
|
|
|
|
|
test "env variables with . are not modified" do
|
2015-08-20 18:52:53 -04:00
|
|
|
headers = make_headers({})
|
2013-03-13 11:25:28 -04:00
|
|
|
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
|
2015-08-20 18:52:53 -04:00
|
|
|
headers = make_headers({})
|
2013-03-13 13:05:01 -04:00
|
|
|
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
|
2016-08-16 03:30:11 -04:00
|
|
|
env = { "HTTP_REFERER" => "/" }
|
2015-08-20 18:52:53 -04:00
|
|
|
headers = make_headers(env)
|
2016-08-06 12:54:50 -04:00
|
|
|
headers["Referer"] = "http://example.com/"
|
2018-03-29 22:29:55 -04:00
|
|
|
headers["CONTENT_TYPE"] = "text/plain"
|
2016-10-28 23:05:58 -04:00
|
|
|
assert_equal({ "HTTP_REFERER" => "http://example.com/",
|
|
|
|
"CONTENT_TYPE" => "text/plain" }, env)
|
2013-03-13 13:05:01 -04:00
|
|
|
end
|
2016-08-26 13:12:33 -04:00
|
|
|
|
|
|
|
test "fetch exception" do
|
|
|
|
assert_raises KeyError do
|
|
|
|
@headers.fetch(:some_key_that_doesnt_exist)
|
|
|
|
end
|
|
|
|
end
|
2009-01-28 23:50:46 -05:00
|
|
|
end
|