From aa9372c9a2623777f1b8ac5996a0a883e5044f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20GALTIER?= <1593976+swoog@users.noreply.github.com> Date: Fri, 14 Jan 2022 16:31:45 +0100 Subject: [PATCH] fix: Do not evaluate all matcher when the first is false --- .../HttpRequestMatcherExtensions.cs | 9 +++------ .../JsonRequestMatchingExtensionTests.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/MockHttp/Extensions/HttpRequestMatcherExtensions.cs b/src/MockHttp/Extensions/HttpRequestMatcherExtensions.cs index c8e876cb..a0873214 100644 --- a/src/MockHttp/Extensions/HttpRequestMatcherExtensions.cs +++ b/src/MockHttp/Extensions/HttpRequestMatcherExtensions.cs @@ -13,18 +13,15 @@ internal static class HttpRequestMatcherExtensions /// if all match the . public static async Task AllAsync(this IEnumerable matchers, MockHttpRequestContext requestContext) { - bool hasMatchedAll = true; foreach (IAsyncHttpRequestMatcher m in matchers) { - if (await m.IsMatchAsync(requestContext).ConfigureAwait(false)) + if (!await m.IsMatchAsync(requestContext).ConfigureAwait(false)) { - continue; + return false; } - - hasMatchedAll = false; } - return hasMatchedAll; + return true; } /// diff --git a/test/MockHttp.Json.Tests/JsonRequestMatchingExtensionTests.cs b/test/MockHttp.Json.Tests/JsonRequestMatchingExtensionTests.cs index 809783db..87665d69 100644 --- a/test/MockHttp.Json.Tests/JsonRequestMatchingExtensionTests.cs +++ b/test/MockHttp.Json.Tests/JsonRequestMatchingExtensionTests.cs @@ -1,5 +1,6 @@ using System.Net; using System.Text; +using FluentAssertions; using MockHttp.FluentAssertions; using MockHttp.Json.Newtonsoft; using Newtonsoft.Json; @@ -34,6 +35,24 @@ public async Task Given_json_content_matching_request_when_matching_should_be_tr // Assert response.Should().HaveStatusCode(HttpStatusCode.OK); } + + [Fact] + public async Task Given_not_a_json_content_matching_request_and_a_different_request_uri_when_matching_should_not_test_others_matcher() + { + var obj = new TestClass { SomeProperty = "value" }; + + _httpMock + .When(m => m + .RequestUri("/users") + .JsonContent(obj)) + .Respond(HttpStatusCode.OK); + + // Act + Func act = () => _httpClient.PostAsync("http://0.0.0.0", new StringContent("test=test", Encoding.UTF8, "application/x-www-form-urlencoded")); + + // Assert + await act.Should().NotThrowAsync(); + } [Theory] [InlineData(true)]