@@ -21,6 +21,7 @@ public class ApiGatewayEmulatorProcessTests : IAsyncDisposable
21
21
private readonly Mock < IRemainingArguments > _mockRemainingArgs = new Mock < IRemainingArguments > ( ) ;
22
22
private readonly ITestOutputHelper _testOutputHelper ;
23
23
private Process ? _lambdaProcess ;
24
+ private readonly List < string > _setEnvironmentVariables = new List < string > ( ) ;
24
25
25
26
public ApiGatewayEmulatorProcessTests ( ITestOutputHelper testOutputHelper )
26
27
{
@@ -59,7 +60,6 @@ public async Task TestLambdaToUpperV2()
59
60
finally
60
61
{
61
62
await cancellationTokenSource . CancelAsync ( ) ;
62
- await CleanupProcesses ( ) ;
63
63
}
64
64
}
65
65
@@ -95,7 +95,6 @@ public async Task TestLambdaToUpperRest()
95
95
finally
96
96
{
97
97
await cancellationTokenSource . CancelAsync ( ) ;
98
- await CleanupProcesses ( ) ;
99
98
}
100
99
}
101
100
@@ -131,7 +130,6 @@ public async Task TestLambdaToUpperV1()
131
130
finally
132
131
{
133
132
await cancellationTokenSource . CancelAsync ( ) ;
134
- await CleanupProcesses ( ) ;
135
133
}
136
134
}
137
135
@@ -173,7 +171,6 @@ public async Task TestLambdaBinaryResponse()
173
171
finally
174
172
{
175
173
await cancellationTokenSource . CancelAsync ( ) ;
176
- await CleanupProcesses ( ) ;
177
174
}
178
175
}
179
176
@@ -209,7 +206,39 @@ public async Task TestLambdaReturnString()
209
206
finally
210
207
{
211
208
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 ( ) ;
213
242
}
214
243
}
215
244
@@ -234,10 +263,28 @@ private async Task<HttpResponseMessage> TestEndpoint(TestConfig config, int apiG
234
263
} ;
235
264
}
236
265
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
+
237
284
private void StartTestToolProcess ( ApiGatewayEmulatorMode apiGatewayMode , TestConfig config , int lambdaPort , int apiGatewayPort , CancellationTokenSource cancellationTokenSource )
238
285
{
239
- Environment . SetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" , "Development" ) ;
240
- Environment . SetEnvironmentVariable ( "APIGATEWAY_EMULATOR_ROUTE_CONFIG" , $@ "{{
286
+ SetEnvironmentVariable ( "ASPNETCORE_ENVIRONMENT" , "Development" ) ;
287
+ SetEnvironmentVariable ( "APIGATEWAY_EMULATOR_ROUTE_CONFIG" , $@ "{{
241
288
""LambdaResourceName"": ""{ config . RouteName } "",
242
289
""Endpoint"": ""http://localhost:{ lambdaPort } "",
243
290
""HttpMethod"": ""{ config . HttpMethod } "",
@@ -362,6 +409,24 @@ private void LogMessage(string message)
362
409
_testOutputHelper . WriteLine ( message ) ;
363
410
}
364
411
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
+
365
430
private async Task CleanupProcesses ( )
366
431
{
367
432
var processes = new [ ] { _lambdaProcess } ;
@@ -386,5 +451,6 @@ private async Task CleanupProcesses()
386
451
public async ValueTask DisposeAsync ( )
387
452
{
388
453
await CleanupProcesses ( ) ;
454
+ CleanupEnvironmentVariables ( ) ;
389
455
}
390
456
}
0 commit comments