【问题标题】:Mock response data with serverless framework使用无服务器框架模拟响应数据
【发布时间】:2020-02-13 17:01:00
【问题描述】:

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

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

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

我如何才能在此处实际返回数据?我尝试将application/json: {...} 添加到template,但这不起作用,我尝试在statusCodes 下添加一个正文,但那里也没有运气。

似乎没有这方面的任何文档...我怎样才能返回一个实际的身体?

【问题讨论】:

    标签: serverless-framework


    【解决方案1】:

    您可以通过设置response.template 的值来做到这一点。但是,这不是使用 application/json 之类的 request 键来完成的,您只需直接设置 template

    返回一个字符串foo

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

    返回 JSON

    response:
      template: ${file(foo.txt)}
        statusCodes:
          201:
          pattern: ''
    
    
    # Where foo.txt contains regular JSON
    
    {
      "foo":"bar"
    }
    

    【讨论】:

      【解决方案2】:

      这就是我返回模拟响应数据的方法...

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

      【讨论】:

        猜你喜欢
        • 2017-10-05
        • 2017-03-08
        • 2021-11-25
        • 1970-01-01
        • 2022-10-13
        • 2019-03-25
        • 2017-10-08
        • 1970-01-01
        相关资源
        最近更新 更多