Closed duychuongvn closed 4 years ago
@duychuongvn There are several options ; however I'm not sure if you use these.
Run the WireMock.Net as standalone (console) app and add some hardcoded c# code to get and set the signature. Like https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L77
If the Signature is not that complex, maybe you can use some response templating using Handlebars.net or Handlebars.Net.Helpers See https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating
I've listed an issue to also support C# in the response (https://github.com/WireMock-Net/WireMock.Net/issues/330) but this is not yet implemented.
Hi @StefH ,
Thank you very much for your help. I'll follow you direction.
Please let me know if this works for you, so I can close this issue.
Hi @StefH ,
It works. This is my solution:
var responseMappings = Server.Mappings.Where(x => x.Provider is Response);
foreach (var mapping in responseMappings)
{
Response responseProvider = (Response)mapping.Provider;
var responseMessage = responseProvider.ResponseMessage;
responseProvider.WithCallback(requestMessage =>
{
var response = new ResponseMessage()
{
StatusCode = responseProvider.ResponseMessage.StatusCode,
};
response.BodyData = GetBodyData(responseMessage);
var signature= SignMessage(requestMessage, response.BodyData.BodyAsString);
response.AddHeader(CONTENT_TYPE, TEXT_XML);
response.AddHeader(XRESPONSE_HEADER, signature);
return response;
});
}
Cheers, Thank you for your support.
@duychuongvn
In your case : I would not loop the mappings and update the response, but just register that WithCallback
mapping directly:
server
.Given(Request.Create().WithPath("/test").UsingGet())
.RespondWith(Response.Create().WithCallback(requestMessage =>
{
string body = "My Body data";
var response = new ResponseMessage()
{
StatusCode = 200,
BodyData = body
};
var signature = SignMessage(requestMessage, body);
response.AddHeader(CONTENT_TYPE, TEXT_XML);
response.AddHeader(XRESPONSE_HEADER, signature);
return response;
}));
Or is this not possible in your scenario?
Hi @StefH In my case, I would like to static json file instead of hard code I want to add dynamic singature with response text, content-type, timestamp,... into the response header for static mapping files then I have to loop and create the other response from the file.
Ah I see. In that case your solution is the best one.
Hello, I'm deploying API based on .NetCore 3.0, I would like to use WireMock on .NetCore too.
I have an issue:
Some 3rd API require Signature to verify client request and ask client to verify their responses. It means that to integrate with 3rd party, we have to add Siguature to Request Header and verify Signature in the Response Header from 3rd party too.
We are using admin mock interface (read all mock data in the folder). How can we apply the interceptor to genrate Signature based on each mock data? Note: In the signature, there is dynamic information like timestamp, we cannot fix this value in the mock data
Thank you for you support.