无服务器框架Lamba集成 - 请求模式验证

问题描述 投票:0回答:0

我看了一下在SO上能找到的几个答案,但我还是没有任何运气。我正在使用Serverless,并试图使用api gateway lambda集成对一个lambda函数的请求体进行验证。在运行 sls offline 并使用postman进行POST请求,无论body是什么,请求都会成功。验证似乎根本没有发生。

我的情况是这样的...

serverless.yml。

service: onboard

# plugins
plugins:
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1

functions:
  onboard:
    handler: api/onboard.onboard
    events:
      - http:
          path: onboard
          method: post
          integration: lambda
          request:
            passThrough: NEVER
            schema:
              application/json: ${file(models/onboard.schema.json)}
            template:
              application/json: '{ "body" : "$input.body" }'

apionboard.js

const onboard = async (event) => {
  const response = {
    message: event
  };

  return response;
};

exports.onboard = onboard;

modelsonboard.schema.json。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "https://path.to/onboard",
  "title": "title",
  "description": "description",
  "type": "object",
  "properties": {
    "environment": { "type": "string" },
    "git": { 
      "type": "object",
      "properties": {
        "repo": {
          "type": "string",
          "format": "uri",
          "pattern": ".git$"
        },
        "token": { "type": "string" }
      },
      "required": ["repo", "token"],
      "maxProperties": 2
    },
    "name": {  "type": "string" },
    "team": {
      "type": "string",
      "pattern": "(?i)(^red$|^blue$|^green$|^yellow$|^black$|^white$)"
    }
  },
  "additionalProperties": false,
  "required": ["name", "team"]
}
aws-lambda aws-api-gateway jsonschema serverless-framework
0
投票

这里有几个需要注意的地方。

  1. 你的测试请求必须通过与你配置的内容类型相匹配的内容,否则验证将被忽略。就我所见,强制使用Content-Type的唯一方法是在你的Lambda函数中添加最小验证。
  2. serverless-offline可能不会考虑到请求验证。最好在云端进行测试。就我个人而言,我避免在本地进行这种类型的测试,而是尽量在云端进行测试。随着Serverless框架发布的名为Studio的新功能,它提供了快速的修改时间,在几秒钟内推送到云端,并为你提供了一个类似邮递员的界面来发送测试到你的云端基础设施,使测试变得更加容易。

0
投票

Serverless-offline不支持请求验证。https:/github.comdheraultserverless-offlineissues369。

最好通过定义模拟响应在apigateway上单独测试。这将确保lambda响应(如果基于集成类型的不正确)不会造成任何误导。

© www.soinside.com 2019 - 2024. All rights reserved.