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

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

Conversation

everestmz
Copy link

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

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
Copy link
Member

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
Copy link
Author

Added a test case for this 👍

@rafaelfranca
Copy link
Member

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

@rafaelfranca
Copy link
Member

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

@matthewd
Copy link
Member

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

@everestmz everestmz force-pushed the as-json-sets-request-to-json-for-controller-test branch from 73b6f59 to 3db57bd Compare August 18, 2016 22:09
@everestmz
Copy link
Author

Documentation & changelog updated as well!

@javierjulio
Copy link
Contributor

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

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
rafaelfranca added a commit that referenced this pull request Aug 19, 2016
…or-controller-test

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

Backported in 09d313a

@tak1n
Copy link
Contributor

tak1n commented Aug 19, 2016

@evmunro, @rafaelfranca awesome thanks! ❤️

@kaspth
Copy link
Contributor

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants