@@ -6,8 +6,9 @@ import { lf } from '../function.js';
66import { LambdaRequest } from '../request.js' ;
77import { LambdaHttpRequest } from '../request.http.js' ;
88import { LambdaHttpResponse } from '../response.http.js' ;
9- import { AlbExample , ApiGatewayExample , CloudfrontExample } from './examples.js' ;
9+ import { AlbExample , ApiGatewayExample , clone , CloudfrontExample } from './examples.js' ;
1010import { fakeLog } from './log.js' ;
11+ import { HttpMethods } from '../router.js' ;
1112
1213function assertAlbResult ( x : unknown ) : asserts x is ALBResult { }
1314function assertCloudfrontResult ( x : unknown ) : asserts x is CloudFrontResultResponse { }
@@ -29,8 +30,71 @@ o.spec('LambdaWrap', () => {
2930 fakeLog . logs = [ ] ;
3031 } ) ;
3132
33+ o ( 'should handle middleware' , async ( ) => {
34+ const fn = lf . http ( fakeLog ) ;
35+ fn . router . all ( '*' , ( req ) : LambdaHttpResponse | void => {
36+ if ( req . path . includes ( 'fail' ) ) return new LambdaHttpResponse ( 500 , 'Failed' ) ;
37+ } ) ;
38+ fn . router . get ( '/v1/ping' , ( ) => new LambdaHttpResponse ( 200 , 'Ok' ) ) ;
39+
40+ const newReq = clone ( ApiGatewayExample ) ;
41+ newReq . path = '/v1/ping' ;
42+ const ret = await new Promise ( ( resolve ) => fn ( newReq , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
43+ assertAlbResult ( ret ) ;
44+
45+ o ( ret . statusCode ) . equals ( 200 ) ;
46+
47+ newReq . path = '/v1/ping/fail' ;
48+ const retB = await new Promise ( ( resolve ) => fn ( newReq , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
49+ assertAlbResult ( retB ) ;
50+ o ( retB . statusCode ) . equals ( 500 ) ;
51+ } ) ;
52+
53+ o ( 'should wrap all http methods' , async ( ) => {
54+ const fn = lf . http ( fakeLog ) ;
55+ const methods : string [ ] = [ ] ;
56+
57+ function bind ( r : string ) {
58+ return ( ) : LambdaHttpResponse => {
59+ methods . push ( r . toUpperCase ( ) ) ;
60+ return new LambdaHttpResponse ( 200 , 'Ok' ) ;
61+ } ;
62+ }
63+ fn . router . get ( '*' , bind ( 'get' ) ) ;
64+ fn . router . delete ( '*' , bind ( 'delete' ) ) ;
65+ fn . router . head ( '*' , bind ( 'head' ) ) ;
66+ fn . router . options ( '*' , bind ( 'options' ) ) ;
67+ fn . router . post ( '*' , bind ( 'post' ) ) ;
68+ fn . router . patch ( '*' , bind ( 'patch' ) ) ;
69+ fn . router . put ( '*' , bind ( 'put' ) ) ;
70+
71+ const headers : Record < HttpMethods , number > = {
72+ DELETE : 1 ,
73+ ALL : 0 ,
74+ GET : 1 ,
75+ OPTIONS : 1 ,
76+ HEAD : 1 ,
77+ POST : 1 ,
78+ PATCH : 1 ,
79+ PUT : 1 ,
80+ } ;
81+
82+ const requests = Object . entries ( headers )
83+ . filter ( ( f ) => f [ 1 ] === 1 )
84+ . map ( ( f ) => f [ 0 ] ) ;
85+
86+ for ( const req of requests ) {
87+ const newReq = clone ( ApiGatewayExample ) ;
88+ newReq . httpMethod = req ;
89+ await new Promise ( ( resolve ) => fn ( newReq , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
90+ }
91+
92+ o ( methods ) . deepEquals ( requests ) ;
93+ } ) ;
94+
3295 o ( 'should log a metalog at the end of the request' , async ( ) => {
33- const fn = lf . http ( fakeLambda , fakeLog ) ;
96+ const fn = lf . http ( fakeLog ) ;
97+ fn . router . get ( '/v1/tiles/:tileSet/:projection/:z/:x/:y.json' , fakeLambda ) ;
3498 await new Promise ( ( resolve ) => fn ( AlbExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
3599
36100 o ( fakeLog . logs . length ) . equals ( 1 ) ;
@@ -39,15 +103,16 @@ o.spec('LambdaWrap', () => {
39103 o ( firstLog [ '@type' ] ) . equals ( 'report' ) ;
40104 o ( typeof firstLog [ 'duration' ] === 'number' ) . equals ( true ) ;
41105 o ( firstLog [ 'status' ] ) . equals ( 200 ) ;
42- o ( firstLog [ 'method' ] ) . equals ( 'POST ' ) ;
106+ o ( firstLog [ 'method' ] ) . equals ( 'GET ' ) ;
43107 o ( firstLog [ 'path' ] ) . equals ( '/v1/tiles/aerial/EPSG:3857/6/3/41.json' ) ;
44108 o ( firstLog [ 'id' ] ) . equals ( requests [ 0 ] . id ) ;
45109 o ( firstLog [ 'setTest' ] ) . equals ( requests [ 0 ] . id ) ;
46110 o ( firstLog [ 'correlationId' ] ) . equals ( requests [ 0 ] . correlationId ) ;
47111 } ) ;
48112
49113 o ( 'should respond to alb events' , async ( ) => {
50- const fn = lf . http ( fakeLambda , fakeLog ) ;
114+ const fn = lf . http ( fakeLog ) ;
115+ fn . router . get ( '/v1/tiles/:tileSet/:projection/:z/:x/:y.json' , fakeLambda ) ;
51116 const ret = await new Promise ( ( resolve ) => fn ( AlbExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
52117
53118 assertAlbResult ( ret ) ;
@@ -64,7 +129,8 @@ o.spec('LambdaWrap', () => {
64129 } ) ;
65130
66131 o ( 'should respond to cloudfront events' , async ( ) => {
67- const fn = lf . http ( fakeLambda ) ;
132+ const fn = lf . http ( fakeLog ) ;
133+ fn . router . get ( '/v1/tiles/:tileSet/:projection/:z/:x/:y.json' , fakeLambda ) ;
68134 const ret = await new Promise ( ( resolve ) => fn ( CloudfrontExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
69135
70136 assertCloudfrontResult ( ret ) ;
@@ -79,7 +145,8 @@ o.spec('LambdaWrap', () => {
79145 } ) ;
80146
81147 o ( 'should respond to api gateway events' , async ( ) => {
82- const fn = lf . http ( fakeLambda ) ;
148+ const fn = lf . http ( fakeLog ) ;
149+ fn . router . get ( '/v1/tiles/:tileSet/:projection/:z/:x/:y.json' , fakeLambda ) ;
83150 const ret = await new Promise ( ( resolve ) => fn ( ApiGatewayExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
84151
85152 assertsApiGatewayResult ( ret ) ;
@@ -94,8 +161,9 @@ o.spec('LambdaWrap', () => {
94161 } ) ;
95162
96163 o ( 'should handle http exceptions' , async ( ) => {
97- const fn = lf . http ( ( ) => {
98- throw new Error ( 'Fake' ) ;
164+ const fn = lf . http ( fakeLog ) ;
165+ fn . router . all ( '*' , ( ) => {
166+ throw new Error ( 'Error' ) ;
99167 } ) ;
100168 const ret = await new Promise ( ( resolve ) => fn ( ApiGatewayExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
101169
@@ -152,7 +220,8 @@ o.spec('LambdaWrap', () => {
152220 o ( 'should disable "server" header if no server name set' , async ( ) => {
153221 const serverName = lf . ServerName ;
154222 lf . ServerName = null ;
155- const fn = lf . http ( fakeLambda ) ;
223+ const fn = lf . http ( ) ;
224+ fn . router . all ( '*' , fakeLambda ) ;
156225 const ret = await new Promise ( ( resolve ) => fn ( ApiGatewayExample , fakeContext , ( a , b ) => resolve ( b ) ) ) ;
157226
158227 lf . ServerName = serverName ;
0 commit comments