From 333670ceb96e74399645fca3201969e63c3c403b Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 10 Jul 2016 22:02:12 +0200 Subject: Let TestResponse assign a parser. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously we'd only assign a response parser when a request came through Action Dispatch integration tests. This made calls to `parsed_body` when a TestResponse was manually instantiated — though own doing or perhaps from a framework — unintentionally blow up because no parser was set at that time. The response can lookup a parser entirely through its own ivars. Extract request encoder to its own file and assume that a viable content type is present at TestResponse instantiation. Since the default response parser is a no-op, making `parsed_body` equal to `body`, no exceptions will be thrown. --- actionpack/test/dispatch/test_response_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionpack/test/dispatch/test_response_test.rb') diff --git a/actionpack/test/dispatch/test_response_test.rb b/actionpack/test/dispatch/test_response_test.rb index a4f9d56a6a..72e06b4590 100644 --- a/actionpack/test/dispatch/test_response_test.rb +++ b/actionpack/test/dispatch/test_response_test.rb @@ -17,4 +17,12 @@ class TestResponseTest < ActiveSupport::TestCase assert_response_code_range 500..599, :server_error? assert_response_code_range 400..499, :client_error? end + + test "response parsing" do + response = ActionDispatch::TestResponse.create(200, {}, '') + assert_equal response.body, response.parsed_body + + response = ActionDispatch::TestResponse.create(200, { 'Content-Type' => 'application/json' }, '{ "foo": "fighters" }') + assert_equal({ 'foo' => 'fighters' }, response.parsed_body) + end end -- cgit v1.2.3