Skip to content

Commit 9ede516

Browse files
committed
add sns testing
1 parent 4c94f61 commit 9ede516

22 files changed

+188
-63
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/index.ts

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

src/api/api-parser.test.ts renamed to src/api/parser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stringify } from 'querystring';
22
import createEvent from '@serverless/event-mocks';
3-
import { Request, Body } from './api-parser';
3+
import { Request, Body } from './parser';
44

55
describe('Body parsing', () => {
66
it('Parses json body', () => {
File renamed without changes.

src/api/api-responses.test.ts renamed to src/api/responses.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import createEvent from '@serverless/event-mocks';
2-
import { success, error, invalid, redirect } from './api-responses';
2+
import { success, error, invalid, redirect } from './responses';
33

44
describe('API responses', () => {
55
// @ts-ignore
@@ -13,6 +13,13 @@ describe('API responses', () => {
1313
});
1414
});
1515

16+
it('Handles success response without payload', () => {
17+
expect(success()).toEqual({
18+
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true },
19+
statusCode: 200
20+
});
21+
});
22+
1623
it('Handles error response', () => {
1724
expect(() => error('error')).toThrow('error');
1825
});
@@ -25,6 +32,13 @@ describe('API responses', () => {
2532
});
2633
});
2734

35+
it('Handles invalid response without validation errors', () => {
36+
expect(invalid()).toEqual({
37+
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true },
38+
statusCode: 400
39+
});
40+
});
41+
2842
it('Handles redirect response', () => {
2943
expect(redirect('url')).toEqual({
3044
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true, Location: 'url' },

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const HEADERS = {
77
'Access-Control-Allow-Credentials': true
88
};
99

10-
export function success(payload: any = null) {
10+
export function success(payload?: any) {
1111
const response = { statusCode: 200, headers: HEADERS };
1212
if (payload) {
1313
response['body'] = JSON.stringify(payload);
@@ -16,8 +16,11 @@ export function success(payload: any = null) {
1616
return response;
1717
}
1818

19-
export function invalid(errors: string[] = []) {
20-
const response = { statusCode: 400, headers: HEADERS, body: JSON.stringify({ errors }) };
19+
export function invalid(errors?: string[]) {
20+
const response = { statusCode: 400, headers: HEADERS };
21+
if (errors) {
22+
response['body'] = JSON.stringify({ errors });
23+
}
2124
metrics.invalid(response);
2225
return response;
2326
}
@@ -29,7 +32,7 @@ export function redirect(url: string) {
2932
return response;
3033
}
3134

32-
export function error(error: any = '') {
35+
export function error(error?: any) {
3336
metrics.error(error);
3437
throw new Error(error);
3538
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import createEvent from '@serverless/event-mocks';
2-
import { apiWrapper, ApiSignature } from './api-wrapper';
2+
import { apiWrapper, ApiSignature } from './wrapper';
33

44
describe('API wrapper', () => {
55
// @ts-ignore

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { APIGatewayEvent } from 'aws-lambda';
2-
import { Request } from './api-parser';
3-
import { success, invalid, redirect, error } from './api-responses';
2+
import { Request } from './parser';
3+
import { success, invalid, redirect, error } from './responses';
44

55
export function apiWrapper<T extends Function>(fn: T): T {
66
return <any>function(event: APIGatewayEvent) {

src/sns/index.ts

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

src/sns/parser.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import createEvent from '@serverless/event-mocks';
2+
import { SnsParser } from './parser';
3+
4+
describe('Stream parsing', () => {
5+
it('Parses string messages', () => {
6+
const Sns = {
7+
Message: 'hello world'
8+
};
9+
// @ts-ignore
10+
const event = createEvent('aws:sns', { Records: [{ Sns }] });
11+
const message = new SnsParser(event).getMessage();
12+
expect(message).toEqual('hello world');
13+
});
14+
15+
it('Parses JSON stringified messages', () => {
16+
const Sns = {
17+
Message: JSON.stringify({ hello: 'world' })
18+
};
19+
// @ts-ignore
20+
const event = createEvent('aws:sns', { Records: [{ Sns }] });
21+
const message = new SnsParser(event).getMessage();
22+
expect(message).toEqual({ hello: 'world' });
23+
});
24+
});

0 commit comments

Comments
 (0)