提交 a9fde9a2 编写于 作者: J Joshua Peek

Cleanup dispatch path

上级 3b35366d
......@@ -65,18 +65,23 @@ def to_prepare(identifier = nil, &block)
include ActiveSupport::Callbacks
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch
# DEPRECATE: Remove arguments
# DEPRECATE: Remove arguments, since they are only used by CGI
def initialize(output = $stdout, request = nil, response = nil)
@output, @request, @response = output, request, response
@output = output
@app = @@middleware.build(lambda { |env| self.dup._call(env) })
end
def dispatch
begin
run_callbacks :before_dispatch
handle_request
controller = Routing::Routes.recognize(@request)
controller.process(@request, @response).to_a
rescue Exception => exception
failsafe_rescue exception
if controller ||= (::ApplicationController rescue Base)
controller.process_with_exception(@request, @response, exception).to_a
else
raise exception
end
ensure
run_callbacks :after_dispatch, :enumerator => :reverse_each
end
......@@ -123,19 +128,5 @@ def checkin_connections
return if @request.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end
protected
def handle_request
@controller = Routing::Routes.recognize(@request)
@controller.process(@request, @response).out
end
def failsafe_rescue(exception)
if @controller ||= (::ApplicationController rescue Base)
@controller.process_with_exception(@request, @response, exception).out
else
raise exception
end
end
end
end
......@@ -83,7 +83,7 @@ def status
@status || super
end
def out(&block)
def to_a(&block)
@block = block
@status = headers.delete("Status")
if [204, 304].include?(status.to_i)
......@@ -93,7 +93,6 @@ def out(&block)
[status, headers.to_hash, self]
end
end
alias to_a out
def each(&callback)
if @body.respond_to?(:call)
......
......@@ -96,7 +96,9 @@ def test_to_prepare_with_identifier_replaces
private
def dispatch(cache_classes = true)
Dispatcher.any_instance.stubs(:handle_request).returns([200, {}, 'response'])
controller = mock()
controller.stubs(:process).returns([200, {}, 'response'])
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
Dispatcher.define_dispatcher_callbacks(cache_classes)
@dispatcher.call({})
end
......
......@@ -236,7 +236,7 @@ def test_simple_output
@response.body = "Hello, World!"
@response.prepare!
status, headers, body = @response.out
status, headers, body = @response.to_a
assert_equal "200 OK", status
assert_equal({
"Content-Type" => "text/html; charset=utf-8",
......@@ -257,7 +257,7 @@ def test_streaming_block
end
@response.prepare!
status, headers, body = @response.out
status, headers, body = @response.to_a
assert_equal "200 OK", status
assert_equal({"Content-Type" => "text/html; charset=utf-8", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers)
......@@ -293,6 +293,6 @@ def test_status
private
def response_headers
@response.prepare!
@response.out[1]
@response.to_a[1]
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册