// Copyright © WireMock.Net
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Server;
using Xunit;
namespace WireMock.Net.Tests;
public partial class WireMockServerTests
{
private const string RequestXml =
"""
""";
private readonly string _responseFilePath = Path.Combine(Environment.CurrentDirectory, "__admin", "mappings", "responseWithTransformer.xml");
[Fact]
public async Task WireMockServer_WithTransformer_WithBody_ShouldWork()
{
// Arrange
using var server = WireMockServer.Start();
server
.WhenRequest(req => req
.WithPath("/withbody")
.UsingPost())
.ThenRespondWith(rsp => rsp
.WithSuccess()
.WithBody(File.ReadAllText(_responseFilePath))
.WithTransformer());
// Act
var response = await GetResponseAsync(server, "/withbody");
// Assert
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