How to handle encrypted payload in requests and responses? #500

Closed
opened 2025-12-29 08:29:10 +01:00 by adam · 5 comments
Owner

Originally created by @jsyrjala on GitHub (Mar 20, 2023).

I have message format where requests and responses have a JSON body where one of the elements contains
an another JSON element as encrypted.

Both the request and response look essentially like this. All the interesting stuff is inside the encryptedField.

{
  "header":{"headerField":  "headerValue"},
  "encryptedField": "base64_encoded_encrypted_json_blob"
}

I'd like to decrypt the request, then generate a response based on the decrypted request. And finally I should encrypt the response again before sending it to the client.

I know how to decrypt/encrypt the data but how to plug that into WireMock.Net?

I was hoping that there would be possibility to have functions that convert request bodies at the start of the flow and then convert response bodies at the end of the flow.

Originally created by @jsyrjala on GitHub (Mar 20, 2023). I have message format where requests and responses have a JSON body where one of the elements contains an another JSON element as encrypted. Both the request and response look essentially like this. All the interesting stuff is inside the `encryptedField`. ```json { "header":{"headerField": "headerValue"}, "encryptedField": "base64_encoded_encrypted_json_blob" } ``` I'd like to decrypt the request, then generate a response based on the decrypted request. And finally I should encrypt the response again before sending it to the client. I know how to decrypt/encrypt the data but how to plug that into WireMock.Net? I was hoping that there would be possibility to have functions that convert request bodies at the start of the flow and then convert response bodies at the end of the flow.
adam added the question label 2025-12-29 08:29:10 +01:00
adam closed this issue 2025-12-29 08:29:10 +01:00
Author
Owner

@jsyrjala commented on GitHub (Mar 20, 2023):

After some digging, it seems that at least it is possible to implement IResponseProvider, and use that to handle the request and response as you want. However this, makes using for example WireMock's template functionality hard or impossible.

server.Given(Request.Create().WithPath("/nexo/").UsingPost()).RespondWith(new MyResponse());

private class MyResponse : IResponseProvider
    {
        public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(
            IMapping mapping,
            IRequestMessage requestMessage,
            WireMockServerSettings settings
        )
        {
            Console.WriteLine(requestMessage.BodyAsJson);
            var msg = new ResponseMessage()
            {
                StatusCode = 200,
                FaultType = FaultType.NONE,
                BodyData = new BodyData
                {
                    BodyAsString = "can_have_any_data_here",
                    DetectedBodyType = BodyType.String,
                },
            };
            return (msg, mapping);
        }
    }

@jsyrjala commented on GitHub (Mar 20, 2023): After some digging, it seems that at least it is possible to implement IResponseProvider, and use that to handle the request and response as you want. However this, makes using for example WireMock's template functionality hard or impossible. ```csharp server.Given(Request.Create().WithPath("/nexo/").UsingPost()).RespondWith(new MyResponse()); private class MyResponse : IResponseProvider { public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync( IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings ) { Console.WriteLine(requestMessage.BodyAsJson); var msg = new ResponseMessage() { StatusCode = 200, FaultType = FaultType.NONE, BodyData = new BodyData { BodyAsString = "can_have_any_data_here", DetectedBodyType = BodyType.String, }, }; return (msg, mapping); } } ```
Author
Owner

@StefH commented on GitHub (Mar 20, 2023):

Instead of building your complete ResponseProvider, you can also use the following WithBody method:

 IResponseBuilder WithBody(Func<IRequestMessage, Task<string>> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null);

For a example see
https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithCallbackTests.cs#L89

@StefH commented on GitHub (Mar 20, 2023): Instead of building your complete ResponseProvider, you can also use the following **WithBody** method: ``` C# IResponseBuilder WithBody(Func<IRequestMessage, Task<string>> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null); ``` For a example see https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithCallbackTests.cs#L89
Author
Owner

@StefH commented on GitHub (Mar 20, 2023):

However, if you want to use HandleBars.Net templating, you can use the base64decode Helper, see
https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki/String#stringbase64decode

Or you can register you own specific Handlebars.Net.Helper:
Register this with the HandlebarsRegistrationCallback in the settings class.

@StefH commented on GitHub (Mar 20, 2023): However, if you want to use HandleBars.Net templating, you can use the base64decode Helper, see https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki/String#stringbase64decode Or you can register you own specific Handlebars.Net.Helper: Register this with the HandlebarsRegistrationCallback in the settings class.
Author
Owner

@StefH commented on GitHub (Apr 9, 2023):

@jsyrjala
Did you manage to get it working.

@StefH commented on GitHub (Apr 9, 2023): @jsyrjala Did you manage to get it working.
Author
Owner

@jsyrjala commented on GitHub (Apr 9, 2023):

Thanks. It is working now.

@jsyrjala commented on GitHub (Apr 9, 2023): Thanks. It is working now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#500