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

Redirecting function's requests to mock server #64

Closed
oren0e opened this issue Jan 19, 2022 · 2 comments
Closed

Redirecting function's requests to mock server #64

oren0e opened this issue Jan 19, 2022 · 2 comments

Comments

@oren0e
Copy link

oren0e commented Jan 19, 2022

Hi,
I want to be able to test a function that does not have the server's url as one of it's arguments. For example - if I have a function like eval_code(code: String) which uses (directly or through yet another function) e.g reqwests POST requests in its body, and let's say the requests are being sent to https://example.com/endpoint - then I want to be able to redirect those requests to the mock server in my tests, automatically.
So theoretically what I want to have is define a mock server on some real address - and then, each time a request/response is being sent to/from that address it will automatically be redirected to my mock server, allowing me to test the functions that interact with that address.
I hope my explanation was clear enough, and maybe that's even possible right now - I just couldn't find a way to do it.

@oren0e
Copy link
Author

oren0e commented Jan 19, 2022

I thought I could use a standalone server, but I want to do that in code. being able to define the port and the host when starting the server seems very trivial.
Then I can use this same server for multiple tests.

let server = MockServer::start(host="http://localhost", port=12345);

@alexliesenfeld
Copy link
Owner

I'm not aware of any method to intercept requests to a certain host. Depending on your concrete use case, you may get away with adding an entry to /etc/hosts. I'll close this issue though, because I don't think it's directly related to httpmock. Let me know if I'm mistaken and we'll reopen this issue.

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