alexliesenfeld / httpmock

HTTP mocking library for Rust.
MIT License
435 stars 40 forks source link

Redirecting function's requests to mock server #64

Closed oren0e closed 2 years ago

oren0e commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.