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

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.4.5</VersionPrefix>
<VersionPrefix>1.4.6</VersionPrefix>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>

View File

@@ -1,7 +1,8 @@
using JetBrains.Annotations;
using System;
using System.Text;
using System.Threading.Tasks;
namespace WireMock.ResponseBuilders
{
/// <summary>
@@ -27,6 +28,15 @@ namespace WireMock.ResponseBuilders
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBody([NotNull] Func<RequestMessage, string> bodyFactory, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
/// <summary>
/// WithBody : Create a ... response based on a callback function.
/// </summary>
/// <param name="bodyFactory">The async delegate to build the body.</param>
/// <param name="destination">The Body Destination format (SameAsSource, String or Bytes).</param>
/// <param name="encoding">The body encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithBody([NotNull] Func<RequestMessage, Task<string>> bodyFactory, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
/// <summary>
/// WithBody : Create a ... response based on a bytearray.
/// </summary>

View File

@@ -167,7 +167,7 @@ namespace WireMock.ResponseBuilders
{
Check.NotNull(bodyFactory, nameof(bodyFactory));
return WithCallbackInternal(false, req => new ResponseMessage
return WithCallbackInternal(true, req => new ResponseMessage
{
BodyData = new BodyData
{
@@ -178,6 +178,22 @@ namespace WireMock.ResponseBuilders
});
}
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, Task{string}}, string, Encoding)"/>
public IResponseBuilder WithBody(Func<RequestMessage, Task<string>> bodyFactory, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
{
Check.NotNull(bodyFactory, nameof(bodyFactory));
return WithCallbackInternal(true, async req => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = await bodyFactory(req),
Encoding = encoding ?? Encoding.UTF8
}
});
}
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
public IResponseBuilder WithBody(byte[] body, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
{
@@ -356,7 +372,7 @@ namespace WireMock.ResponseBuilders
}
ResponseMessage responseMessage;
if (Callback == null && CallbackAsync == null)
if (!WithCallbackUsed)
{
responseMessage = ResponseMessage;
}
@@ -371,16 +387,16 @@ namespace WireMock.ResponseBuilders
responseMessage = await CallbackAsync(requestMessage);
}
if (!WithCallbackUsed)
// Copy StatusCode from ResponseMessage (if defined)
if (ResponseMessage.StatusCode != null)
{
// Copy StatusCode from ResponseMessage
responseMessage.StatusCode = ResponseMessage.StatusCode;
}
// Copy Headers from ResponseMessage (if defined)
if (ResponseMessage.Headers != null)
{
responseMessage.Headers = ResponseMessage.Headers;
}
// Copy Headers from ResponseMessage (if defined)
if (ResponseMessage.Headers != null)
{
responseMessage.Headers = ResponseMessage.Headers;
}
}

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()
{