From 922a066e9299fd94e6946ca6d4dc1507b275d288 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Fri, 5 Aug 2016 14:20:21 -0400 Subject: [PATCH] Fix GET JSON integration test request to use method override When a `GET` request is sent `as: :json` in an integration test the test should use Rack's method override to change to a post request so the paramters are included in the postdata. Otherwise it will not encode the parameters correctly for the integration test. Because integration test sets up it's own middleware, `Rack::MethodOverride` needs to be included in the integration tests as well. `headers ||= {}` was moved so that headers are never nil. They should default to a hash. Fixes #26033 [Eileen M. Uchitelle & Aaron Patterson] --- .../lib/action_dispatch/testing/integration.rb | 7 ++++++- actionpack/test/abstract_unit.rb | 1 + actionpack/test/controller/integration_test.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index 9404a18699545..7211c0bb3c3d7 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -327,6 +327,12 @@ def non_kwarg_request_warning # 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{://} location = URI.parse(path) @@ -363,7 +369,6 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n } 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 fcbbfe8a18162..0f2dd44d87728 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -110,6 +110,7 @@ def self.build_app(routes = nil) 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 a155365b5debb..b9e6a18b6285d 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -1222,6 +1222,22 @@ def test_get_parameters_with_as_option 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|