mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Fix for WithTransformer and JsonBody as list (#1315)
* Fix for WithTransformer and JsonBody as list * Fix WithTransformer when the response BodyAsJson is a List
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using HandlebarsDotNet.Helpers.Models;
|
||||
@@ -170,8 +171,8 @@ internal class Transformer : ITransformer
|
||||
WalkNode(transformerContext, options, jToken, model);
|
||||
break;
|
||||
|
||||
case Array bodyAsArray:
|
||||
jToken = JArray.FromObject(bodyAsArray, _jsonSerializer);
|
||||
case var bodyAsEnumerable when bodyAsEnumerable is IEnumerable and not string:
|
||||
jToken = JArray.FromObject(bodyAsEnumerable, _jsonSerializer);
|
||||
WalkNode(transformerContext, options, jToken, model);
|
||||
break;
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
@@ -44,6 +43,78 @@ public partial class WireMockServerTests
|
||||
response.Should().Contain("Hello, Stef!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithTransformer_WithJsonBodyAsArray_ShouldWork()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start();
|
||||
server
|
||||
.WhenRequest(req => req
|
||||
.WithPath("/withbody")
|
||||
.UsingPost()
|
||||
)
|
||||
.ThenRespondWith(rsp => rsp
|
||||
.WithSuccess()
|
||||
.WithBodyAsJson(new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
test = "test",
|
||||
secret = true
|
||||
},
|
||||
new
|
||||
{
|
||||
test = "123",
|
||||
secret = false
|
||||
}
|
||||
}
|
||||
)
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
// Act
|
||||
var response = await GetResponseAsync(server, "/withbody");
|
||||
|
||||
// Assert
|
||||
response.Should().Be("""[{"test":"test","secret":true},{"test":"123","secret":false}]""");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithTransformer_WithJsonBodyAsList_ShouldWork()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.Start();
|
||||
server
|
||||
.WhenRequest(req => req
|
||||
.WithPath("/withbody")
|
||||
.UsingPost()
|
||||
)
|
||||
.ThenRespondWith(rsp => rsp
|
||||
.WithSuccess()
|
||||
.WithBodyAsJson(
|
||||
new List<object>
|
||||
{
|
||||
new
|
||||
{
|
||||
test = "test",
|
||||
secret = true
|
||||
},
|
||||
new
|
||||
{
|
||||
test = "123",
|
||||
secret = false
|
||||
}
|
||||
})
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
// Act
|
||||
var response = await GetResponseAsync(server, "/withbody");
|
||||
|
||||
// Assert
|
||||
response.Should().Be("""[{"test":"test","secret":true},{"test":"123","secret":false}]""");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithTransformerBefore_WithBodyFromFile_ShouldWork()
|
||||
{
|
||||
@@ -88,8 +159,7 @@ public partial class WireMockServerTests
|
||||
|
||||
private static async Task<string> GetResponseAsync(WireMockServer server, string relativePath)
|
||||
{
|
||||
using HttpClient httpClient = new HttpClient();
|
||||
httpClient.BaseAddress = new Uri(server.Urls[0]);
|
||||
using var httpClient = server.CreateClient();
|
||||
|
||||
using var requestContent = new StringContent(RequestXml);
|
||||
using var responseMsg = await httpClient.PostAsync(relativePath, requestContent);
|
||||
|
||||
Reference in New Issue
Block a user