@@ -327,10 +327,17 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
327
327
328
328
#[ cfg( any( feature = "apigw_rest" , feature = "apigw_http" , feature = "apigw_websockets" ) ) ]
329
329
fn 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}" )
334
341
}
335
342
}
336
343
@@ -768,4 +775,25 @@ mod tests {
768
775
let url = build_request_uri ( "/path with spaces/and multiple segments" , & HeaderMap :: new ( ) , None , None ) ;
769
776
assert_eq ! ( "/path%20with%20spaces/and%20multiple%20segments" , url) ;
770
777
}
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
+ }
771
799
}
0 commit comments