Skip to content

Commit 2938b9a

Browse files
committed
Use default host and port if endpoint not specified
1 parent 9a2858d commit 2938b9a

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
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.Host}:{settings.Port}";
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: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task TestLambdaToUpperV2()
3838

3939
try
4040
{
41-
await StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
41+
StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
4242
await WaitForGatewayHealthCheck();
4343
await StartLambdaProcess(config);
4444

@@ -69,7 +69,7 @@ public async Task TestLambdaToUpperRest()
6969

7070
try
7171
{
72-
await StartTestToolProcess(ApiGatewayEmulatorMode.Rest, config);
72+
StartTestToolProcess(ApiGatewayEmulatorMode.Rest, config);
7373
await WaitForGatewayHealthCheck();
7474
await StartLambdaProcess(config);
7575

@@ -100,7 +100,7 @@ public async Task TestLambdaToUpperV1()
100100

101101
try
102102
{
103-
await StartTestToolProcess(ApiGatewayEmulatorMode.HttpV1, config);
103+
StartTestToolProcess(ApiGatewayEmulatorMode.HttpV1, config);
104104
await WaitForGatewayHealthCheck();
105105
await StartLambdaProcess(config);
106106

@@ -131,7 +131,7 @@ public async Task TestLambdaBinaryResponse()
131131

132132
try
133133
{
134-
await StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
134+
StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
135135
await WaitForGatewayHealthCheck();
136136
await StartLambdaProcess(config);
137137

@@ -168,7 +168,38 @@ public async Task TestLambdaReturnString()
168168

169169
try
170170
{
171-
await StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
171+
StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config);
172+
await WaitForGatewayHealthCheck();
173+
await StartLambdaProcess(config);
174+
175+
var response = await TestEndpoint(config);
176+
var responseContent = await response.Content.ReadAsStringAsync();
177+
178+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
179+
Assert.Equal("HELLO WORLD", responseContent);
180+
}
181+
finally
182+
{
183+
await CleanupProcesses();
184+
}
185+
}
186+
187+
[Fact]
188+
public async Task TestLambdaWithNullEndpoint()
189+
{
190+
var testProjectDir = Path.GetFullPath("../../../../");
191+
var config = new TestConfig
192+
{
193+
TestToolPath = Path.GetFullPath(Path.Combine(testProjectDir, "../src/Amazon.Lambda.TestTool")),
194+
LambdaPath = Path.GetFullPath(Path.Combine(testProjectDir, "LambdaTestFunctionV2/src/LambdaTestFunctionV2")),
195+
FunctionName = "LambdaTestFunctionV2",
196+
RouteName = "testfunction",
197+
HttpMethod = "Post"
198+
};
199+
200+
try
201+
{
202+
StartTestToolProcess(ApiGatewayEmulatorMode.HttpV2, config, useNullEndpoint: true);
172203
await WaitForGatewayHealthCheck();
173204
await StartLambdaProcess(config);
174205

@@ -205,7 +236,7 @@ private async Task<HttpResponseMessage> TestEndpoint(TestConfig config, HttpCont
205236
};
206237
}
207238

208-
private async Task StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestConfig config)
239+
private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestConfig config, bool useNullEndpoint = false)
209240
{
210241
var startInfo = new ProcessStartInfo
211242
{
@@ -217,14 +248,21 @@ private async Task StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, T
217248
CreateNoWindow = true
218249
};
219250

220-
startInfo.EnvironmentVariables["ASPNETCORE_ENVIRONMENT"] = "Development";
221-
startInfo.EnvironmentVariables["APIGATEWAY_EMULATOR_ROUTE_CONFIG"] = $@"{{
251+
var routeConfig = useNullEndpoint
252+
? $@"{{
253+
""LambdaResourceName"": ""{config.RouteName}"",
254+
""HttpMethod"": ""{config.HttpMethod}"",
255+
""Path"": ""/{config.RouteName}""
256+
}}"
257+
: $@"{{
222258
""LambdaResourceName"": ""{config.RouteName}"",
223259
""Endpoint"": ""http://localhost:{LambdaPort}"",
224260
""HttpMethod"": ""{config.HttpMethod}"",
225261
""Path"": ""/{config.RouteName}""
226262
}}";
227263

264+
startInfo.EnvironmentVariables["APIGATEWAY_EMULATOR_ROUTE_CONFIG"] = routeConfig;
265+
228266
_mainProcess = Process.Start(startInfo) ?? throw new Exception("Failed to start test tool process");
229267
ConfigureProcessLogging(_mainProcess, "TestTool");
230268
}

0 commit comments

Comments
 (0)