2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/hash/indifferent_access"
|
|
|
|
require "rack/utils"
|
2010-03-28 08:15:02 -04:00
|
|
|
|
2009-04-30 18:26:03 -04:00
|
|
|
module ActionDispatch
|
|
|
|
class TestRequest < Request
|
2016-08-06 12:51:43 -04:00
|
|
|
DEFAULT_ENV = Rack::MockRequest.env_for("/",
|
|
|
|
"HTTP_HOST" => "test.host",
|
|
|
|
"REMOTE_ADDR" => "0.0.0.0",
|
|
|
|
"HTTP_USER_AGENT" => "Rails Testing",
|
2013-07-25 02:46:54 -04:00
|
|
|
)
|
2009-04-30 20:23:50 -04:00
|
|
|
|
2015-07-08 19:09:49 -04:00
|
|
|
# Create a new test request with default `env` values
|
|
|
|
def self.create(env = {})
|
2012-05-21 15:24:18 -04:00
|
|
|
env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
|
2015-07-08 19:27:02 -04:00
|
|
|
env["rack.request.cookie_hash"] ||= {}.with_indifferent_access
|
2015-07-08 19:09:49 -04:00
|
|
|
new(default_env.merge(env))
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
2015-07-08 19:09:49 -04:00
|
|
|
def self.default_env
|
|
|
|
DEFAULT_ENV
|
|
|
|
end
|
|
|
|
private_class_method :default_env
|
|
|
|
|
2009-04-30 18:26:03 -04:00
|
|
|
def request_method=(method)
|
2016-09-22 11:36:23 -04:00
|
|
|
super(method.to_s.upcase)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def host=(host)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("HTTP_HOST", host)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def port=(number)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("SERVER_PORT", number.to_i)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_uri=(uri)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("REQUEST_URI", uri)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def path=(path)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("PATH_INFO", path)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
2009-05-01 00:31:20 -04:00
|
|
|
def action=(action_name)
|
2014-07-02 02:42:41 -04:00
|
|
|
path_parameters[:action] = action_name.to_s
|
2009-05-01 00:31:20 -04:00
|
|
|
end
|
|
|
|
|
2009-04-30 18:26:03 -04:00
|
|
|
def if_modified_since=(last_modified)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("HTTP_IF_MODIFIED_SINCE", last_modified)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def if_none_match=(etag)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("HTTP_IF_NONE_MATCH", etag)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def remote_addr=(addr)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("REMOTE_ADDR", addr)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_agent=(user_agent)
|
2016-08-06 12:51:43 -04:00
|
|
|
set_header("HTTP_USER_AGENT", user_agent)
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def accept=(mime_types)
|
2016-08-06 12:51:43 -04:00
|
|
|
delete_header("action_dispatch.request.accepts")
|
|
|
|
set_header("HTTP_ACCEPT", Array(mime_types).collect(&:to_s).join(","))
|
2009-04-30 18:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|