diff options
author | David Chen <darkbaby123@gmail.com> | 2016-07-31 15:05:08 +0800 |
---|---|---|
committer | David Chen <darkbaby123@gmail.com> | 2016-08-07 23:44:31 +0800 |
commit | 23ce9e9b99771080355dccfee6a8453e103682e4 (patch) | |
tree | f1b79bbbe77ad6bd8b8d98b9cf841df6df37d47d /actionpack/test | |
parent | 8f26f31934b4f0c694a081d2a89996f3935768d1 (diff) | |
download | rails-23ce9e9b99771080355dccfee6a8453e103682e4.tar.gz rails-23ce9e9b99771080355dccfee6a8453e103682e4.tar.bz2 rails-23ce9e9b99771080355dccfee6a8453e103682e4.zip |
Fix Accept header overridden when "xhr: true" in integration test
In integration test when specify the "Accept" header with "xhr: true"
option, the Accept header is overridden with a default xhr Accept
header. The issue only affects HTTP header "Accept" but not CGI variable
"HTTP_ACCEPT".
For example:
get '/page', headers: { 'Accept' => 'application/json' }, xhr: true
# This is WRONG! And the response.content_type is also affected.
# It should be "application/json"
assert_equal "text/javascript, text/html, ...", request.accept
assert_equal 'text/html', response.content_type
The issue is in `ActionDispatch::Integration::RequestHelpers`. When
setting "xhr: true" the helper sets a default HTTP_ACCEPT if blank.
But the code doesn't consider supporting both HTTP header style and
CGI variable style.
For detail see this GitHub issue:
https://github.com/rails/rails/issues/25859
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/integration_test.rb | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index 0d6a441789..210757fb76 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -403,6 +403,7 @@ class IntegrationProcessTest < ActionDispatch::IntegrationTest respond_to do |format| format.html { render plain: "OK", status: 200 } format.js { render plain: "JS OK", status: 200 } + format.json { render json: "JSON OK", status: 200 } format.xml { render xml: "<root></root>", status: 200 } format.rss { render xml: "<root></root>", status: 200 } format.atom { render xml: "<root></root>", status: 200 } @@ -727,6 +728,18 @@ class IntegrationProcessTest < ActionDispatch::IntegrationTest assert_includes @response.headers, "c" end + def test_accept_not_overriden_when_xhr_true + with_test_route_set do + get "/get", headers: { "Accept" => "application/json" }, xhr: true + assert_equal "application/json", request.accept + assert_equal "application/json", response.content_type + + get "/get", headers: { "HTTP_ACCEPT" => "application/json" }, xhr: true + assert_equal "application/json", request.accept + assert_equal "application/json", response.content_type + end + end + private def with_default_headers(headers) original = ActionDispatch::Response.default_headers |