Add `as` to encode a request as a specific mime type. #21671

Merged
merged 1 commit into from Feb 10, 2016

Conversation

Projects
None yet
4 participants
@kaspth
Member

kaspth commented Sep 18, 2015

Turns longish integration test setup like

post articles_path(format: :json), params: { article: { title: 'Ahoy!' } }.to_json,
  headers: { 'Content-Type' => 'application/json' }

into

post articles_path, params: { article: { title: 'Ahoy!' } }, as: :json

And removes the need to add helpers like post_json etc.

I'm not sure if encoder is the term to use internally. Perhaps interpreter would be better. Suggestions welcome.

Perhaps we'd need more mime types. Suggestions welcome.

@kaspth kaspth added the actionpack label Sep 18, 2015

@kaspth kaspth self-assigned this Sep 18, 2015

@kaspth kaspth added this to the 5.0.0 milestone Sep 18, 2015

@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Sep 19, 2015

Member

All right, added some tests and documentation.

Member

kaspth commented Sep 19, 2015

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

This comment has been minimized.

@kaspth

kaspth Sep 19, 2015

Member

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.

@kaspth

kaspth Sep 19, 2015

Member

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.

This comment has been minimized.

@rafaelfranca

rafaelfranca Dec 31, 2015

Member

I don't think so. Try to test it in a controller with curl.

@rafaelfranca

rafaelfranca Dec 31, 2015

Member

I don't think so. Try to test it in a controller with curl.

@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Oct 18, 2015

Member

I like the idea behind this. 👍 I remember having to do those setups before, and indeed we could make that setup easier.

Member

arthurnn commented Oct 18, 2015

I like the idea behind this. 👍 I remember having to do those setups before, and indeed we could make that setup easier.

@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Oct 18, 2015

Member

dont forget to add a changelog for it.

Member

arthurnn commented Oct 18, 2015

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)

This comment has been minimized.

@kaspth

kaspth Oct 18, 2015

Member

Should probably do a test for how much of an impact this has on our integration test speed.

@kaspth

kaspth Oct 18, 2015

Member

Should probably do a test for how much of an impact this has on our integration test speed.

This comment has been minimized.

@rafaelfranca

rafaelfranca Dec 31, 2015

Member

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.

@rafaelfranca

rafaelfranca Dec 31, 2015

Member

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.

@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Oct 18, 2015

Member

@arthurnn cool 😁

Member

kaspth commented Oct 18, 2015

@arthurnn cool 😁

@rafaelfranca rafaelfranca modified the milestones: 5.0.0, 5.0.0 [temp] Dec 31, 2015

Add `as` to encode a request as a specific mime type.
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
```
+ 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

This comment has been minimized.

@kaspth

kaspth Jan 4, 2016

Member

@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 😁

@kaspth

kaspth Jan 4, 2016

Member

@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 😁

@kaspth

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Jan 4, 2016

Member

Reworked this to use Mime types as well, which simplified the tiny public API some more.

Member

kaspth commented Jan 4, 2016

Reworked this to use Mime types as well, which simplified the tiny public API some more.

@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Feb 10, 2016

Member

Lovely! We have one of those annoying post_json helpers as well.

Member

dhh commented Feb 10, 2016

Lovely! We have one of those annoying post_json helpers as well.

dhh added a commit that referenced this pull request Feb 10, 2016

Merge pull request #21671 from kaspth/integration-request-encoding-he…
…lpers

Add `as` to encode a request as a specific mime type.

@dhh dhh merged commit 688996d into rails:master Feb 10, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kaspth kaspth deleted the kaspth:integration-request-encoding-helpers branch Feb 10, 2016

y-yagi added a commit to y-yagi/rails that referenced this pull request May 27, 2016

y-yagi added a commit to y-yagi/rails that referenced this pull request May 27, 2016

y-yagi added a commit to y-yagi/rails that referenced this pull request May 27, 2016

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 7, 2016

Fix API controller tests by assigning them the encoding type
- Fixes #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.

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jun 7, 2016

Fix API controller tests by assigning them the encoding type
- Fixes #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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment