Skip to content

Commit 9dcf76f

Browse files
tests: this commit includes credential interceptor tests to validate authorization bearer header injection via x-credential.
1 parent a1ecc66 commit 9dcf76f

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace Comanda.Payments.TestSuite.Unit.Intecerptors;
2+
3+
public sealed class CredentialInterceptorTests
4+
{
5+
[Fact(DisplayName = "[interceptor] - when x-credential header is provided then bearer authorization should be injected")]
6+
public async Task When_CredentialHeaderIsProvided_ThenBearerAuthorizationShouldBeInjected()
7+
{
8+
var credential = Identifier.Generate<Payment>();
9+
var httpContext = new DefaultHttpContext();
10+
11+
httpContext.Request.Headers[WebApi.Constants.Headers.Credential] = credential;
12+
13+
var accessor = new HttpContextAccessor
14+
{
15+
HttpContext = httpContext
16+
};
17+
18+
var probe = new ProbeHandler();
19+
var interceptor = new CredentialInterceptor(accessor)
20+
{
21+
InnerHandler = probe
22+
};
23+
24+
var invoker = new HttpMessageInvoker(interceptor);
25+
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost/payments");
26+
27+
_ = await invoker.SendAsync(request, TestContext.Current.CancellationToken);
28+
29+
Assert.NotNull(probe.CapturedRequest);
30+
Assert.NotNull(probe.CapturedRequest.Headers.Authorization);
31+
32+
Assert.Equal("Bearer", probe.CapturedRequest.Headers.Authorization.Scheme);
33+
Assert.Equal(credential, probe.CapturedRequest.Headers.Authorization.Parameter);
34+
}
35+
36+
private sealed class ProbeHandler : HttpMessageHandler
37+
{
38+
public HttpRequestMessage? CapturedRequest { get; private set; }
39+
40+
protected override Task<HttpResponseMessage> SendAsync(
41+
HttpRequestMessage request, CancellationToken cancellationToken)
42+
{
43+
CapturedRequest = request;
44+
45+
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
46+
}
47+
}
48+
}

Boundaries/Comanda.Payments/Tests/Usings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
global using System.Net.Mime;
55

66
global using Microsoft.AspNetCore.Mvc.Testing;
7+
global using Microsoft.AspNetCore.Http;
78
global using Microsoft.Extensions.DependencyInjection;
89

910
global using Comanda.Payments.Domain.Aggregates;
@@ -21,6 +22,7 @@
2122
global using Comanda.Payments.TestSuite.Fixtures;
2223
global using Comanda.Payments.TestSuite.Mocks;
2324
global using Comanda.Payments.WebApi;
25+
global using Comanda.Payments.WebApi.Interceptors;
2426

2527
global using HttpsRichardy.Internal.Essentials.Concepts;
2628
global using HttpsRichardy.Internal.Essentials.Utilities;

0 commit comments

Comments
 (0)