From 77bbf1e905f2ff82d715bac3f0b255cf3ade1d97 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 11 Feb 2016 21:43:28 +0100 Subject: [PATCH] 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. --- actionpack/CHANGELOG.md | 2 +- .../lib/action_dispatch/testing/integration.rb | 12 ++++++++---- .../lib/action_dispatch/testing/test_response.rb | 2 +- actionpack/test/controller/integration_test.rb | 10 ++++++++++ 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 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n 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 @@ def build_full_uri(path, env) 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 @@ def encode_params(params) @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 @@ def method_missing(sym, *args, &block) # 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 @@ def self.from_response(response) 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 @@ def test_registering_custom_encoder 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| -- GitLab