mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-25 10:19:04 +02:00
When using ResponseMessageTransformer : keep BodyEncoding (#460)
This commit is contained in:
@@ -5,6 +5,7 @@ using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using WireMock.Validation;
|
||||
@@ -106,6 +107,7 @@ namespace WireMock.Transformers
|
||||
|
||||
responseMessage.BodyData = new BodyData
|
||||
{
|
||||
Encoding = original.BodyData.Encoding,
|
||||
DetectedBodyType = original.BodyData.DetectedBodyType,
|
||||
DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType,
|
||||
BodyAsJson = jToken
|
||||
@@ -195,6 +197,7 @@ namespace WireMock.Transformers
|
||||
|
||||
responseMessage.BodyData = new BodyData
|
||||
{
|
||||
Encoding = original.BodyData.Encoding,
|
||||
DetectedBodyType = original.BodyData.DetectedBodyType,
|
||||
DetectedBodyTypeFromContentType = original.BodyData.DetectedBodyTypeFromContentType,
|
||||
BodyAsString = templateBodyAsString(template)
|
||||
|
||||
@@ -402,5 +402,31 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
||||
// Assert
|
||||
Check.That(JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson)).Equals("{\"name\":\"WireMock\"}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_Handlebars_WithBodyAsString_KeepsEncoding()
|
||||
{
|
||||
// Assign
|
||||
const string text = "my-text";
|
||||
Encoding enc = Encoding.Unicode;
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsString = text,
|
||||
DetectedBodyType = BodyType.String,
|
||||
Encoding = enc
|
||||
};
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo_object"), "POST", ClientIp, bodyData);
|
||||
|
||||
var response = Response.Create()
|
||||
.WithBody("{{request.body}}", BodyDestinationFormat.SameAsSource, enc)
|
||||
.WithTransformer();
|
||||
|
||||
// Act
|
||||
var responseMessage = await response.ProvideResponseAsync(request, _settings);
|
||||
|
||||
// Assert
|
||||
responseMessage.BodyData.BodyAsString.Should().Be(text);
|
||||
responseMessage.BodyData.Encoding.Should().Be(enc);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user