alexliesenfeld / httpmock

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

Request not matching with any route #102

Closed sam-rusty closed 3 months ago

sam-rusty commented 3 months ago

I am trying to mock stripe api by following the docs, but looks like i am doing something wrong. I have the following code:

std::env::set_var("STRIPE_API_URL", stripe_server.url("/v1"));
stripe_server
        .mock(|when, then| {
            when.path("/payment_intents");
            then.status(200);
        });

and in my stripe service i am using the STRIPE_API_URL variable and the endpoint look like this http://127.0.0.1:51914/v1/payment_intents, which should matches with the mock server. but i am getting {"message":"Request did not match any route or mock"} I tried adding method and header_exists to when but nothing work.

What am i missing?

Thanks!

alexliesenfeld commented 3 months ago

Thanks for reaching out.

The stripe_server.mock call returns a mock object, which you can use to check method calls and get ideas about what might be going wrong in your code.

Here is an example: https://github.com/alexliesenfeld/httpmock/blob/60e68ed338230f4efe7f35f3378e2fca945a26a2/tests/examples/getting_started_tests.rs#L23

sam-rusty commented 3 months ago

That prints No request has been received by the mock server.

alexliesenfeld commented 3 months ago

Then your client has not sent any requests to the mock server. Make sure the stripe client actually sends the request to the correct mock server.

Please be aware that the mock server address is not static by default, as the port gets automatically assigned by the operating system. You can get the address like this:

https://github.com/alexliesenfeld/httpmock/blob/60e68ed338230f4efe7f35f3378e2fca945a26a2/tests/examples/getting_started_tests.rs#L20

or like this:

https://github.com/alexliesenfeld/httpmock/blob/60e68ed338230f4efe7f35f3378e2fca945a26a2/tests/examples/getting_started_tests.rs#L45

If you cannot set the address for your client programmatically (i.e., if you can only use environment variables), maybe it’s worth taking a look at std::env::set, probably in combination with the serial_test crate.

sam-rusty commented 3 months ago

if you look at my code above, i am getting the address from mock server and passing to env variable to use inside of my stripe service. i verified that it is sending the request using mock server address including the port . to fix, i just replaced the httpmock with mockito and it works without any changes to stripe service.

alexliesenfeld commented 3 months ago

I see you set up the mock to respond when.path("/payment_intents"). The correct HTTP path would probably be /v1/payment_intents, since that‘s the request path the server sees. See this working example project for more details.

If it simply starts working for you by replacing libraries, I would suggest double checking your client code or mock setup again. For example, No request has been received by the mock server. indicates that your client did not sent any requests to the server (i.e., the server did not receive anything during the test).

If you still have problems I could help you fixing it, but it would be good to have an example project that does not work for you. This example project does work, so maybe you can spot any differences between it and your code.

Please feel free to reopen this issue should you want to continue.