aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2015-09-19 00:11:18 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2016-01-04 23:07:34 +0100
commit52bb2d36d3141dcd8217221065d9b5fa2b12deba (patch)
tree806f79dd361e1bb2a881dd628dcbfc7df82d5d1d /actionpack/test/controller
parent904e3f4465cb2b874bd99000e96e2e6e0e03844c (diff)
downloadrails-52bb2d36d3141dcd8217221065d9b5fa2b12deba.tar.gz
rails-52bb2d36d3141dcd8217221065d9b5fa2b12deba.tar.bz2
rails-52bb2d36d3141dcd8217221065d9b5fa2b12deba.zip
Add `as` to encode a request as a specific mime type.
Turns ``` post articles_path(format: :json), params: { article: { name: 'Ahoy!' } }.to_json, headers: { 'Content-Type' => 'application/json' } ``` into ``` post articles_path, params: { article: { name: 'Ahoy!' } }, as: :json ```
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r--actionpack/test/controller/integration_test.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index d0a1d1285f..296bc1baad 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -1126,3 +1126,46 @@ class IntegrationRequestsWithSessionSetup < ActionDispatch::IntegrationTest
assert_equal({"user_name"=>"david"}, cookies.to_hash)
end
end
+
+class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest
+ class FooController < ActionController::Base
+ def foos
+ render plain: 'ok'
+ end
+ end
+
+ def test_encoding_as_json
+ assert_encoded_as :json, content_type: 'application/json'
+ end
+
+ def test_encoding_as_without_mime_registration
+ assert_raise ArgumentError do
+ ActionDispatch::IntegrationTest.register_encoder :wibble
+ end
+ end
+
+ def test_registering_custom_encoder
+ Mime::Type.register 'text/wibble', :wibble
+
+ ActionDispatch::IntegrationTest.register_encoder(:wibble, &:itself)
+
+ assert_encoded_as :wibble, content_type: 'text/wibble',
+ parsed_parameters: Hash.new # Unregistered MIME Type can't be parsed
+ ensure
+ Mime::Type.unregister :wibble
+ end
+
+ private
+ def assert_encoded_as(format, content_type:, parsed_parameters: { 'foo' => 'fighters' })
+ with_routing do |routes|
+ routes.draw { post ':action' => FooController }
+
+ post '/foos', params: { foo: 'fighters' }, as: format
+
+ assert_response :success
+ assert_match "foos.#{format}", request.path
+ assert_equal content_type, request.content_type
+ assert_equal parsed_parameters, request.request_parameters
+ end
+ end
+end