【问题标题】:aws lambda js unit testingaws lambda js 单元测试
【发布时间】:2018-04-26 09:29:21
【问题描述】:

我一直在研究 aws lambda。人们如何测试 api 网关请求响应的工具?在 Java 中,我有一个有点像这样的 lambda。

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
...
@Test
void turnsBarToFooTest() {

    TestContext ctx = new TestContext(); //implements  com.amazonaws.services.lambda.runtime.Context

    Fooer handler = new Fooer();

    APIGatewayProxyRequestEvent request = new APIGatewayProxyRequestEvent();
    Map<String, String> params = HashMap.of("thing_to_foo", "bar");
    request.setPathParameters(params.toJavaMap());

    APIGatewayProxyResponseEvent response = handler.handleRequest(request, ctx);
    assertEquals(200, response.getStatusCode().intValue());
    assertEquals("foo", response.getBody());
}

我很想用 Jest 和 ES6 做一些非常简单的事情来复制上述内容。是否有类似的已知事件对象可供使用?我怎样才能用笑话把它们连接起来。

【问题讨论】:

    标签: ecmascript-6 aws-lambda aws-api-gateway jestjs


    【解决方案1】:

    我在 Lambda for CloudFront 中创建了基于 Host 标头添加安全标头的功能。为了测试,我使用了 JEST,并且基本上像这样在 AWS 中模拟了对象。

    google.test.js:

    const handler = require('../../src/functions/google').handler;
    const testEventGenerator = require('./cloudfront-event-template-generator');
    
    test('it adds xss protection', () => {
      const event = testEventGenerator();
      const callback = jest.fn();
      handler(event, {}, callback);
      expect(event.Records[0].cf.response.headers['x-xss-protection'][0].key).toBe('X-XSS-Protection');
      expect(event.Records[0].cf.response.headers['x-xss-protection'][0].value).toBe('1; mode=block');
      expect(callback.mock.calls.length).toBe(1);
    });
    

    cloudfront-event-template-generator.js:

    module.exports = () => ({
      Records: [
        {
          cf: {
            config: {
              distributionId: 'EXAMPLE'
            },
            request: {
              headers: {
                host: [
                  {
                    key: 'host',
                    value: 'www.google.com'
                  }
                ]
              }
            },
            response: {
              status: 200,
              headers: {
                'last-modified': [
                  {
                    key: 'Last-Modified',
                    value: '2016-11-25'
                  }
                ],
                vary: [
                  {
                    key: 'Vary',
                    value: '*'
                  }
                ],
                'x-amz-meta-last-modified': [
                  {
                    key: 'X-Amz-Meta-Last-Modified',
                    value: '2016-01-01'
                  }
                ]
              },
              statusDescription: 'OK'
            }
          }
        }
      ]
    });
    

    【讨论】:

      【解决方案2】:

      我已经在做我想做的事了。 Martin 对测试响应的想法会随着参与度的增加而有所帮助,谢谢。

      test('fooer will foo a bar', done => {
      
        const context = {}
        const request = {pathParameters:{thing_to_foo:'foo'}}
        const callback = (bar, foo) => {
          expect(foo.body).toBe('bar')
          done()
        }
        myHandler.handler(request, context, callback)
      
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-30
        • 2020-09-15
        • 2019-01-22
        • 1970-01-01
        • 2023-04-04
        • 2017-07-24
        • 2018-05-30
        相关资源
        最近更新 更多