-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
Add as
to encode a request as a specific mime type.
#21671
Conversation
e8e5f46
to
749b74b
Compare
All right, added some tests and documentation. |
content_type: 'text/wibble', param_encoder: -> params { params.to_s } | ||
|
||
assert_encoded_as :wibble, content_type: 'text/wibble', | ||
parsed_parameters: Hash.new # Unregistered MIME Type can't be parsed |
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.
Am I right in making this claim? Definitely feels like it could use some shine, but don't have a fully formed idea of which direction to take it.
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.
I don't think so. Try to test it in a controller with curl.
I like the idea behind this. 👍 I remember having to do those setups before, and indeed we could make that setup easier. |
dont forget to add a changelog for it. |
def process(method, path, params: nil, headers: nil, env: nil, xhr: false) | ||
def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil) | ||
request_encoder = RequestEncoder.encoder(as) | ||
location = URI.parse(path) |
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.
Should probably do a test for how much of an impact this has on our integration test speed.
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.
Can we defer the path parsing to append_format_to
? If we don't need to append a format and if the paths is not a full path there is no need to parsing.
@arthurnn cool 😁 |
Turns ``` post articles_path(format: :json), params: { article: { name: 'Ahoy!' } }.to_json, headers: { 'Content-Type' => 'application/json' } ``` into ``` post articles_path, params: { article: { name: 'Ahoy!' } }, as: :json ```
749b74b
to
52bb2d3
Compare
ActionDispatch::IntegrationTest.register_encoder(:wibble, &:itself) | ||
|
||
assert_encoded_as :wibble, content_type: 'text/wibble', | ||
parsed_parameters: Hash.new # Unregistered MIME Type can't be parsed |
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.
@rafaelfranca tried your suggestion to call a custom controller via curl, but couldn't get it to work.
When I byebug request_parameters
it seems to fetch_header
which calls @env.fetch
which already seems to have an empty hash for that key. Would you have any ideas why? I'll keep digging 😁
Reworked this to use Mime types as well, which simplified the tiny public API some more. |
Lovely! We have one of those annoying post_json helpers as well. |
…lpers Add `as` to encode a request as a specific mime type.
- Fixes rails#25183. - The `as: :json` feature was added in rails#21671 and recommended to use for JSON endpoints so let's use it by default for API controller tests.
Turns longish integration test setup like
into
And removes the need to add helpers like
post_json
etc.I'm not sure if
encoder
is the term to use internally. Perhapsinterpreter
would be better. Suggestions welcome.Perhaps we'd need more mime types. Suggestions welcome.