Skip to content

Commit 362aa2e

Browse files
committed
Add additional test for asp.net workloads
1 parent 23daca6 commit 362aa2e

5 files changed

Lines changed: 84 additions & 5 deletions

File tree

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/Amazon.Lambda.AspNetCoreServer.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ItemGroup>
4242
<ProjectReference Include="..\..\src\Amazon.Lambda.APIGatewayEvents\Amazon.Lambda.APIGatewayEvents.csproj" />
4343
<ProjectReference Include="..\..\src\Amazon.Lambda.ApplicationLoadBalancerEvents\Amazon.Lambda.ApplicationLoadBalancerEvents.csproj" />
44+
<ProjectReference Include="..\..\src\Amazon.Lambda.RuntimeSupport\Amazon.Lambda.RuntimeSupport.csproj" />
4445
<ProjectReference Include="..\TestWebApp\TestWebApp.csproj" />
4546
<ProjectReference Include="..\..\src\Amazon.Lambda.TestUtilities\Amazon.Lambda.TestUtilities.csproj" />
4647
</ItemGroup>

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestApiGatewayHttpApiV2Calls.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.IO.Compression;
@@ -9,13 +9,15 @@
99
using System.Threading.Tasks;
1010

1111
using Amazon.Lambda.APIGatewayEvents;
12+
using Amazon.Lambda.RuntimeSupport;
13+
using Amazon.Lambda.Serialization.SystemTextJson;
1214
using Amazon.Lambda.TestUtilities;
1315

1416
using Newtonsoft.Json;
1517
using Newtonsoft.Json.Linq;
1618

1719
using TestWebApp;
18-
20+
using TestWebApp.Controllers;
1921
using Xunit;
2022

2123

@@ -282,6 +284,30 @@ public async Task TestTraceIdSetFromLambdaContext()
282284
}
283285
}
284286

287+
#if NET8_0_OR_GREATER
288+
/// <summary>
289+
/// Verifies that <see cref="HttpV2LambdaFunction.GetBeforeSnapshotRequests"/> is invoked during startup.
290+
/// </summary>
291+
/// <returns></returns>
292+
[Fact]
293+
public async Task TestSnapStartInitialization()
294+
{
295+
using var e1 = new EnvironmentVariableHelper("AWS_LAMBDA_FUNCTION_NAME", nameof(TestSnapStartInitialization));
296+
using var e2 = new EnvironmentVariableHelper("AWS_LAMBDA_INITIALIZATION_TYPE", "snap-start");
297+
using var e3 = new EnvironmentVariableHelper("AWS_LAMBDA_RUNTIME_API", "localhost:123");
298+
299+
var t = LambdaBootstrapBuilder.Create<APIGatewayHttpApiV2ProxyRequest>(
300+
new TestWebApp.HttpV2LambdaFunction().FunctionHandlerAsync,
301+
new DefaultLambdaJsonSerializer())
302+
.Build()
303+
.RunAsync();
304+
305+
await Task.Delay(1000);
306+
307+
Assert.True(SnapStartController.Invoked);
308+
}
309+
#endif
310+
285311
private async Task<APIGatewayHttpApiV2ProxyResponse> InvokeAPIGatewayRequest(string fileName, bool configureApiToReturnExceptionDetail = false)
286312
{
287313
return await InvokeAPIGatewayRequestWithContent(new TestLambdaContext(), GetRequestContent(fileName), configureApiToReturnExceptionDetail);
@@ -310,4 +336,19 @@ private string GetRequestContent(string fileName)
310336
return requestStr;
311337
}
312338
}
339+
340+
public class EnvironmentVariableHelper : IDisposable
341+
{
342+
private string _name;
343+
private string? _oldValue;
344+
public EnvironmentVariableHelper(string name, string value)
345+
{
346+
_name = name;
347+
_oldValue = Environment.GetEnvironmentVariable(name);
348+
349+
Environment.SetEnvironmentVariable(name, value);
350+
}
351+
352+
public void Dispose() => Environment.SetEnvironmentVariable(_name, _oldValue);
353+
}
313354
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace TestWebApp.Controllers;
7+
8+
[Route("api/[controller]")]
9+
public class SnapStartController
10+
{
11+
/// <summary>
12+
/// Set when <see cref="Get"/> is invoked
13+
/// </summary>
14+
public static bool Invoked { get; set; }
15+
16+
17+
[HttpGet]
18+
public string Get()
19+
{
20+
Invoked = true;
21+
22+
return "Invoked set to true";
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
using System.Collections.Generic;
2+
using System.Net.Http;
3+
using System;
14
using Amazon.Lambda.AspNetCoreServer;
25
namespace TestWebApp
36
{
47
public class HttpV2LambdaFunction : APIGatewayHttpApiV2ProxyFunction<Startup>
58
{
9+
#if NET8_0_OR_GREATER
10+
protected override IEnumerable<HttpRequestMessage> GetBeforeSnapshotRequests() =>
11+
[
12+
new HttpRequestMessage(HttpMethod.Get, "api/SnapStart")
13+
];
14+
#endif
615
}
716
}

Libraries/test/TestWebApp/Middleware.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Amazon.Lambda.Core;
1+
using Amazon.Lambda.Core;
22
using Microsoft.AspNetCore.Http;
33
using System;
44
using System.Collections.Generic;
@@ -23,8 +23,12 @@ public async Task Invoke(HttpContext context)
2323

2424
context.Response.OnStarting(x =>
2525
{
26-
var lambdaContext = context.Items["LambdaContext"] as ILambdaContext;
27-
lambdaContext?.Logger.LogLine("OnStarting Called");
26+
if (context.Items.ContainsKey("LambdaContext"))
27+
{
28+
var lambdaContext = context.Items["LambdaContext"] as ILambdaContext;
29+
lambdaContext?.Logger.LogLine("OnStarting Called");
30+
}
31+
2832
return Task.FromResult(0);
2933
}, context);
3034

0 commit comments

Comments
 (0)