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

Allow setting of request CONTENT-TYPE with as: in controller tests #26212

Merged
merged 1 commit into from Aug 19, 2016

Conversation

Projects
None yet
8 participants
@everestmz
Contributor

everestmz commented Aug 18, 2016

Summary

Adds the option for specifications such as as: :json to set the request header, for get, post,... etc methods in controller tests.

For example: post :action, params: { ... }, as: :json sets the CONTENT-TYPE of the request to application/json.

r? @rafaelfranca

@rails-bot

This comment has been minimized.

rails-bot commented Aug 18, 2016

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (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.

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 18, 2016

Could you add a test for this feature in https://github.com/rails/rails/blob/master/actionpack/test/controller/test_case_test.rb?

It will be something like https://github.com/rails/rails/blob/master/actionpack/test/controller/test_case_test.rb#L640-L647, but we will need to assert the Content type and if the parameters where parsed using the content type provided.

@everestmz

This comment has been minimized.

Contributor

everestmz commented Aug 18, 2016

Added a test case for this 👍

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 18, 2016

could you squash your commits and a CHANGELOG entry on top of https://github.com/rails/rails/blob/master/actionpack/CHANGELOG.md?

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 18, 2016

Oh, forgot about the documentation. Could you update the process documentation to talk about this option?

@matthewd

This comment has been minimized.

Member

matthewd commented Aug 18, 2016

Does this do more than making the option shorter to type? 😕

I'm slightly concerned that it creates a parallel in the APIs that isn't supported by the implementations. cc @kaspth

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 18, 2016

Does this do more than making the option shorter to type? 😕

Yes, it actually set the content type. ActionController::TestCase doesn't support setting the content type in the process method you have to explicitly set the request.content_type. In Rails 4.2 it worked fine because we were not using the same request object that we use in real requests, now it doesn't work anymore and you have to set the content type if you want your parameters to be parsed as a JSON request.

We already have :as option in ActionDispatch::IntegrationTest, so we are just using the same API here.

Everest Munro-Zeisberger
Set the request type if as: is specified
Documentation & testing

@everestmz everestmz force-pushed the everestmz:as-json-sets-request-to-json-for-controller-test branch to 3db57bd Aug 18, 2016

@everestmz

This comment has been minimized.

Contributor

everestmz commented Aug 18, 2016

Documentation & changelog updated as well!

@javierjulio

This comment has been minimized.

Contributor

javierjulio commented Aug 19, 2016

@rafaelfranca thanks for the update and continuing to see this through! Great work @evmunro! This will hopefully make it easier for those upgrading to Rails 5 from 4.2 since I was spending a lot of time tackling this issue. Thanks ❤️

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 19, 2016

To give context why this change is needed:

Since c546a2b, ActionController::TestCase is doing the encoding/decoding roundtrip in the parameters. Before it was just using the same arguments that was given to the methods. Now, the way the parameters are decoded depends on the mime type of the request. This means that in 5.0 the code that worked in 4.2 may not work anymore because we default to 'application/x-www-form-urlencoded' mime type and the test was expecting the json roading trip.

To fix this in Rails 5.0.0 you have to set the request's content type by hand. While this is an acceptable solution it is too cumbersome. ActionDispatch::IntegrationTest already has the :as option to set the mime type that will be used to do the parameter parsing so we are adding the same option to ActionController::TestCase.

@rafaelfranca rafaelfranca merged commit 03b9a11 into rails:master Aug 19, 2016

2 checks passed

codeclimate Code Climate didn't find any new or fixed issues.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Aug 19, 2016

Merge pull request #26212 from evmunro/as-json-sets-request-to-json-f…
…or-controller-test

Allow setting of request CONTENT-TYPE with as: in controller tests
@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 19, 2016

Backported in 09d313a

@tak1n

This comment has been minimized.

Contributor

tak1n commented Aug 19, 2016

@evmunro, @rafaelfranca awesome thanks! ❤️

@kaspth

This comment has been minimized.

Member

kaspth commented Aug 19, 2016

Neat! As long as we also make @response.parsed_body work, I think we're good.

It depends on us having a content type at the time we create the response class:

@response_parser = RequestEncoder.parser(content_type)

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