使用无服务器框架模拟响应数据

Mock response data with serverless framework

文档给出以下内容作为模拟示例:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: hello
          cors: true
          method: get
          integration: mock
          request:
            template:
              application/json: '{"statusCode": 200}'
          response:
            template: $input.path('$')
            statusCodes:
              201:
                pattern: ''

这确实创建了一个模拟响应...除了它是空的。

我怎么才能真正return这里的数据?我试过将 application/json: {...} 添加到 template,但这不起作用,我尝试在 statusCodes 下添加 body 但也没有成功。

似乎没有关于此的任何文档...我怎样才能 return 一个实际的 body?

您可以通过设置 response.template 的值来实现。但是,这并不是像 request 那样使用 application/json 键完成的,您只需直接设置 template

Return 一个字符串 foo

response:
  template: "foo"
    statusCodes:
      201:
      pattern: ''

Return JSON

response:
  template: ${file(foo.txt)}
    statusCodes:
      201:
      pattern: ''


# Where foo.txt contains regular JSON

{
  "foo":"bar"
}

这就是我对 return 模拟响应数据所做的...

functions:

  helloworld:
    handler: api/handler.mock
    events:
      - http:
          path: ''
          method: get
          integration: mock
          request:
            template:
              application/json: '{"statusCode": 200}'          
          response:
            template: '{"code": 200,"message": "Helloworld!"}'
            statusCodes:
              200:
                body: '{"code": 200,"message": "Helloworld!"}'