Skip to content

Commit

Permalink
Update mirror auth tests
Browse files Browse the repository at this point in the history
adds missing verify
share common setup
  • Loading branch information
Splamy committed Mar 30, 2024
1 parent f299463 commit bf80138
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions tests/BaGetter.Core.Tests/Upstream/UpstreamAuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,16 @@ public async Task TestMirrorBasicAuthConfiguration()
};
});

mock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = JsonContent.Create(TestServices)
})
.Callback<HttpRequestMessage, CancellationToken>((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<HttpClient>();
var res = await client.GetFromJsonAsync<ServiceIndexResponse>("http://localhost/v3/index.json");

mock.VerifyAll();
}

[Fact]
Expand All @@ -82,22 +76,16 @@ public async Task TestMirrorBearerAuthConfiguration()
};
});

mock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = JsonContent.Create(TestServices)
})
.Callback<HttpRequestMessage, CancellationToken>((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<HttpClient>();
var res = await client.GetFromJsonAsync<ServiceIndexResponse>("http://localhost/v3/index.json");

mock.VerifyAll();
}

[Fact]
Expand All @@ -115,26 +103,20 @@ public async Task TestMirrorCustomAuthConfiguration()
};
});

mock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = JsonContent.Create(TestServices)
})
.Callback<HttpRequestMessage, CancellationToken>((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<HttpClient>();
var res = await client.GetFromJsonAsync<ServiceIndexResponse>("http://localhost/v3/index.json");

mock.VerifyAll();
}

private static (IServiceProvider serivces, Mock<HttpMessageHandler> mockHandler) SetupApp(Action<MirrorOptions> setupOptions)
{
Mock<HttpMessageHandler> mockHandler = new();
Mock<HttpMessageHandler> mockHandler = new(MockBehavior.Strict);

var serviceProvider = new ServiceCollection()
.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build())
Expand All @@ -150,4 +132,20 @@ private static (IServiceProvider serivces, Mock<HttpMessageHandler> mockHandler)

return (serviceProvider, mockHandler);
}

private static void SetupExpectedMockCall(Mock<HttpMessageHandler> mockHandler, Action<HttpRequestMessage> assert)
{
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = JsonContent.Create(TestServices)
})
.Callback<HttpRequestMessage, CancellationToken>((r, c) =>
{
assert(r);
})
.Verifiable(Times.Once());
}
}

0 comments on commit bf80138

Please sign in to comment.