Skip to content

Commit bb370a1

Browse files
committed
refactor: replace usage of apiGatewayRequest by makeAPIGatewayRequestEvent on
Response tests - makeAPIGatewayRequestEvent accepts an input object, allowing the developer to change values of desired fields when needed, while apiGatewayRequest would always return the same object
1 parent f2a317f commit bb370a1

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

tests/Response.test.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import _ from 'underscore';
22
import { expect } from 'chai';
33
import { fail } from 'assert';
44
import { Request, Response, Application } from '../src';
5-
import { apiGatewayRequest, handlerContext, albRequest, albMultiValHeadersRequest } from './samples';
5+
import {
6+
handlerContext,
7+
albRequest,
8+
albMultiValHeadersRequest,
9+
makeAPIGatewayRequestEvent,
10+
} from './samples';
611
import sinon, { spy, assert, SinonSpy, SinonSandbox, SinonFakeTimers } from 'sinon';
712
import { StringArrayOfStringsMap, StringMap } from '@silvermine/toolbox';
813
import { RequestEvent, CookieOpts } from '../src/request-response-types';
@@ -28,7 +33,7 @@ describe('Response', () => {
2833

2934
beforeEach(() => {
3035
app = new Application();
31-
sampleReq = new Request(app, apiGatewayRequest(), handlerContext());
36+
sampleReq = new Request(app, makeAPIGatewayRequestEvent(), handlerContext());
3237
sampleResp = new Response(app, sampleReq, EMPTY_CB);
3338
});
3439

@@ -152,7 +157,7 @@ describe('Response', () => {
152157
// This is for JS-only functionality. TypeScript users will be safeguarded from
153158
// doing this by type safety.
154159
const val: any = true,
155-
req = new Request(app, apiGatewayRequest(), handlerContext()),
160+
req = new Request(app, makeAPIGatewayRequestEvent(), handlerContext()),
156161
logger = new ConsoleLogger({ interface: 'ALB', getTimeUntilFnTimeout: () => { return 0; } }),
157162
errorFnSpy = sinon.spy();
158163

@@ -479,15 +484,15 @@ describe('Response', () => {
479484

480485
if (referer === null) {
481486
// use the existing header
482-
evt = apiGatewayRequest();
487+
evt = makeAPIGatewayRequestEvent();
483488
} else if (referer === false) {
484489
// remove existing header
485-
evt = apiGatewayRequest();
490+
evt = makeAPIGatewayRequestEvent();
486491
delete evt.multiValueHeaders.Referer;
487492
delete evt.headers.Referer;
488493
} else {
489494
// override the existing one
490-
evt = _.extend(apiGatewayRequest(), {
495+
evt = _.extend(makeAPIGatewayRequestEvent(), {
491496
multiValueHeaders: { 'Referer': [ referer ] },
492497
headers: { 'Referer': referer },
493498
});
@@ -619,7 +624,7 @@ describe('Response', () => {
619624

620625
describe('body property', () => {
621626
it('contains what was sent in the response', () => {
622-
const resp = new Response(app, new Request(app, apiGatewayRequest(), handlerContext()), spy());
627+
const resp = new Response(app, new Request(app, makeAPIGatewayRequestEvent(), handlerContext()), spy());
623628

624629
resp.send({ foo: 'bar' });
625630
expect(resp.body).to.eql(JSON.stringify({ foo: 'bar' }));
@@ -680,10 +685,10 @@ describe('Response', () => {
680685
expect(cb.firstCall.args).to.eql([ undefined, output ]);
681686
};
682687

683-
it('sends immediately - APIGW', () => { test(apiGatewayRequest(), false, false); });
688+
it('sends immediately - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, false); });
684689
it('sends immediately - ALB', () => { test(albRequest(), 'OK', false); });
685690
it('sends immediately - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', false); });
686-
it('sends after setting headers - APIGW', () => { test(apiGatewayRequest(), false, true); });
691+
it('sends after setting headers - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, true); });
687692
it('sends after setting headers - ALB', () => { test(albRequest(), 'OK', true); });
688693
it('sends after setting headers - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', true); });
689694
});
@@ -715,10 +720,10 @@ describe('Response', () => {
715720
expect(cb.firstCall.args).to.eql([ undefined, output ]);
716721
};
717722

718-
it('sends immediately - APIGW', () => { test(apiGatewayRequest(), false, false); });
723+
it('sends immediately - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, false); });
719724
it('sends immediately - ALB', () => { test(albRequest(), 'OK', false); });
720725
it('sends immediately - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', false); });
721-
it('sends after headers - APIGW', () => { test(apiGatewayRequest(), false, true); });
726+
it('sends after headers - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, true); });
722727
it('sends after headers - ALB', () => { test(albRequest(), 'OK', true); });
723728
it('sends after headers - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', true); });
724729

@@ -730,7 +735,7 @@ describe('Response', () => {
730735
}
731736
};
732737

733-
it('sends with alternate status code - APIGW', () => { test(apiGatewayRequest(), false, false, ext1); });
738+
it('sends with alternate status code - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, false, ext1); });
734739
it('sends with alternate status code - ALB', () => { test(albRequest(), 'OK', false, ext1); });
735740
it('sends with alternate status code - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', false, ext1); });
736741

@@ -743,7 +748,7 @@ describe('Response', () => {
743748
}
744749
};
745750

746-
it('overrides previously-set content type - APIGW', () => { test(apiGatewayRequest(), false, false, ext2); });
751+
it('overrides previously-set content type - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, false, ext2); });
747752
it('overrides previously-set content type - ALB', () => { test(albRequest(), 'OK', false, ext2); });
748753
it('overrides previously-set content type - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', false, ext2); });
749754
});
@@ -794,7 +799,7 @@ describe('Response', () => {
794799
expect(cb.firstCall.args).to.eql([ undefined, output ]);
795800
};
796801

797-
it('sends immediately - APIGW', () => { test(apiGatewayRequest(), false); });
802+
it('sends immediately - APIGW', () => { test(makeAPIGatewayRequestEvent(), false); });
798803
it('sends immediately - ALB', () => { test(albRequest(), 'OK'); });
799804
it('sends immediately - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK'); });
800805

@@ -806,7 +811,7 @@ describe('Response', () => {
806811
}
807812
};
808813

809-
it('sends after headers - APIGW', () => { test(apiGatewayRequest(), false, ext1); });
814+
it('sends after headers - APIGW', () => { test(makeAPIGatewayRequestEvent(), false, ext1); });
810815
it('sends after headers - ALB', () => { test(albRequest(), 'OK', ext1); });
811816
it('sends after headers - ALB MV', () => { test(albMultiValHeadersRequest(), 'OK', ext1); });
812817

@@ -815,7 +820,7 @@ describe('Response', () => {
815820
};
816821

817822
it('works with custom callback param name - APIGW', () => {
818-
test(apiGatewayRequest(), false, ext2, { queryParamName: 'cbFnName' });
823+
test(makeAPIGatewayRequestEvent(), false, ext2, { queryParamName: 'cbFnName' });
819824
});
820825
it('works with custom callback param name - ALB', () => {
821826
test(albRequest(), 'OK', ext2, { queryParamName: 'cbFnName' });
@@ -831,7 +836,7 @@ describe('Response', () => {
831836
};
832837

833838
it('works like JSON when no callback in query - APIGW', () => {
834-
test(apiGatewayRequest(), false, expectJSON, { queryParamName: false });
839+
test(makeAPIGatewayRequestEvent(), false, expectJSON, { queryParamName: false });
835840
});
836841
it('works like JSON when no callback in query - ALB', () => {
837842
test(albRequest(), 'OK', expectJSON, { queryParamName: false });
@@ -841,7 +846,7 @@ describe('Response', () => {
841846
});
842847

843848
it('works like JSON when non-callback param is in query - APIGW', () => {
844-
test(apiGatewayRequest(), false, expectJSON, { queryParamName: 'notcallback' });
849+
test(makeAPIGatewayRequestEvent(), false, expectJSON, { queryParamName: 'notcallback' });
845850
});
846851
it('works like JSON when non-callback param is in query - ALB', () => {
847852
test(albRequest(), 'OK', expectJSON, { queryParamName: 'notcallback' });
@@ -851,7 +856,7 @@ describe('Response', () => {
851856
});
852857

853858
it('uses the first callback param value listed - APIGW', () => {
854-
test(apiGatewayRequest(), false, undefined, {
859+
test(makeAPIGatewayRequestEvent(), false, undefined, {
855860
queryParamValues: [ 'callbackOne', 'callbackTwo' ],
856861
});
857862
});
@@ -867,7 +872,7 @@ describe('Response', () => {
867872
});
868873

869874
it('allows for the callback param value to contain an array index - APIGW', () => {
870-
test(apiGatewayRequest(), false, undefined, {
875+
test(makeAPIGatewayRequestEvent(), false, undefined, {
871876
queryParamValues: [ 'callbacks[123]' ],
872877
});
873878
});
@@ -883,7 +888,7 @@ describe('Response', () => {
883888
});
884889

885890
it('returns JSON when callback param value contains invalid characters - APIGW', () => {
886-
test(apiGatewayRequest(), false, expectJSON, {
891+
test(makeAPIGatewayRequestEvent(), false, expectJSON, {
887892
queryParamValues: [ 'bad;fn()' ],
888893
});
889894
});
@@ -905,7 +910,7 @@ describe('Response', () => {
905910
};
906911

907912
it('escapes UTF newline and paragraph separators - APIGW', () => {
908-
test(apiGatewayRequest(), false, ext3, { responseObject: utfInput });
913+
test(makeAPIGatewayRequestEvent(), false, ext3, { responseObject: utfInput });
909914
});
910915
it('escapes UTF newline and paragraph separators - ALB', () => {
911916
test(albRequest(), 'OK', ext3, { responseObject: utfInput });
@@ -1040,7 +1045,7 @@ describe('Response', () => {
10401045
o.multiValueHeaders['Content-Type'] = [ 'text/html' ];
10411046
};
10421047

1043-
it('sends string immediately - APIGW', () => { test(apiGatewayRequest(), 200, false, 'body', ext1); });
1048+
it('sends string immediately - APIGW', () => { test(makeAPIGatewayRequestEvent(), 200, false, 'body', ext1); });
10441049
it('sends string immediately - ALB', () => { test(albRequest(), 200, 'OK', 'body', ext1); });
10451050
it('sends string immediately - ALB MV', () => { test(albMultiValHeadersRequest(), 200, 'OK', 'body', ext1); });
10461051

@@ -1049,7 +1054,7 @@ describe('Response', () => {
10491054
o.multiValueHeaders['Content-Type'] = [ 'foo/bar' ];
10501055
};
10511056

1052-
it('sends string with existing content type - APIGW', () => { test(apiGatewayRequest(), 200, false, 'body', ext2); });
1057+
it('sends string with existing content type - APIGW', () => { test(makeAPIGatewayRequestEvent(), 200, false, 'body', ext2); });
10531058
it('sends string with existing content type - ALB', () => { test(albRequest(), 200, 'OK', 'body', ext2); });
10541059
it('sends string with existing content type - ALB MV', () => { test(albMultiValHeadersRequest(), 200, 'OK', 'body', ext2); });
10551060

@@ -1060,7 +1065,7 @@ describe('Response', () => {
10601065
o.multiValueHeaders['Content-Type'] = [ 'application/json; charset=utf-8' ];
10611066
};
10621067

1063-
it('sends JSON immediately - APIGW', () => { test(apiGatewayRequest(), 200, false, bodyObj, ext3); });
1068+
it('sends JSON immediately - APIGW', () => { test(makeAPIGatewayRequestEvent(), 200, false, bodyObj, ext3); });
10641069
it('sends JSON immediately - ALB', () => { test(albRequest(), 200, 'OK', bodyObj, ext3); });
10651070
it('sends JSON immediately - ALB MV', () => { test(albMultiValHeadersRequest(), 200, 'OK', bodyObj, ext3); });
10661071
});
@@ -1085,10 +1090,10 @@ describe('Response', () => {
10851090
};
10861091

10871092
_.each({ 'OK': 200, 'Created': 201, 'Not Found': 404, 'I\'m a teapot': 418 }, (code, msg) => {
1088-
it(`sends immediately - APIGW - ${code} ${msg}`, () => { test(apiGatewayRequest(), code, false, false); });
1093+
it(`sends immediately - APIGW - ${code} ${msg}`, () => { test(makeAPIGatewayRequestEvent(), code, false, false); });
10891094
it(`sends immediately - ALB - ${code} ${msg}`, () => { test(albRequest(), code, msg, false); });
10901095
it(`sends immediately - ALB MV - ${code} ${msg}`, () => { test(albMultiValHeadersRequest(), code, msg, false); });
1091-
it(`sends after headers - APIGW - ${code} ${msg}`, () => { test(apiGatewayRequest(), code, false, true); });
1096+
it(`sends after headers - APIGW - ${code} ${msg}`, () => { test(makeAPIGatewayRequestEvent(), code, false, true); });
10921097
it(`sends after headers - ALB - ${code} ${msg}`, () => { test(albRequest(), code, msg, true); });
10931098
it(`sends after headers - ALB MV - ${code} ${msg}`, () => { test(albMultiValHeadersRequest(), code, msg, true); });
10941099
});

0 commit comments

Comments
 (0)