mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-29 05:42:14 +02:00
Add WithBodyAsJson builder method with accepts a Func (#881)
* Add WithBodyAsJson builder method with accepts a Func * ut
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -33,6 +35,11 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Todo
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public static class MainApp
|
public static class MainApp
|
||||||
{
|
{
|
||||||
public static void Run()
|
public static void Run()
|
||||||
@@ -54,6 +61,32 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
var s = WireMockServer.Start();
|
var s = WireMockServer.Start();
|
||||||
s.Stop();
|
s.Stop();
|
||||||
|
|
||||||
|
var todos = new Dictionary<int, Todo>();
|
||||||
|
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.WithPath("todos")
|
||||||
|
.UsingGet()
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBodyAsJson(todos.Values)
|
||||||
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingGet()
|
||||||
|
.WithPath("todos")
|
||||||
|
.WithParam("id")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBodyAsJson(rm => todos[int.Parse(rm.Query!["id"].ToString())])
|
||||||
|
);
|
||||||
|
|
||||||
|
var httpClient = server.CreateClient();
|
||||||
|
//server.Stop();
|
||||||
|
|
||||||
var httpAndHttpsWithPort = WireMockServer.Start(new WireMockServerSettings
|
var httpAndHttpsWithPort = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
HostingScheme = HostingScheme.HttpAndHttps,
|
HostingScheme = HostingScheme.HttpAndHttps,
|
||||||
@@ -71,7 +104,7 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
string url2 = "http://localhost:9092/";
|
string url2 = "http://localhost:9092/";
|
||||||
string url3 = "https://localhost:9443/";
|
string url3 = "https://localhost:9443/";
|
||||||
|
|
||||||
var server = WireMockServer.Start(new WireMockServerSettings
|
server = WireMockServer.Start(new WireMockServerSettings
|
||||||
{
|
{
|
||||||
AllowCSharpCodeMatcher = true,
|
AllowCSharpCodeMatcher = true,
|
||||||
Urls = new[] { url1, url2, url3 },
|
Urls = new[] { url1, url2, url3 },
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
|
|||||||
IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null);
|
IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBody : Create a ... response based on a callback function.
|
/// WithBody : Create a ... response based on a async callback function.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bodyFactory">The async delegate to build the body.</param>
|
/// <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="destination">The Body Destination format (SameAsSource, String or Bytes).</param>
|
||||||
@@ -63,6 +63,22 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder
|
|||||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
IResponseBuilder WithBodyAsJson(object body, bool indented);
|
IResponseBuilder WithBodyAsJson(object body, bool indented);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithBodyAsJson : Create a ... response based on a callback function.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bodyFactory">The delegate to build the body.</param>
|
||||||
|
/// <param name="encoding">The body encoding.</param>
|
||||||
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
|
IResponseBuilder WithBodyAsJson(Func<IRequestMessage, object> bodyFactory, Encoding? encoding = null);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// WithBodyAsJson : Create a ... response based on a async callback function.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bodyFactory">The async delegate to build the body.</param>
|
||||||
|
/// <param name="encoding">The body encoding.</param>
|
||||||
|
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||||
|
IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WithBodyFromFile : Create a ... response based on a File.
|
/// WithBodyFromFile : Create a ... response based on a File.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public partial class Response
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
public IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
||||||
{
|
{
|
||||||
Guard.NotNull(bodyFactory, nameof(bodyFactory));
|
Guard.NotNull(bodyFactory);
|
||||||
|
|
||||||
return WithCallbackInternal(true, req => new ResponseMessage
|
return WithCallbackInternal(true, req => new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -30,7 +30,7 @@ public partial class Response
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBody(Func<IRequestMessage, Task<string>> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
public IResponseBuilder WithBody(Func<IRequestMessage, Task<string>> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
||||||
{
|
{
|
||||||
Guard.NotNull(bodyFactory, nameof(bodyFactory));
|
Guard.NotNull(bodyFactory);
|
||||||
|
|
||||||
return WithCallbackInternal(true, async req => new ResponseMessage
|
return WithCallbackInternal(true, async req => new ResponseMessage
|
||||||
{
|
{
|
||||||
@@ -70,7 +70,7 @@ public partial class Response
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
|
public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
|
||||||
{
|
{
|
||||||
Guard.NotNull(filename);
|
Guard.NotNull(filename);
|
||||||
@@ -127,7 +127,7 @@ public partial class Response
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, Encoding, bool?)"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBodyAsJson(object body, Encoding? encoding = null, bool? indented = null)
|
public IResponseBuilder WithBodyAsJson(object body, Encoding? encoding = null, bool? indented = null)
|
||||||
{
|
{
|
||||||
Guard.NotNull(body);
|
Guard.NotNull(body);
|
||||||
@@ -144,12 +144,46 @@ public partial class Response
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, bool)"/>
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBodyAsJson(object body, bool indented)
|
public IResponseBuilder WithBodyAsJson(object body, bool indented)
|
||||||
{
|
{
|
||||||
return WithBodyAsJson(body, null, indented);
|
return WithBodyAsJson(body, null, indented);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IResponseBuilder WithBodyAsJson(Func<IRequestMessage, object> bodyFactory, Encoding? encoding = null)
|
||||||
|
{
|
||||||
|
Guard.NotNull(bodyFactory);
|
||||||
|
|
||||||
|
return WithCallbackInternal(true, req => new ResponseMessage
|
||||||
|
{
|
||||||
|
BodyData = new BodyData
|
||||||
|
{
|
||||||
|
Encoding = encoding ?? Encoding.UTF8,
|
||||||
|
DetectedBodyType = BodyType.Json,
|
||||||
|
BodyAsJson = bodyFactory(req),
|
||||||
|
IsFuncUsed = "Func<IRequestMessage, object>"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IResponseBuilder WithBodyAsJson(Func<IRequestMessage, Task<object>> bodyFactory, Encoding? encoding = null)
|
||||||
|
{
|
||||||
|
Guard.NotNull(bodyFactory);
|
||||||
|
|
||||||
|
return WithCallbackInternal(true, async req => new ResponseMessage
|
||||||
|
{
|
||||||
|
BodyData = new BodyData
|
||||||
|
{
|
||||||
|
Encoding = encoding ?? Encoding.UTF8,
|
||||||
|
DetectedBodyType = BodyType.Json,
|
||||||
|
BodyAsJson = await bodyFactory(req).ConfigureAwait(false),
|
||||||
|
IsFuncUsed = "Func<IRequestMessage, Task<object>>"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null)
|
public IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -120,28 +120,6 @@ public class ResponseWithBodyTests
|
|||||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task Response_ProvideResponse_WithBody_Object_Indented()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var body = new BodyData
|
|
||||||
{
|
|
||||||
DetectedBodyType = BodyType.String,
|
|
||||||
BodyAsString = "abc"
|
|
||||||
};
|
|
||||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
|
||||||
|
|
||||||
object x = new { message = "Hello" };
|
|
||||||
var responseBuilder = Response.Create().WithBodyAsJson(x, true);
|
|
||||||
|
|
||||||
// act
|
|
||||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
|
||||||
|
|
||||||
// then
|
|
||||||
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
|
||||||
Check.That(response.Message.BodyData.BodyAsJsonIndented).IsEqualTo(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_WithBody_String_SameAsSource_Encoding()
|
public async Task Response_ProvideResponse_WithBody_String_SameAsSource_Encoding()
|
||||||
{
|
{
|
||||||
@@ -196,6 +174,70 @@ public class ResponseWithBodyTests
|
|||||||
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
Check.That(response.Message.BodyData.Encoding).Equals(Encoding.ASCII);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_WithBodyAsJson_Object_Indented()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var body = new BodyData
|
||||||
|
{
|
||||||
|
DetectedBodyType = BodyType.String,
|
||||||
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body);
|
||||||
|
|
||||||
|
object x = new { message = "Hello" };
|
||||||
|
var responseBuilder = Response.Create().WithBodyAsJson(x, true);
|
||||||
|
|
||||||
|
// act
|
||||||
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// then
|
||||||
|
Check.That(response.Message.BodyData.BodyAsJson).Equals(x);
|
||||||
|
Check.That(response.Message.BodyData.BodyAsJsonIndented).IsEqualTo(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_WithBodyAsJson_FuncObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requestBody = new BodyData
|
||||||
|
{
|
||||||
|
DetectedBodyType = BodyType.String,
|
||||||
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||||
|
|
||||||
|
object responseBody = new { message = "Hello" };
|
||||||
|
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => responseBody);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponse_WithBodyAsJson_AsyncFuncObject()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var requestBody = new BodyData
|
||||||
|
{
|
||||||
|
DetectedBodyType = BodyType.String,
|
||||||
|
BodyAsString = "abc"
|
||||||
|
};
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, requestBody);
|
||||||
|
|
||||||
|
object responseBody = new { message = "Hello" };
|
||||||
|
var responseBuilder = Response.Create().WithBodyAsJson(requestMessage => Task.FromResult(responseBody));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Message.BodyData!.BodyAsJson.Should().BeEquivalentTo(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_WithJsonBodyAndTransform()
|
public async Task Response_ProvideResponse_WithJsonBodyAndTransform()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user