Skip to content

Commit

Permalink
fix(fizz-buzz-api): api gateway proxy event result version
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Emonds committed Apr 8, 2021
1 parent 64bbebb commit 1693085
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
6 changes: 4 additions & 2 deletions packages/infrastructure/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/lambda/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'source-map-support/register';

import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export default async (event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2<string>> => {
export default async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log('request:', JSON.stringify(event, undefined, 2));

if (!event.body) {
return 'No number!';
return { body: 'No number!', statusCode: 400 };
}

const body = JSON.parse(event.body);
Expand Down Expand Up @@ -38,5 +38,5 @@ export default async (event: APIGatewayProxyEventV2): Promise<APIGatewayProxyRes

// var response = { "response": outputString }

return outputString;
return { body: outputString, statusCode: 200 };
};
32 changes: 16 additions & 16 deletions packages/lambda/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import lambdaTester from 'lambda-tester';

import fizzBuzzApiHandler from '../src';

describe('Fizz Buzz API handler', function () {
test('verifies not matching response', async () => {
return lambdaTester(fizzBuzzApiHandler)
.event(({ body: JSON.stringify({ number: 22 }) } as unknown) as APIGatewayProxyEventV2)
.expectResult((result: APIGatewayProxyResultV2<string>) => {
expect(result).toStrictEqual('Not matching');
.event(({ body: JSON.stringify({ number: 22 }) } as unknown) as APIGatewayProxyEvent)
.expectResult((result: APIGatewayProxyResult) => {
expect(result).toStrictEqual({ body: 'Not matching', statusCode: 200 });
});
});

test('verifies fizzbuzz response', async () => {
return lambdaTester(fizzBuzzApiHandler)
.event(({ body: JSON.stringify({ number: 15 }) } as unknown) as APIGatewayProxyEventV2)
.expectResult((result: APIGatewayProxyResultV2<string>) => {
expect(result).toStrictEqual('fizzbuzz');
.event(({ body: JSON.stringify({ number: 15 }) } as unknown) as APIGatewayProxyEvent)
.expectResult((result: APIGatewayProxyResult) => {
expect(result).toStrictEqual({ body: 'fizzbuzz', statusCode: 200 });
});
});

test('verifies fizz response', async () => {
return lambdaTester(fizzBuzzApiHandler)
.event(({ body: JSON.stringify({ number: 21 }) } as unknown) as APIGatewayProxyEventV2)
.expectResult((result: APIGatewayProxyResultV2<string>) => {
expect(result).toStrictEqual('fizz');
.event(({ body: JSON.stringify({ number: 21 }) } as unknown) as APIGatewayProxyEvent)
.expectResult((result: APIGatewayProxyResult) => {
expect(result).toStrictEqual({ body: 'fizz', statusCode: 200 });
});
});

test('verifies buzz response', async () => {
return lambdaTester(fizzBuzzApiHandler)
.event(({ body: JSON.stringify({ number: 25 }) } as unknown) as APIGatewayProxyEventV2)
.expectResult((result: APIGatewayProxyResultV2<string>) => {
expect(result).toStrictEqual('buzz');
.event(({ body: JSON.stringify({ number: 25 }) } as unknown) as APIGatewayProxyEvent)
.expectResult((result: APIGatewayProxyResult) => {
expect(result).toStrictEqual({ body: 'buzz', statusCode: 200 });
});
});

test('verifies empty body response', async () => {
return lambdaTester(fizzBuzzApiHandler)
.event(({} as unknown) as APIGatewayProxyEventV2)
.expectResult((result: APIGatewayProxyResultV2<string>) => {
expect(result).toStrictEqual('No number!');
.event(({} as unknown) as APIGatewayProxyEvent)
.expectResult((result: APIGatewayProxyResult) => {
expect(result).toStrictEqual({ body: 'No number!', statusCode: 400 });
});
});
});

0 comments on commit 1693085

Please sign in to comment.