2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2009-04-30 18:26:03 -04:00
|
|
|
|
|
|
|
class TestRequestTest < ActiveSupport::TestCase
|
|
|
|
test "sane defaults" do
|
2015-07-08 19:09:49 -04:00
|
|
|
env = ActionDispatch::TestRequest.create.env
|
2009-04-30 18:26:03 -04:00
|
|
|
|
|
|
|
assert_equal "GET", env.delete("REQUEST_METHOD")
|
2009-11-10 19:57:55 -05:00
|
|
|
assert_equal "off", env.delete("HTTPS")
|
2009-04-30 18:26:03 -04:00
|
|
|
assert_equal "http", env.delete("rack.url_scheme")
|
|
|
|
assert_equal "example.org", env.delete("SERVER_NAME")
|
|
|
|
assert_equal "80", env.delete("SERVER_PORT")
|
|
|
|
assert_equal "/", env.delete("PATH_INFO")
|
|
|
|
assert_equal "", env.delete("SCRIPT_NAME")
|
|
|
|
assert_equal "", env.delete("QUERY_STRING")
|
|
|
|
assert_equal "0", env.delete("CONTENT_LENGTH")
|
|
|
|
|
|
|
|
assert_equal "test.host", env.delete("HTTP_HOST")
|
|
|
|
assert_equal "0.0.0.0", env.delete("REMOTE_ADDR")
|
|
|
|
assert_equal "Rails Testing", env.delete("HTTP_USER_AGENT")
|
|
|
|
|
2014-12-19 04:26:19 -05:00
|
|
|
assert_equal [1, 3], env.delete("rack.version")
|
2009-04-30 18:26:03 -04:00
|
|
|
assert_equal "", env.delete("rack.input").string
|
|
|
|
assert_kind_of StringIO, env.delete("rack.errors")
|
|
|
|
assert_equal true, env.delete("rack.multithread")
|
|
|
|
assert_equal true, env.delete("rack.multiprocess")
|
|
|
|
assert_equal false, env.delete("rack.run_once")
|
|
|
|
end
|
2009-04-30 20:23:50 -04:00
|
|
|
|
|
|
|
test "cookie jar" do
|
2015-07-08 19:09:49 -04:00
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2009-04-30 20:23:50 -04:00
|
|
|
|
|
|
|
assert_equal({}, req.cookies)
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil req.env["HTTP_COOKIE"]
|
2009-04-30 20:23:50 -04:00
|
|
|
|
2011-03-29 19:46:27 -04:00
|
|
|
req.cookie_jar["user_name"] = "david"
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_cookies({ "user_name" => "david" }, req.cookie_jar)
|
2009-04-30 20:23:50 -04:00
|
|
|
|
2011-03-29 19:46:27 -04:00
|
|
|
req.cookie_jar["login"] = "XJ-122"
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_cookies({ "user_name" => "david", "login" => "XJ-122" }, req.cookie_jar)
|
2011-06-05 07:03:04 -04:00
|
|
|
|
2013-01-21 21:35:14 -05:00
|
|
|
assert_nothing_raised do
|
2011-07-14 15:01:39 -04:00
|
|
|
req.cookie_jar["login"] = nil
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_cookies({ "user_name" => "david", "login" => nil }, req.cookie_jar)
|
2013-01-21 21:35:14 -05:00
|
|
|
end
|
2011-07-14 15:01:39 -04:00
|
|
|
|
2011-06-05 07:03:04 -04:00
|
|
|
req.cookie_jar.delete(:login)
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_cookies({ "user_name" => "david" }, req.cookie_jar)
|
2011-06-05 07:03:04 -04:00
|
|
|
|
|
|
|
req.cookie_jar.clear
|
|
|
|
assert_cookies({}, req.cookie_jar)
|
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
req.cookie_jar.update(user_name: "david")
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_cookies({ "user_name" => "david" }, req.cookie_jar)
|
2009-04-30 20:23:50 -04:00
|
|
|
end
|
2011-03-29 19:46:27 -04:00
|
|
|
|
2015-08-21 16:33:50 -04:00
|
|
|
test "does not complain when there is no application config" do
|
2015-07-08 19:09:49 -04:00
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2012-05-21 15:24:18 -04:00
|
|
|
assert_equal false, req.env.empty?
|
|
|
|
end
|
|
|
|
|
2013-07-25 02:46:54 -04:00
|
|
|
test "default remote address is 0.0.0.0" do
|
2015-07-08 19:09:49 -04:00
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "0.0.0.0", req.remote_addr
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "allows remote address to be overridden" do
|
2016-08-06 12:54:50 -04:00
|
|
|
req = ActionDispatch::TestRequest.create("REMOTE_ADDR" => "127.0.0.1")
|
|
|
|
assert_equal "127.0.0.1", req.remote_addr
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "default host is test.host" do
|
2015-07-08 19:09:49 -04:00
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "test.host", req.host
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "allows host to be overridden" do
|
2016-08-06 12:54:50 -04:00
|
|
|
req = ActionDispatch::TestRequest.create("HTTP_HOST" => "www.example.com")
|
|
|
|
assert_equal "www.example.com", req.host
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "default user agent is 'Rails Testing'" do
|
2015-07-08 19:09:49 -04:00
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Rails Testing", req.user_agent
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "allows user agent to be overridden" do
|
2016-08-06 12:54:50 -04:00
|
|
|
req = ActionDispatch::TestRequest.create("HTTP_USER_AGENT" => "GoogleBot")
|
|
|
|
assert_equal "GoogleBot", req.user_agent
|
2013-07-25 02:46:54 -04:00
|
|
|
end
|
|
|
|
|
2016-09-22 11:36:23 -04:00
|
|
|
test "request_method getter and setter" do
|
|
|
|
req = ActionDispatch::TestRequest.create
|
|
|
|
req.request_method # to reproduce bug caused by memoization
|
|
|
|
req.request_method = "POST"
|
|
|
|
assert_equal "POST", req.request_method
|
|
|
|
end
|
|
|
|
|
2016-04-28 20:26:58 -04:00
|
|
|
test "setter methods" do
|
|
|
|
req = ActionDispatch::TestRequest.create({})
|
2016-08-06 12:54:50 -04:00
|
|
|
get = "GET"
|
2016-04-28 20:26:58 -04:00
|
|
|
|
|
|
|
[
|
2016-08-06 12:54:50 -04:00
|
|
|
"request_method=", "host=", "request_uri=", "path=", "if_modified_since=", "if_none_match=",
|
|
|
|
"remote_addr=", "user_agent=", "accept="
|
2016-04-28 20:26:58 -04:00
|
|
|
].each do |method|
|
|
|
|
req.send(method, get)
|
|
|
|
end
|
|
|
|
|
|
|
|
req.port = 8080
|
2016-08-06 12:54:50 -04:00
|
|
|
req.accept = "hello goodbye"
|
|
|
|
|
|
|
|
assert_equal(get, req.get_header("REQUEST_METHOD"))
|
|
|
|
assert_equal(get, req.get_header("HTTP_HOST"))
|
|
|
|
assert_equal(8080, req.get_header("SERVER_PORT"))
|
|
|
|
assert_equal(get, req.get_header("REQUEST_URI"))
|
|
|
|
assert_equal(get, req.get_header("PATH_INFO"))
|
|
|
|
assert_equal(get, req.get_header("HTTP_IF_MODIFIED_SINCE"))
|
|
|
|
assert_equal(get, req.get_header("HTTP_IF_NONE_MATCH"))
|
|
|
|
assert_equal(get, req.get_header("REMOTE_ADDR"))
|
|
|
|
assert_equal(get, req.get_header("HTTP_USER_AGENT"))
|
|
|
|
assert_nil(req.get_header("action_dispatch.request.accepts"))
|
|
|
|
assert_equal("hello goodbye", req.get_header("HTTP_ACCEPT"))
|
2016-04-28 20:26:58 -04:00
|
|
|
end
|
|
|
|
|
2011-03-29 19:46:27 -04:00
|
|
|
private
|
|
|
|
def assert_cookies(expected, cookie_jar)
|
|
|
|
assert_equal(expected, cookie_jar.instance_variable_get("@cookies"))
|
|
|
|
end
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|