mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-22 08:48:46 +02:00
Fix WithCallback logic when using other fluent builder statements (#587)
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.4.5</VersionPrefix>
|
<VersionPrefix>1.4.6</VersionPrefix>
|
||||||
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
|
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
|
||||||
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
|
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
|
||||||
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WireMock.ResponseBuilders
|
namespace WireMock.ResponseBuilders
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -27,6 +28,15 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
IResponseBuilder WithBody([NotNull] Func<RequestMessage, string> bodyFactory, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
|
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>
|
/// <summary>
|
||||||
/// WithBody : Create a ... response based on a bytearray.
|
/// WithBody : Create a ... response based on a bytearray.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
{
|
{
|
||||||
Check.NotNull(bodyFactory, nameof(bodyFactory));
|
Check.NotNull(bodyFactory, nameof(bodyFactory));
|
||||||
|
|
||||||
return WithCallbackInternal(false, req => new ResponseMessage
|
return WithCallbackInternal(true, req => new ResponseMessage
|
||||||
{
|
{
|
||||||
BodyData = new BodyData
|
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)"/>
|
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
|
||||||
public IResponseBuilder WithBody(byte[] body, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
|
public IResponseBuilder WithBody(byte[] body, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
|
||||||
{
|
{
|
||||||
@@ -356,7 +372,7 @@ namespace WireMock.ResponseBuilders
|
|||||||
}
|
}
|
||||||
|
|
||||||
ResponseMessage responseMessage;
|
ResponseMessage responseMessage;
|
||||||
if (Callback == null && CallbackAsync == null)
|
if (!WithCallbackUsed)
|
||||||
{
|
{
|
||||||
responseMessage = ResponseMessage;
|
responseMessage = ResponseMessage;
|
||||||
}
|
}
|
||||||
@@ -371,16 +387,16 @@ namespace WireMock.ResponseBuilders
|
|||||||
responseMessage = await CallbackAsync(requestMessage);
|
responseMessage = await CallbackAsync(requestMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!WithCallbackUsed)
|
// Copy StatusCode from ResponseMessage (if defined)
|
||||||
|
if (ResponseMessage.StatusCode != null)
|
||||||
{
|
{
|
||||||
// Copy StatusCode from ResponseMessage
|
|
||||||
responseMessage.StatusCode = ResponseMessage.StatusCode;
|
responseMessage.StatusCode = ResponseMessage.StatusCode;
|
||||||
|
}
|
||||||
|
|
||||||
// Copy Headers from ResponseMessage (if defined)
|
// Copy Headers from ResponseMessage (if defined)
|
||||||
if (ResponseMessage.Headers != null)
|
if (ResponseMessage.Headers != null)
|
||||||
{
|
{
|
||||||
responseMessage.Headers = ResponseMessage.Headers;
|
responseMessage.Headers = ResponseMessage.Headers;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,6 +217,35 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
Check.That(responseMessage.Headers["H2"].ToString()).IsEqualTo("X2");
|
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]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_WithJsonBodyAndTransform_Func()
|
public async Task Response_ProvideResponse_WithJsonBodyAndTransform_Func()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using WireMock.Handlers;
|
using WireMock.Handlers;
|
||||||
@@ -79,6 +81,38 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
responseMessage.StatusCode.Should().Be(302);
|
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]
|
[Fact]
|
||||||
public async Task Response_WithCallback_And_UseTransformer_Is_True()
|
public async Task Response_WithCallback_And_UseTransformer_Is_True()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user