Skip to content

Commit d51554c

Browse files
Fixed an issue where delimited string and primitive values in payload were not parsed properly by Lambda Test Tool.
1 parent f275753 commit d51554c

File tree

10 files changed

+187
-2
lines changed

10 files changed

+187
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.TestTool.BlazorTester",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Fixed an issue where delimited string and primitive values in payload were not parsed properly by Lambda Test Tool."
8+
]
9+
}
10+
]
11+
}

Tools/LambdaTestTool/aws-lambda-test-tool-netcore.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessTemplateYamlExamp
3838
EndProject
3939
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToUpperFunc", "tests\LambdaFunctions\ToUpperFunc\ToUpperFunc.csproj", "{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}"
4040
EndProject
41+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegerFunc", "tests\LambdaFunctions\IntegerFunc\IntegerFunc.csproj", "{04026578-14B5-4EE0-8732-C4448F83356D}"
42+
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4345
Debug|Any CPU = Debug|Any CPU
@@ -92,6 +94,10 @@ Global
9294
{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Debug|Any CPU.Build.0 = Debug|Any CPU
9395
{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Release|Any CPU.ActiveCfg = Release|Any CPU
9496
{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C}.Release|Any CPU.Build.0 = Release|Any CPU
97+
{04026578-14B5-4EE0-8732-C4448F83356D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
98+
{04026578-14B5-4EE0-8732-C4448F83356D}.Debug|Any CPU.Build.0 = Debug|Any CPU
99+
{04026578-14B5-4EE0-8732-C4448F83356D}.Release|Any CPU.ActiveCfg = Release|Any CPU
100+
{04026578-14B5-4EE0-8732-C4448F83356D}.Release|Any CPU.Build.0 = Release|Any CPU
95101
EndGlobalSection
96102
GlobalSection(SolutionProperties) = preSolution
97103
HideSolutionNode = FALSE
@@ -110,6 +116,7 @@ Global
110116
{1055D389-9927-4EED-9876-4B497CB3B3C2} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1}
111117
{13B8F61E-5E34-46CD-B76F-A92D9DCF6946} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1}
112118
{7BBBAB9A-1630-4B15-AEB8-FA90BBDC165C} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1}
119+
{04026578-14B5-4EE0-8732-C4448F83356D} = {BFD718DB-4526-4BED-B2B0-BB446EDFFEA1}
113120
EndGlobalSection
114121
GlobalSection(ExtensibilityGlobals) = postSolution
115122
SolutionGuid = {E6C77567-6F16-4EE3-8743-ADE6B68434FD}

Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.IO;
66
using System.Linq;
7+
using System.Text.Json;
78

89
namespace Amazon.Lambda.TestTool
910
{
@@ -318,6 +319,30 @@ private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, Co
318319
}
319320
}
320321

322+
try
323+
{
324+
var doc = JsonDocument.Parse(payload);
325+
}
326+
catch (JsonException)
327+
{
328+
try
329+
{
330+
if (!payload.StartsWith("[") && !payload.StartsWith("{") && !payload.StartsWith("\""))
331+
{
332+
payload = "\"" + payload + "\"";
333+
JsonDocument.Parse(payload);
334+
}
335+
else
336+
{
337+
throw;
338+
}
339+
}
340+
catch (JsonException)
341+
{
342+
throw new ArgumentException("Provided payload for Lambda function is not a valid JSON document.");
343+
}
344+
}
345+
321346
return payload;
322347
}
323348

Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/Amazon.Lambda.TestTool.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ProjectReference Include="..\..\src\Amazon.Lambda.TestTool\Amazon.Lambda.TestTool.csproj" />
2424
<ProjectReference Include="..\LambdaFunctions\AspNetCoreAPIExample\AspNetCoreAPIExample.csproj" />
2525
<ProjectReference Include="..\LambdaFunctions\FunctionSignatureExamples\FunctionSignatureExamples.csproj" />
26+
<ProjectReference Include="..\LambdaFunctions\IntegerFunc\IntegerFunc.csproj" />
2627
<ProjectReference Include="..\LambdaFunctions\S3EventFunction\S3EventFunction.csproj" />
2728
<ProjectReference Include="..\LambdaFunctions\ServerlessFunctionTemplateYamlExample\ServerlessFunctionTemplateYamlExample.csproj" />
2829
<ProjectReference Include="..\LambdaFunctions\ServerlessTemplateExample\ServerlessTemplateExample.csproj" />

Tools/LambdaTestTool/tests/Amazon.Lambda.TestTool.Tests/NoUiStartupTests.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ namespace Amazon.Lambda.TestTool.Tests
1010
{
1111
public class NoUiStartupTests
1212
{
13-
14-
1513
[Fact]
1614
public void DirectFunctionCallFromConfig()
1715
{
@@ -106,6 +104,36 @@ public void DirectFunctionCallFromConfigWithDisableLogs()
106104
Assert.Equal("\"HELLO WORLD\"", runConfiguration.OutputWriter.ToString());
107105
}
108106

107+
[Fact]
108+
public void DirectFunctionCallNonDelimitedPayloadStringFromConfig()
109+
{
110+
var runConfiguration = CreateRunConfiguration();
111+
var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc");
112+
113+
TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "hello WORLD" }, runConfiguration);
114+
Assert.Contains("HELLO WORLD", runConfiguration.OutputWriter.ToString());
115+
}
116+
117+
[Fact]
118+
public void DirectFunctionCallSingleQuoteDelimitedPayloadStringFromConfig()
119+
{
120+
var runConfiguration = CreateRunConfiguration();
121+
var buildPath = TestUtils.GetLambdaFunctionBuildPath("ToUpperFunc");
122+
123+
TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "'hello WORLD'" }, runConfiguration);
124+
Assert.Contains("'HELLO WORLD'", runConfiguration.OutputWriter.ToString());
125+
}
126+
127+
[Fact]
128+
public void DirectFunctionCallIntegerPayloadFromConfig()
129+
{
130+
var runConfiguration = CreateRunConfiguration();
131+
var buildPath = TestUtils.GetLambdaFunctionBuildPath("IntegerFunc");
132+
133+
TestToolStartup.Startup("Unit Tests", null, new string[] { "--path", buildPath, "--no-ui", "--payload", "42" }, runConfiguration);
134+
Assert.Contains("Hello 42", runConfiguration.OutputWriter.ToString());
135+
}
136+
109137
private TestToolStartup.RunConfiguration CreateRunConfiguration()
110138
{
111139
return new TestToolStartup.RunConfiguration
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
using Amazon.Lambda.Core;
8+
9+
10+
namespace IntegerFunc
11+
{
12+
public class Function
13+
{
14+
15+
/// <summary>
16+
/// A simple function that takes an integer and returns a string.
17+
/// </summary>
18+
/// <param name="input"></param>
19+
/// <param name="context"></param>
20+
/// <returns></returns>
21+
[LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
22+
public static string FunctionHandler(int input, ILambdaContext context)
23+
{
24+
context.Logger.LogLine($"Executing function with input: {input}");
25+
Console.WriteLine("Testing Console Logging");
26+
27+
return $"Hello {input}";
28+
}
29+
}
30+
}
31+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
5+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
10+
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.1.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<DotNetCliToolReference Include="Amazon.Lambda.Tools" Version="2.1.3" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# AWS Lambda Empty Function Project
2+
3+
This starter project consists of:
4+
* Function.cs - class file containing a class with a single function handler method
5+
* aws-lambda-tools-defaults.json - default argument settings for use with Visual Studio and command line deployment tools for AWS
6+
7+
You may also have a test project depending on the options selected.
8+
9+
The generated function handler is a simple method accepting an integer argument that returns a string. Replace the body of this method, and parameters, to suit your needs.
10+
11+
## Here are some steps to follow from Visual Studio:
12+
13+
To deploy your function to AWS Lambda, right click the project in Solution Explorer and select *Publish to AWS Lambda*.
14+
15+
To view your deployed function open its Function View window by double-clicking the function name shown beneath the AWS Lambda node in the AWS Explorer tree.
16+
17+
To perform testing against your deployed function use the Test Invoke tab in the opened Function View window.
18+
19+
To configure event sources for your deployed function, for example to have your function invoked when an object is created in an Amazon S3 bucket, use the Event Sources tab in the opened Function View window.
20+
21+
To update the runtime configuration of your deployed function use the Configuration tab in the opened Function View window.
22+
23+
To view execution logs of invocations of your function use the Logs tab in the opened Function View window.
24+
25+
## Here are some steps to follow to get started from the command line:
26+
27+
Once you have edited your function you can use the following command lines to build, test and deploy your function to AWS Lambda from the command line (these examples assume the project name is *EmptyFunction*):
28+
29+
Restore dependencies
30+
```
31+
cd "BlueprintBaseName"
32+
dotnet restore
33+
```
34+
35+
Execute unit tests
36+
```
37+
cd "BlueprintBaseName/test/BlueprintBaseName.Tests"
38+
dotnet test
39+
```
40+
41+
Deploy function to AWS Lambda
42+
```
43+
cd "BlueprintBaseName/src/BlueprintBaseName"
44+
dotnet lambda deploy-function
45+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"Information": [
3+
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
4+
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
5+
6+
"dotnet lambda help",
7+
8+
"All the command line options for the Lambda command can be specified in this file."
9+
],
10+
11+
"profile": "default",
12+
"region": "us-west-2",
13+
"configuration": "Release",
14+
"framework": "netcoreapp3.1",
15+
"function-runtime": "dotnetcore3.1",
16+
"function-memory-size": 256,
17+
"function-timeout": 30,
18+
"function-handler": "IntegerFunc::IntegerFunc.Function::FunctionHandler"
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)