Add as to encode a request as a specific mime type.#21671
Conversation
e8e5f46 to
749b74b
Compare
|
All right, added some tests and documentation. |
There was a problem hiding this comment.
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.
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. |
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
@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_jsonetc.I'm not sure if
encoderis the term to use internally. Perhapsinterpreterwould be better. Suggestions welcome.Perhaps we'd need more mime types. Suggestions welcome.