1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow using parsed_body in ActionController::TestCase

… by switching the initialzation of an appropriate response parser
in `ActionDispatch::TestResponse` from eagerly to lazily.

By doing so, the response parser can be correctly set for
`ActionController::TestCase`, which doesn't include
the content type header in the constructor but only sets it at
a later time.

Fixes #34676.
This commit is contained in:
Tobias Bühlmann 2018-12-16 11:02:45 +01:00
parent ce48b5a366
commit 8246a8139c
No known key found for this signature in database
GPG key ID: 67257538FEFC2D10
3 changed files with 37 additions and 6 deletions

View file

@ -1,3 +1,21 @@
* Allow using `parsed_body` in `ActionController::TestCase`.
In addition to `ActionDispatch::IntegrationTest`, allow using
`parsed_body` in `ActionController::TestCase`:
```
class SomeControllerTest < ActionController::TestCase
def test_some_action
post :action, body: { foo: 'bar' }
assert_equal({ "foo" => "bar" }, response.parsed_body)
end
end
```
Fixes #34676.
*Tobias Bühlmann*
* Raise an error on root route naming conflicts. * Raise an error on root route naming conflicts.
Raises an ArgumentError when multiple root routes are defined in the Raises an ArgumentError when multiple root routes are defined in the

View file

@ -14,11 +14,6 @@ module ActionDispatch
new response.status, response.headers, response.body new response.status, response.headers, response.body
end end
def initialize(*) # :nodoc:
super
@response_parser = RequestEncoder.parser(content_type)
end
# Was the response successful? # Was the response successful?
def success? def success?
ActiveSupport::Deprecation.warn(<<-MSG.squish) ActiveSupport::Deprecation.warn(<<-MSG.squish)
@ -47,7 +42,11 @@ module ActionDispatch
end end
def parsed_body def parsed_body
@parsed_body ||= @response_parser.call(body) @parsed_body ||= response_parser.call(body)
end
def response_parser
@response_parser ||= RequestEncoder.parser(content_type)
end end
end end
end end

View file

@ -156,6 +156,10 @@ XML
render html: '<body class="foo"></body>'.html_safe render html: '<body class="foo"></body>'.html_safe
end end
def render_json
render json: request.raw_post
end
def boom def boom
raise "boom!" raise "boom!"
end end
@ -965,6 +969,16 @@ XML
assert_equal "q=test2", @response.body assert_equal "q=test2", @response.body
end end
def test_parsed_body_without_as_option
post :render_json, body: { foo: "heyo" }
assert_equal({ "foo" => "heyo" }, response.parsed_body)
end
def test_parsed_body_with_as_option
post :render_json, body: { foo: "heyo" }, as: :json
assert_equal({ "foo" => "heyo" }, response.parsed_body)
end
end end
class ResponseDefaultHeadersTest < ActionController::TestCase class ResponseDefaultHeadersTest < ActionController::TestCase