@@ -17,6 +17,7 @@ import {
17
17
getUrlParts ,
18
18
invalidateCDNOnRequest ,
19
19
isExternal ,
20
+ normalizeLocationHeader ,
20
21
revalidateIfRequired ,
21
22
unescapeRegex ,
22
23
} from "@opennextjs/aws/core/routing/util.js" ;
@@ -876,3 +877,66 @@ describe("constructNextUrl", () => {
876
877
expect ( result ) . toBe ( "http://localhost/base/path" ) ;
877
878
} ) ;
878
879
} ) ;
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/about?query=æøå!&" ,
909
+ "http://localhost:3000" ,
910
+ true ,
911
+ ) ;
912
+ expect ( result ) . toBe ( "/about?query=%C3%A6%C3%B8%C3%A5!" ) ;
913
+ } ) ;
914
+
915
+ it ( "should normalize and respect already encoded characters in location header" , ( ) => {
916
+ const result = normalizeLocationHeader (
917
+ "http://localhost:3000/path?query=test%2F%2F" ,
918
+ "http://localhost:3000" ,
919
+ true ,
920
+ ) ;
921
+ expect ( result ) . toBe ( "/path?query=test%2F%2F" ) ;
922
+ } ) ;
923
+
924
+ it ( "should preserve multiple query parameters in location header" , ( ) => {
925
+ const result = normalizeLocationHeader (
926
+ "http://localhost:3000/path?query1=value1&query2=value2&query1=value3&query2=value4&random=randomvalue" ,
927
+ "http://localhost:3000" ,
928
+ true ,
929
+ ) ;
930
+ expect ( result ) . toBe (
931
+ "/path?query1=value1&query1=value3&query2=value2&query2=value4&random=randomvalue" ,
932
+ ) ;
933
+ } ) ;
934
+
935
+ it ( "should return absolute URL correctly when the base URL is different" , ( ) => {
936
+ const result = normalizeLocationHeader (
937
+ "https://example.com/path?query=1" ,
938
+ "http://localhost:3000" ,
939
+ ) ;
940
+ expect ( result ) . toBe ( "https://example.com/path?query=1" ) ;
941
+ } ) ;
942
+ } ) ;
0 commit comments