diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 9a76b68ae1..c8ad8cee68 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -327,6 +327,12 @@ module ActionDispatch # Performs the actual request. def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) request_encoder = RequestEncoder.encoder(as) + headers ||= {} + + if method == :get && as == :json && params + headers['X-Http-Method-Override'] = 'GET' + method = :post + end if path =~ %r{://} path = build_expanded_path(path, request_encoder) do |location| @@ -361,7 +367,6 @@ module ActionDispatch } if xhr - headers ||= {} headers['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' headers['HTTP_ACCEPT'] ||= [Mime[:js], Mime[:html], Mime[:xml], 'text/xml', '*/*'].join(', ') end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index c8a45a0851..2d189f9f27 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -104,6 +104,7 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase middleware.use ActionDispatch::Callbacks middleware.use ActionDispatch::Cookies middleware.use ActionDispatch::Flash + middleware.use Rack::MethodOverride middleware.use Rack::Head yield(middleware) if block_given? end diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index e02b0b267d..7ad8dbb2bd 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -1238,6 +1238,22 @@ class IntegrationRequestEncodersTest < ActionDispatch::IntegrationTest end end + def test_get_request_with_json_uses_method_override_and_sends_a_post_request + with_routing do |routes| + routes.draw do + ActiveSupport::Deprecation.silence do + get ':action' => FooController + end + end + + get '/foos_json', params: { foo: 'heyo' }, as: :json + + assert_equal 'POST', request.method + assert_equal 'GET', request.headers['X-Http-Method-Override'] + assert_equal({ 'foo' => 'heyo' }, response.parsed_body) + end + end + private def post_to_foos(as:) with_routing do |routes|