Fix ResponseMessageTransformer

This commit is contained in:
Stef Heyenrath
2018-02-28 18:05:10 +01:00
parent 0be8ee1174
commit 22298114d7
2 changed files with 37 additions and 8 deletions

View File

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

View File

@@ -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()
{