Skip to content

Commit 9dce817

Browse files
committed
add more unit tests
1 parent d4a5c06 commit 9dce817

File tree

8 files changed

+65
-18
lines changed

8 files changed

+65
-18
lines changed

src/api/api-wrapper.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import createEvent from '@serverless/event-mocks';
2+
import { apiWrapper, ApiSignature } from './api-wrapper';
3+
4+
describe('API wrapper', () => {
5+
// @ts-ignore
6+
const event = createEvent('aws:apiGateway', {});
7+
8+
it('Handles success callback', () => {
9+
function mockHandler({ success }: ApiSignature) {
10+
return success('success');
11+
}
12+
expect(apiWrapper(mockHandler)(event)).toEqual({
13+
body: JSON.stringify('success'),
14+
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true },
15+
statusCode: 200
16+
});
17+
});
18+
19+
it('Handles error callback', () => {
20+
function mockHandler({ error }: ApiSignature) {
21+
return error('error');
22+
}
23+
expect(() => {
24+
apiWrapper(mockHandler)(event);
25+
}).toThrow('error');
26+
});
27+
28+
it('Handles invalid callback', () => {
29+
function mockHandler({ invalid }: ApiSignature) {
30+
return invalid(['invalid']);
31+
}
32+
expect(apiWrapper(mockHandler)(event)).toEqual({
33+
body: JSON.stringify({ errors: ['invalid'], event }),
34+
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true },
35+
statusCode: 400
36+
});
37+
});
38+
39+
it('Handles redirect callback', () => {
40+
function mockHandler({ redirect }: ApiSignature) {
41+
return redirect('url');
42+
}
43+
expect(apiWrapper(mockHandler)(event)).toEqual({
44+
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true, Location: 'url' },
45+
statusCode: 302
46+
});
47+
});
48+
});

src/api/api.ts renamed to src/api/api-wrapper.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { APIGatewayEvent, Context, Callback } from 'aws-lambda';
2-
import { Metrics, Body } from '../common';
2+
import { Metrics } from '../common';
3+
import { Body } from './body';
34

45
const metrics = new Metrics('API Gateway');
56

@@ -9,35 +10,35 @@ const HEADERS = {
910
};
1011

1112
export function apiWrapper<T extends Function>(fn: T): T {
12-
return <any>function(event: APIGatewayEvent, context: Context, callback: Callback) {
13+
return <any>function(event: APIGatewayEvent) {
1314
const { body, path, query, request, auth, headers, testRequest } = getRequestFields(event);
1415
metrics.common(request);
1516

16-
function success(payload: any = null): void {
17+
function success(payload: any = null) {
1718
const response = { statusCode: 200, headers: HEADERS };
1819
if (payload) {
1920
response['body'] = JSON.stringify(payload);
2021
}
2122
metrics.success(payload);
22-
return callback(null, response);
23+
return response;
2324
}
2425

25-
function invalid(errors: string[] = []): void {
26-
const response = { statusCode: 400, headers: HEADERS, body: JSON.stringify({ errors, request }) };
26+
function invalid(errors: string[] = []) {
27+
const response = { statusCode: 400, headers: HEADERS, body: JSON.stringify({ errors, event }) };
2728
metrics.invalid(response);
28-
return callback(null, response);
29+
return response;
2930
}
3031

31-
function redirect(url: string): void {
32+
function redirect(url: string) {
3233
HEADERS['Location'] = url;
3334
const response = { statusCode: 302, headers: HEADERS };
3435
metrics.redirect(response);
35-
return callback(null, response);
36+
return response;
3637
}
3738

38-
function error(error: any = ''): void {
39+
function error(error: any = '') {
3940
metrics.error(error);
40-
return callback(error);
41+
throw new Error(error);
4142
}
4243

4344
const signature: ApiSignature = {
File renamed without changes.

src/common/body.ts renamed to src/api/body.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse } from 'querystring';
2-
import { logger } from './log';
2+
import { logger } from '../common/log';
33

44
export class Body {
55
constructor(private body: any, private headers: { [name: string]: string }) {}

src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './api';
1+
export * from './api-wrapper';

src/auth/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ export function authWrapper<T extends Function>(fn: T): T {
1111
function valid(jwt: any): void {
1212
const policy = generatePolicy(jwt);
1313
metrics.valid(policy);
14-
return callback(null, policy);
14+
return policy;
1515
}
1616

1717
function invalid(message: any = ''): void {
1818
metrics.invalid(message);
19-
return callback('Unauthorized');
19+
throw new Error('Unauthorized');
2020
}
2121

2222
function error(error: any = ''): void {
2323
metrics.error(error);
24-
return callback(error);
24+
throw new Error(error);
2525
}
2626

2727
const signature: AuthorizerSignature = { event, token, valid, invalid, error };

src/common/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './log';
22
export * from './metrics';
3-
export * from './body';

src/stream/stream.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ describe('Stream parsing', () => {
108108
tableArn: 'arn:aws:dynamodb:us-east-1:1234567890:table/table',
109109
eventName: 'PUT'
110110
};
111-
112111
expect(versions).toHaveLength(2);
113112
expect(versions).toContainEqual(firstVersion);
114113
expect(versions).toContainEqual(secondVersion);

0 commit comments

Comments
 (0)