Mock response data with serverless framework

烂漫一生 提交于 2020-04-16 05:54:06

问题


The docs give the following as a mock example:

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: ''

This does create a mock response...except it's empty.

How can I actually return data here? I've tried adding application/json: {...} to template, but that doesn't work, I've tried adding a body under statusCodes but no luck there either.

There doesn't seem to be any documentation on this...how can I return an actual body?


回答1:


You can do this by setting the value of response.template. However, this isn't done using an application/json key like request is, you just set template directly.

Return a string 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"
}



回答2:


This is what I do to return mock response data...

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!"}'


来源:https://stackoverflow.com/questions/59778964/mock-response-data-with-serverless-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!