@@ -2,7 +2,12 @@ import _ from 'underscore';
2
2
import { expect } from 'chai' ;
3
3
import { fail } from 'assert' ;
4
4
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' ;
6
11
import sinon , { spy , assert , SinonSpy , SinonSandbox , SinonFakeTimers } from 'sinon' ;
7
12
import { StringArrayOfStringsMap , StringMap } from '@silvermine/toolbox' ;
8
13
import { RequestEvent , CookieOpts } from '../src/request-response-types' ;
@@ -28,7 +33,7 @@ describe('Response', () => {
28
33
29
34
beforeEach ( ( ) => {
30
35
app = new Application ( ) ;
31
- sampleReq = new Request ( app , apiGatewayRequest ( ) , handlerContext ( ) ) ;
36
+ sampleReq = new Request ( app , makeAPIGatewayRequestEvent ( ) , handlerContext ( ) ) ;
32
37
sampleResp = new Response ( app , sampleReq , EMPTY_CB ) ;
33
38
} ) ;
34
39
@@ -152,7 +157,7 @@ describe('Response', () => {
152
157
// This is for JS-only functionality. TypeScript users will be safeguarded from
153
158
// doing this by type safety.
154
159
const val : any = true ,
155
- req = new Request ( app , apiGatewayRequest ( ) , handlerContext ( ) ) ,
160
+ req = new Request ( app , makeAPIGatewayRequestEvent ( ) , handlerContext ( ) ) ,
156
161
logger = new ConsoleLogger ( { interface : 'ALB' , getTimeUntilFnTimeout : ( ) => { return 0 ; } } ) ,
157
162
errorFnSpy = sinon . spy ( ) ;
158
163
@@ -479,15 +484,15 @@ describe('Response', () => {
479
484
480
485
if ( referer === null ) {
481
486
// use the existing header
482
- evt = apiGatewayRequest ( ) ;
487
+ evt = makeAPIGatewayRequestEvent ( ) ;
483
488
} else if ( referer === false ) {
484
489
// remove existing header
485
- evt = apiGatewayRequest ( ) ;
490
+ evt = makeAPIGatewayRequestEvent ( ) ;
486
491
delete evt . multiValueHeaders . Referer ;
487
492
delete evt . headers . Referer ;
488
493
} else {
489
494
// override the existing one
490
- evt = _ . extend ( apiGatewayRequest ( ) , {
495
+ evt = _ . extend ( makeAPIGatewayRequestEvent ( ) , {
491
496
multiValueHeaders : { 'Referer' : [ referer ] } ,
492
497
headers : { 'Referer' : referer } ,
493
498
} ) ;
@@ -619,7 +624,7 @@ describe('Response', () => {
619
624
620
625
describe ( 'body property' , ( ) => {
621
626
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 ( ) ) ;
623
628
624
629
resp . send ( { foo : 'bar' } ) ;
625
630
expect ( resp . body ) . to . eql ( JSON . stringify ( { foo : 'bar' } ) ) ;
@@ -680,10 +685,10 @@ describe('Response', () => {
680
685
expect ( cb . firstCall . args ) . to . eql ( [ undefined , output ] ) ;
681
686
} ;
682
687
683
- it ( 'sends immediately - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , false , false ) ; } ) ;
688
+ it ( 'sends immediately - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , false , false ) ; } ) ;
684
689
it ( 'sends immediately - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , false ) ; } ) ;
685
690
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 ) ; } ) ;
687
692
it ( 'sends after setting headers - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , true ) ; } ) ;
688
693
it ( 'sends after setting headers - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' , true ) ; } ) ;
689
694
} ) ;
@@ -715,10 +720,10 @@ describe('Response', () => {
715
720
expect ( cb . firstCall . args ) . to . eql ( [ undefined , output ] ) ;
716
721
} ;
717
722
718
- it ( 'sends immediately - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , false , false ) ; } ) ;
723
+ it ( 'sends immediately - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , false , false ) ; } ) ;
719
724
it ( 'sends immediately - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , false ) ; } ) ;
720
725
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 ) ; } ) ;
722
727
it ( 'sends after headers - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , true ) ; } ) ;
723
728
it ( 'sends after headers - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' , true ) ; } ) ;
724
729
@@ -730,7 +735,7 @@ describe('Response', () => {
730
735
}
731
736
} ;
732
737
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 ) ; } ) ;
734
739
it ( 'sends with alternate status code - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , false , ext1 ) ; } ) ;
735
740
it ( 'sends with alternate status code - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' , false , ext1 ) ; } ) ;
736
741
@@ -743,7 +748,7 @@ describe('Response', () => {
743
748
}
744
749
} ;
745
750
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 ) ; } ) ;
747
752
it ( 'overrides previously-set content type - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , false , ext2 ) ; } ) ;
748
753
it ( 'overrides previously-set content type - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' , false , ext2 ) ; } ) ;
749
754
} ) ;
@@ -794,7 +799,7 @@ describe('Response', () => {
794
799
expect ( cb . firstCall . args ) . to . eql ( [ undefined , output ] ) ;
795
800
} ;
796
801
797
- it ( 'sends immediately - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , false ) ; } ) ;
802
+ it ( 'sends immediately - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , false ) ; } ) ;
798
803
it ( 'sends immediately - ALB' , ( ) => { test ( albRequest ( ) , 'OK' ) ; } ) ;
799
804
it ( 'sends immediately - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' ) ; } ) ;
800
805
@@ -806,7 +811,7 @@ describe('Response', () => {
806
811
}
807
812
} ;
808
813
809
- it ( 'sends after headers - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , false , ext1 ) ; } ) ;
814
+ it ( 'sends after headers - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , false , ext1 ) ; } ) ;
810
815
it ( 'sends after headers - ALB' , ( ) => { test ( albRequest ( ) , 'OK' , ext1 ) ; } ) ;
811
816
it ( 'sends after headers - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 'OK' , ext1 ) ; } ) ;
812
817
@@ -815,7 +820,7 @@ describe('Response', () => {
815
820
} ;
816
821
817
822
it ( 'works with custom callback param name - APIGW' , ( ) => {
818
- test ( apiGatewayRequest ( ) , false , ext2 , { queryParamName : 'cbFnName' } ) ;
823
+ test ( makeAPIGatewayRequestEvent ( ) , false , ext2 , { queryParamName : 'cbFnName' } ) ;
819
824
} ) ;
820
825
it ( 'works with custom callback param name - ALB' , ( ) => {
821
826
test ( albRequest ( ) , 'OK' , ext2 , { queryParamName : 'cbFnName' } ) ;
@@ -831,7 +836,7 @@ describe('Response', () => {
831
836
} ;
832
837
833
838
it ( 'works like JSON when no callback in query - APIGW' , ( ) => {
834
- test ( apiGatewayRequest ( ) , false , expectJSON , { queryParamName : false } ) ;
839
+ test ( makeAPIGatewayRequestEvent ( ) , false , expectJSON , { queryParamName : false } ) ;
835
840
} ) ;
836
841
it ( 'works like JSON when no callback in query - ALB' , ( ) => {
837
842
test ( albRequest ( ) , 'OK' , expectJSON , { queryParamName : false } ) ;
@@ -841,7 +846,7 @@ describe('Response', () => {
841
846
} ) ;
842
847
843
848
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' } ) ;
845
850
} ) ;
846
851
it ( 'works like JSON when non-callback param is in query - ALB' , ( ) => {
847
852
test ( albRequest ( ) , 'OK' , expectJSON , { queryParamName : 'notcallback' } ) ;
@@ -851,7 +856,7 @@ describe('Response', () => {
851
856
} ) ;
852
857
853
858
it ( 'uses the first callback param value listed - APIGW' , ( ) => {
854
- test ( apiGatewayRequest ( ) , false , undefined , {
859
+ test ( makeAPIGatewayRequestEvent ( ) , false , undefined , {
855
860
queryParamValues : [ 'callbackOne' , 'callbackTwo' ] ,
856
861
} ) ;
857
862
} ) ;
@@ -867,7 +872,7 @@ describe('Response', () => {
867
872
} ) ;
868
873
869
874
it ( 'allows for the callback param value to contain an array index - APIGW' , ( ) => {
870
- test ( apiGatewayRequest ( ) , false , undefined , {
875
+ test ( makeAPIGatewayRequestEvent ( ) , false , undefined , {
871
876
queryParamValues : [ 'callbacks[123]' ] ,
872
877
} ) ;
873
878
} ) ;
@@ -883,7 +888,7 @@ describe('Response', () => {
883
888
} ) ;
884
889
885
890
it ( 'returns JSON when callback param value contains invalid characters - APIGW' , ( ) => {
886
- test ( apiGatewayRequest ( ) , false , expectJSON , {
891
+ test ( makeAPIGatewayRequestEvent ( ) , false , expectJSON , {
887
892
queryParamValues : [ 'bad;fn()' ] ,
888
893
} ) ;
889
894
} ) ;
@@ -905,7 +910,7 @@ describe('Response', () => {
905
910
} ;
906
911
907
912
it ( 'escapes UTF newline and paragraph separators - APIGW' , ( ) => {
908
- test ( apiGatewayRequest ( ) , false , ext3 , { responseObject : utfInput } ) ;
913
+ test ( makeAPIGatewayRequestEvent ( ) , false , ext3 , { responseObject : utfInput } ) ;
909
914
} ) ;
910
915
it ( 'escapes UTF newline and paragraph separators - ALB' , ( ) => {
911
916
test ( albRequest ( ) , 'OK' , ext3 , { responseObject : utfInput } ) ;
@@ -1040,7 +1045,7 @@ describe('Response', () => {
1040
1045
o . multiValueHeaders [ 'Content-Type' ] = [ 'text/html' ] ;
1041
1046
} ;
1042
1047
1043
- it ( 'sends string immediately - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , 200 , false , 'body' , ext1 ) ; } ) ;
1048
+ it ( 'sends string immediately - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , 200 , false , 'body' , ext1 ) ; } ) ;
1044
1049
it ( 'sends string immediately - ALB' , ( ) => { test ( albRequest ( ) , 200 , 'OK' , 'body' , ext1 ) ; } ) ;
1045
1050
it ( 'sends string immediately - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 200 , 'OK' , 'body' , ext1 ) ; } ) ;
1046
1051
@@ -1049,7 +1054,7 @@ describe('Response', () => {
1049
1054
o . multiValueHeaders [ 'Content-Type' ] = [ 'foo/bar' ] ;
1050
1055
} ;
1051
1056
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 ) ; } ) ;
1053
1058
it ( 'sends string with existing content type - ALB' , ( ) => { test ( albRequest ( ) , 200 , 'OK' , 'body' , ext2 ) ; } ) ;
1054
1059
it ( 'sends string with existing content type - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 200 , 'OK' , 'body' , ext2 ) ; } ) ;
1055
1060
@@ -1060,7 +1065,7 @@ describe('Response', () => {
1060
1065
o . multiValueHeaders [ 'Content-Type' ] = [ 'application/json; charset=utf-8' ] ;
1061
1066
} ;
1062
1067
1063
- it ( 'sends JSON immediately - APIGW' , ( ) => { test ( apiGatewayRequest ( ) , 200 , false , bodyObj , ext3 ) ; } ) ;
1068
+ it ( 'sends JSON immediately - APIGW' , ( ) => { test ( makeAPIGatewayRequestEvent ( ) , 200 , false , bodyObj , ext3 ) ; } ) ;
1064
1069
it ( 'sends JSON immediately - ALB' , ( ) => { test ( albRequest ( ) , 200 , 'OK' , bodyObj , ext3 ) ; } ) ;
1065
1070
it ( 'sends JSON immediately - ALB MV' , ( ) => { test ( albMultiValHeadersRequest ( ) , 200 , 'OK' , bodyObj , ext3 ) ; } ) ;
1066
1071
} ) ;
@@ -1085,10 +1090,10 @@ describe('Response', () => {
1085
1090
} ;
1086
1091
1087
1092
_ . 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 ) ; } ) ;
1089
1094
it ( `sends immediately - ALB - ${ code } ${ msg } ` , ( ) => { test ( albRequest ( ) , code , msg , false ) ; } ) ;
1090
1095
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 ) ; } ) ;
1092
1097
it ( `sends after headers - ALB - ${ code } ${ msg } ` , ( ) => { test ( albRequest ( ) , code , msg , true ) ; } ) ;
1093
1098
it ( `sends after headers - ALB MV - ${ code } ${ msg } ` , ( ) => { test ( albMultiValHeadersRequest ( ) , code , msg , true ) ; } ) ;
1094
1099
} ) ;
0 commit comments