From eee3534b1a5614d824a34e2c761faaeab07c2eb4 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Wed, 10 Feb 2016 21:46:51 +0100 Subject: Add `parsed_body` to spare writing out parsing routines. When testing: ```ruby post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json ``` It's common to want to make assertions on the response body. Perhaps the server responded with JSON, so you write `JSON.parse(response.body)`. But that gets tedious real quick. Instead add `parsed_body` which will automatically parse the reponse body as what the last request was encoded `as`. --- actionpack/test/controller/integration_test.rb | 35 ++++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 296bc1baad..cb524bacb2 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -1129,13 +1129,23 @@ end class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest class FooController < ActionController::Base - def foos + def foos_json + render json: params.permit(:foo) + end + + def foos_wibble render plain: 'ok' end end def test_encoding_as_json - assert_encoded_as :json, content_type: 'application/json' + post_to_foos as: :json do + assert_response :success + assert_match 'foos_json.json', request.path + assert_equal 'application/json', request.content_type + assert_equal({ 'foo' => 'fighters' }, request.request_parameters) + assert_equal({ 'foo' => 'fighters' }, response.parsed_body) + end end def test_encoding_as_without_mime_registration @@ -1147,25 +1157,28 @@ class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest def test_registering_custom_encoder Mime::Type.register 'text/wibble', :wibble - ActionDispatch::IntegrationTest.register_encoder(:wibble, &:itself) + ActionDispatch::IntegrationTest.register_encoder(:wibble, + param_encoder: -> params { params }) - assert_encoded_as :wibble, content_type: 'text/wibble', - parsed_parameters: Hash.new # Unregistered MIME Type can't be parsed + post_to_foos as: :wibble do + assert_response :success + assert_match 'foos_wibble.wibble', request.path + assert_equal 'text/wibble', request.content_type + assert_equal Hash.new, request.request_parameters # Unregistered MIME Type can't be parsed. + assert_equal 'ok', response.parsed_body + end ensure Mime::Type.unregister :wibble end private - def assert_encoded_as(format, content_type:, parsed_parameters: { 'foo' => 'fighters' }) + def post_to_foos(as:) with_routing do |routes| routes.draw { post ':action' => FooController } - post '/foos', params: { foo: 'fighters' }, as: format + post "/foos_#{as}", params: { foo: 'fighters' }, as: as - assert_response :success - assert_match "foos.#{format}", request.path - assert_equal content_type, request.content_type - assert_equal parsed_parameters, request.request_parameters + yield end end end -- cgit v1.2.3