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

can't stub request #787

Open
Asdfif opened this issue Jul 13, 2022 · 8 comments
Open

can't stub request #787

Asdfif opened this issue Jul 13, 2022 · 8 comments
Labels

Comments

@Asdfif
Copy link

Asdfif commented Jul 13, 2022

I stub request Excon.stub({url: 'http://example.com'}, {:body => 'body', :status => 200})
Then i send request Excon.get('http://example.com', mock: true)
And i got an error Excon::Error::StubNotFound: no stubs matched
Why? I can't find answer on stack over flow.
Help me please

@geemus
Copy link
Contributor

geemus commented Jul 13, 2022

@Asdfif - Great question, I think you may have identified an edge case where the matching to stubs is overly strict that we should probably fix up. When that stub is created, it parses the URL and it looks like it interprets the path as being a blank string, whereas when the call is made it defaults to a path of '/'. Since blank string and / are not the same, it doesn't match (although in this case I would argue that it really ought to.

I can work on a patch some time in the near future, but in the mean time you can also work around this problem by being explicit with the path when you create the stub (by adding a /). So if you stub the request this way, it appears to work as you originally expected: Excon.stub({url: 'http://example.com/'}, {:body => 'body', :status => 200}).

Hope that helps, let me know if you have questions and I'll update this if/when I get a chance to fix and release.

@Asdfif
Copy link
Author

Asdfif commented Jul 13, 2022

@geemus It works. Thank you!

@Asdfif
Copy link
Author

Asdfif commented Jul 13, 2022

@geemus but if i need to stub request like this http://example.com/:id. how should i do it?

@Asdfif
Copy link
Author

Asdfif commented Jul 13, 2022

i try to use RegexpExcon.stub({url: /https?:\/\/example?.com\// }, {:body => 'body', :status => 200}) but it raise an error URI::InvalidURIError: bad URI(is not URI?)

@geemus
Copy link
Contributor

geemus commented Jul 13, 2022

@Asdfif great question. The stubs are setup so that if you specify a nil value, it will match anything. So that might be the easiest. So one option would be something like:

Excon.stub({host: 'example.com', scheme: 'https', port: 443}, {body: 'body', 'status' => 200})

By specifying host/scheme/port directly it avoids the URI parsing (which is where we ended up with the path issues before). This particular example say that anything matching that host/scheme/port should get that response (and since path is not specified, ANY path should get that response).

Conversely, you can use a regex here, but similarly I think you need to do it in the context of the parsed params (instead of the url). So in that case it might look like:

Excon.stub({host: 'example.com', scheme: 'https', port: 443, path: /\d+/}, {body: 'body', 'status' => 200})

That should match anything that matches the host/scheme/port and has a path containing one or more digits.

Does that answer your question?

@Asdfif
Copy link
Author

Asdfif commented Jul 13, 2022

it works, thank you <3

@geemus
Copy link
Contributor

geemus commented Jul 13, 2022

@Asdfif glad that got you what you needed. Thanks for asking and discussing.

I think I have a bug to fix for the first case (I would expect a url with no path to match there, and I think most other people would too). I can try to find some time soon, I'll have to think about how best to do it. Since there is a pretty straightforward work around, I'm also not as worried or hurried.

That and your subsequent questions make me feel like we probably have a lot of room to improve the documentation around stubs in the README as well though. I'd certainly welcome your help there. I'd love to hear if you have suggestions on how we could make that clearer and more accurate, or would love even more if you would be up for helping us with a pull request to improve the documentation.

Thanks!

@github-actions
Copy link

This issue has been marked inactive and will be closed if no further activity occurs.

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

No branches or pull requests

2 participants