mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Can static mappings be used in xunit tests? #282
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jamoore5 on GitHub (Jun 30, 2020).
Can static mappings be used in xunit tests?
I tried creating a mapping under __admin/mappings in the test directory but the server does not seem to be finding the definitions.
Is it something that is supported?
@StefH commented on GitHub (Jun 30, 2020):
Yes, this is possible.
I use the same logic:
https://github.com/WireMock-Net/WireMock.Net/tree/master/test/WireMock.Net.Tests/__admin/mappings
Note that
CopyToOutputDirectoryshould be set correctly : https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj#L89And when running in XUnit, use the correct c# code to get the folder + files:
5e76a82a21/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyFromFileTests.cs (L22)@jamoore5 commented on GitHub (Jul 1, 2020):
Thanks, @StefH that was very helpful.
I am seeing the mapping when the server starts, but none of them are matching the request. Any recommendations on how to debug why the mapping is not a match?
@jamoore5 commented on GitHub (Jul 1, 2020):
Loging the request and the response
the mapping
I do not see why these are not matching?
@StefH commented on GitHub (Jul 1, 2020):
When using postman, it works fine?

@jamoore5 commented on GitHub (Jul 1, 2020):
Yes it seem to work fine when I call it through the code with a standalone server, it is only failing when it is setup in the unit test. I tested the same call and mapping with a stand-alone server and it passes. I confirmed that the static mapping is loaded in the test, it just does not match.
Very confused.
@StefH commented on GitHub (Jul 1, 2020):
https://github.com/WireMock-Net/WireMock.Net/pull/482
@StefH commented on GitHub (Jul 1, 2020):
I've created a new preview version (NuGet : WireMock.Net.1.2.13-ci-13536) --> https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions
This one includes in the LogEntry a Partial which shows which things can match or don't match.
--> https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Server/WireMockServer.LogEntries.cs#L42
@StefH commented on GitHub (Jul 4, 2020):
@jamoore5
See this wiki page to help you finding your matching issue when you use the specified preview version.
(https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching-Tips)
@jamoore5 commented on GitHub (Jul 8, 2020):
Thank, the directions were very helpful. The problem looks to be that the body is not being included in the
WireMock.RequestMessage. I added a logger to the HttpClient handler and do see the body is being passed.Is there an easy way to print the LogEntry to console?
@jamoore5 commented on GitHub (Jul 8, 2020):
@StefH commented on GitHub (Jul 8, 2020):
Can you post your full unit test code?
@jamoore5 commented on GitHub (Jul 8, 2020):
Sorry code is in a privarte repo, so I cannot just share the branch. I will see if I can simply the test without the production code and still reproduce the error I am seeing.
Edit so far when I smipfy it to just make an http call it works. going to have to make a mock project to call to see if the calling through WebApplicationFactory is the problem.
@StefH commented on GitHub (Jul 8, 2020):
Can you please post the c# code for "Fetch_User_By_Id_66" ? Because that's the one which gives issues.
And indeed, just use
new HttpClient()instead of the WebApplicationFactory.@jamoore5 commented on GitHub (Jul 8, 2020):
I
am trying to integration test my applications api that makes external calls to another api. That is why I am using WebApplicationFactory.
Maybe it is the wrong approach to integration tests?
await Client.GetAsync("/api/v1.0/entities/6/User/2");call my web app, which then makes an external call to the mock server.It works when I make the direct call to the webserver but does not work when I make the indirect call through my api.
I will try to make a mock project and see if I can reproduce the issue.
@StefH commented on GitHub (Jul 8, 2020):
You want to test
a client --> web app --> external API = WireMockAre you sure the web-app does a POST request to /interactionapi/SystemService.svc/User/search ?
And how does BodyData look?

@jamoore5 commented on GitHub (Jul 8, 2020):
I added this handler on the HttpClient
and it is outputing
So I am confident that there is a body and some bug is causing the mock server not to see it.
But so far I cannot simplify the code and still reproduce the error. However I am still trying to make a simpler project that reproduces the bug.
@jamoore5 commented on GitHub (Jul 8, 2020):
I finally reproduced the issue
The cause of the problem
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);If you comment tnat line out the test passes.
Link to sample solution
Is this a bug in the plugin?
@StefH commented on GitHub (Jul 9, 2020):
Do you use windows operating system for running the tests + WireMock?
I guess you already have read this page ? --> https://docs.microsoft.com/en-us/dotnet/core/run-time-config/networking
@jamoore5 commented on GitHub (Jul 9, 2020):
MacOS
it seems that we need to turn off SocketsHttpHandler because
@StefH commented on GitHub (Jul 11, 2020):
Can this issue be closed?
@jamoore5 commented on GitHub (Jul 13, 2020):
I figured out the cause of the issue, but am not sure what to do to fix that mapping do not with my project. I guess if the configuration we are using is not supported, this can be closed.
@StefH commented on GitHub (Jul 13, 2020):
Yes sorry, it seems that using the
SocketsHttpHandleras you do is not supported by WireMock.NetSo I will close this issue now.