mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 08:48:28 +02: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
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using HandlebarsDotNet.Helpers.Models;
|
using HandlebarsDotNet.Helpers.Models;
|
||||||
@@ -170,8 +171,8 @@ internal class Transformer : ITransformer
|
|||||||
WalkNode(transformerContext, options, jToken, model);
|
WalkNode(transformerContext, options, jToken, model);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Array bodyAsArray:
|
case var bodyAsEnumerable when bodyAsEnumerable is IEnumerable and not string:
|
||||||
jToken = JArray.FromObject(bodyAsArray, _jsonSerializer);
|
jToken = JArray.FromObject(bodyAsEnumerable, _jsonSerializer);
|
||||||
WalkNode(transformerContext, options, jToken, model);
|
WalkNode(transformerContext, options, jToken, model);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
// Copyright © WireMock.Net
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using WireMock.RequestBuilders;
|
|
||||||
using WireMock.ResponseBuilders;
|
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -44,6 +43,78 @@ public partial class WireMockServerTests
|
|||||||
response.Should().Contain("Hello, Stef!");
|
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]
|
[Fact]
|
||||||
public async Task WireMockServer_WithTransformerBefore_WithBodyFromFile_ShouldWork()
|
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)
|
private static async Task<string> GetResponseAsync(WireMockServer server, string relativePath)
|
||||||
{
|
{
|
||||||
using HttpClient httpClient = new HttpClient();
|
using var httpClient = server.CreateClient();
|
||||||
httpClient.BaseAddress = new Uri(server.Urls[0]);
|
|
||||||
|
|
||||||
using var requestContent = new StringContent(RequestXml);
|
using var requestContent = new StringContent(RequestXml);
|
||||||
using var responseMsg = await httpClient.PostAsync(relativePath, requestContent);
|
using var responseMsg = await httpClient.PostAsync(relativePath, requestContent);
|
||||||
|
|||||||
Reference in New Issue
Block a user