77
88USERVER_NAMESPACE_BEGIN
99
10+ using http::EncodeS3Key;
1011using http::UrlEncode;
12+ using http::UrlEncodePathSegment;
1113
1214namespace {
1315
@@ -22,6 +24,98 @@ TEST(UrlEncode, Latin) {
2224 EXPECT_EQ (str, UrlEncode (str));
2325}
2426
27+ TEST (UrlEncodePathSegment, Empty) { EXPECT_EQ (" " , UrlEncodePathSegment (" " )); }
28+
29+ TEST (UrlEncodePathSegment, Latin) {
30+ constexpr std::string_view str = " SomeText1234567890" ;
31+ EXPECT_EQ (str, UrlEncodePathSegment (str));
32+ }
33+
34+ TEST (UrlEncodePathSegment, UnreservedChars) {
35+ // RFC 3986 unreserved: - _ . ~
36+ constexpr std::string_view str = " file-name_test.txt~backup" ;
37+ EXPECT_EQ (str, UrlEncodePathSegment (str));
38+ }
39+
40+ TEST (UrlEncodePathSegment, PathSafeSpecialChars) {
41+ // Additional path-safe: $ & , : = @
42+ constexpr std::string_view str = " price$100&tax:50=total@rate" ;
43+ EXPECT_EQ (str, UrlEncodePathSegment (str));
44+ }
45+
46+ TEST (UrlEncodePathSegment, SpacesAndSpecial) {
47+ constexpr std::string_view str = " file with spaces.txt" ;
48+ EXPECT_EQ (" file%20with%20spaces.txt" , UrlEncodePathSegment (str));
49+ }
50+
51+ TEST (UrlEncodePathSegment, SlashShouldNotBeEncoded) {
52+ // Slash should be encoded in path segment context
53+ constexpr std::string_view str = " folder/file" ;
54+ EXPECT_EQ (" folder%2Ffile" , UrlEncodePathSegment (str));
55+ }
56+
57+ TEST (UrlEncodePathSegment, QueryChars) {
58+ // ? and # should be encoded
59+ constexpr std::string_view str = " file?query#fragment" ;
60+ EXPECT_EQ (" file%3Fquery%23fragment" , UrlEncodePathSegment (str));
61+ }
62+
63+ TEST (EncodeS3Key, Empty) { EXPECT_EQ (" " , EncodeS3Key (" " )); }
64+
65+ TEST (EncodeS3Key, SimpleKey) {
66+ constexpr std::string_view key = " simple-key.txt" ;
67+ EXPECT_EQ (key, EncodeS3Key (key));
68+ }
69+
70+ TEST (EncodeS3Key, WithSpaces) {
71+ constexpr std::string_view key = " file with spaces.txt" ;
72+ EXPECT_EQ (" file%20with%20spaces.txt" , EncodeS3Key (key));
73+ }
74+
75+ TEST (EncodeS3Key, WithSlashes) {
76+ constexpr std::string_view key = " folder/subfolder/file.txt" ;
77+ EXPECT_EQ (" folder/subfolder/file.txt" , EncodeS3Key (key));
78+ }
79+
80+ TEST (EncodeS3Key, WithSlashesAndSpaces) {
81+ constexpr std::string_view key = " folder/file with spaces.txt" ;
82+ EXPECT_EQ (" folder/file%20with%20spaces.txt" , EncodeS3Key (key));
83+ }
84+
85+ TEST (EncodeS3Key, ComplexKey) {
86+ constexpr std::string_view key = " path/to/my file (copy).txt" ;
87+ // Parentheses are encoded as they're not in our path-safe set (RFC 3986 unreserved + S3-safe)
88+ EXPECT_EQ (" path/to/my%20file%20%28copy%29.txt" , EncodeS3Key (key));
89+ }
90+
91+ TEST (EncodeS3Key, SpecialCharsInSegments) {
92+ // Path-safe chars should be preserved, others encoded
93+ constexpr std::string_view key = " folder/file-name_with.dots~and$symbols&commas,colons:equals=at@sign" ;
94+ EXPECT_EQ (" folder/file-name_with.dots~and$symbols&commas,colons:equals=at@sign" , EncodeS3Key (key));
95+ }
96+
97+ TEST (EncodeS3Key, LeadingSlash) {
98+ // Leading slash should be preserved as S3 path separator
99+ constexpr std::string_view key = " /leading/slash.txt" ;
100+ EXPECT_EQ (" /leading/slash.txt" , EncodeS3Key (key));
101+ }
102+
103+ TEST (EncodeS3Key, MultipleSpacesInPath) {
104+ constexpr std::string_view key = " folder with spaces/file with spaces.txt" ;
105+ EXPECT_EQ (" folder%20with%20spaces/file%20with%20spaces.txt" , EncodeS3Key (key));
106+ }
107+
108+ TEST (EncodeS3Key, UnicodeCharacters) {
109+ // Unicode characters should be percent-encoded
110+ // UTF-8 encoding of Cyrillic 'ф' (U+0444) is D1 84, 'а' (U+0430) is D0 B0, 'й' (U+0439) is D0 B9
111+ constexpr std::string_view key = " folder/файл.txt" ;
112+ auto result = EncodeS3Key (key);
113+ // Check that Cyrillic characters are percent-encoded
114+ EXPECT_EQ (result, " folder/%D1%84%D0%B0%D0%B9%D0%BB.txt" );
115+ EXPECT_TRUE (result.find (" folder/" ) != std::string::npos);
116+ EXPECT_TRUE (result.find (" .txt" ) != std::string::npos);
117+ }
118+
25119TEST (UrlEncode, Special) {
26120 constexpr std::string_view str = " Text with spaces,?&=" ;
27121 EXPECT_EQ (" Text%20with%20spaces%2C%3F%26%3D" , UrlEncode (str));
0 commit comments