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

Respect the params_encoder in Faraday::Adapter::Test #1316

Merged
merged 3 commits into from Aug 30, 2021

Conversation

yykamei
Copy link
Contributor

@yykamei yykamei commented Aug 28, 2021

Close #1312

Faraday provides a feature to let its callers configure
params_encoder, but Faraday::Adapter::Test had always created test
stubs with #parse_nested_query (NestedParamsEncoder). So, we had been not
able to test such a connection with Faraday::Adapter::Test. This might
be inconvenient for users who want to use non-default params_encoder,
such as Faraday::FlatParamsEncoder.

This patch changed the adapter to use the same params_encoder as its
connection in order to solve the problem.

Todos

  • Tests
  • Documentation (I thought the update of documentation is not required in this PR)

Faraday provides a feature to let its callers configure
`params_encoder`, but `Faraday::Adapter::Test` had always created test
stubs with `#parse_nested_query` (`NestedParamsEncoder`). So, we had not
able to test such a connection with `Faraday::Adapter::Test`. This might
be inconvenient for users who want to use non-default `params_encoder`,
such as `Faraday::FlatParamsEncoder`.

This patch changed the adapter to use the same `params_encoder` as its
connection in order to solve the problem.
@yykamei yykamei marked this pull request as ready for review August 28, 2021 13:04
# uncomment to raise Stubs::NotFound
# assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
stubs.verify_stubbed_calls
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to add this test in #1298 😅

Copy link
Member

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @yykamei, I left a comment with a suggestion on how to keep the code cleaner and more maintainable. I'm scared that the duplication introduced might cause issues in the future when only one side is updated and not the other.

I'm also open to different solutions that solve the same issue in different ways, mine is only an idea

examples/client_spec.rb Outdated Show resolved Hide resolved
Comment on lines 247 to 248
params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder
env[:params] = params_encoder.decode(env[:url].query) || {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmh I really like how much simpler the matches? methods signature are now, thanks to the env being passed, but I notice there's a lot a of repeated code between here and there. These two lines for example, and line 242 as well.

I wonder if instead of reusing env it might make sense to introduce a new Struct, internal and only used in the Test adapter, that holds this information so we only need to calculate it once. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your quick review! It makes sense 👍 I'll try it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to avoid similar codes with 91689f0. How about the change?

yykamei and others added 2 commits August 29, 2021 10:09
Co-authored-by: Matt <iMacTia@users.noreply.github.com>
In order to avoid dups of `params_encoder.decode`, set `env[:params]`
with the vaule generated by `params_encoder.decode` before calling
`stubs.match`.
Copy link
Member

@iMacTia iMacTia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! Yes the code is definitely cleaner 👏
LGTM 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants