diff --git a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs index 02dd6679..a6db1f59 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Net; using System.Threading.Tasks; 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 void Run() @@ -54,6 +61,32 @@ namespace WireMock.Net.ConsoleApplication var s = WireMockServer.Start(); s.Stop(); + var todos = new Dictionary(); + + 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 { HostingScheme = HostingScheme.HttpAndHttps, @@ -71,7 +104,7 @@ namespace WireMock.Net.ConsoleApplication string url2 = "http://localhost:9092/"; string url3 = "https://localhost:9443/"; - var server = WireMockServer.Start(new WireMockServerSettings + server = WireMockServer.Start(new WireMockServerSettings { AllowCSharpCodeMatcher = true, Urls = new[] { url1, url2, url3 }, diff --git a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs index 1b700dce..e138822e 100644 --- a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs @@ -29,7 +29,7 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder IResponseBuilder WithBody(Func bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null); /// - /// WithBody : Create a ... response based on a callback function. + /// WithBody : Create a ... response based on a async callback function. /// /// The async delegate to build the body. /// The Body Destination format (SameAsSource, String or Bytes). @@ -63,6 +63,22 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder /// A . IResponseBuilder WithBodyAsJson(object body, bool indented); + /// + /// WithBodyAsJson : Create a ... response based on a callback function. + /// + /// The delegate to build the body. + /// The body encoding. + /// A . + IResponseBuilder WithBodyAsJson(Func bodyFactory, Encoding? encoding = null); + + /// + /// WithBodyAsJson : Create a ... response based on a async callback function. + /// + /// The async delegate to build the body. + /// The body encoding. + /// A . + IResponseBuilder WithBodyAsJson(Func> bodyFactory, Encoding? encoding = null); + /// /// WithBodyFromFile : Create a ... response based on a File. /// diff --git a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs index 852eafa1..ea95b7da 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs @@ -13,7 +13,7 @@ public partial class Response /// public IResponseBuilder WithBody(Func bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null) { - Guard.NotNull(bodyFactory, nameof(bodyFactory)); + Guard.NotNull(bodyFactory); return WithCallbackInternal(true, req => new ResponseMessage { @@ -30,7 +30,7 @@ public partial class Response /// public IResponseBuilder WithBody(Func> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null) { - Guard.NotNull(bodyFactory, nameof(bodyFactory)); + Guard.NotNull(bodyFactory); return WithCallbackInternal(true, async req => new ResponseMessage { @@ -70,7 +70,7 @@ public partial class Response return this; } - /// + /// public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) { Guard.NotNull(filename); @@ -127,7 +127,7 @@ public partial class Response return this; } - /// + /// public IResponseBuilder WithBodyAsJson(object body, Encoding? encoding = null, bool? indented = null) { Guard.NotNull(body); @@ -144,12 +144,46 @@ public partial class Response return this; } - /// + /// public IResponseBuilder WithBodyAsJson(object body, bool indented) { return WithBodyAsJson(body, null, indented); } + /// + public IResponseBuilder WithBodyAsJson(Func 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" + } + }); + } + + /// + public IResponseBuilder WithBodyAsJson(Func> 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>" + } + }); + } + /// public IResponseBuilder WithBody(object body, IJsonConverter converter, JsonConverterOptions? options = null) { diff --git a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs index e3702f95..52ce149a 100644 --- a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs +++ b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs @@ -120,28 +120,6 @@ public class ResponseWithBodyTests 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] 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); } + [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] public async Task Response_ProvideResponse_WithJsonBodyAndTransform() {