-
Notifications
You must be signed in to change notification settings - Fork 490
feat: update invalid route handling based on emulator mode #1909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
philasmar
merged 2 commits into
feature/lambdatesttool-v2
from
asmarp/update-invalid-route-return
Dec 20, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Extensions/ResultExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Amazon.Lambda.TestTool.Models; | ||
|
||
namespace Amazon.Lambda.TestTool.Extensions; | ||
|
||
/// <summary> | ||
/// A class for common API Gateway responses. | ||
/// </summary> | ||
public static class ApiGatewayResults | ||
{ | ||
/// <summary> | ||
/// Returns a 'Not Found' for HTTP API Gateway mode and 'Missing Authentication Token' for Rest. | ||
/// </summary> | ||
/// <param name="context">The <see cref="HttpContext"/> to update.</param> | ||
/// <param name="emulatorMode">The API Gateway Emulator mode.</param> | ||
/// <returns></returns> | ||
public static IResult RouteNotFound(HttpContext context, ApiGatewayEmulatorMode emulatorMode) | ||
{ | ||
if (emulatorMode == ApiGatewayEmulatorMode.Rest) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
context.Response.Headers.Append("x-amzn-errortype", "MissingAuthenticationTokenException"); | ||
return Results.Json(new { message = "Missing Authentication Token" }); | ||
} | ||
else | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
return Results.Json(new { message = "Not Found" }); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Processes/ApiGatewayEmulatorProcessTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using System.Net; | ||
using Amazon.Lambda.TestTool.Commands.Settings; | ||
using Amazon.Lambda.TestTool.Models; | ||
using Amazon.Lambda.TestTool.Processes; | ||
using Amazon.Lambda.TestTool.UnitTests.Helpers; | ||
|
||
namespace Amazon.Lambda.TestTool.UnitTests.Processes; | ||
|
||
public class ApiGatewayEmulatorProcessTests | ||
{ | ||
[Theory] | ||
[InlineData(ApiGatewayEmulatorMode.Rest, HttpStatusCode.Forbidden, "{\"message\":\"Missing Authentication Token\"}")] | ||
[InlineData(ApiGatewayEmulatorMode.HttpV1, HttpStatusCode.NotFound, "{\"message\":\"Not Found\"}")] | ||
[InlineData(ApiGatewayEmulatorMode.HttpV2, HttpStatusCode.NotFound, "{\"message\":\"Not Found\"}")] | ||
public async Task RouteNotFound(ApiGatewayEmulatorMode mode, HttpStatusCode statusCode, string body) | ||
{ | ||
// Arrange | ||
var cancellationSource = new CancellationTokenSource(); | ||
cancellationSource.CancelAfter(5000); | ||
var settings = new RunCommandSettings { ApiGatewayEmulatorPort = 9003, ApiGatewayEmulatorMode = mode, NoLaunchWindow = true}; | ||
var apiUrl = $"http://{settings.Host}:{settings.ApiGatewayEmulatorPort}/__lambda_test_tool_apigateway_health__"; | ||
|
||
// Act | ||
var process = ApiGatewayEmulatorProcess.Startup(settings, cancellationSource.Token); | ||
var isApiRunning = await TestHelpers.WaitForApiToStartAsync(apiUrl); | ||
var response = await TestHelpers.SendRequest($"{process.ServiceUrl}/invalid"); | ||
await cancellationSource.CancelAsync(); | ||
|
||
// Assert | ||
await process.RunningTask; | ||
Assert.True(isApiRunning); | ||
Assert.Equal(statusCode, response.StatusCode); | ||
Assert.Equal(body, await response.Content.ReadAsStringAsync()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.