Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ashishdhingra opened this issue Sep 1, 2023 Discussed in #1569 · 1 comment
Labels
bug This issue is a bug. module/lambda-test-tool p2 This is a standard priority issue queued

Comments

@ashishdhingra
Copy link
Contributor

Discussed in #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
Copy link
Contributor Author

As mentioned by @normj in #1569 (comment), this is a test tool bug.

@ashishdhingra ashishdhingra added bug This issue is a bug. module/lambda-test-tool needs-review p2 This is a standard priority issue queued and removed annotations needs-review labels Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/lambda-test-tool p2 This is a standard priority issue queued
Projects
None yet
Development

No branches or pull requests

1 participant