Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,22 @@ public async Task InitializeAsync()
/// (200 or 401) is returned. Use this for any test that asserts an authorized request succeeds.
/// </summary>
public Task<HttpResponseMessage> GetWithValidTokenAsync(string url)
{
return GetWithTokenAsync(url, "valid-token");
}

/// <summary>
/// Sends a GET with the given bearer token, retrying on a transient 403 from API Gateway the same
/// way as <see cref="GetWithValidTokenAsync"/>. 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.
/// </summary>
public Task<HttpResponseMessage> 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;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit.Sdk;

namespace TestCustomAuthorizerApp.IntegrationTests;

/// <summary>
/// Status code assertions that include API Gateway diagnostics in the failure message.
/// </summary>
public static class ResponseAssert
{
/// <summary>
/// Fails the test if the response status code is not <paramref name="expected"/>. 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.
/// </summary>
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) : "<none>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading