aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2016-02-10 21:46:51 +0100
committerKasper Timm Hansen <kaspth@gmail.com>2016-02-10 21:46:51 +0100
commiteee3534b1a5614d824a34e2c761faaeab07c2eb4 (patch)
tree4de249ed1a91c151200d7d09355f3ba3e2bd44f8 /actionpack/test
parent688996da7b25080a1a2ef74f5b4789f3e5eb670d (diff)
downloadrails-eee3534b1a5614d824a34e2c761faaeab07c2eb4.tar.gz
rails-eee3534b1a5614d824a34e2c761faaeab07c2eb4.tar.bz2
rails-eee3534b1a5614d824a34e2c761faaeab07c2eb4.zip
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`.
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/controller/integration_test.rb35
1 files changed, 24 insertions, 11 deletions
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