@@ -5,20 +5,19 @@ const pathParams = require(`${APP_ROOT}/src/pathParametersCache`);
55const expect = require ( 'chai' ) . expect ;
66
77describe ( 'Configuring path parameter caching' , ( ) => {
8- let serverless , cacheSettings ;
8+ let serverless ;
99 let serviceName = 'cat-api' , stage = 'dev' ;
1010
1111 describe ( 'when there are no endpoints' , ( ) => {
1212 before ( ( ) => {
1313 serverless = given . a_serverless_instance ( serviceName )
1414 . withApiGatewayCachingConfig ( true , '0.5' , 45 )
1515 . forStage ( stage ) ;
16- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
1716 } ) ;
1817
1918 it ( 'should do nothing to the serverless instance' , ( ) => {
2019 let stringified = JSON . stringify ( serverless ) ;
21- when_configuring_path_parameters ( cacheSettings , serverless ) ;
20+ when_configuring_path_parameters ( serverless ) ;
2221 let stringifiedAfter = JSON . stringify ( serverless ) ;
2322 expect ( stringified ) . to . equal ( stringifiedAfter ) ;
2423 } ) ;
@@ -32,12 +31,11 @@ describe('Configuring path parameter caching', () => {
3231 . withApiGatewayCachingConfig ( true , '0.5' , 45 )
3332 . forStage ( stage )
3433 . withFunction ( endpoint ) ;
35- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
3634 } ) ;
3735
3836 it ( 'should do nothing to the serverless instance' , ( ) => {
3937 let stringified = JSON . stringify ( serverless ) ;
40- when_configuring_path_parameters ( cacheSettings , serverless ) ;
38+ when_configuring_path_parameters ( serverless ) ;
4139 let stringifiedAfter = JSON . stringify ( serverless ) ;
4240 expect ( stringified ) . to . equal ( stringifiedAfter ) ;
4341 } ) ;
@@ -61,9 +59,8 @@ describe('Configuring path parameter caching', () => {
6159 . forStage ( stage )
6260 . withFunction ( functionWithCaching )
6361 . withFunction ( functionWithoutCaching ) ;
64- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
6562
66- when_configuring_path_parameters ( cacheSettings , serverless ) ;
63+ when_configuring_path_parameters ( serverless ) ;
6764 } ) ;
6865
6966 describe ( 'on the method corresponding with the endpoint with cache key parameters' , ( ) => {
@@ -156,8 +153,7 @@ describe('Configuring path parameter caching', () => {
156153 method = serverless . getMethodResourceForFunction ( functionWithCachingName ) ;
157154 method . Properties . RequestParameters [ `method.${ cacheKeyParameters [ 0 ] . name } ` ] = isRequired ;
158155
159- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
160- when_configuring_path_parameters ( cacheSettings , serverless )
156+ when_configuring_path_parameters ( serverless )
161157 } ) ;
162158
163159 it ( 'should keep configuration' , ( ) => {
@@ -191,9 +187,7 @@ describe('Configuring path parameter caching', () => {
191187 . withFunction ( firstFunctionWithCaching )
192188 . withFunction ( secondFunctionWithCaching ) ;
193189
194- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
195-
196- when_configuring_path_parameters ( cacheSettings , serverless ) ;
190+ when_configuring_path_parameters ( serverless ) ;
197191 } ) ;
198192
199193 describe ( 'on the method corresponding with the first endpoint with cache key parameters' , ( ) => {
@@ -267,26 +261,25 @@ describe('Configuring path parameter caching', () => {
267261 } ) ;
268262 } ) ;
269263 } ) ;
264+
270265 describe ( 'when there are two endpoints with a cache key parameter on the same function' , ( ) => {
271- let method , functionName , firstEndpointCacheKeyParameters , secondEndpointCacheKeyParameters ;
272- before ( ( ) => {
273- functionName = 'catpaw' ;
274-
275- firstEndpointCacheKeyParameters = [ { name : 'request.path.pawId' } ] ;
276- secondEndpointCacheKeyParameters = [ { name : 'request.path.pawId' } ] ;
266+ let method , functionName , firstEndpointCacheKeyParameters , secondEndpointCacheKeyParameters ;
267+ before ( ( ) => {
268+ functionName = 'catpaw' ;
277269
278- let firstFunctionWithCaching = given . a_serverless_function ( functionName )
279- . withHttpEndpoint ( 'get' , '/cat/paw/{pawId}' , { enabled : true , cacheKeyParameters : firstEndpointCacheKeyParameters } )
280- . withHttpEndpoint ( 'delete' , '/cat/paw/{pawId}' , { enabled : true , cacheKeyParameters : secondEndpointCacheKeyParameters } ) ;
281- serverless = given . a_serverless_instance ( serviceName )
282- . withApiGatewayCachingConfig ( true , '0.5' , 45 )
283- . forStage ( stage )
284- . withFunction ( firstFunctionWithCaching )
285-
286- cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
270+ firstEndpointCacheKeyParameters = [ { name : 'request.path.pawId' } ] ;
271+ secondEndpointCacheKeyParameters = [ { name : 'request.path.pawId' } ] ;
287272
288- when_configuring_path_parameters ( cacheSettings , serverless ) ;
289- } ) ;
273+ let firstFunctionWithCaching = given . a_serverless_function ( functionName )
274+ . withHttpEndpoint ( 'get' , '/cat/paw/{pawId}' , { enabled : true , cacheKeyParameters : firstEndpointCacheKeyParameters } )
275+ . withHttpEndpoint ( 'delete' , '/cat/paw/{pawId}' , { enabled : true , cacheKeyParameters : secondEndpointCacheKeyParameters } ) ;
276+ serverless = given . a_serverless_instance ( serviceName )
277+ . withApiGatewayCachingConfig ( true , '0.5' , 45 )
278+ . forStage ( stage )
279+ . withFunction ( firstFunctionWithCaching )
280+
281+ when_configuring_path_parameters ( serverless ) ;
282+ } ) ;
290283
291284 describe ( 'on the method corresponding with the first endpoint with cache key parameters' , ( ) => {
292285 before ( ( ) => {
@@ -324,7 +317,7 @@ describe('Configuring path parameter caching', () => {
324317 } ) ;
325318
326319 describe ( 'on the method corresponding with the second endpoint with cache key parameters' , ( ) => {
327- before ( ( ) => {
320+ before ( ( ) => {
328321 method = serverless . getMethodResourceForMethodName ( "ApiGatewayMethodCatPawPawidVarDelete" ) ;
329322 } ) ;
330323
@@ -349,7 +342,60 @@ describe('Configuring path parameter caching', () => {
349342 it ( 'should set integration cache key parameters' , ( ) => {
350343 for ( let parameter of secondEndpointCacheKeyParameters ) {
351344 expect ( method . Properties . Integration . CacheKeyParameters )
352- . to . include ( `method.${ parameter . name } ` ) ;
345+ . to . include ( `method.${ parameter . name } ` ) ;
346+ }
347+ } ) ;
348+
349+ it ( 'should set a cache namespace' , ( ) => {
350+ expect ( method . Properties . Integration . CacheNamespace ) . to . exist ;
351+ } ) ;
352+ } ) ;
353+ } ) ;
354+
355+ describe ( 'when an http event path contains the \'+\' special character' , ( ) => {
356+ let cacheKeyParameters , functionWithCachingName ;
357+ before ( ( ) => {
358+ functionWithCachingName = 'get-cat-by-paw-id' ;
359+ cacheKeyParameters = [ { name : 'request.path.pawId' } , { name : 'request.header.Accept-Language' } ] ;
360+
361+ let functionWithCaching = given . a_serverless_function ( functionWithCachingName )
362+ . withHttpEndpoint ( 'get' , '/cat/{pawId+}' , { enabled : true , cacheKeyParameters } ) ;
363+
364+ serverless = given . a_serverless_instance ( serviceName )
365+ . withApiGatewayCachingConfig ( true , '0.5' , 45 )
366+ . forStage ( stage )
367+ . withFunction ( functionWithCaching ) ;
368+
369+ when_configuring_path_parameters ( serverless )
370+ } ) ;
371+
372+ describe ( 'on the corresponding method' , ( ) => {
373+ before ( ( ) => {
374+ method = serverless . getMethodResourceForFunction ( functionWithCachingName ) ;
375+ } ) ;
376+
377+ it ( 'should configure cache key parameters as request parameters' , ( ) => {
378+ for ( let parameter of cacheKeyParameters ) {
379+ expect ( method . Properties . RequestParameters )
380+ . to . deep . include ( {
381+ [ `method.${ parameter . name } ` ] : { }
382+ } ) ;
383+ }
384+ } ) ;
385+
386+ it ( 'should set integration request parameters' , ( ) => {
387+ for ( let parameter of cacheKeyParameters ) {
388+ expect ( method . Properties . Integration . RequestParameters )
389+ . to . deep . include ( {
390+ [ `integration.${ parameter . name } ` ] : `method.${ parameter . name } `
391+ } ) ;
392+ }
393+ } ) ;
394+
395+ it ( 'should set integration cache key parameters' , ( ) => {
396+ for ( let parameter of cacheKeyParameters ) {
397+ expect ( method . Properties . Integration . CacheKeyParameters )
398+ . to . include ( `method.${ parameter . name } ` ) ;
353399 }
354400 } ) ;
355401
@@ -360,6 +406,7 @@ describe('Configuring path parameter caching', () => {
360406 } ) ;
361407} ) ;
362408
363- const when_configuring_path_parameters = ( settings , serverless ) => {
364- return pathParams . addPathParametersCacheConfig ( settings , serverless ) ;
409+ const when_configuring_path_parameters = ( serverless ) => {
410+ let cacheSettings = new ApiGatewayCachingSettings ( serverless ) ;
411+ return pathParams . addPathParametersCacheConfig ( cacheSettings , serverless ) ;
365412}
0 commit comments