2016-08-06 13:16:09 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/core_ext/object/with_options"
|
|
|
|
require "active_support/core_ext/object/json"
|
2014-08-08 14:41:11 -04:00
|
|
|
|
|
|
|
class PathGenerationTest < ActiveSupport::TestCase
|
|
|
|
attr_reader :app
|
|
|
|
|
2015-04-24 19:42:28 -04:00
|
|
|
class TestSet < ActionDispatch::Routing::RouteSet
|
2014-08-08 14:41:11 -04:00
|
|
|
def initialize(block)
|
|
|
|
@block = block
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
2015-08-24 18:18:29 -04:00
|
|
|
class Request < DelegateClass(ActionDispatch::Request)
|
|
|
|
def initialize(target, url_helpers, block)
|
|
|
|
super(target)
|
|
|
|
@url_helpers = url_helpers
|
2014-08-08 14:41:11 -04:00
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
2015-08-24 18:18:29 -04:00
|
|
|
def controller_class
|
|
|
|
url_helpers = @url_helpers
|
2014-08-08 14:41:11 -04:00
|
|
|
block = @block
|
|
|
|
Class.new(ActionController::Base) {
|
2015-08-24 18:18:29 -04:00
|
|
|
include url_helpers
|
2014-08-08 14:41:11 -04:00
|
|
|
define_method(:process) { |name| block.call(self) }
|
|
|
|
def to_a; [200, {}, []]; end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-24 18:18:29 -04:00
|
|
|
def make_request(env)
|
2016-08-07 19:05:28 -04:00
|
|
|
Request.new(super, url_helpers, @block)
|
2014-08-08 14:41:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_request(uri_or_host, method, path, script_name = nil)
|
|
|
|
host = uri_or_host.host unless path
|
|
|
|
path ||= uri_or_host.path
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
params = { "PATH_INFO" => path,
|
2016-08-06 13:16:09 -04:00
|
|
|
"REQUEST_METHOD" => method,
|
|
|
|
"HTTP_HOST" => host }
|
2014-08-08 14:41:11 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
params["SCRIPT_NAME"] = script_name if script_name
|
2014-08-08 14:41:11 -04:00
|
|
|
|
|
|
|
status, headers, body = app.call(params)
|
|
|
|
new_body = []
|
|
|
|
body.each { |part| new_body << part }
|
|
|
|
body.close if body.respond_to? :close
|
|
|
|
[status, headers, new_body]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_original_script_name
|
|
|
|
original_logger = Rails.logger
|
|
|
|
Rails.logger = Logger.new nil
|
|
|
|
|
|
|
|
app = Class.new(Rails::Application) {
|
|
|
|
attr_accessor :controller
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
app = self
|
|
|
|
@routes = TestSet.new ->(c) { app.controller = c }
|
|
|
|
secrets.secret_key_base = "foo"
|
|
|
|
secrets.secret_token = "foo"
|
|
|
|
end
|
|
|
|
def app; routes; end
|
|
|
|
}
|
|
|
|
|
|
|
|
@app = app
|
|
|
|
app.routes.draw { resource :blogs }
|
|
|
|
|
|
|
|
url = URI("http://example.org/blogs")
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
send_request(url, "GET", nil, "/FOO")
|
|
|
|
assert_equal "/FOO/blogs", app.instance.controller.blogs_path
|
2014-08-08 14:41:11 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
send_request(url, "GET", nil)
|
|
|
|
assert_equal "/blogs", app.instance.controller.blogs_path
|
2014-08-08 14:41:11 -04:00
|
|
|
ensure
|
|
|
|
Rails.logger = original_logger
|
|
|
|
end
|
|
|
|
end
|