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

Error when path_prefix is set and Faraday::Adapter::Test::Stubs is used #18

Open
RenzoMinelli opened this issue May 31, 2023 · 2 comments

Comments

@RenzoMinelli
Copy link
Contributor

In saddle version 0.1.0 the faraday version was bumped to ~> 0.9.0. With this change the stubs changed behaviour.
Before 0.1.0:

stubs = Faraday::Adapter::Test::Stubs.new
default_client = Saddle::Client.create(:stubs => stubs, :path_prefix => 'api/v1') 
stubs.get('/api/v1') { [200, {}, 'party'] }
default_client.requester.get('') # Returned party

After 0.1.0

stubs = Faraday::Adapter::Test::Stubs.new
default_client = Saddle::Client.create(:stubs => stubs, :path_prefix => 'api/v1') 
stubs.get('/api/v1') { [200, {}, 'party'] }
default_client.requester.get('') 
# Returned error
# Faraday::Adapter::Test::Stubs::NotFound (no stubbed request for get http://localhost/api/v1/ )

It seems that when making a request to '' the path that is used is the path_prefix + '/'.
The stub with faraday version < 0.9.0 also added the '/' at the end, but this changed in 0.9.0, where they started using Faraday::Utils.normalize_path which ends up using URI.

Is the '/' expected to be added at the end of the path or is this a bug? If it's the expected way just changing the stub to

stubs.get('/api/v1/') { [200, {}, 'party'] }

fixes the issue.

As a side note, after 0.1.0 when making a request with something rather than blank it always adds the '/'. For example:

stubs = Faraday::Adapter::Test::Stubs.new
default_client = Saddle::Client.create(:stubs => stubs, :path_prefix => 'api/v1')

stubs.get('/api/v1') { [200, {}, 'party'] }
default_client.requester.get('?a=1') # Faraday::Adapter::Test::Stubs::NotFound (no stubbed request for get http://localhost/api/v1/?a=2 )

stubs.get('/api/v1/test') { [200, {}, 'party'] }
default_client.requester.get('test') # Returns "party"
default_client.requester.get('/test') # Returns "party"

Before it has some strange behaviour:

stubs = Faraday::Adapter::Test::Stubs.new
default_client = Saddle::Client.create(:stubs => stubs, :path_prefix => 'api/v1')

stubs.get('/api/v1') { [200, {}, 'party'] }
default_client.requester.get('?a=1') # Returns "party"

stubs.get('/api/v1/test') { [200, {}, 'party'] }
default_client.requester.get('test') # Faraday::Adapter::Test::Stubs::NotFound (no stubbed request for get /api/v1/api/v1/test )
default_client.requester.get('/test') # Returns "party"
@RenzoMinelli RenzoMinelli changed the title Error when 'path_prefix' is set and `Faraday::Adapter::Test::Stubs' is used Error when path_prefix is set and Faraday::Adapter::Test::Stubs is used May 31, 2023
@mLewisLogic
Copy link
Owner

I like the proposal to add the / to the end of the path in the stub invocation. Seems the cleanest way to align with how Farady does path normalization.

@RenzoMinelli
Copy link
Contributor Author

Sounds good to me, thanks.

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

No branches or pull requests

2 participants