Skip to content

Commit 0cea399

Browse files
committed
update test
1 parent a914986 commit 0cea399

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

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

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ApiGatewayEmulatorProcessTests : IAsyncDisposable
2121
private readonly Mock<IRemainingArguments> _mockRemainingArgs = new Mock<IRemainingArguments>();
2222
private readonly ITestOutputHelper _testOutputHelper;
2323
private Process? _lambdaProcess;
24+
private readonly List<string> _setEnvironmentVariables = new List<string>();
2425

2526
public ApiGatewayEmulatorProcessTests(ITestOutputHelper testOutputHelper)
2627
{
@@ -59,7 +60,6 @@ public async Task TestLambdaToUpperV2()
5960
finally
6061
{
6162
await cancellationTokenSource.CancelAsync();
62-
await CleanupProcesses();
6363
}
6464
}
6565

@@ -95,7 +95,6 @@ public async Task TestLambdaToUpperRest()
9595
finally
9696
{
9797
await cancellationTokenSource.CancelAsync();
98-
await CleanupProcesses();
9998
}
10099
}
101100

@@ -131,7 +130,6 @@ public async Task TestLambdaToUpperV1()
131130
finally
132131
{
133132
await cancellationTokenSource.CancelAsync();
134-
await CleanupProcesses();
135133
}
136134
}
137135

@@ -173,7 +171,6 @@ public async Task TestLambdaBinaryResponse()
173171
finally
174172
{
175173
await cancellationTokenSource.CancelAsync();
176-
await CleanupProcesses();
177174
}
178175
}
179176

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

@@ -234,10 +263,28 @@ private async Task<HttpResponseMessage> TestEndpoint(TestConfig config, int apiG
234263
};
235264
}
236265

266+
private void StartTestToolProcessWithNullEndpoint(ApiGatewayEmulatorMode apiGatewayMode, int apiGatewayPort, TestConfig config, CancellationTokenSource cancellationTokenSource)
267+
{
268+
SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
269+
SetEnvironmentVariable("APIGATEWAY_EMULATOR_ROUTE_CONFIG", $@"{{
270+
""LambdaResourceName"": ""{config.RouteName}"",
271+
""HttpMethod"": ""{config.HttpMethod}"",
272+
""Path"": ""/{config.RouteName}""
273+
}}");
274+
275+
cancellationTokenSource.CancelAfter(5000);
276+
var settings = new RunCommandSettings { NoLaunchWindow = true, ApiGatewayEmulatorMode = apiGatewayMode, ApiGatewayEmulatorPort = apiGatewayPort};
277+
278+
var command = new RunCommand(_mockInteractiveService.Object);
279+
var context = new CommandContext(new List<string>(), _mockRemainingArgs.Object, "run", null);
280+
281+
_ = command.ExecuteAsync(context, settings, cancellationTokenSource);
282+
}
283+
237284
private void StartTestToolProcess(ApiGatewayEmulatorMode apiGatewayMode, TestConfig config, int lambdaPort, int apiGatewayPort, CancellationTokenSource cancellationTokenSource)
238285
{
239-
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
240-
Environment.SetEnvironmentVariable("APIGATEWAY_EMULATOR_ROUTE_CONFIG", $@"{{
286+
SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
287+
SetEnvironmentVariable("APIGATEWAY_EMULATOR_ROUTE_CONFIG", $@"{{
241288
""LambdaResourceName"": ""{config.RouteName}"",
242289
""Endpoint"": ""http://localhost:{lambdaPort}"",
243290
""HttpMethod"": ""{config.HttpMethod}"",
@@ -362,6 +409,24 @@ private void LogMessage(string message)
362409
_testOutputHelper.WriteLine(message);
363410
}
364411

412+
private void SetEnvironmentVariable(string name, string value)
413+
{
414+
Environment.SetEnvironmentVariable(name, value);
415+
if (!_setEnvironmentVariables.Contains(name))
416+
{
417+
_setEnvironmentVariables.Add(name);
418+
}
419+
}
420+
421+
private void CleanupEnvironmentVariables()
422+
{
423+
foreach (var variable in _setEnvironmentVariables)
424+
{
425+
Environment.SetEnvironmentVariable(variable, null);
426+
}
427+
_setEnvironmentVariables.Clear();
428+
}
429+
365430
private async Task CleanupProcesses()
366431
{
367432
var processes = new[] { _lambdaProcess };
@@ -386,5 +451,6 @@ private async Task CleanupProcesses()
386451
public async ValueTask DisposeAsync()
387452
{
388453
await CleanupProcesses();
454+
CleanupEnvironmentVariables();
389455
}
390456
}

0 commit comments

Comments
 (0)