@@ -327,10 +327,17 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
327327
328328#[ cfg( any( feature = "apigw_rest" , feature = "apigw_http" , feature = "apigw_websockets" ) ) ]
329329fn apigw_path_with_stage ( stage : & Option < String > , path : & str ) -> String {
330- match stage {
331- None => path. into ( ) ,
332- Some ( stage) if stage == "$default" => path. into ( ) ,
333- Some ( stage) => format ! ( "/{stage}{path}" ) ,
330+ let stage = match stage {
331+ None => return path. into ( ) ,
332+ Some ( stage) if stage == "$default" => return path. into ( ) ,
333+ Some ( stage) => stage,
334+ } ;
335+
336+ let prefix = format ! ( "/{stage}/" ) ;
337+ if path. starts_with ( & prefix) {
338+ path. into ( )
339+ } else {
340+ format ! ( "/{stage}{path}" )
334341 }
335342}
336343
@@ -531,7 +538,7 @@ mod tests {
531538 assert_eq ! ( req. method( ) , "GET" ) ;
532539 assert_eq ! (
533540 req. uri( ) ,
534- "https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/test/ hello?name=me"
541+ "https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello?name=me"
535542 ) ;
536543
537544 // Ensure this is an APIGW request
@@ -733,7 +740,7 @@ mod tests {
733740 ) ;
734741 let req = result. expect ( "failed to parse request" ) ;
735742 assert_eq ! ( req. method( ) , "GET" ) ;
736- assert_eq ! ( req. uri( ) , "/test/test/ hello?name=me" ) ;
743+ assert_eq ! ( req. uri( ) , "/test/hello?name=me" ) ;
737744 }
738745
739746 #[ test]
@@ -768,4 +775,25 @@ mod tests {
768775 let url = build_request_uri ( "/path with spaces/and multiple segments" , & HeaderMap :: new ( ) , None , None ) ;
769776 assert_eq ! ( "/path%20with%20spaces/and%20multiple%20segments" , url) ;
770777 }
778+
779+ #[ test]
780+ fn deserializes_apigw_http_request_with_stage_in_path ( ) {
781+ let input = include_str ! ( "../tests/data/apigw_v2_proxy_request_with_stage_in_path.json" ) ;
782+ let result = from_str ( input) ;
783+ assert ! (
784+ result. is_ok( ) ,
785+ "event was not parsed as expected {result:?} given {input}"
786+ ) ;
787+ let req = result. expect ( "failed to parse request" ) ;
788+ assert_eq ! ( "/Prod/my/path" , req. uri( ) . path( ) ) ;
789+ assert_eq ! ( "/Prod/my/path" , req. raw_http_path( ) ) ;
790+ }
791+
792+ #[ test]
793+ fn test_apigw_path_with_stage ( ) {
794+ assert_eq ! ( "/path" , apigw_path_with_stage( & None , "/path" ) ) ;
795+ assert_eq ! ( "/path" , apigw_path_with_stage( & Some ( "$default" . into( ) ) , "/path" ) ) ;
796+ assert_eq ! ( "/Prod/path" , apigw_path_with_stage( & Some ( "Prod" . into( ) ) , "/Prod/path" ) ) ;
797+ assert_eq ! ( "/Prod/path" , apigw_path_with_stage( & Some ( "Prod" . into( ) ) , "/path" ) ) ;
798+ }
771799}
0 commit comments