Fix WithCallback logic when using other fluent builder statements (#587)

This commit is contained in:
Stef Heyenrath
2021-02-26 13:10:25 +01:00
committed by GitHub
parent e23249c144
commit aa8510fab3
5 changed files with 101 additions and 12 deletions

View File

@@ -217,6 +217,35 @@ namespace WireMock.Net.Tests.ResponseBuilders
Check.That(responseMessage.Headers["H2"].ToString()).IsEqualTo("X2");
}
[Fact]
public async Task Response_ProvideResponse_WithBody_FuncAsync()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/test"), "GET", ClientIp);
var response = Response.Create()
.WithStatusCode(500)
.WithHeader("H1", "X1")
.WithHeader("H2", "X2")
.WithBody(async req =>
{
await Task.Delay(1);
return $"path: {req.Path}";
});
// Act
var responseMessage = await response.ProvideResponseAsync(request, _settings);
// Assert
Check.That(responseMessage.BodyData.BodyAsString).IsEqualTo("path: /test");
Check.That(responseMessage.BodyData.BodyAsBytes).IsNull();
Check.That(responseMessage.BodyData.BodyAsJson).IsNull();
Check.That(responseMessage.BodyData.Encoding.CodePage).Equals(Encoding.UTF8.CodePage);
Check.That(responseMessage.StatusCode).IsEqualTo(500);
Check.That(responseMessage.Headers["H1"].ToString()).IsEqualTo("X1");
Check.That(responseMessage.Headers["H2"].ToString()).IsEqualTo("X2");
}
[Fact]
public async Task Response_ProvideResponse_WithJsonBodyAndTransform_Func()
{

View File

@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using WireMock.Handlers;
@@ -79,6 +81,38 @@ namespace WireMock.Net.Tests.ResponseBuilders
responseMessage.StatusCode.Should().Be(302);
}
[Fact]
public async Task Response_WithCallback_And_WithStatusCode_And_WithHeader()
{
// Assign
var header = "X-UserId";
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(request => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = request.Path + "Bar"
},
StatusCode = HttpStatusCode.NotFound,
Headers = new Dictionary<string, WireMockList<string>>
{
{ header, new WireMockList<string>("NA") }
}
})
.WithStatusCode(HttpStatusCode.Accepted)
.WithHeader(header, "Stef");
// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);
// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(HttpStatusCode.Accepted);
responseMessage.Headers[header].Should().ContainSingle("Stef");
}
[Fact]
public async Task Response_WithCallback_And_UseTransformer_Is_True()
{