提交 e306ca36 编写于 作者: A Andrew White

Refactor the handling of default_url_options in integration tests

This commit improves the handling of default_url_options in integration
tests by making behave closer to how a real application operates.

Specifically the following issues have been addressed:

* Options specified in routes.rb are used (fixes #546)
* Options specified in controllers are used
* Request parameters are recalled correctly
* Tests can override default_url_options directly
上级 5277d550
......@@ -201,9 +201,16 @@ def initialize(app)
reset!
end
remove_method :default_url_options
def default_url_options
{ :host => host, :protocol => https? ? "https" : "http" }
def url_options
@url_options ||= default_url_options.dup.tap do |url_options|
url_options.reverse_merge!(controller.url_options) if controller
if @app.respond_to?(:routes) && @app.routes.respond_to?(:default_url_options)
url_options.reverse_merge!(@app.routes.default_url_options)
end
url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http")
end
end
# Resets the instance. This can be used to reset the state information
......@@ -216,6 +223,7 @@ def reset!
@controller = @request = @response = nil
@_mock_session = nil
@request_count = 0
@url_options = nil
self.host = DEFAULT_HOST
self.remote_addr = "127.0.0.1"
......@@ -310,6 +318,7 @@ def process(method, path, parameters = nil, rack_env = nil)
response = _mock_session.last_response
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
@html_document = nil
@url_options = nil
@controller = session.last_request.env['action_controller.instance']
......@@ -367,12 +376,14 @@ def copy_session_variables! #:nodoc:
end
end
extend ActiveSupport::Concern
include ActionDispatch::Routing::UrlFor
def default_url_options
reset! unless integration_session
integration_session.default_url_options
end
def url_options
def default_url_options=(options)
reset! unless integration_session
integration_session.url_options
integration_session.default_url_options = options
end
def respond_to?(method, include_private = false)
......@@ -476,6 +487,7 @@ def integration_session
class IntegrationTest < ActiveSupport::TestCase
include Integration::Runner
include ActionController::TemplateAssertions
include ActionDispatch::Routing::UrlFor
@@app = nil
......@@ -495,5 +507,10 @@ def self.app=(app)
def app
super || self.class.app
end
def url_options
reset! unless integration_session
integration_session.url_options
end
end
end
......@@ -615,3 +615,83 @@ def app
assert_equal '[FILTERED]', request.filtered_env['rack.request.form_vars']
end
end
class UrlOptionsIntegrationTest < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def index
render :text => "foo#index"
end
def show
render :text => "foo#show"
end
def edit
render :text => "foo#show"
end
end
class BarController < ActionController::Base
def default_url_options
{ :host => "bar.com" }
end
def index
render :text => "foo#index"
end
end
def self.routes
@routes ||= ActionDispatch::Routing::RouteSet.new
end
def self.call(env)
routes.call(env)
end
def app
self.class
end
routes.draw do
default_url_options :host => "foo.com"
scope :module => "url_options_integration_test" do
get "/foo" => "foo#index", :as => :foos
get "/foo/:id" => "foo#show", :as => :foo
get "/foo/:id/edit" => "foo#edit", :as => :edit_foo
get "/bar" => "bar#index", :as => :bars
end
end
test "session uses default url options from routes" do
assert_equal "http://foo.com/foo", foos_url
end
test "current host overrides default url options from routes" do
get "/foo"
assert_response :success
assert_equal "http://www.example.com/foo", foos_url
end
test "controller can override default url options from request" do
get "/bar"
assert_response :success
assert_equal "http://bar.com/foo", foos_url
end
test "test can override default url options" do
default_url_options[:host] = "foobar.com"
assert_equal "http://foobar.com/foo", foos_url
get "/bar"
assert_response :success
assert_equal "http://foobar.com/foo", foos_url
end
test "current request path parameters are recalled" do
get "/foo/1"
assert_response :success
assert_equal "/foo/1/edit", url_for(:action => 'edit', :only_path => true)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册