diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-07-08 16:09:49 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-07-08 16:09:49 -0700 |
commit | 78a5124bf09d0cd285d4d1a6242bd67badb2f621 (patch) | |
tree | 9514761d8dec2abc98ea10ef6a8ca86fb9b5cf42 | |
parent | db41f33d7c9ec436b78d2eb3afa96c9afcc9cca5 (diff) | |
download | rails-78a5124bf09d0cd285d4d1a6242bd67badb2f621.tar.gz rails-78a5124bf09d0cd285d4d1a6242bd67badb2f621.tar.bz2 rails-78a5124bf09d0cd285d4d1a6242bd67badb2f621.zip |
add a new constructor for allocating test requests
4 files changed, 31 insertions, 29 deletions
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 49a6d1c145..c63dde4ff6 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -14,6 +14,18 @@ module ActionController TestSession.new end + # Create a new test request with default `env` values + def self.create + env = {} + env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application + new(default_env.merge(env), new_session) + end + + def self.default_env + DEFAULT_ENV + end + private_class_method :default_env + def initialize(env, session) super(env) @@ -70,12 +82,6 @@ module ActionController @fullpath = @ip = @remote_ip = @protocol = nil @env['action_dispatch.request.query_parameters'] = {} end - - private - - def default_env - DEFAULT_ENV - end end class TestResponse < ActionDispatch::TestResponse @@ -516,7 +522,7 @@ module ActionController end end - @request = build_request({}, TestRequest.new_session) + @request = TestRequest.create @request.env["rack.request.cookie_hash"] = {}.with_indifferent_access @response = build_response @response_klass @response.request = @request @@ -527,10 +533,6 @@ module ActionController end end - def build_request(env, session) - TestRequest.new(env, session) - end - def build_response(klass) klass.new end diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb index 48765fe83c..543c7b78a1 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb @@ -183,7 +183,7 @@ module ActionDispatch end # Assume given controller - request = build_request({}, ActionController::TestRequest.new_session) + request = ActionController::TestRequest.create if path =~ %r{://} fail_on(URI::InvalidURIError, msg) do diff --git a/actionpack/lib/action_dispatch/testing/test_request.rb b/actionpack/lib/action_dispatch/testing/test_request.rb index 0d712f0592..b9d11cb530 100644 --- a/actionpack/lib/action_dispatch/testing/test_request.rb +++ b/actionpack/lib/action_dispatch/testing/test_request.rb @@ -10,11 +10,17 @@ module ActionDispatch "rack.request.cookie_hash" => {}.with_indifferent_access ) - def initialize(env) + # Create a new test request with default `env` values + def self.create(env = {}) env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application - super(default_env.merge(env)) + new(default_env.merge(env)) end + def self.default_env + DEFAULT_ENV + end + private_class_method :default_env + def request_method=(method) @env['REQUEST_METHOD'] = method.to_s.upcase end @@ -59,11 +65,5 @@ module ActionDispatch @env.delete('action_dispatch.request.accepts') @env['HTTP_ACCEPT'] = Array(mime_types).collect(&:to_s).join(",") end - - private - - def default_env - DEFAULT_ENV - end end end diff --git a/actionpack/test/dispatch/test_request_test.rb b/actionpack/test/dispatch/test_request_test.rb index 5613d4a879..ede1cec4e6 100644 --- a/actionpack/test/dispatch/test_request_test.rb +++ b/actionpack/test/dispatch/test_request_test.rb @@ -2,7 +2,7 @@ require 'abstract_unit' class TestRequestTest < ActiveSupport::TestCase test "sane defaults" do - env = ActionDispatch::TestRequest.new({}).env + env = ActionDispatch::TestRequest.create.env assert_equal "GET", env.delete("REQUEST_METHOD") assert_equal "off", env.delete("HTTPS") @@ -27,7 +27,7 @@ class TestRequestTest < ActiveSupport::TestCase end test "cookie jar" do - req = ActionDispatch::TestRequest.new({}) + req = ActionDispatch::TestRequest.create({}) assert_equal({}, req.cookies) assert_equal nil, req.env["HTTP_COOKIE"] @@ -55,38 +55,38 @@ class TestRequestTest < ActiveSupport::TestCase test "does not complain when Rails.application is nil" do Rails.stubs(:application).returns(nil) - req = ActionDispatch::TestRequest.new({}) + req = ActionDispatch::TestRequest.create({}) assert_equal false, req.env.empty? end test "default remote address is 0.0.0.0" do - req = ActionDispatch::TestRequest.new({}) + req = ActionDispatch::TestRequest.create({}) assert_equal '0.0.0.0', req.remote_addr end test "allows remote address to be overridden" do - req = ActionDispatch::TestRequest.new('REMOTE_ADDR' => '127.0.0.1') + req = ActionDispatch::TestRequest.create('REMOTE_ADDR' => '127.0.0.1') assert_equal '127.0.0.1', req.remote_addr end test "default host is test.host" do - req = ActionDispatch::TestRequest.new({}) + req = ActionDispatch::TestRequest.create({}) assert_equal 'test.host', req.host end test "allows host to be overridden" do - req = ActionDispatch::TestRequest.new('HTTP_HOST' => 'www.example.com') + req = ActionDispatch::TestRequest.create('HTTP_HOST' => 'www.example.com') assert_equal 'www.example.com', req.host end test "default user agent is 'Rails Testing'" do - req = ActionDispatch::TestRequest.new({}) + req = ActionDispatch::TestRequest.create({}) assert_equal 'Rails Testing', req.user_agent end test "allows user agent to be overridden" do - req = ActionDispatch::TestRequest.new('HTTP_USER_AGENT' => 'GoogleBot') + req = ActionDispatch::TestRequest.create('HTTP_USER_AGENT' => 'GoogleBot') assert_equal 'GoogleBot', req.user_agent end |