diff options
author | Jeremy Kemper <jeremykemper@gmail.com> | 2015-02-24 17:28:45 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremykemper@gmail.com> | 2015-02-25 09:49:25 -0700 |
commit | f6e293ec54f02f83cdb37502bea117f66f87bcae (patch) | |
tree | bf2d9a7d1de683860700a1c11ca0594bd304fc1e /actionpack/test/controller | |
parent | 2abed7af6e4b87da7277952e9ad73681ac337f2e (diff) | |
download | rails-f6e293ec54f02f83cdb37502bea117f66f87bcae.tar.gz rails-f6e293ec54f02f83cdb37502bea117f66f87bcae.tar.bz2 rails-f6e293ec54f02f83cdb37502bea117f66f87bcae.zip |
Fix default headers in test responses
Fixes regression in #18423. Merge default headers for new responses,
but don't merge when creating a response from the last session request.
hat tip @senny :heart:
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/integration_test.rb | 34 | ||||
-rw-r--r-- | actionpack/test/controller/test_case_test.rb | 46 |
2 files changed, 64 insertions, 16 deletions
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 9ab1549e8e..438c044da2 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -397,9 +397,9 @@ class IntegrationProcessTest < ActionDispatch::IntegrationTest redirect_to action_url('get') end - def remove_default_header - response.headers.except! 'X-Frame-Options' - head :ok + def remove_header + response.headers.delete params[:header] + head :ok, 'c' => '3' end end @@ -640,25 +640,27 @@ class IntegrationProcessTest < ActionDispatch::IntegrationTest end end - def test_removed_default_headers_on_test_response_are_not_reapplied + def test_respect_removal_of_default_headers_by_a_controller_action with_test_route_set do - begin - header_to_remove = 'X-Frame-Options' - original_default_headers = ActionDispatch::Response.default_headers - ActionDispatch::Response.default_headers = { - 'X-Content-Type-Options' => 'nosniff', - header_to_remove => 'SAMEORIGIN', - } - get '/remove_default_header' - assert_includes headers, 'X-Content-Type-Options' - assert_not_includes headers, header_to_remove, "Should not contain removed default header" - ensure - ActionDispatch::Response.default_headers = original_default_headers + with_default_headers 'a' => '1', 'b' => '2' do + get '/remove_header', params: { header: 'a' } end end + + assert_not_includes @response.headers, 'a', 'Response should not include default header removed by the controller action' + assert_includes @response.headers, 'b' + assert_includes @response.headers, 'c' end private + def with_default_headers(headers) + original = ActionDispatch::Response.default_headers + ActionDispatch::Response.default_headers = headers + yield + ensure + ActionDispatch::Response.default_headers = original + end + def with_test_route_set with_routing do |set| controller = ::IntegrationProcessTest::IntegrationController.clone diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index ca854040b7..e348749f78 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -965,6 +965,52 @@ XML end end +class ResponseDefaultHeadersTest < ActionController::TestCase + class TestController < ActionController::Base + def remove_header + headers.delete params[:header] + head :ok, 'C' => '3' + end + end + + setup do + @original = ActionDispatch::Response.default_headers + @defaults = { 'A' => '1', 'B' => '2' } + ActionDispatch::Response.default_headers = @defaults + end + + teardown do + ActionDispatch::Response.default_headers = @original + end + + def setup + super + @controller = TestController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @request.env['PATH_INFO'] = nil + @routes = ActionDispatch::Routing::RouteSet.new.tap do |r| + r.draw do + get ':controller(/:action(/:id))' + end + end + end + + test "response contains default headers" do + # Response headers start out with the defaults + assert_equal @defaults, response.headers + + get :remove_header, params: { header: 'A' } + assert_response :ok + + # After a request, the response in the test case doesn't have the + # defaults merged on top again. + assert_not_includes response.headers, 'A' + assert_includes response.headers, 'B' + assert_includes response.headers, 'C' + end +end + module EngineControllerTests class Engine < ::Rails::Engine isolate_namespace EngineControllerTests |