Skip to content
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

Merged
merged 1 commit into from
Aug 7, 2016

Conversation

darkbaby123
Copy link
Contributor

@darkbaby123 darkbaby123 commented Jul 31, 2016

Summary

Fixes #25859

@rails-bot
Copy link

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`.
Copy link
Contributor

@kaspth kaspth Aug 2, 2016

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*

@kaspth
Copy link
Contributor

kaspth commented Aug 2, 2016

We should write regression tests for this as well to prove neither HTTP_ACCEPT nor Accept are overriden.

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.

@kaspth kaspth self-assigned this Aug 2, 2016
@kaspth kaspth added this to the 5.0.1 milestone Aug 2, 2016
@darkbaby123
Copy link
Contributor Author

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.

@kaspth
Copy link
Contributor

kaspth commented Aug 7, 2016

@darkbaby123 somewhere in this test case class might be a good place:

class IntegrationProcessTest < ActionDispatch::IntegrationTest

@darkbaby123 darkbaby123 force-pushed the fix_xhr_overwrite_headers_in_test branch 3 times, most recently from 2ae8089 to 1973603 Compare August 7, 2016 14:59
@darkbaby123
Copy link
Contributor Author

@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
Copy link
Contributor

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
@darkbaby123 darkbaby123 force-pushed the fix_xhr_overwrite_headers_in_test branch from 1973603 to 23ce9e9 Compare August 7, 2016 15:44
@kaspth kaspth merged commit 15a600f into rails:master Aug 7, 2016
kaspth added a commit that referenced this pull request Aug 7, 2016
…in_test

Fix Accept header overwritten issue when "xhr: true" in integration test
@kaspth
Copy link
Contributor

kaspth commented Aug 7, 2016

Thanks for the help! I've backported this to the pending 5.0.1 as well: 9047d9e

@darkbaby123 darkbaby123 deleted the fix_xhr_overwrite_headers_in_test branch August 8, 2016 01:15
@darkbaby123
Copy link
Contributor Author

@kaspth Glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants