aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2016-02-11 21:43:28 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2016-02-11 22:47:17 +0100
commit77bbf1e905f2ff82d715bac3f0b255cf3ade1d97 (patch)
tree92d656470f586125b41293f2d268d4d96855df29 /actionpack
parentf9f98b75b57f7ab264e6af0abcda898fe166b828 (diff)
downloadrails-77bbf1e905f2ff82d715bac3f0b255cf3ade1d97.tar.gz
rails-77bbf1e905f2ff82d715bac3f0b255cf3ade1d97.tar.bz2
rails-77bbf1e905f2ff82d715bac3f0b255cf3ade1d97.zip
Make `parsed_body` extract parser from the content type.
We're not guaranteed to have a `RequestEncoder` to assign on `get` requests because we aren't extracting the parser from the response content type. Until now.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG.md2
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb12
-rw-r--r--actionpack/lib/action_dispatch/testing/test_response.rb2
-rw-r--r--actionpack/test/controller/integration_test.rb10
4 files changed, 20 insertions, 6 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index bf964d06e9..a26e1f70a8 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -26,7 +26,7 @@
class ApiTest < ActionDispatch::IntegrationTest
test 'creates articles' do
assert_difference -> { Article.count } do
- post articles_path, { article: { title: 'Ahoy!' } }, as: :json
+ post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json
end
assert_equal({ id: Article.last.id, title: 'Ahoy!' }, response.parsed_body)
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index 742bce1ca6..f4534b4173 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -381,7 +381,7 @@ module ActionDispatch
response = _mock_session.last_response
@response = ActionDispatch::TestResponse.from_response(response)
@response.request = @request
- @response.response_parser = request_encoder
+ @response.response_parser = RequestEncoder.parser(@response.content_type)
@html_document = nil
@url_options = nil
@@ -397,6 +397,8 @@ module ActionDispatch
class RequestEncoder # :nodoc:
@encoders = {}
+ attr_reader :response_parser
+
def initialize(mime_name, param_encoder, response_parser, url_encoded_form = false)
@mime = Mime[mime_name]
@@ -424,8 +426,9 @@ module ActionDispatch
@param_encoder.call(params)
end
- def parse_body(body)
- @response_parser.call(body)
+ def self.parser(content_type)
+ mime = Mime::Type.lookup(content_type)
+ encoder(mime ? mime.ref : nil).response_parser
end
def self.encoder(name)
@@ -726,7 +729,8 @@ module ActionDispatch
# response_parser: -> body { body }
#
# Where `param_encoder` defines how the params should be encoded and
- # `response_parser` defines how the response body should be parsed.
+ # `response_parser` defines how the response body should be parsed through
+ # `parsed_body`.
#
# Consult the Rails Testing Guide for more.
diff --git a/actionpack/lib/action_dispatch/testing/test_response.rb b/actionpack/lib/action_dispatch/testing/test_response.rb
index 58d3e6eb0f..9d4b73a43d 100644
--- a/actionpack/lib/action_dispatch/testing/test_response.rb
+++ b/actionpack/lib/action_dispatch/testing/test_response.rb
@@ -22,7 +22,7 @@ module ActionDispatch
attr_writer :response_parser # :nodoc:
def parsed_body
- @response_parser.parse_body(body)
+ @parsed_body ||= @response_parser.call(body)
end
end
end
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index cb524bacb2..ea50f05f4d 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -1171,6 +1171,16 @@ class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest
Mime::Type.unregister :wibble
end
+ def test_parsed_body_without_as_option
+ with_routing do |routes|
+ routes.draw { get ':action' => FooController }
+
+ get '/foos_json.json', params: { foo: 'heyo' }
+
+ assert_equal({ 'foo' => 'heyo' }, response.parsed_body)
+ end
+ end
+
private
def post_to_foos(as:)
with_routing do |routes|