提交 6d4ac272 编写于 作者: M Matthew Draper

Clean up the test request/response even after an exception

上级 c4cb6862
......@@ -527,34 +527,37 @@ def process(action, *args)
@request.set_header k, @controller.config.relative_url_root
end
@controller.recycle!
@controller.dispatch(action, @request, @response)
@request = @controller.request
@response = @controller.response
@request.delete_header 'HTTP_COOKIE'
begin
@controller.recycle!
@controller.dispatch(action, @request, @response)
ensure
@request = @controller.request
@response = @controller.response
@request.delete_header 'HTTP_COOKIE'
if @request.have_cookie_jar?
unless @request.cookie_jar.committed?
@request.cookie_jar.write(@response)
self.cookies.update(@request.cookie_jar.instance_variable_get(:@cookies))
end
end
@response.prepare!
if @request.have_cookie_jar?
unless @request.cookie_jar.committed?
@request.cookie_jar.write(@response)
self.cookies.update(@request.cookie_jar.instance_variable_get(:@cookies))
if flash_value = @request.flash.to_session_value
@request.session['flash'] = flash_value
else
@request.session.delete('flash')
end
end
@response.prepare!
if flash_value = @request.flash.to_session_value
@request.session['flash'] = flash_value
else
@request.session.delete('flash')
end
if xhr
@request.delete_header 'HTTP_X_REQUESTED_WITH'
@request.delete_header 'HTTP_ACCEPT'
end
@request.query_string = ''
if xhr
@request.delete_header 'HTTP_X_REQUESTED_WITH'
@request.delete_header 'HTTP_ACCEPT'
@response.sent!
end
@request.query_string = ''
@response.sent!
@response
end
......
......@@ -154,6 +154,10 @@ def test_with_body
render html: '<body class="foo"></body>'.html_safe
end
def boom
raise 'boom!'
end
private
def generate_url(opts)
......@@ -981,6 +985,26 @@ def test_redirect_url_only_cares_about_location_header
assert_redirected_to 'created resource'
end
end
def test_exception_in_action_reaches_test
assert_raise(RuntimeError) do
process :boom, method: "GET"
end
end
def test_request_state_is_cleared_after_exception
assert_raise(RuntimeError) do
process :boom,
method: "GET",
params: { q: 'test1' }
end
process :test_query_string,
method: "GET",
params: { q: 'test2' }
assert_equal "q=test2", @response.body
end
end
class ResponseDefaultHeadersTest < ActionController::TestCase
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册