diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs index ad25e470d..e0a08f1fd 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs @@ -105,27 +105,38 @@ public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult private double IsMatch(RequestMessage requestMessage) { - if (requestMessage.Body != null) - { - if (Matcher is IStringMatcher stringMatcher) - { - return stringMatcher.IsMatch(requestMessage.Body); - } - } - + // Check if the matcher is a IObjectMatcher if (Matcher is IObjectMatcher objectMatcher) { + // If the body is a JSON object, try to match. if (requestMessage.BodyAsJson != null) { return objectMatcher.IsMatch(requestMessage.BodyAsJson); } + // If the body is a byte array, try to match. if (requestMessage.BodyAsBytes != null) { return objectMatcher.IsMatch(requestMessage.BodyAsBytes); } } + // Check if the matcher is a IStringMatcher + if (Matcher is IStringMatcher stringMatcher) + { + // If the body is a JSON object, try to use Body (string) to match. + if (requestMessage.BodyAsJson != null && requestMessage.Body != null) + { + return stringMatcher.IsMatch(requestMessage.Body); + } + + // If the string body is defined, try to match. + if (requestMessage.Body != null) + { + return stringMatcher.IsMatch(requestMessage.Body); + } + } + if (Func != null) { return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body)); diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs index 875c1974a..bd5126aed 100644 --- a/src/WireMock.Net/RequestMessage.cs +++ b/src/WireMock.Net/RequestMessage.cs @@ -65,7 +65,7 @@ public class RequestMessage public string RawQuery { get; } /// - /// The body as string. + /// The original body as string, this is defined when Body or BodyAsJson are not null. /// public string Body { get; } @@ -113,7 +113,7 @@ public class RequestMessage /// The body. /// The headers. /// The cookies. - public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null) + public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData body = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null) { Check.NotNull(url, nameof(url)); Check.NotNull(method, nameof(method)); @@ -140,43 +140,6 @@ public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] stri Query = ParseQuery(RawQuery); } - /// - /// Initializes a new instance of the class. - /// - /// The original url. - /// The HTTP method. - /// The client IP Address. - /// The bodyAsBytes byte[]. - /// The body string. - /// The body encoding - /// The headers. - /// The cookies. - public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null) - { - Check.NotNull(url, nameof(url)); - Check.NotNull(method, nameof(method)); - Check.NotNull(clientIP, nameof(clientIP)); - - Url = url.ToString(); - Protocol = url.Scheme; - Host = url.Host; - Port = url.Port; - Origin = $"{url.Scheme}://{url.Host}:{url.Port}"; - Path = WebUtility.UrlDecode(url.AbsolutePath); - PathSegments = Path.Split('/').Skip(1).ToArray(); - Method = method.ToLower(); - ClientIP = clientIP; - - BodyAsBytes = bodyAsBytes; - Body = body; - BodyEncoding = bodyEncoding; - - Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList(header.Value)); - Cookies = cookies; - RawQuery = WebUtility.UrlDecode(url.Query); - Query = ParseQuery(RawQuery); - } - private static IDictionary> ParseQuery(string queryString) { if (string.IsNullOrEmpty(queryString)) diff --git a/src/WireMock.Net/Util/BodyData.cs b/src/WireMock.Net/Util/BodyData.cs index 3e9d2d1db..2444b0f1f 100644 --- a/src/WireMock.Net/Util/BodyData.cs +++ b/src/WireMock.Net/Util/BodyData.cs @@ -13,7 +13,7 @@ public class BodyData public Encoding Encoding { get; set; } /// - /// The body as string. + /// The body as string, this is defined when BodyAsString or BodyAsJson are not null. /// public string BodyAsString { get; set; } diff --git a/src/WireMock.Net/Util/BodyParser.cs b/src/WireMock.Net/Util/BodyParser.cs index 617dbff28..c551ddc43 100644 --- a/src/WireMock.Net/Util/BodyParser.cs +++ b/src/WireMock.Net/Util/BodyParser.cs @@ -58,6 +58,7 @@ public static async Task Parse([NotNull] Stream stream, [CanBeNull] st else if (contentTypeHeaderValue != null && contentTypeHeaderValue.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) { var stringData = await ReadStringAsync(stream); + data.BodyAsString = stringData.Item1; data.Encoding = stringData.Item2; try diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index d5bae3190..8c080da81 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Text; using System.Threading.Tasks; using NFluent; using WireMock.Matchers; @@ -18,6 +20,7 @@ namespace WireMock.Net.Tests public partial class FluentMockServerTests : IDisposable { private FluentMockServer _server; + private static string jsonRequestMessage = @"{ ""message"" : ""Hello server"" }"; // For for AppVeyor + OpenCover private string GetCurrentFolder() @@ -348,6 +351,39 @@ public async Task FluentMockServer_Should_respond_to_request_bodyAsBytes() Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 }); } + public static IEnumerable ValidMatchersForHelloServerJsonMessage => + new List + { + new object[] { new WildcardMatcher("*Hello server*"), "application/json" }, + new object[] { new WildcardMatcher("*Hello server*"), "text/plain" }, + new object[] { new ExactMatcher(jsonRequestMessage), "application/json" }, + new object[] { new ExactMatcher(jsonRequestMessage), "text/plain" }, + new object[] { new RegexMatcher("Hello server"), "application/json" }, + new object[] { new RegexMatcher("Hello server"), "text/plain" }, + new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "application/json" }, + new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" } + }; + + [Theory] + [MemberData(nameof(ValidMatchersForHelloServerJsonMessage))] + public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json(IMatcher matcher, string contentType) + { + // Assign + _server = FluentMockServer.Start(); + + _server + .Given(Request.Create().WithPath("/foo").WithBody(matcher)) + .RespondWith(Response.Create().WithBody("Hello client")); + + // Act + var content = new StringContent(jsonRequestMessage, Encoding.UTF8, contentType); + var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + "/foo", content); + + // Assert + var responseString = await response.Content.ReadAsStringAsync(); + Check.That(responseString).Equals("Hello client"); + } + [Fact] public async Task FluentMockServer_Should_respond_404_for_unexpected_request() { @@ -569,4 +605,4 @@ public void Dispose() _serverForProxyForwarding?.Stop(); } } -} \ No newline at end of file +} diff --git a/test/WireMock.Net.Tests/RequestCookieTests.cs b/test/WireMock.Net.Tests/RequestCookieTests.cs index 20dbc890b..843924175 100644 --- a/test/WireMock.Net.Tests/RequestCookieTests.cs +++ b/test/WireMock.Net.Tests/RequestCookieTests.cs @@ -18,7 +18,7 @@ public void Request_WithCookie_OK() var spec = Request.Create().UsingAnyMethod().WithCookie("session", "a*"); // when - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary { { "session", "abc" } }); + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, new Dictionary { { "session", "abc" } }); // then var requestMatchResult = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs index e0a59f7ed..0b6a4ab28 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs @@ -87,7 +87,34 @@ public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_IStringMatcher Check.That(score).IsEqualTo(0.0d); // Verify - stringMatcherMock.Verify(m => m.IsMatch("b"), Times.Never); + stringMatcherMock.Verify(m => m.IsMatch(It.IsAny()), Times.Never); + } + + [Fact] + public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_and_BodyAsString_IStringMatcher() + { + // Assign + var body = new BodyData + { + BodyAsJson = new { value = 42 }, + BodyAsString = "orig" + }; + var stringMatcherMock = new Mock(); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(0.5d); + + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", body); + + var matcher = new RequestMessageBodyMatcher(stringMatcherMock.Object); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.5d); + + // Verify + stringMatcherMock.Verify(m => m.IsMatch(It.IsAny()), Times.Once); } [Fact] diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index cfdea63e4..07457870f 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -5,6 +5,7 @@ using Xunit; using WireMock.RequestBuilders; using WireMock.Matchers.Request; +using WireMock.Util; namespace WireMock.Net.Tests { @@ -33,9 +34,11 @@ public void Should_exclude_requests_not_matching_given_headers() var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tatata"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tata" } } }); + var body = new BodyData + { + BodyAsString = "whatever" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary { { "X-toto", new[] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -49,9 +52,11 @@ public void Should_exclude_requests_not_matching_given_headers_ignorecase() var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "abc", false); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "ABC" } } }); + var body = new BodyData + { + BodyAsString = "whatever" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary { { "X-toto", new[] { "ABC" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -65,17 +70,17 @@ public void Should_specify_requests_matching_given_header_prefix() var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tata*"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "TaTa" } } }); + var body = new BodyData + { + BodyAsString = "whatever" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary { { "X-toto", new[] { "TaTa" } } }); // then var requestMatchResult = new RequestMatchResult(); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0); } - - [Fact] public void Should_specify_requests_matching_given_body() { @@ -83,16 +88,17 @@ public void Should_specify_requests_matching_given_body() var spec = Request.Create().UsingAnyMethod().WithBody("Hello world!"); // when - string bodyAsString = "Hello world!"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "Hello world!" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0); } - [Fact] public void Should_exclude_requests_not_matching_given_body() { @@ -100,9 +106,11 @@ public void Should_exclude_requests_not_matching_given_body() var spec = Request.Create().UsingAnyMethod().WithBody(" Hello world! "); // when - string bodyAsString = "xxx"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tata" } } }); + var body = new BodyData + { + BodyAsString = "xxx" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary { { "X-toto", new[] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs index 1aae75514..2bab6cca9 100644 --- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs +++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Text; using Newtonsoft.Json; using NFluent; @@ -76,9 +75,11 @@ public void Request_WithBodyExactMatcher() var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat")); // when - string bodyAsString = "cat"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "cat" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -92,9 +93,11 @@ public void Request_WithBodyWildcardMatcher() var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*")); // when - string bodyAsString = "Hello world!"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tatata" } } }); + var body = new BodyData + { + BodyAsString = "Hello world!" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -108,14 +111,16 @@ public void Request_WithBodyXPathMatcher_true() var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")); // when - string xmlBodyAsString = @" + var body = new BodyData + { + BodyAsString = @" abc def xyz - "; - byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8); + " + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -129,14 +134,16 @@ public void Request_WithBodyXPathMatcher_false() var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]")); // when - string xmlBodyAsString = @" + var body = new BodyData + { + BodyAsString = @" abc def xyz - "; - byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, xmlBodyAsString, Encoding.UTF8); + " + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -150,9 +157,11 @@ public void Request_WithBodyJsonPathMatcher_true() var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); // when - string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -166,9 +175,11 @@ public void Request_WithBodyJsonPathMatcher_false() var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when - string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); @@ -186,6 +197,7 @@ public void Request_WithBodyAsJson_Object_JsonPathMatcher_true() var bodyData = new BodyData { BodyAsJson = JsonConvert.DeserializeObject(jsonString), + BodyAsString = jsonString, Encoding = Encoding.UTF8 }; @@ -195,6 +207,7 @@ public void Request_WithBodyAsJson_Object_JsonPathMatcher_true() var requestMatchResult = new RequestMatchResult(); Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0); } + [Fact] public void Request_WithBodyAsJson_Array_JsonPathMatcher_1() { @@ -206,6 +219,7 @@ public void Request_WithBodyAsJson_Array_JsonPathMatcher_1() var bodyData = new BodyData { BodyAsJson = JsonConvert.DeserializeObject(jsonString), + BodyAsString = jsonString, Encoding = Encoding.UTF8 }; @@ -227,6 +241,7 @@ public void Request_WithBodyAsJson_Array_JsonPathMatcher_2() var bodyData = new BodyData { BodyAsJson = JsonConvert.DeserializeObject(jsonString), + BodyAsString = jsonString, Encoding = Encoding.UTF8 }; diff --git a/test/WireMock.Net.Tests/RequestWithPathTests.cs b/test/WireMock.Net.Tests/RequestWithPathTests.cs index 2693d3c09..d75edeb3d 100644 --- a/test/WireMock.Net.Tests/RequestWithPathTests.cs +++ b/test/WireMock.Net.Tests/RequestWithPathTests.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; -using System.Text; using NFluent; using WireMock.Matchers; using Xunit; using WireMock.RequestBuilders; using WireMock.Matchers.Request; +using WireMock.Util; namespace WireMock.Net.Tests { @@ -20,9 +20,11 @@ public void Request_WithPath_WithHeader_Match() var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata"); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "X-toto", new[] { "tata" } } }); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, new Dictionary { { "X-toto", new[] { "tata" } } }); // then var requestMatchResult = new RequestMatchResult(); @@ -105,9 +107,11 @@ public void Should_specify_requests_matching_given_path_and_method_delete() var spec = Request.Create().WithPath("/foo").UsingDelete(); // when - string bodyAsString = "whatever"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "whatever" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", ClientIp, body); // then var requestMatchResult = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyHandlebarsTests.cs b/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyHandlebarsTests.cs index 91f18d160..a76dbb3d7 100644 --- a/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyHandlebarsTests.cs +++ b/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyHandlebarsTests.cs @@ -41,9 +41,11 @@ public async Task Response_ProvideResponse_Handlebars_WithBodyAsJson() public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "whatever" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); var response = Response.Create() .WithBody("test {{request.url}} {{request.path}} {{request.method}}") @@ -60,9 +62,11 @@ public async Task Response_ProvideResponse_Handlebars_UrlPathVerb() public async Task Response_ProvideResponse_Handlebars_Query() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", ClientIp, body); var response = Response.Create() .WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}") @@ -79,9 +83,11 @@ public async Task Response_ProvideResponse_Handlebars_Query() public async Task Response_ProvideResponse_Handlebars_Header() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); @@ -98,9 +104,11 @@ public async Task Response_ProvideResponse_Handlebars_Header() public async Task Response_ProvideResponse_Handlebars_Headers() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, new Dictionary { { "Content-Type", new[] { "text/plain" } } }); var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}", "{{request.url}}").WithBody("test").WithTransformer(); @@ -118,9 +126,11 @@ public async Task Response_ProvideResponse_Handlebars_Headers() public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost:1234"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost:1234"), "POST", ClientIp, body); var response = Response.Create() .WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}") diff --git a/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyTests.cs b/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyTests.cs index 50812f54c..cdcfe22d3 100644 --- a/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyTests.cs +++ b/test/WireMock.Net.Tests/ResponseBuilderTests/ResponseWithBodyTests.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using NFluent; using WireMock.ResponseBuilders; +using WireMock.Util; using Xunit; namespace WireMock.Net.Tests.ResponseBuilderTests @@ -15,9 +16,11 @@ public class ResponseWithBodyTests public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_String() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.String, Encoding.ASCII); @@ -34,9 +37,11 @@ public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_S public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_Bytes() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); var response = Response.Create().WithBody(new byte[] { 48, 49 }, BodyDestinationFormat.SameAsSource, Encoding.ASCII); @@ -53,9 +58,11 @@ public async Task Response_ProvideResponse_WithBody_Bytes_Encoding_Destination_B public async Task Response_ProvideResponse_WithBody_String_Encoding() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); var response = Response.Create().WithBody("test", null, Encoding.ASCII); @@ -71,9 +78,11 @@ public async Task Response_ProvideResponse_WithBody_String_Encoding() public async Task Response_ProvideResponse_WithBody_Object_Encoding() { // given - string bodyAsString = "abc"; - byte[] body = Encoding.UTF8.GetBytes(bodyAsString); - var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8); + var body = new BodyData + { + BodyAsString = "abc" + }; + var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body); object x = new { value = "test" }; var response = Response.Create().WithBodyAsJson(x, Encoding.ASCII); @@ -137,7 +146,7 @@ public async Task Response_ProvideResponse_WithBody_String_Json_Encoding() // Assert Check.That(responseMessage.Body).IsNull(); Check.That(responseMessage.BodyAsBytes).IsNull(); - Check.That(((dynamic) responseMessage.BodyAsJson).value).Equals(42); + Check.That(((dynamic)responseMessage.BodyAsJson).value).Equals(42); Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII); } }