mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 01:08:49 +02:00
Fix ResponseMessageTransformer
This commit is contained in:
@@ -21,15 +21,19 @@ namespace WireMock.Transformers
|
|||||||
var template = new { request = requestMessage };
|
var template = new { request = requestMessage };
|
||||||
|
|
||||||
// Body
|
// 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)
|
if (!bodyIsJson)
|
||||||
{
|
{
|
||||||
responseMessage.Body = templateBody(template);
|
responseMessage.Body = templateBody(template);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(templateBody(template));
|
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(templateBody(template));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
|
using WireMock.Util;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
@@ -12,6 +14,29 @@ namespace WireMock.Net.Tests
|
|||||||
{
|
{
|
||||||
private const string ClientIp = "::1";
|
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]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user