-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Accept header overwritten issue when "xhr: true" in integration test #26003
Fix Accept header overwritten issue when "xhr: true" in integration test #26003
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
@@ -1,3 +1,10 @@ | |||
* Fix `xhr: true` issue in `ActionDispatch::Integration::RequestHelpers`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think the title says much. I think we could condense this to:
* Don't override the `Accept` header in integration tests when called with `xhr: true`.
Fixes #25859.
*David Chen*
We should write regression tests for this as well to prove neither Can we also put that lovely investigative work you did in the issue into your commit message? That way future readers won't have to go to GitHub for info. |
Can you tell me which file the test should be written? If I should create a new test file, which file name and path do you suggest? I thought to write a test before but didn't find a proper place. Because this is a issue in test helper. |
@darkbaby123 somewhere in this test case class might be a good place:
|
2ae8089
to
1973603
Compare
@kaspth Yes. I just finished the work and put the test into that file. Thanks for the test code so I can find a bug for my previous solution! The changelog and commit and also modified. |
@@ -727,6 +728,16 @@ def test_respect_removal_of_default_headers_by_a_controller_action | |||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a test for HTTP_ACCEPT
too.
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: rails#25859
1973603
to
23ce9e9
Compare
…in_test Fix Accept header overwritten issue when "xhr: true" in integration test
Thanks for the help! I've backported this to the pending 5.0.1 as well: 9047d9e |
@kaspth Glad to help! |
Summary
Fixes #25859