0

I've been looking into aws lambda. How are people testing the harness for api gateway requests responses? In Java I have a lambda that's a bit like this.

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());
}

I'd love to do something really simple with Jest and ES6 to replicate the above. Are there similar known events objects to use? How can I wire them up with jest.

2 Answers 2

1

I made function for adding security headers based on Host header in Lambda for CloudFront. For testing I used JEST and basically mocked objects in AWS like this.

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'
        }
      }
    }
  ]
});
Sign up to request clarification or add additional context in comments.

Comments

0

I've got jest doing what I wanted. Martin's idea of the test responses will be helpful as it gets more involved, thanks.

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)

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.