2017-11-13 7 views
0

나는 aws lambda를 조사 해왔다. api gateway 요청에 대한 응답을 테스트하는 사람들은 어떻게 반응합니까? 자바에서는 람다가 약간 있습니다.aws lambda js 단위 테스트

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에서 정말 간단하게하고 싶습니다. 유사한 이벤트 객체를 사용할 수 있습니까? 농담으로 그들을 어떻게 묶을 수 있습니까?

답변

1

CloudFront 용 Lambda의 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' 
     } 
     } 
    } 
    ] 
}); 
0

나는 내가 원하는 일을 농담을 가지고있다. 테스트 응답에 대한 마틴의 생각은 도움이 될 것입니다. 감사합니다.

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) 

})