From a29f4b5e49f1325bc79f13be264e69b008e4882d Mon Sep 17 00:00:00 2001 From: Norm Johanson Date: Mon, 30 Nov 2026 20:38:24 +0000 Subject: [PATCH] Retry transient 403s in custom authorizer missing-context tests The five *_WithMissingAuthorizerContextKey_ReturnsUnauthorized tests sent a single request with the partial-context token. A freshly deployed API Gateway stage can briefly return 403 on the authorizer path, which the valid-token tests already retry. The REST API variant failed intermittently in AWS CI with 403 instead of the expected 401. - Add GetWithTokenAsync to the fixture and use it for the partial-context requests, so they get the same 403 retry as the valid-token requests. - Add ResponseAssert.StatusCodeAsync, which includes the response body and the API Gateway error type and request ID headers when the status code does not match. --- .../HttpApiV1Tests.cs | 10 ++--- .../HttpApiV2Tests.cs | 20 +++------- .../IntegrationTestContextFixture.cs | 13 +++++- .../NonStringAuthorizerTests.cs | 10 ++--- .../ResponseAssert.cs | 40 +++++++++++++++++++ .../RestApiTests.cs | 10 ++--- 6 files changed, 67 insertions(+), 36 deletions(-) create mode 100644 Libraries/test/TestCustomAuthorizerApp.IntegrationTests/ResponseAssert.cs diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV1Tests.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV1Tests.cs index ea122eef0..04ef7885d 100644 --- a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV1Tests.cs +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV1Tests.cs @@ -61,14 +61,10 @@ public async Task HttpApiV1UserInfo_WithValidAuth_ReturnsAuthorizerContext() [Fact] public async Task HttpApiV1UserInfo_WithMissingAuthorizerContextKey_ReturnsUnauthorized() { - // Arrange - use partial-context token that authorizes but omits expected context keys - var request = new HttpRequestMessage(HttpMethod.Get, $"{_fixture.HttpApiUrl}/api/http-v1-user-info"); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "partial-context"); - - // Act - var response = await _fixture.HttpClient.SendAsync(request); + // Act - use partial-context token that authorizes but omits expected context keys + var response = await _fixture.GetWithTokenAsync($"{_fixture.HttpApiUrl}/api/http-v1-user-info", "partial-context"); // Assert - generated Lambda handler returns 401 when context key is missing - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + await ResponseAssert.StatusCodeAsync(HttpStatusCode.Unauthorized, response); } } diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV2Tests.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV2Tests.cs index c7f280c95..573dae0db 100644 --- a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV2Tests.cs +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/HttpApiV2Tests.cs @@ -72,15 +72,11 @@ public async Task UserInfo_WithValidAuth_ReturnsAuthorizerContext() [Fact] public async Task UserInfo_WithMissingAuthorizerContextKey_ReturnsUnauthorized() { - // Arrange - use partial-context token that authorizes but omits expected context keys - var request = new HttpRequestMessage(HttpMethod.Get, $"{_fixture.HttpApiUrl}/api/user-info"); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "partial-context"); - - // Act - var response = await _fixture.HttpClient.SendAsync(request); + // Act - use partial-context token that authorizes but omits expected context keys + var response = await _fixture.GetWithTokenAsync($"{_fixture.HttpApiUrl}/api/user-info", "partial-context"); // Assert - generated Lambda handler returns 401 when context key is missing - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + await ResponseAssert.StatusCodeAsync(HttpStatusCode.Unauthorized, response); } /// @@ -109,14 +105,10 @@ public async Task IHttpResult_WithValidAuth_ReturnsSuccess() [Fact] public async Task IHttpResult_WithMissingAuthorizerContextKey_ReturnsUnauthorized() { - // Arrange - use partial-context token that authorizes but omits expected context keys - var request = new HttpRequestMessage(HttpMethod.Get, $"{_fixture.HttpApiUrl}/api/ihttpresult-user-info"); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "partial-context"); - - // Act - var response = await _fixture.HttpClient.SendAsync(request); + // Act - use partial-context token that authorizes but omits expected context keys + var response = await _fixture.GetWithTokenAsync($"{_fixture.HttpApiUrl}/api/ihttpresult-user-info", "partial-context"); // Assert - generated Lambda handler returns 401 when context key is missing - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + await ResponseAssert.StatusCodeAsync(HttpStatusCode.Unauthorized, response); } } diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs index 31c041122..75b2c0735 100644 --- a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs @@ -106,11 +106,22 @@ public async Task InitializeAsync() /// (200 or 401) is returned. Use this for any test that asserts an authorized request succeeds. /// public Task GetWithValidTokenAsync(string url) + { + return GetWithTokenAsync(url, "valid-token"); + } + + /// + /// Sends a GET with the given bearer token, retrying on a transient 403 from API Gateway the same + /// way as . Use this for any token the authorizer allows, such as + /// "partial-context", where a 403 is never the expected result. If the authorizer really denies the + /// request, the retries time out and the last 403 is returned, so the test still fails. + /// + public Task GetWithTokenAsync(string url, string token) { return RetryHelper.SendWithRetryOnForbiddenAsync(HttpClient, () => { var request = new HttpRequestMessage(HttpMethod.Get, url); - request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "valid-token"); + request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); return request; }); } diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/NonStringAuthorizerTests.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/NonStringAuthorizerTests.cs index bb9d7c77e..4b475a4fa 100644 --- a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/NonStringAuthorizerTests.cs +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/NonStringAuthorizerTests.cs @@ -69,15 +69,11 @@ public async Task NonStringUserInfo_WithValidAuth_ReturnsConvertedValues() [Fact] public async Task NonStringUserInfo_WithMissingAuthorizerContextKey_ReturnsUnauthorized() { - // Arrange - use partial-context token that authorizes but omits expected context keys - var request = new HttpRequestMessage(HttpMethod.Get, $"{_fixture.HttpApiUrl}/api/nonstring-user-info"); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "partial-context"); - - // Act - var response = await _fixture.HttpClient.SendAsync(request); + // Act - use partial-context token that authorizes but omits expected context keys + var response = await _fixture.GetWithTokenAsync($"{_fixture.HttpApiUrl}/api/nonstring-user-info", "partial-context"); // Assert - generated Lambda handler returns 401 when context key is missing - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + await ResponseAssert.StatusCodeAsync(HttpStatusCode.Unauthorized, response); } /// diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/ResponseAssert.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/ResponseAssert.cs new file mode 100644 index 000000000..4accb6d67 --- /dev/null +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/ResponseAssert.cs @@ -0,0 +1,40 @@ +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Xunit.Sdk; + +namespace TestCustomAuthorizerApp.IntegrationTests; + +/// +/// Status code assertions that include API Gateway diagnostics in the failure message. +/// +public static class ResponseAssert +{ + /// + /// Fails the test if the response status code is not . The failure message + /// includes the response body and the API Gateway error type and request ID headers. These show which + /// layer rejected the request: for example, a 403 whose body reports an explicit deny came from the + /// authorizer, while other 403 bodies come from API Gateway before the authorizer ran. + /// + public static async Task StatusCodeAsync(HttpStatusCode expected, HttpResponseMessage response) + { + if (response.StatusCode == expected) + { + return; + } + + var body = await response.Content.ReadAsStringAsync(); + throw new XunitException( + $"Expected status {(int)expected} ({expected}) but got {(int)response.StatusCode} ({response.StatusCode}) " + + $"from {response.RequestMessage?.Method} {response.RequestMessage?.RequestUri}.\n" + + $"x-amzn-ErrorType: {GetHeader(response, "x-amzn-ErrorType")}\n" + + $"x-amzn-RequestId: {GetHeader(response, "x-amzn-RequestId")}\n" + + $"x-amz-apigw-id: {GetHeader(response, "x-amz-apigw-id")}\n" + + $"Body: {body}"); + } + + private static string GetHeader(HttpResponseMessage response, string name) + { + return response.Headers.TryGetValues(name, out var values) ? string.Join(", ", values) : ""; + } +} diff --git a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/RestApiTests.cs b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/RestApiTests.cs index 26b7b7c22..6d7aeb66d 100644 --- a/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/RestApiTests.cs +++ b/Libraries/test/TestCustomAuthorizerApp.IntegrationTests/RestApiTests.cs @@ -61,14 +61,10 @@ public async Task RestUserInfo_WithValidAuth_ReturnsAuthorizerContext() [Fact] public async Task RestUserInfo_WithMissingAuthorizerContextKey_ReturnsUnauthorized() { - // Arrange - use partial-context token that authorizes but omits expected context keys - var request = new HttpRequestMessage(HttpMethod.Get, $"{_fixture.RestApiUrl}/api/rest-user-info"); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "partial-context"); - - // Act - var response = await _fixture.HttpClient.SendAsync(request); + // Act - use partial-context token that authorizes but omits expected context keys + var response = await _fixture.GetWithTokenAsync($"{_fixture.RestApiUrl}/api/rest-user-info", "partial-context"); // Assert - generated Lambda handler returns 401 when context key is missing - Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + await ResponseAssert.StatusCodeAsync(HttpStatusCode.Unauthorized, response); } }