@@ -17,6 +17,7 @@ import {
1717 getUrlParts ,
1818 invalidateCDNOnRequest ,
1919 isExternal ,
20+ normalizeLocationHeader ,
2021 revalidateIfRequired ,
2122 unescapeRegex ,
2223} from "@opennextjs/aws/core/routing/util.js" ;
@@ -876,3 +877,45 @@ describe("constructNextUrl", () => {
876877 expect ( result ) . toBe ( "http://localhost/base/path" ) ;
877878 } ) ;
878879} ) ;
880+
881+ describe ( "normalizeLocationHeader" , ( ) => {
882+ it ( "should normalize relative location header" , ( ) => {
883+ const result = normalizeLocationHeader (
884+ "http://localhost:3000/path" ,
885+ "http://localhost:3000" ,
886+ ) ;
887+ expect ( result ) . toBe ( "/path" ) ;
888+ } ) ;
889+
890+ it ( "should not change absolute location header" , ( ) => {
891+ const result = normalizeLocationHeader (
892+ "https://opennext.js.org/aws" ,
893+ "http://localhost:3000" ,
894+ ) ;
895+ expect ( result ) . toBe ( "https://opennext.js.org/aws" ) ;
896+ } ) ;
897+
898+ it ( "should normalize relative location with query parameters" , ( ) => {
899+ const result = normalizeLocationHeader (
900+ "http://localhost:3000/path?query=1" ,
901+ "http://localhost:3000" ,
902+ ) ;
903+ expect ( result ) . toBe ( "/path?query=1" ) ;
904+ } ) ;
905+
906+ it ( "should normalize and encode special characters in location header" , ( ) => {
907+ const result = normalizeLocationHeader (
908+ "http://localhost:3000/æøå?query=æøå!&" ,
909+ "http://localhost:3000" ,
910+ ) ;
911+ expect ( result ) . toBe ( "/%C3%A6%C3%B8%C3%A5?query=%C3%A6%C3%B8%C3%A5%21" ) ;
912+ } ) ;
913+
914+ it ( "should normalize and respect already encoded characthers in location header" , ( ) => {
915+ const result = normalizeLocationHeader (
916+ "http://localhost:3000/path?query=test%2F%2F" ,
917+ "http://localhost:3000" ,
918+ ) ;
919+ expect ( result ) . toBe ( "/path?query=test%2F%2F" ) ;
920+ } ) ;
921+ } ) ;
0 commit comments