From bf8013890274a404e682592ccc84cedbd01521a4 Mon Sep 17 00:00:00 2001 From: Splamy Date: Wed, 13 Mar 2024 20:24:11 +0100 Subject: [PATCH] Update mirror auth tests adds missing verify share common setup --- .../Upstream/UpstreamAuthenticationTests.cs | 76 +++++++++---------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/tests/BaGetter.Core.Tests/Upstream/UpstreamAuthenticationTests.cs b/tests/BaGetter.Core.Tests/Upstream/UpstreamAuthenticationTests.cs index 2d31a854..0c5908bc 100644 --- a/tests/BaGetter.Core.Tests/Upstream/UpstreamAuthenticationTests.cs +++ b/tests/BaGetter.Core.Tests/Upstream/UpstreamAuthenticationTests.cs @@ -52,22 +52,16 @@ public async Task TestMirrorBasicAuthConfiguration() }; }); - mock.Protected() - .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) - .ReturnsAsync(new HttpResponseMessage - { - StatusCode = HttpStatusCode.OK, - Content = JsonContent.Create(TestServices) - }) - .Callback((r, c) => - { - Assert.Equal("Basic", r.Headers.Authorization.Scheme); - Assert.Equal("dXNlcjpwYXNzd29yZA==", r.Headers.Authorization.Parameter); - }) - .Verifiable(Times.Once()); + SetupExpectedMockCall(mock, r => + { + Assert.Equal("Basic", r.Headers.Authorization.Scheme); + Assert.Equal("dXNlcjpwYXNzd29yZA==", r.Headers.Authorization.Parameter); + }); var client = services.GetRequiredService(); var res = await client.GetFromJsonAsync("http://localhost/v3/index.json"); + + mock.VerifyAll(); } [Fact] @@ -82,22 +76,16 @@ public async Task TestMirrorBearerAuthConfiguration() }; }); - mock.Protected() - .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) - .ReturnsAsync(new HttpResponseMessage - { - StatusCode = HttpStatusCode.OK, - Content = JsonContent.Create(TestServices) - }) - .Callback((r, c) => - { - Assert.Equal("Bearer", r.Headers.Authorization.Scheme); - Assert.Equal("token", r.Headers.Authorization.Parameter); - }) - .Verifiable(Times.Once()); + SetupExpectedMockCall(mock, r => + { + Assert.Equal("Bearer", r.Headers.Authorization.Scheme); + Assert.Equal("token", r.Headers.Authorization.Parameter); + }); var client = services.GetRequiredService(); var res = await client.GetFromJsonAsync("http://localhost/v3/index.json"); + + mock.VerifyAll(); } [Fact] @@ -115,26 +103,20 @@ public async Task TestMirrorCustomAuthConfiguration() }; }); - mock.Protected() - .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) - .ReturnsAsync(new HttpResponseMessage - { - StatusCode = HttpStatusCode.OK, - Content = JsonContent.Create(TestServices) - }) - .Callback((r, c) => - { - Assert.Equal("value1", r.Headers.GetValues("X-Auth").First()); - }) - .Verifiable(Times.Once()); + SetupExpectedMockCall(mock, r => + { + Assert.Equal("value1", r.Headers.GetValues("X-Auth").First()); + }); var client = services.GetRequiredService(); var res = await client.GetFromJsonAsync("http://localhost/v3/index.json"); + + mock.VerifyAll(); } private static (IServiceProvider serivces, Mock mockHandler) SetupApp(Action setupOptions) { - Mock mockHandler = new(); + Mock mockHandler = new(MockBehavior.Strict); var serviceProvider = new ServiceCollection() .AddSingleton(new ConfigurationBuilder().Build()) @@ -150,4 +132,20 @@ private static (IServiceProvider serivces, Mock mockHandler) return (serviceProvider, mockHandler); } + + private static void SetupExpectedMockCall(Mock mockHandler, Action assert) + { + mockHandler.Protected() + .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) + .ReturnsAsync(new HttpResponseMessage + { + StatusCode = HttpStatusCode.OK, + Content = JsonContent.Create(TestServices) + }) + .Callback((r, c) => + { + assert(r); + }) + .Verifiable(Times.Once()); + } }