in Tools/LambdaTestTool/src/Amazon.Lambda.TestTool/TestToolStartup.cs [267:347]
private static string DeterminePayload(LocalLambdaOptions localLambdaOptions, CommandLineOptions commandOptions, string lambdaAssemblyDirectory, string lambdaProjectDirectory, RunConfiguration runConfiguration)
{
var payload = commandOptions.Payload;
bool payloadFileFound = false;
if (!string.IsNullOrEmpty(payload))
{
if (Path.IsPathFullyQualified(payload) && File.Exists(payload))
{
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {payload}");
payload = File.ReadAllText(payload);
payloadFileFound = true;
}
else
{
// Look to see if the payload value is a file in
// * Directory with user Lambda assemblies.
// * Lambda project directory
// * Properties directory under the project directory. This is to make it easy to reconcile from the launchSettings.json file.
// * Is a saved sample request from the web interface
var possiblePaths = new[]
{
Path.Combine(lambdaAssemblyDirectory, payload),
Path.Combine(lambdaProjectDirectory, payload),
Path.Combine(lambdaProjectDirectory, "Properties", payload),
Path.Combine(localLambdaOptions.GetPreferenceDirectory(false), new SampleRequestManager(localLambdaOptions.GetPreferenceDirectory(false)).GetSaveRequestRelativePath(payload))
};
foreach (var possiblePath in possiblePaths)
{
if (File.Exists(possiblePath))
{
if (!shouldDisableLogs) runConfiguration.OutputWriter.WriteLine($"... Using payload with from the file {Path.GetFullPath(possiblePath)}");
payload = File.ReadAllText(possiblePath);
payloadFileFound = true;
break;
}
}
}
}
if (!payloadFileFound)
{
if (!shouldDisableLogs)
{
if (!string.IsNullOrEmpty(payload))
{
runConfiguration.OutputWriter.WriteLine($"... Using payload with the value {payload}");
}
else
{
runConfiguration.OutputWriter.WriteLine("... No payload configured. If a payload is required set the --payload switch to a file path or a JSON document.");
}
}
}
try
{
var doc = JsonDocument.Parse(payload);
}
catch (JsonException)
{
try
{
if (!payload.StartsWith("[") && !payload.StartsWith("{") && !payload.StartsWith("\""))
{
payload = "\"" + payload + "\"";
JsonDocument.Parse(payload);
}
else
{
throw;
}
}
catch (JsonException)
{
throw new ArgumentException("Provided payload for Lambda function is not a valid JSON document.");
}
}
return payload;
}