Skip to content

Commit 6b857ec

Browse files
Use default host and port if endpoint not specified (#1941)
1 parent 9880680 commit 6b857ec

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/ApiGatewayEmulatorProcess.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
9696
PayloadStream = lambdaRequestStream
9797
};
9898

99-
using var lambdaClient = CreateLambdaServiceClient(routeConfig);
99+
using var lambdaClient = CreateLambdaServiceClient(routeConfig, settings);
100100
var response = await lambdaClient.InvokeAsync(invokeRequest);
101101

102102
if (response.FunctionError != null)
@@ -111,13 +111,11 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
111111
{
112112
var lambdaResponse = response.ToApiGatewayHttpApiV2ProxyResponse();
113113
await lambdaResponse.ToHttpResponseAsync(context);
114-
return;
115114
}
116115
else
117116
{
118117
var lambdaResponse = response.ToApiGatewayProxyResponse(settings.ApiGatewayEmulatorMode.Value);
119118
await lambdaResponse.ToHttpResponseAsync(context, settings.ApiGatewayEmulatorMode.Value);
120-
return;
121119
}
122120
});
123121

@@ -131,12 +129,27 @@ public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, Can
131129
};
132130
}
133131

134-
private static IAmazonLambda CreateLambdaServiceClient(ApiGatewayRouteConfig routeConfig)
132+
/// <summary>
133+
/// Creates an Amazon Lambda service client with the specified configuration.
134+
/// </summary>
135+
/// <param name="routeConfig">The API Gateway route configuration containing the endpoint information.
136+
/// If the endpoint is specified in routeConfig, it will be used as the service URL.</param>
137+
/// <param name="settings">The run command settings containing host and port information.
138+
/// If routeConfig endpoint is null, the service URL will be constructed using settings.Host and settings.Port.</param>
139+
/// <returns>An instance of IAmazonLambda configured with the specified endpoint and credentials.</returns>
140+
/// <remarks>
141+
/// The function uses hard-coded AWS credentials ("accessKey", "secretKey") for authentication since they are not actually being used.
142+
/// The service URL is determined by either:
143+
/// - Using routeConfig.Endpoint if it's not null
144+
/// - Combining settings.Host and settings.Port if routeConfig.Endpoint is null
145+
/// </remarks>
146+
private static IAmazonLambda CreateLambdaServiceClient(ApiGatewayRouteConfig routeConfig, RunCommandSettings settings)
135147
{
136-
// TODO: Handle routeConfig.Endpoint to null and use the settings versions of runtime.
148+
var endpoint = routeConfig.Endpoint ?? $"http://{settings.LambdaEmulatorHost}:{settings.LambdaEmulatorPort}";
149+
137150
var lambdaConfig = new AmazonLambdaConfig
138151
{
139-
ServiceURL = routeConfig.Endpoint
152+
ServiceURL = endpoint
140153
};
141154

142155
return new AmazonLambdaClient(new Amazon.Runtime.BasicAWSCredentials("accessKey", "secretKey"), lambdaConfig);

Tools/LambdaTestTool-v2/tests/Amazon.Lambda.TestTool.IntegrationTests/ApiGatewayEmulatorProcessTests.cs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public async Task TestLambdaToUpperV2()
6161
finally
6262
{
6363
await cancellationTokenSource.CancelAsync();
64-
await CleanupProcesses();
6564
}
6665
}
6766

@@ -97,7 +96,6 @@ public async Task TestLambdaToUpperRest()
9796
finally
9897
{
9998
await cancellationTokenSource.CancelAsync();
100-
await CleanupProcesses();
10199
}
102100
}
103101

@@ -133,7 +131,6 @@ public async Task TestLambdaToUpperV1()
133131
finally
134132
{
135133
await cancellationTokenSource.CancelAsync();
136-
await CleanupProcesses();
137134
}
138135
}
139136

@@ -175,7 +172,6 @@ public async Task TestLambdaBinaryResponse()
175172
finally
176173
{
177174
await cancellationTokenSource.CancelAsync();
178-
await CleanupProcesses();
179175
}
180176
}
181177

@@ -211,7 +207,39 @@ public async Task TestLambdaReturnString()
211207
finally
212208
{
213209
await cancellationTokenSource.CancelAsync();
214-
await CleanupProcesses();
210+
}
211+
}
212+
213+
[Fact]
214+
public async Task TestLambdaWithNullEndpoint()
215+
{
216+
var testProjectDir = Path.GetFullPath("../../../../../testapps");
217+
var config = new TestConfig
218+
{
219+
TestToolPath = Path.GetFullPath(Path.Combine(testProjectDir, "../src/Amazon.Lambda.TestTool")),
220+
LambdaPath = Path.GetFullPath(Path.Combine(testProjectDir, "LambdaTestFunctionV2")),
221+
FunctionName = "LambdaTestFunctionV2",
222+
RouteName = "testfunction",
223+
HttpMethod = "Post"
224+
};
225+
226+
var cancellationTokenSource = new CancellationTokenSource();
227+
228+
try
229+
{
230+
StartTestToolProcessWithNullEndpoint(ApiGatewayEmulatorMode.HttpV2, Constants.DefaultApiGatewayEmulatorPort, config, cancellationTokenSource);
231+
await WaitForGatewayHealthCheck(Constants.DefaultApiGatewayEmulatorPort);
232+
await StartLambdaProcess(config, Constants.DefaultLambdaEmulatorPort);
233+
234+
var response = await TestEndpoint(config, Constants.DefaultApiGatewayEmulatorPort );
235+
var responseContent = await response.Content.ReadAsStringAsync();
236+
237+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
238+
Assert.Equal("HELLO WORLD", responseContent);
239+
}
240+
finally
241+
{
242+
await cancellationTokenSource.CancelAsync();
215243
}
216244
}
217245

@@ -236,6 +264,24 @@ private async Task<HttpResponseMessage> TestEndpoint(TestConfig config, int apiG
236264
};
237265
}
238266

267+
private void StartTestToolProcessWithNullEndpoint(ApiGatewayEmulatorMode apiGatewayMode, int apiGatewayPort, TestConfig config, CancellationTokenSource cancellationTokenSource)
268+
{
269+
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
270+
Environment.SetEnvironmentVariable("APIGATEWAY_EMULATOR_ROUTE_CONFIG", $@"{{
271+
""LambdaResourceName"": ""{config.RouteName}"",
272+
""HttpMethod"": ""{config.HttpMethod}"",
273+
""Path"": ""/{config.RouteName}""
274+
}}");
275+
276+
cancellationTokenSource.CancelAfter(5000);
277+
var settings = new RunCommandSettings { NoLaunchWindow = true, ApiGatewayEmulatorMode = apiGatewayMode, ApiGatewayEmulatorPort = apiGatewayPort};
278+
279+
var command = new RunCommand(_mockInteractiveService.Object, _mockEnvironmentManager.Object);
280+
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
281+
282+
_ = command.ExecuteAsync(context, settings, cancellationTokenSource);
283+
}
284+
239285
private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestConfig config, int lambdaPort, int apiGatewayPort, CancellationTokenSource cancellationTokenSource)
240286
{
241287
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

0 commit comments

Comments
 (0)