dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.16k stars 794 forks source link

CONVERT_TO_TEXT parameter does not convert binary data of the request to base64 #1774

Open amperioromano opened 3 months ago

amperioromano commented 3 months ago

Bug Report

This is the very exact bug as reported in https://github.com/dherault/serverless-offline/issues/1107, which was closed as resolved, but it doesn't look like it is.

A very simple configutation setting in the request that it converts the payload to base64 (contentHandling: CONVERT_TO_TEXT) is not converting the body to base64, eo event.payload is not the expected base64-encoded data.

Current Behavior

Sample Code

service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs18.x
  stage: dev

functions:
  hello:
    events:
      - http:
          method: post
          path: hello
          request:
            contentHandling: CONVERT_TO_TEXT
    handler: handler.hello
"use strict"

const { stringify } = JSON

exports.hello = async function hello(event) {

  // event.payload should be encoded as base64
  return {
    body: stringify({ foo: "bar" }),
    statusCode: 200,
  }
}

Expected behavior/code

event.payload should be encoded as base64 when contentHandling is set to CONVERT_TO_TEXT

Environment

Possible Solution

I think the error is in the HttpServer https://github.com/dherault/serverless-offline/blob/c85a19272c59ad9e7cf1aea74e3442c7607b533a/src/events/alb/HttpServer.js#L176 where it always encodes the payload like this

       // Payload processing
      const encoding = detectEncoding(request)

      request.payload = request.payload && request.payload.toString(encoding)

detectEncoding only returns either utf8 or binary, but it should be base64 for request contentHandling set to CONVERT_TO_TEXT