From c5d8e477d60d3426d7f853c74467539e0c7e280d Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 4 Feb 2024 15:12:08 +0000 Subject: [PATCH] Add missing coverage - Add unit tests for missing coverage. - Simplify some tests. --- .../HttpRequestInterceptionBuilderTests.cs | 286 +++++++++++++----- 1 file changed, 216 insertions(+), 70 deletions(-) diff --git a/tests/HttpClientInterception.Tests/HttpRequestInterceptionBuilderTests.cs b/tests/HttpClientInterception.Tests/HttpRequestInterceptionBuilderTests.cs index 9ea3699d..c1ce2aa5 100644 --- a/tests/HttpClientInterception.Tests/HttpRequestInterceptionBuilderTests.cs +++ b/tests/HttpClientInterception.Tests/HttpRequestInterceptionBuilderTests.cs @@ -2238,108 +2238,254 @@ public static async Task Builder_For_Posted_Json_To_Match_Intercepts_Requests_Th public static void Builder_ForAll_Throws_ArgumentNullException_If_Custom_Matching_Delegate_Is_Null() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll((Predicate[])null) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); + + Predicate[] predicates = null; - // Act & Assert - Should.Throw(InterceptionBuilder).ParamName.ShouldBe("predicates"); + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)).ParamName.ShouldBe("predicates"); } [Fact] public static void Builder_ForAll_Throws_InvalidOperationException_If_Custom_Matching_Delegate_Is_Empty() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll(Array.Empty>()) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); + + var predicates = Array.Empty>(); - // Act & Assert - Should.Throw(InterceptionBuilder); + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)); } [Fact] public static void Builder_ForAll_Throws_ArgumentNullException_If_Async_Custom_Matching_Delegate_Is_Null() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll((Func>[])null) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); - // Act & Assert - Should.Throw(InterceptionBuilder).ParamName.ShouldBe("predicates"); + Func>[] predicates = null; + + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)).ParamName.ShouldBe("predicates"); } [Fact] public static void Builder_ForAll_Throws_InvalidOperationException_If_Async_Custom_Matching_Delegate_Is_Empty() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll(Array.Empty>>()) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); + + var predicates = Array.Empty>>(); - // Act & Assert - Should.Throw(InterceptionBuilder); + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)); } [Fact] public static void Builder_ForAll_Throws_InvalidOperationException_If_At_Least_One_Async_Custom_Matching_Delegate_Is_Null() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll(new Func>[] { _ => Task.FromResult(true), null }) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); + + Func>[] predicates = + [ + _ => Task.FromResult(true), + null, + ]; - // Act & Assert - Should.Throw(InterceptionBuilder); + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)); } [Fact] public static void Builder_ForAll_Throws_InvalidOperationException_If_At_Least_One_Custom_Matching_Delegate_Is_Null() { // Arrange - static HttpRequestInterceptionBuilder InterceptionBuilder() => - new HttpRequestInterceptionBuilder() - .Requests() - .ForHttps() - .ForHost("api.github.com") - .ForPath("orgs/justeattakeaway") - .ForAll(new Predicate[] { _ => true, null }) - .Responds() - .WithStatus(HttpStatusCode.OK); + var builder = new HttpRequestInterceptionBuilder(); + + Predicate[] predicates = + [ + _ => true, + null, + ]; + + // Act and Assert + Should.Throw(() => builder.ForAll(predicates)); + } + + [Fact] + public static async Task Builder_ForAll_Matches_Request_With_One_Predicate() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Predicate[] predicates = + [ + _ => true, + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act + string actual = await client.GetStringAsync("https://google.com/"); + + // Assert + actual.ShouldBe(expected); + } + + [Fact] + public static async Task Builder_ForAll_Matches_Request_With_One_Async_Predicate() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Func>[] predicates = + [ + _ => Task.FromResult(true), + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act + string actual = await client.GetStringAsync("https://google.com/"); + + // Assert + actual.ShouldBe(expected); + } + + [Fact] + public static async Task Builder_ForAll_Matches_Request_With_Multiple_Predicates() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Predicate[] predicates = + [ + _ => true, + _ => true, + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act + string actual = await client.GetStringAsync("https://google.com/"); + + // Assert + actual.ShouldBe(expected); + } + + [Fact] + public static async Task Builder_ForAll_Matches_Request_With_Multiple_Async_Predicates() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Func>[] predicates = + [ + _ => Task.FromResult(true), + _ => Task.FromResult(true), + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act + string actual = await client.GetStringAsync("https://google.com/"); + + // Assert + actual.ShouldBe(expected); + } - // Act & Assert - Should.Throw(InterceptionBuilder); + [Fact] + public static async Task Builder_ForAll_Does_Not_Match_Request_With_Multiple_Predicates_If_Any_False() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Predicate[] predicates = + [ + _ => true, + _ => false, + _ => true, + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act and Assert + await Should.ThrowAsync(() => client.GetStringAsync("https://google.com/")); + } + + [Fact] + public static async Task Builder_ForAll_Matches_Request_With_Multiple_Async_Predicates_If_Any_False() + { + // Arrange + string expected = Guid.NewGuid().ToString(); + Func>[] predicates = + [ + _ => Task.FromResult(true), + _ => Task.FromResult(false), + _ => Task.FromResult(true), + ]; + + var builder = new HttpRequestInterceptionBuilder() + .Requests() + .ForAll(predicates) + .Responds() + .WithContent(expected); + + var options = new HttpClientInterceptorOptions() + .ThrowsOnMissingRegistration() + .Register(builder); + + using var client = options.CreateHttpClient(); + + // Act and Assert + await Should.ThrowAsync(() => client.GetStringAsync("https://google.com/")); } [Fact] @@ -2422,19 +2568,19 @@ public static async Task Can_Do_Custom_Matching_Even_Number_Requests_Return_200_ using var client = options.CreateHttpClient(); - // Act & Assert (First request) + // Act and Assert (First request) HttpResponseMessage firstResponse = await client.GetAsync(requestUri); firstResponse.StatusCode.ShouldBe(HttpStatusCode.OK); - // Act & Assert (Second request) + // Act and Assert (Second request) HttpResponseMessage secondResponse = await client.GetAsync(requestUri); secondResponse.StatusCode.ShouldBe(HttpStatusCode.TooManyRequests); - // Act & Assert (Third request) + // Act and Assert (Third request) HttpResponseMessage thirdResponse = await client.GetAsync(requestUri); thirdResponse.StatusCode.ShouldBe(HttpStatusCode.OK); - // Act & Assert (Fourth request) + // Act and Assert (Fourth request) HttpResponseMessage fourthResponse = await client.GetAsync(requestUri); fourthResponse.StatusCode.ShouldBe(HttpStatusCode.TooManyRequests); }