WireMock.Net is a flexible library for stubbing and mocking web HTTP responses using requests matching criteria.
8/20/2020 1:37:54 AM [Info] : WireMock.Net server listening at http://localhost:8700
$ echo "$(curl -H 'Accept:*/*' http://localhost:8700)"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (7) Failed to connect to localhost port 8700: Connection refused
WireMockServer.Start
. When we run tests in CI then we want to use docker container with WireMock and connect to it from the test code and from the tested application. Besides that difference, we would like to have a consistent API to define mocking rules. However, it doesn't seem to be possible right now because WireMock client uses different models to define mappings (MappingModel
) and the MappingConverter
which could convert IMapping
to MappingModel
has internal
modifier. Is it possible to expose the method which could perform that conversion? Or am I doing something wrong and there's an option to define Mappings once and use it with WireMockServer and with standalone (docker) app?
WireMockServerSettings settings = null;
settings = new WireMockServerSettings
{
Urls = new[] { appSettings["url"] },
StartAdminInterface = true,
AllowPartialMapping =true,
Logger = new WireMockConsoleLogger(),
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = appSettings["proxyUrl"],
}
};
var wiremockServer = StandAloneApp.Start(settings);
wiremockServer
.Given(
Request.Create().WithPath("/Accounts/api/securityhelper/getauthtoken")
.UsingGet()
.WithCookie("paycorAuth", new RegexMatcher(".*"))
)
//.AtPriority(100)
.RespondWith(
Response.Create()
.WithProxy(appSettings["proxyUrl"])
.WithBody("12345")
);
wiremockServer.SaveStaticMappings();
Console.WriteLine("Press any key to stop the server");
Console.ReadKey();
}
}
@genesispvtltd I did already comment on your issue (WireMock-Net/WireMock.Net#580) and I'll copy-paste the info also here as a reference:
Short answer: WireMock.Net does not yet support a "Smart Proxy" functionality as you describe.
Long answer: This can probably be implemented as defined here:
Scenario 1
In case the real endpoint is up (we need to determine the criteria for up, like != 200 OK), proxy the request + response to the real endpoint.
Scenario 2
In case the real endpoint is down, try to find a configured mapping:
If the mapping is found, return that mapping.
If the mapping is not found, just return default 404 'mapping not found'.
@StefH need some information regarding NTLM authentication some of our endpoints are using NTLM authentication so i need to use proxying there so the code looks like below Cookie AuthCookie = null;
try
{
var authRequest = WebRequest.Create("https://localhost:7777/Accounts/Auth") as HttpWebRequest;
authRequest.CookieContainer = new CookieContainer();
authRequest.AllowAutoRedirect = false;
authRequest.ContentType = "application/x-www-form-urlencoded";
authRequest.KeepAlive = true;
CredentialCache userCredentials = new CredentialCache();
userCredentials.Add(
new Uri("https://live.com/"),
"NTLM",
new NetworkCredential("username", "Password", ""));
authRequest.Credentials = userCredentials;
using (var authResponse = authRequest.GetResponse() as HttpWebResponse)
{
AuthCookie = authResponse.Cookies["payment"];
}
}
catch (Exception ex)
{
}
And I have created a stub as well wireMockServer
.Given(
Request.Create().WithPath("/Accounts/Auth")
.UsingGet()
)
.RespondWith(
Response.Create()
.WithProxy(proxyUrl)
);
But when I proxy I am getting the underlying connection is closed message any example how I can use NTLM authentication I would appreciate thanks
Hello!
I try to install the WireMock.Net NuGet package (https://www.nuget.org/packages/WireMock.Net/) with the command line: "Install-Package WireMock.Net"
After a while, the command line tells me: "Install-Package: Unable to find dependent package(s) (Handlebars.Net)"? Handlebars.Net is a dependency of WireMock, however, I thought, NuGet resolves the dependencies automatically.
_serviceMock = FluentMockServer.Start(new FluentMockServerSettings {
Logger = new WireMockConsoleLogger()
});
Logger
is a IWireMockLogger
instead a ILogger
I want to create a mock response for SOAP service which accepts XML request body. I'm using XPathMatcher for request matching. Request is matching successfully. I need to read one attribute of request.body and create the response using that.
My XML request is like this. I need the token id to create the response body.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.Test.nl/XMLHeader/10" xmlns:req="http://www.Test.nl/Betalen/card/Services/RdplDbTknLystByOvkLyst/8/Req">
<soapenv:Header>
<ns:ReqHeader>
<ns:HeaderVersion>10</ns:HeaderVersion>
<ns:MessageId>MsgId10</ns:MessageId>
<ns:ServiceVersion>8</ns:ServiceVersion>
<ns:MessageTimestamp>?</ns:MessageTimestamp>
</ns:ReqHeader>
</soapenv:Header>
<soapenv:Body>
<req:RdplDbTknLystByOvkLyst_REQ>
<req:Code>CSC</req:Code>
<!--Optional:-->
<req:Detail>Test</req:Detail>
<req:TokenIdLijst>
<!--1 to 10 repetitions:-->
<req:Tokenid>0000083256</req:Tokenid>
<req:Tokenid>0000083259</req:Tokenid>
</req:TokenIdLijst>
</req:RdplDbTknLystByOvkLyst_REQ>
</soapenv:Body>
</soapenv:Envelope>
I am using these matcher model while creating mapping
var matcherModel = new MatcherModel
{
Name = "WildcardMatcher",
Pattern = "/xpathsoap",
RejectOnMatch = false,
IgnoreCase = true
};
var bodymatcherModel = new MatcherModel
{
Name = "XPathMatcher",
Pattern = "//*[local-name() = 'RdplDbTknLystByOvkLyst_REQ']",
IgnoreCase = true
};
I am trying to get the response as below.
var response = new ResponseModel()
{
StatusCode = 201,
Headers = header,
Body = "{{ XPath.SelectNodes request.body \"//TokenIdLijst/Tokenid \"}}",
UseTransformer = true
};
I am able to get the whole request.body. But Response.body is always empty after applying xpath.
Hi everyone.
I need some help.
I have situation like that:
One wiremock server which starts in static [BeforeTestRun] (I need special port, so can not run it on every test).
Many unit tests which work in parallel.
Every unit test use wiremock with the same path, but they have InScenario(testName).
For example test1 ping google.com and should get NotFound.
I have added .InScenario("test1").
Test2 should get InternalError on the same request. I have added .InScenario("test2").
But when you run 10+ tests at once, some of them gets wrong responses (not from their scenarios).
it looks like the scenarios are mixing.
How should I specify the scenarion for every separate test correctly?
@Gnannondorf
That could maybe work. (maybe you need to update your hosts file)
Another option could be to use different ports.