WireMock not working as expected #323

Closed
opened 2025-12-29 15:20:46 +01:00 by adam · 12 comments
Owner

Originally created by @redgoose on GitHub (Jan 14, 2021).

Originally assigned to: @StefH on GitHub.

I'm trying to write some integration tests using Microsoft.AspNetCore.TestHost.TestServer and WireMock and not able to get things working as expected.

This is my test and here is the HTTP call I'm trying to mock.

Despite following this guide, my test fails and makes an outbound HTTP call.

Am I matching incorrectly? Is WireMock not meant to be used with TestServer?

Thanks in advance!

Originally created by @redgoose on GitHub (Jan 14, 2021). Originally assigned to: @StefH on GitHub. I'm trying to write some integration tests using Microsoft.AspNetCore.TestHost.TestServer and WireMock and not able to get things working as expected. This is my [test](https://github.com/redgoose/dotnet-learning/blob/master/Tests/UnitTest1.cs#L56) and here is the [HTTP call](https://github.com/redgoose/dotnet-learning/blob/master/TodoAPI/Integrations/IpifyClient.cs#L20) I'm trying to mock. Despite following [this guide](https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests), my test fails and makes an outbound HTTP call. Am I matching incorrectly? Is WireMock not meant to be used with TestServer? Thanks in advance!
adam added the question label 2025-12-29 15:20:46 +01:00
adam closed this issue 2025-12-29 15:20:48 +01:00
Author
Owner

@redgoose commented on GitHub (Jan 14, 2021):

hmm... added an additional test here that doesn't use TestServer and uses the IpifyClient directly and that also fails. Any ideas?

@redgoose commented on GitHub (Jan 14, 2021): hmm... added an additional test [here](https://github.com/redgoose/dotnet-learning/blob/master/Tests/UnitTest1.cs#L84) that doesn't use TestServer and uses the IpifyClient directly and that also fails. Any ideas?
Author
Owner

@StefH commented on GitHub (Jan 15, 2021):

@redgoose
The WithPath is used like "/pathabc"

Did you try WithParam(...) ?

See
477f3b5cd3/test/WireMock.Net.Tests/RequestTests.cs (L129)

@StefH commented on GitHub (Jan 15, 2021): @redgoose The WithPath is used like `"/pathabc"` Did you try `WithParam(...)` ? See https://github.com/WireMock-Net/WireMock.Net/blob/477f3b5cd346a1805a6281fd62bf1e61e791065a/test/WireMock.Net.Tests/RequestTests.cs#L129
Author
Owner

@redgoose commented on GitHub (Jan 15, 2021):

yep i tried with Request.Create().WithParam("format", "json") as well.

@redgoose commented on GitHub (Jan 15, 2021): yep i tried with `Request.Create().WithParam("format", "json")` as well.
Author
Owner

@StefH commented on GitHub (Jan 20, 2021):

@redgoose

I think that the http-client is not connecting to the WireMock.Net server.

(You need to send the request to server.Urls[0])

I've updated the wiki:
https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests

And here is an example:
92923a12ae/test/WireMock.Net.Tests/WireMockServerTests.cs (L268)

@StefH commented on GitHub (Jan 20, 2021): @redgoose I think that the http-client is not connecting to the WireMock.Net server. (You need to send the request to `server.Urls[0]`) I've updated the wiki: https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests And here is an example: https://github.com/WireMock-Net/WireMock.Net/blob/92923a12ae70aa5dc7bc228e49499d2ed7e911d8/test/WireMock.Net.Tests/WireMockServerTests.cs#L268
Author
Owner

@redgoose commented on GitHub (Feb 1, 2021):

That example doesn't make sense to me. Why are we POSTing to that url? The example in the previous revision made sense since the SUT would do something to trigger an HTTP request. var response = _sut.DoSomething();

Are you able to checkout my test app (https://github.com/redgoose/dotnet-learning) and see if you can get test TestAPIBaz to work?

Also, you've introduced an undeclared variable stringContent into your example.

@redgoose commented on GitHub (Feb 1, 2021): That example doesn't make sense to me. Why are we POSTing to that url? The example in the previous revision made sense since the SUT would do something to trigger an HTTP request. `var response = _sut.DoSomething();` Are you able to checkout my test app (https://github.com/redgoose/dotnet-learning) and see if you can get test `TestAPIBaz` to work? Also, you've introduced an undeclared variable `stringContent` into your example.
Author
Owner

@StefH commented on GitHub (Feb 1, 2021):

Example is updated to use Get and the URL is changed to _server.Urls[0]}/foo.
This does make more sense in the example.

I'll checkout your example and see if I can fix it.

@StefH commented on GitHub (Feb 1, 2021): Example is updated to use Get and the URL is changed to `_server.Urls[0]}/foo`. This does make more sense in the example. I'll checkout your example and see if I can fix it.
Author
Owner

@StefH commented on GitHub (Feb 1, 2021):

See PR : https://github.com/redgoose/dotnet-learning/pull/1

@StefH commented on GitHub (Feb 1, 2021): See PR : https://github.com/redgoose/dotnet-learning/pull/1
Author
Owner

@redgoose commented on GitHub (Feb 2, 2021):

Thanks for your help so far. I looked at the PR and have a question. Why on this line are we matching on /api/learning/baz when I'm trying to stub out the HTTP call to https://api.ipify.org?format=json ?

The logic is, GET /api/learning/baz makes an HTTP GET to https://api.ipify.org?format=json. It's that latter request I'm trying to stub out.

@redgoose commented on GitHub (Feb 2, 2021): Thanks for your help so far. I looked at the PR and have a question. Why on this [line ](https://github.com/redgoose/dotnet-learning/pull/1/files#diff-0b8b7c4df4e7f9ee16e33c4850d60761449510462bf44c29b63cd80c76607f87L66-R66) are we matching on `/api/learning/baz` when I'm trying to stub out the HTTP call to `https://api.ipify.org?format=json` ? The logic is, GET `/api/learning/baz` makes an HTTP GET to `https://api.ipify.org?format=json`. It's that latter request I'm trying to stub out.
Author
Owner

@StefH commented on GitHub (Feb 3, 2021):

For this example it does not really matter.

If you setup a stub , and then call wiremock using the same path or query parameters ; that will match.

Also view this to get a general understanding on usage:
https://www.youtube.com/watch?v=IJa6DyJOxzk

@StefH commented on GitHub (Feb 3, 2021): For this example it does not really matter. If you setup a stub , and then call wiremock using the same path or query parameters ; that will match. Also view this to get a general understanding on usage: https://www.youtube.com/watch?v=IJa6DyJOxzk
Author
Owner

@redgoose commented on GitHub (Feb 9, 2021):

Okay, what about this test. I'm unable to get the matching to work, do you know what the problem is?

@redgoose commented on GitHub (Feb 9, 2021): Okay, what about this [test](https://github.com/redgoose/dotnet-learning/blob/master/Tests/UnitTest1.cs#L84). I'm unable to get the matching to work, do you know what the problem is?
Author
Owner

@StefH commented on GitHub (Feb 9, 2021):

  1. The IpyfyClient needs to be updated in such a way that it's possible to provide the real base-address and also the mocked base address from WireMock
  2. For a match, you need to use WithParam(...)

See my updated PR
https://github.com/redgoose/dotnet-learning/pull/1

@StefH commented on GitHub (Feb 9, 2021): 1. The IpyfyClient needs to be updated in such a way that it's possible to provide the real base-address and also the mocked base address from WireMock 2. For a match, you need to use `WithParam(...)` See my updated PR https://github.com/redgoose/dotnet-learning/pull/1
Author
Owner

@redgoose commented on GitHub (Feb 10, 2021):

thanks so much for your assistance!

@redgoose commented on GitHub (Feb 10, 2021): thanks so much for your assistance!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#323