Add Header Response by using interceptor #286

Closed
opened 2025-12-29 15:19:45 +01:00 by adam · 7 comments
Owner

Originally created by @duychuongvn on GitHub (Aug 4, 2020).

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.

Originally created by @duychuongvn on GitHub (Aug 4, 2020). 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.
adam added the question label 2025-12-29 15:19:45 +01:00
adam closed this issue 2025-12-29 15:19:45 +01:00
Author
Owner

@StefH commented on GitHub (Aug 4, 2020):

@duychuongvn
There are several options ; however I'm not sure if you use these.

  1. 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

  2. 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

  3. 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.

@StefH commented on GitHub (Aug 4, 2020): @duychuongvn There are several options ; however I'm not sure if you use these. 1. 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 2. 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 3. 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.
Author
Owner

@duychuongvn commented on GitHub (Aug 6, 2020):

Hi @StefH ,

Thank you very much for your help.
I'll follow you direction.

@duychuongvn commented on GitHub (Aug 6, 2020): Hi @StefH , Thank you very much for your help. I'll follow you direction.
Author
Owner

@StefH commented on GitHub (Aug 6, 2020):

Please let me know if this works for you, so I can close this issue.

@StefH commented on GitHub (Aug 6, 2020): Please let me know if this works for you, so I can close this issue.
Author
Owner

@duychuongvn commented on GitHub (Aug 12, 2020):

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 commented on GitHub (Aug 12, 2020): Hi @StefH , It works. This is my solution: ``` c# 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.
Author
Owner

@StefH commented on GitHub (Aug 12, 2020):

@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;
                    }));

Example: https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L524

Or is this not possible in your scenario?

@StefH commented on GitHub (Aug 12, 2020): @duychuongvn In your case : I would not loop the mappings and update the response, but just register that `WithCallback` mapping directly: ``` c# 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; })); ``` Example: https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs#L524 Or is this not possible in your scenario?
Author
Owner

@duychuongvn commented on GitHub (Aug 12, 2020):

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.

@duychuongvn commented on GitHub (Aug 12, 2020): 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.
Author
Owner

@StefH commented on GitHub (Aug 12, 2020):

Ah I see.
In that case your solution is the best one.

@StefH commented on GitHub (Aug 12, 2020): Ah I see. In that case your solution is the best one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#286