mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-11 21:12:16 +01:00
Fix ResponseMessageTransformer
This commit is contained in:
@@ -21,15 +21,19 @@ namespace WireMock.Transformers
|
||||
var template = new { request = requestMessage };
|
||||
|
||||
// Body
|
||||
var templateBody = Handlebars.Compile(bodyIsJson ? JsonConvert.SerializeObject(original.BodyAsJson) : original.Body);
|
||||
string body = bodyIsJson ? JsonConvert.SerializeObject(original.BodyAsJson) : original.Body;
|
||||
if (body != null)
|
||||
{
|
||||
var templateBody = Handlebars.Compile(body);
|
||||
|
||||
if (!bodyIsJson)
|
||||
{
|
||||
responseMessage.Body = templateBody(template);
|
||||
}
|
||||
else
|
||||
{
|
||||
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(templateBody(template));
|
||||
if (!bodyIsJson)
|
||||
{
|
||||
responseMessage.Body = templateBody(template);
|
||||
}
|
||||
else
|
||||
{
|
||||
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(templateBody(template));
|
||||
}
|
||||
}
|
||||
|
||||
// Headers
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using NFluent;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
@@ -12,6 +14,29 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_Handlebars_WithBodyAsJson()
|
||||
{
|
||||
// given
|
||||
string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||
var bodyData = new BodyData
|
||||
{
|
||||
BodyAsJson = JsonConvert.DeserializeObject(jsonString),
|
||||
Encoding = Encoding.UTF8
|
||||
};
|
||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, bodyData);
|
||||
|
||||
var response = Response.Create()
|
||||
.WithBodyAsJson(new { x = "test {{request.url}}" })
|
||||
.WithTransformer();
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(JsonConvert.SerializeObject(responseMessage.BodyAsJson)).Equals("{\"x\":\"test http://localhost/foo\"}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user