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);
}
}