mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 07:51:41 +02:00
feat: add WithBody(req => dostuff) style callback (#102)
This commit is contained in:
committed by
Stef Heyenrath
parent
2b8a58c68c
commit
3f2c139f90
@@ -18,6 +18,17 @@ namespace WireMock.ResponseBuilders
|
|||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
|
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] string destination = BodyDestinationFormat.SameAsSource, [CanBeNull] Encoding encoding = null);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithBody : Create a ... response based on a callback function.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bodyFactory">The 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,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,6 +167,13 @@ namespace WireMock.ResponseBuilders
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/>
|
||||||
|
public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationFormat.SameAsSource,
|
||||||
|
Encoding encoding = null)
|
||||||
|
{
|
||||||
|
return WithCallback(req => new ResponseMessage {Body = bodyFactory(req)});
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
|
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
|
||||||
public IResponseBuilder WithBody(byte[] body, string destination, Encoding encoding = null)
|
public IResponseBuilder WithBody(byte[] body, string destination, Encoding encoding = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -232,6 +232,27 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(response).IsEqualTo("Hello world!");
|
Check.That(response).IsEqualTo("Hello world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
_server = FluentMockServer.Start();
|
||||||
|
|
||||||
|
_server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.WithPath("/foo")
|
||||||
|
.UsingGet())
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithStatusCode(200)
|
||||||
|
.WithBody(req => $"{{ path: '{req.Path}' }}"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(response).IsEqualTo("{ path: '/foo' }");
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task FluentMockServer_Should_respond_to_request_bodyAsBase64()
|
public async Task FluentMockServer_Should_respond_to_request_bodyAsBase64()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user