2017-11-13 4 views
0

Ich habe in aws Lambda untersucht. Wie testen Menschen den Kabelbaum für API-Gateway-Anfragen? In Java habe ich ein Lambda, das ungefähr so ​​ist.aws lambda js einheit testen

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

Ich würde gerne etwas wirklich einfach mit Jest und ES6 tun, um die oben zu replizieren. Sind ähnliche bekannte Ereignisobjekte zu verwenden? Wie kann ich sie mit Spaß verbinden?

Antwort

1

Ich habe Funktion zum Hinzufügen von Sicherheits-Header basierend auf Host-Header in Lambda für CloudFront gemacht. Zum Testen habe ich JEST verwendet und Objekte in AWS so verspottet.

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

Ich habe Spaß zu tun, was ich wollte. Martins Idee der Testantworten wird hilfreich sein, da es immer mehr involviert wird, danke.

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) 

})