aws-lambda-dotnet icon indicating copy to clipboard operation
aws-lambda-dotnet copied to clipboard

Lambda Test Tool: LambdaAnnotations | Proper use of HttpResults and [FromBody] attribute.

Open ashishdhingra opened this issue 1 year ago • 1 comments

Discussed in https://github.com/aws/aws-lambda-dotnet/discussions/1569

Originally posted by sergio-asenjo August 20, 2023 Hello!

I've been trying to figure out how to properly use HttpResults returning statement in the "Mock Lambda Test Tool".

As a short example, I've tried this:

[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Get, "/")]
public IHttpResult Test(ILambdaContext context)
{
    return HttpResults.Ok("Ok");
}

Doing so, and returning any type of HttpResults, from the UI of Mock Lambda Test Tool I'm getting a base64 return in response. As for this case, I'm getting:

"eyJzdGF0dXNDb2RlIjoyMDAsImhlYWRlcnMiOnsiY29udGVudC10eXBlIjoidGV4dC9wbGFpbiJ9LCJDb29raWVzIjpudWxsLCJib2R5IjoiT2siLCJpc0Jhc2U2NEVuY29kZWQiOmZhbHNlfQ=="

Which translate to:

{
    "statusCode": 200,
    "headers": {
        "content-type": "text/plain"
    },
    "Cookies": null,
    "body": "Ok",
    "isBase64Encoded": false
}

That's correct overall, but for testing purposes, having to decode the base64 to see what I got it's not what I would want to do recurrently. Is this intended or am I missing something? If I deploy it, through browsers I can get instantly a decoded response.

───

A second thing is that I'm trying to use a [FromBody] attribute to get some data passed in the body, like this:

[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Post, "/")]
public IHttpResult Test([FromBody] Request request, ILambdaContext context)
{
    return HttpResults.Ok("Ok");
}

public class Request
{
    public string Name { get; set; }
}

If through the UI Mock Lambda Test Tool I try to send { "name": "test" }, I get a ValidationException, so it doesn't even enter the Test Function completely. This is the response (which is also base64 encoded in the UI).

{
    "statusCode": 400,
    "headers": {
        "Content-Type": "application/json",
        "x-amzn-ErrorType": "ValidationException"
    },
    "cookies": null,
    "body": "{\u0022message\u0022: \u00221 validation error(s) detected: Value  at \u0027body\u0027 failed to satisfy constraint: Value cannot be null. (Parameter \u0027json\u0027)\u0022}",
    "isBase64Encoded": false
}

Am I using this correctly for this case? Before trying annotations, I was using the template that comes with Powertools, there I'd just pass the type (in this case would be Request) and I wouldn't get this error.

For both cases, I'm using the base template Serverless Annotation, and deleted what's inside the main Class, everything else is untouched.

ashishdhingra avatar Sep 01 '23 16:09 ashishdhingra

As mentioned by @normj in https://github.com/aws/aws-lambda-dotnet/discussions/1569#discussioncomment-6881199, this is a test tool bug.

ashishdhingra avatar Sep 01 '23 16:09 ashishdhingra