WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.39k stars 205 forks source link

Add Header Response by using interceptor #497

Closed duychuongvn closed 4 years ago

duychuongvn commented 4 years ago

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.

StefH commented 4 years ago

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

duychuongvn commented 4 years ago

Hi @StefH ,

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

StefH commented 4 years ago

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

duychuongvn commented 4 years ago

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.

StefH commented 4 years ago

@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?

duychuongvn commented 4 years ago

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.

StefH commented 4 years ago

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