Skip to content

Commit

Permalink
test: allow sample generators to change path/method
Browse files Browse the repository at this point in the history
  • Loading branch information
onebytegone committed Jun 20, 2024
1 parent d47be8a commit 1068897
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
32 changes: 14 additions & 18 deletions tests/integration-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ describe('integration tests', () => {
app = new Application();
});

const makeRequestEvent = (path: string, base?: RequestEvent, method?: string): RequestEvent => {
return _.extend(base || apiGatewayRequest(), { path: path, httpMethod: (method || 'GET') });
};

const testWithLastResortHandler = (code: number, desc: string, testHeaders: string[] = [], expectedBody = ''): void => {
const cb = spy(),
evt = makeRequestEvent('/hello/world', albMultiValHeadersRequest()),
evt = albMultiValHeadersRequest('/hello/world'),
headers: StringMap = {},
multiValueHeaders: StringArrayOfStringsMap = {};

Expand All @@ -54,7 +50,7 @@ describe('integration tests', () => {

const testOutcome = (method: string, path: string, expectedBody: string): void => {
const cb = spy(),
evt = makeRequestEvent(path, apiGatewayRequest(), method);
evt = apiGatewayRequest(path, method);

app.run(evt, handlerContext(), cb);

Expand Down Expand Up @@ -121,7 +117,7 @@ describe('integration tests', () => {
};

it('works - APIGW', () => {
const cb = test(makeRequestEvent('/hello/world'));
const cb = test(apiGatewayRequest('/hello/world'));

assert.calledWithExactly(cb, undefined, {
statusCode: 200,
Expand All @@ -137,7 +133,7 @@ describe('integration tests', () => {
});

it('works - ALB', () => {
const cb = test(makeRequestEvent('/hello/world', albRequest()));
const cb = test(albRequest('/hello/world'));

assert.calledWithExactly(cb, undefined, {
statusCode: 200,
Expand All @@ -160,7 +156,7 @@ describe('integration tests', () => {
});

it('works - ALBMV', () => {
const cb = test(makeRequestEvent('/hello/world', albMultiValHeadersRequest()));
const cb = test(albMultiValHeadersRequest('/hello/world'));

assert.calledWithExactly(cb, undefined, {
statusCode: 200,
Expand Down Expand Up @@ -209,7 +205,7 @@ describe('integration tests', () => {

// "%EA" is the unicode code point for an "e with circumflex". The client should
// be sending this character using UTF-8 encoding (i.e. %C3%AA)
const evt = makeRequestEvent('/hello/%EA', albMultiValHeadersRequest()),
const evt = albMultiValHeadersRequest('/hello/%EA'),
cb = spy();

app.run(evt, handlerContext(), cb);
Expand Down Expand Up @@ -341,16 +337,16 @@ describe('integration tests', () => {
describe('other HTTP methods', () => {
// eslint-disable-next-line max-len,max-params
const addTestsForMethod = (method: string, code: number, desc: string, hdrName: string, hdrVal: string, expectedBody: string, prep: () => void, contentType?: string): void => {
const baseEvents = {
'APIGW': apiGatewayRequest(),
'ALB': albRequest(),
'ALBMV': albMultiValHeadersRequest(),
const baseEventGenerators = {
'APIGW': apiGatewayRequest,
'ALB': albRequest,
'ALBMV': albMultiValHeadersRequest,
};

_.each(baseEvents, (baseEvent, eventTypeName) => {
_.each(baseEventGenerators, (baseEventGenerator, eventTypeName) => {
it(`works with HTTP method ${method} - ${eventTypeName}`, () => {
const cb = spy(),
evt = makeRequestEvent('/hello/world', baseEvent, method);
evt = baseEventGenerator('/hello/world', method);

// this request handler should get run for all methods:
app.all('/hello/world', (_req: Request, resp: Response, next: NextCallback): void => {
Expand Down Expand Up @@ -760,7 +756,7 @@ describe('integration tests', () => {
describe('request object', () => {

it('has an immutable context property', () => {
let evt = makeRequestEvent('/test', apiGatewayRequest(), 'GET'),
let evt = apiGatewayRequest('/test', 'GET'),
ctx = handlerContext(true),
handler;

Expand Down Expand Up @@ -915,7 +911,7 @@ describe('integration tests', () => {

// "%EA" is the unicode code point for an "e with circumflex". The client should be
// sending this character using UTF-8 encoding (i.e. %C3%AA)
const evt = makeRequestEvent('/hello/%EA', albMultiValHeadersRequest()),
const evt = albMultiValHeadersRequest('/hello/%EA'),
cb = spy();

app.run(evt, handlerContext(), cb);
Expand Down
40 changes: 23 additions & 17 deletions tests/samples.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import _ from 'underscore';
import {
APIGatewayEventRequestContext,
RequestEventRequestContext,
ApplicationLoadBalancerEventRequestContext,
APIGatewayRequestEvent,
ApplicationLoadBalancerRequestEvent } from '../src/request-response-types';
ApplicationLoadBalancerRequestEvent,
RequestEvent,
APIGatewayEventRequestContext,
} from '../src/request-response-types';
import { Context } from 'aws-lambda';

export const handlerContext = (fillAllFields: boolean = false): Context => {
Expand Down Expand Up @@ -51,12 +54,15 @@ export const handlerContext = (fillAllFields: boolean = false): Context => {
return ctx;
};

export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {
type SampleRequestContextGenerator<T extends RequestEventRequestContext> = (path?: string, method?: string) => T;
type SampleRequestGenerator<T extends RequestEvent> = (path?: string, method?: string) => T;

export const apiGatewayRequestContext: SampleRequestContextGenerator<APIGatewayEventRequestContext> = (path?: string, method?: string) => {
return {
accountId: '123456789012',
apiId: 'someapi',
authorizer: null,
httpMethod: 'GET',
httpMethod: method || 'GET',
identity: {
accessKey: null,
accountId: null,
Expand All @@ -74,7 +80,7 @@ export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {
userAgent: 'curl/7.54.0',
userArn: null,
},
path: '/prd',
path: path || '/prd',
protocol: 'HTTP/1.1',
stage: 'prd',
requestId: 'a507736b-259e-11e9-8fcf-4f1f08c4591e',
Expand All @@ -86,16 +92,16 @@ export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => {

export const apiGatewayRequestRawQuery = '?&foo[a]=bar%20b&foo[a]=baz%20c&x=1&x=2&y=z';

export const apiGatewayRequest = (): APIGatewayRequestEvent => {
export const apiGatewayRequest: SampleRequestGenerator<APIGatewayRequestEvent> = (path?: string, method?: string) => {
return {
body: null,
httpMethod: 'GET',
httpMethod: method || 'GET',
isBase64Encoded: false,
path: '/echo/asdf/a',
path: path || '/echo/asdf/a',
resource: '/{proxy+}',
pathParameters: { proxy: 'echo/asdf/a' },
stageVariables: null,
requestContext: apiGatewayRequestContext(),
requestContext: apiGatewayRequestContext(path, method),
headers: {
Accept: '*/*',
'CloudFront-Forwarded-Proto': 'https',
Expand Down Expand Up @@ -148,28 +154,28 @@ export const apiGatewayRequest = (): APIGatewayRequestEvent => {
};
};

export const albRequestContext = (): ApplicationLoadBalancerEventRequestContext => {
export const albRequestContext: SampleRequestContextGenerator<ApplicationLoadBalancerEventRequestContext> = () => {
return {
elb: {
targetGroupArn: 'arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/alb-lambda-prd-tg1/180d40bbdb377b34',
},
};
};

const albRequestBase = (): ApplicationLoadBalancerRequestEvent => {
const albRequestBase = (path?: string, method?: string): ApplicationLoadBalancerRequestEvent => {
return {
body: '',
httpMethod: 'GET',
httpMethod: method || 'GET',
isBase64Encoded: false,
path: '/echo/asdf/a',
path: path || '/echo/asdf/a',
requestContext: albRequestContext(),
};
};

export const albRequestRawQuery = '?&foo%5Ba%5D=baz%20c&x=2&y=z';

export const albRequest = (): ApplicationLoadBalancerRequestEvent => {
return _.extend({}, albRequestBase(), {
export const albRequest: SampleRequestGenerator<ApplicationLoadBalancerRequestEvent> = (path?: string, method?: string) => {
return _.extend({}, albRequestBase(path, method), {
queryStringParameters: {
'foo%5Ba%5D': 'baz%20c',
x: '2',
Expand All @@ -193,8 +199,8 @@ export const albRequest = (): ApplicationLoadBalancerRequestEvent => {

export const albMultiValHeadersRawQuery = '?&foo%5Ba%5D=bar%20b&foo%5Ba%5D=baz%20c&x=1&x=2&y=z';

export const albMultiValHeadersRequest = (): ApplicationLoadBalancerRequestEvent => {
return _.extend({}, albRequestBase(), {
export const albMultiValHeadersRequest: SampleRequestGenerator<ApplicationLoadBalancerRequestEvent> = (path?: string, method?: string) => {
return _.extend({}, albRequestBase(path, method), {
multiValueQueryStringParameters: {
'foo%5Ba%5D': [ 'bar%20b', 'baz c' ],
x: [ '1', '2' ],
Expand Down

0 comments on commit 1068897

Please sign in to comment.