Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Header Response by using interceptor #497

Closed
duychuongvn opened this issue Aug 4, 2020 · 7 comments
Closed

Add Header Response by using interceptor #497

duychuongvn opened this issue Aug 4, 2020 · 7 comments
Labels

Comments

@duychuongvn
Copy link

duychuongvn commented 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.

@duychuongvn duychuongvn changed the title Add Header Response by using Intecepter Add Header Response by using interceptor Aug 4, 2020
@StefH
Copy link
Collaborator

StefH commented 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 (Feature: Add support for CSharpCode Transformer #330) but this is not yet implemented.

@duychuongvn
Copy link
Author

Hi @StefH ,

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

@StefH
Copy link
Collaborator

StefH commented Aug 6, 2020

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

@duychuongvn
Copy link
Author

duychuongvn commented 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.

@StefH StefH closed this as completed Aug 12, 2020
@StefH StefH reopened this Aug 12, 2020
@StefH
Copy link
Collaborator

StefH commented 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?

@duychuongvn
Copy link
Author

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
Copy link
Collaborator

StefH commented Aug 12, 2020

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

@StefH StefH closed this as completed Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants