Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 5 | #include "net/base/mime_util.h" |
| 6 | |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Lei Zhang | de19767 | 2021-04-29 08:11:24 | [diff] [blame] | 9 | #include "base/containers/contains.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 10 | #include "base/strings/string_split.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
servolk | f395553 | 2015-05-16 00:01:59 | [diff] [blame] | 12 | #include "build/build_config.h" |
Yuta Hijikata | 101ed2a | 2020-11-18 07:50:39 | [diff] [blame] | 13 | #include "build/chromeos_buildflags.h" |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 14 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 17 | namespace net { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 18 | |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 19 | using testing::Contains; |
| 20 | |
| 21 | TEST(MimeUtilTest, GetWellKnownMimeTypeFromExtension) { |
| 22 | // String: png\0css |
| 23 | base::FilePath::StringType containsNullByte; |
| 24 | containsNullByte.append(FILE_PATH_LITERAL("png")); |
| 25 | containsNullByte.append(1, FILE_PATH_LITERAL('\0')); |
| 26 | containsNullByte.append(FILE_PATH_LITERAL("css")); |
| 27 | |
| 28 | const struct { |
| 29 | const base::FilePath::StringType extension; |
| 30 | const char* const mime_type; |
| 31 | } tests[] = { |
| 32 | {FILE_PATH_LITERAL("png"), "image/png"}, |
| 33 | {FILE_PATH_LITERAL("PNG"), "image/png"}, |
| 34 | {FILE_PATH_LITERAL("css"), "text/css"}, |
| 35 | {FILE_PATH_LITERAL("pjp"), "image/jpeg"}, |
| 36 | {FILE_PATH_LITERAL("pjpeg"), "image/jpeg"}, |
| 37 | {FILE_PATH_LITERAL("json"), "application/json"}, |
| 38 | {FILE_PATH_LITERAL("js"), "text/javascript"}, |
| 39 | {FILE_PATH_LITERAL("webm"), "video/webm"}, |
| 40 | {FILE_PATH_LITERAL("weba"), "audio/webm"}, |
| 41 | {FILE_PATH_LITERAL("avif"), "image/avif"}, |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 42 | {FILE_PATH_LITERAL("epub"), "application/epub+zip"}, |
| 43 | {FILE_PATH_LITERAL("apk"), "application/vnd.android.package-archive"}, |
| 44 | {FILE_PATH_LITERAL("cer"), "application/x-x509-ca-cert"}, |
| 45 | {FILE_PATH_LITERAL("crt"), "application/x-x509-ca-cert"}, |
| 46 | {FILE_PATH_LITERAL("zip"), "application/zip"}, |
| 47 | {FILE_PATH_LITERAL("ics"), "text/calendar"}, |
| 48 | {FILE_PATH_LITERAL("m3u8"), "application/x-mpegurl"}, |
Eric Lawrence | b822a8e | 2022-02-02 19:03:42 | [diff] [blame] | 49 | {FILE_PATH_LITERAL("csv"), "text/csv"}, |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 50 | {FILE_PATH_LITERAL("not an extension / for sure"), nullptr}, |
| 51 | {containsNullByte, nullptr}}; |
| 52 | |
| 53 | for (const auto& test : tests) { |
| 54 | std::string mime_type; |
| 55 | if (GetWellKnownMimeTypeFromExtension(test.extension, &mime_type)) |
| 56 | EXPECT_EQ(test.mime_type, mime_type); |
| 57 | else |
| 58 | EXPECT_EQ(test.mime_type, nullptr); |
| 59 | } |
| 60 | } |
| 61 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | TEST(MimeUtilTest, ExtensionTest) { |
yawano | c829736 | 2015-05-19 05:47:59 | [diff] [blame] | 63 | // String: png\0css |
| 64 | base::FilePath::StringType containsNullByte; |
| 65 | containsNullByte.append(FILE_PATH_LITERAL("png")); |
| 66 | containsNullByte.append(1, FILE_PATH_LITERAL('\0')); |
| 67 | containsNullByte.append(FILE_PATH_LITERAL("css")); |
| 68 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 69 | const struct { |
yawano | c829736 | 2015-05-19 05:47:59 | [diff] [blame] | 70 | const base::FilePath::StringType extension; |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 71 | const std::vector<std::string> mime_types; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 72 | } tests[] = { |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 73 | {FILE_PATH_LITERAL("png"), {"image/png"}}, |
| 74 | {FILE_PATH_LITERAL("PNG"), {"image/png"}}, |
| 75 | {FILE_PATH_LITERAL("css"), {"text/css"}}, |
| 76 | {FILE_PATH_LITERAL("pjp"), {"image/jpeg"}}, |
| 77 | {FILE_PATH_LITERAL("pjpeg"), {"image/jpeg"}}, |
| 78 | {FILE_PATH_LITERAL("json"), {"application/json"}}, |
| 79 | {FILE_PATH_LITERAL("js"), {"text/javascript"}}, |
| 80 | {FILE_PATH_LITERAL("webm"), {"video/webm"}}, |
| 81 | {FILE_PATH_LITERAL("weba"), {"audio/webm"}}, |
| 82 | {FILE_PATH_LITERAL("avif"), {"image/avif"}}, |
Yuta Hijikata | 101ed2a | 2020-11-18 07:50:39 | [diff] [blame] | 83 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Joel Hockey | b4a0c8b9 | 2022-08-30 04:33:02 | [diff] [blame] | 84 | // These are test cases for testing platform mime types on ChromeOS. |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 85 | {FILE_PATH_LITERAL("epub"), {"application/epub+zip"}}, |
| 86 | {FILE_PATH_LITERAL("apk"), {"application/vnd.android.package-archive"}}, |
Joel Hockey | b4a0c8b9 | 2022-08-30 04:33:02 | [diff] [blame] | 87 | {FILE_PATH_LITERAL("cer"), |
| 88 | { |
| 89 | "application/x-x509-ca-cert", |
| 90 | "application/pkix-cert", // System override for ChromeOS. |
| 91 | }}, |
| 92 | {FILE_PATH_LITERAL("crt"), |
| 93 | { |
| 94 | "application/x-x509-ca-cert", |
| 95 | "application/pkix-cert", // System override for ChromeOS. |
| 96 | }}, |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 97 | {FILE_PATH_LITERAL("zip"), {"application/zip"}}, |
| 98 | {FILE_PATH_LITERAL("ics"), {"text/calendar"}}, |
yawano | c829736 | 2015-05-19 05:47:59 | [diff] [blame] | 99 | #endif |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 100 | {FILE_PATH_LITERAL("m3u8"), |
| 101 | { |
| 102 | "application/x-mpegurl", // Chrome's secondary mapping. |
| 103 | "audio/x-mpegurl", // https://crbug.com/1273061, system override for |
| 104 | // android-arm[64]-test and Linux. Possibly more. |
Joel Hockey | b4a0c8b9 | 2022-08-30 04:33:02 | [diff] [blame] | 105 | "application/vnd.apple.mpegurl", // System override for ChromeOS. |
| 106 | "audio/mpegurl", // System override for mac. |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 107 | }}, |
Eric Lawrence | b822a8e | 2022-02-02 19:03:42 | [diff] [blame] | 108 | {FILE_PATH_LITERAL("csv"), {"text/csv"}}, |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 109 | {FILE_PATH_LITERAL("not an extension / for sure"), {}}, |
| 110 | {containsNullByte, {}} |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 111 | }; |
| 112 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 113 | for (const auto& test : tests) { |
Arthur Sonzogni | b08180bb | 2021-11-24 10:00:44 | [diff] [blame] | 114 | std::string mime_type; |
| 115 | if (GetMimeTypeFromExtension(test.extension, &mime_type)) |
| 116 | EXPECT_THAT(test.mime_types, Contains(mime_type)); |
| 117 | else |
| 118 | EXPECT_TRUE(test.mime_types.empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Asanka Herath | 5a964245 | 2018-11-03 03:19:42 | [diff] [blame] | 122 | // Behavior of GetPreferredExtensionForMimeType() is dependent on the host |
| 123 | // platform since the latter can override the mapping from file extensions to |
| 124 | // MIME types. The tests below would only work if the platform MIME mappings |
| 125 | // don't have mappings for or has an agreeing mapping for each MIME type |
| 126 | // mentioned. |
| 127 | TEST(MimeUtilTest, GetPreferredExtensionForMimeType) { |
| 128 | const struct { |
| 129 | const std::string mime_type; |
| 130 | const base::FilePath::StringType expected_extension; |
| 131 | } kTestCases[] = { |
| 132 | {"application/wasm", FILE_PATH_LITERAL("wasm")}, // Primary |
| 133 | {"application/javascript", FILE_PATH_LITERAL("js")}, // Secondary |
| 134 | {"text/javascript", FILE_PATH_LITERAL("js")}, // Primary |
Asanka Herath | 5a964245 | 2018-11-03 03:19:42 | [diff] [blame] | 135 | {"video/webm", FILE_PATH_LITERAL("webm")}, // Primary |
| 136 | }; |
| 137 | |
| 138 | for (const auto& test : kTestCases) { |
| 139 | base::FilePath::StringType extension; |
| 140 | auto rv = GetPreferredExtensionForMimeType(test.mime_type, &extension); |
| 141 | EXPECT_TRUE(rv); |
| 142 | EXPECT_EQ(test.expected_extension, extension); |
| 143 | } |
| 144 | } |
| 145 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 146 | TEST(MimeUtilTest, FileTest) { |
| 147 | const struct { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 148 | const base::FilePath::CharType* file_path; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 149 | const char* const mime_type; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 150 | bool valid; |
| 151 | } tests[] = { |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 152 | {FILE_PATH_LITERAL("c:\\foo\\bar.css"), "text/css", true}, |
| 153 | {FILE_PATH_LITERAL("c:\\foo\\bar.CSS"), "text/css", true}, |
| 154 | {FILE_PATH_LITERAL("c:\\blah"), "", false}, |
| 155 | {FILE_PATH_LITERAL("/usr/local/bin/mplayer"), "", false}, |
| 156 | {FILE_PATH_LITERAL("/home/foo/bar.css"), "text/css", true}, |
| 157 | {FILE_PATH_LITERAL("/blah."), "", false}, |
| 158 | {FILE_PATH_LITERAL("c:\\blah."), "", false}, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | std::string mime_type; |
| 162 | bool rv; |
| 163 | |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 164 | for (const auto& test : tests) { |
| 165 | rv = GetMimeTypeFromFile(base::FilePath(test.file_path), &mime_type); |
| 166 | EXPECT_EQ(test.valid, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 167 | if (rv) |
Ryan Sleevi | 4625214fa | 2018-05-10 16:42:45 | [diff] [blame] | 168 | EXPECT_EQ(test.mime_type, mime_type); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 172 | TEST(MimeUtilTest, MatchesMimeType) { |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 173 | // MIME types are case insensitive. |
| 174 | EXPECT_TRUE(MatchesMimeType("VIDEO/*", "video/x-mpeg")); |
| 175 | EXPECT_TRUE(MatchesMimeType("video/*", "VIDEO/X-MPEG")); |
| 176 | |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 177 | EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg")); |
| 178 | EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg")); |
[email protected] | 87fbf19 | 2012-09-18 09:43:05 | [diff] [blame] | 179 | EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 180 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg")); |
| 181 | EXPECT_TRUE(MatchesMimeType("application/*+xml", |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 182 | "application/html+xml")); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 183 | EXPECT_TRUE(MatchesMimeType("application/*+xml", "application/+xml")); |
[email protected] | a698f82 | 2013-12-20 01:48:47 | [diff] [blame] | 184 | EXPECT_TRUE(MatchesMimeType("application/*+json", |
| 185 | "application/x-myformat+json")); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 186 | EXPECT_TRUE(MatchesMimeType("aaa*aaa", "aaaaaa")); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 187 | EXPECT_TRUE(MatchesMimeType("*", std::string())); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 188 | EXPECT_FALSE(MatchesMimeType("video/", "video/x-mpeg")); |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 189 | EXPECT_FALSE(MatchesMimeType("VIDEO/", "Video/X-MPEG")); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 190 | EXPECT_FALSE(MatchesMimeType(std::string(), "video/x-mpeg")); |
| 191 | EXPECT_FALSE(MatchesMimeType(std::string(), std::string())); |
| 192 | EXPECT_FALSE(MatchesMimeType("video/x-mpeg", std::string())); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 193 | EXPECT_FALSE(MatchesMimeType("application/*+xml", "application/xml")); |
| 194 | EXPECT_FALSE(MatchesMimeType("application/*+xml", |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 195 | "application/html+xmlz")); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 196 | EXPECT_FALSE(MatchesMimeType("application/*+xml", |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 197 | "applcation/html+xml")); |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 198 | EXPECT_FALSE(MatchesMimeType("aaa*aaa", "aaaaa")); |
[email protected] | ad50e8b | 2012-10-26 03:50:56 | [diff] [blame] | 199 | |
| 200 | EXPECT_TRUE(MatchesMimeType("*", "video/x-mpeg;param=val")); |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 201 | EXPECT_TRUE(MatchesMimeType("*", "Video/X-MPEG;PARAM=VAL")); |
[email protected] | ad50e8b | 2012-10-26 03:50:56 | [diff] [blame] | 202 | EXPECT_TRUE(MatchesMimeType("video/*", "video/x-mpeg;param=val")); |
| 203 | EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg")); |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 204 | EXPECT_FALSE(MatchesMimeType("Video/*;PARAM=VAL", "VIDEO/Mpeg")); |
[email protected] | ad50e8b | 2012-10-26 03:50:56 | [diff] [blame] | 205 | EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/mpeg;param=other")); |
| 206 | EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/mpeg;param=val")); |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 207 | EXPECT_TRUE(MatchesMimeType("Video/*;PARAM=Val", "VIDEO/Mpeg;Param=Val")); |
| 208 | EXPECT_FALSE(MatchesMimeType("Video/*;PARAM=VAL", "VIDEO/Mpeg;Param=Val")); |
[email protected] | ad50e8b | 2012-10-26 03:50:56 | [diff] [blame] | 209 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg", "video/x-mpeg;param=val")); |
| 210 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val", |
| 211 | "video/x-mpeg;param=val")); |
| 212 | EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2", |
| 213 | "video/x-mpeg;param=val")); |
| 214 | EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param2=val2", |
| 215 | "video/x-mpeg;param2=val")); |
| 216 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val", |
| 217 | "video/x-mpeg;param=val;param2=val2")); |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 218 | EXPECT_TRUE(MatchesMimeType("Video/X-Mpeg;Param=Val", |
| 219 | "VIDEO/X-MPEG;PARAM=Val;PARAM2=val2")); |
| 220 | EXPECT_TRUE(MatchesMimeType("Video/X-Mpeg;Param=VAL", |
| 221 | "VIDEO/X-MPEG;PARAM=VAL;PARAM2=val2")); |
| 222 | EXPECT_FALSE(MatchesMimeType("Video/X-Mpeg;Param=val", |
| 223 | "VIDEO/X-MPEG;PARAM=VAL;PARAM2=val2")); |
| 224 | EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param=VAL;param2=val2", |
| 225 | "video/x-mpeg;param=val;param2=val2")); |
[email protected] | ad50e8b | 2012-10-26 03:50:56 | [diff] [blame] | 226 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param2=val2;param=val", |
| 227 | "video/x-mpeg;param=val;param2=val2")); |
| 228 | EXPECT_FALSE(MatchesMimeType("video/x-mpeg;param3=val3;param=val", |
| 229 | "video/x-mpeg;param=val;param2=val2")); |
| 230 | EXPECT_TRUE(MatchesMimeType("video/x-mpeg;param=val ;param2=val2 ", |
| 231 | "video/x-mpeg;param=val;param2=val2")); |
| 232 | |
| 233 | EXPECT_TRUE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val")); |
| 234 | EXPECT_FALSE(MatchesMimeType("*/*;param=val", "video/x-mpeg;param=val2")); |
| 235 | |
| 236 | EXPECT_TRUE(MatchesMimeType("*", "*")); |
| 237 | EXPECT_TRUE(MatchesMimeType("*", "*/*")); |
| 238 | EXPECT_TRUE(MatchesMimeType("*/*", "*/*")); |
| 239 | EXPECT_TRUE(MatchesMimeType("*/*", "*")); |
| 240 | EXPECT_TRUE(MatchesMimeType("video/*", "video/*")); |
| 241 | EXPECT_FALSE(MatchesMimeType("video/*", "*/*")); |
| 242 | EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*")); |
| 243 | EXPECT_TRUE(MatchesMimeType("video/*;param=val", "video/*;param=val")); |
| 244 | EXPECT_FALSE(MatchesMimeType("video/*;param=val", "video/*;param=val2")); |
| 245 | |
| 246 | EXPECT_TRUE(MatchesMimeType("ab*cd", "abxxxcd")); |
| 247 | EXPECT_TRUE(MatchesMimeType("ab*cd", "abx/xcd")); |
| 248 | EXPECT_TRUE(MatchesMimeType("ab/*cd", "ab/xxxcd")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 249 | } |
[email protected] | e9696d57 | 2009-07-17 23:22:55 | [diff] [blame] | 250 | |
Joel Hockey | 1864da8 | 2021-05-15 02:26:12 | [diff] [blame] | 251 | TEST(MimeUtilTest, TestParseMimeType) { |
| 252 | const struct { |
| 253 | std::string type_str; |
| 254 | std::string mime_type; |
| 255 | base::StringPairs params; |
| 256 | } tests[] = { |
| 257 | // Simple tests. |
| 258 | {"image/jpeg", "image/jpeg"}, |
| 259 | {"application/octet-stream;foo=bar;name=\"test.jpg\"", |
| 260 | "application/octet-stream", |
| 261 | {{"foo", "bar"}, {"name", "test.jpg"}}}, |
| 262 | // Quoted string parsing. |
| 263 | {"t/s;name=\"t\\\\est\\\".jpg\"", "t/s", {{"name", "t\\est\".jpg"}}}, |
| 264 | {"t/s;name=\"test.jpg\"", "t/s", {{"name", "test.jpg"}}}, |
| 265 | {"t/s;name=\"test;jpg\"", "t/s", {{"name", "test;jpg"}}}, |
| 266 | // Lenient for no closing quote. |
| 267 | {"t/s;name=\"test.jpg", "t/s", {{"name", "test.jpg"}}}, |
| 268 | {"t/s;name=\"ab\\\"", "t/s", {{"name", "ab\""}}}, |
| 269 | // Strip whitespace from start/end of mime_type. |
| 270 | {" t/s", "t/s"}, |
| 271 | {"t/s ", "t/s"}, |
| 272 | {" t/s ", "t/s"}, |
| 273 | {"t/=", "t/="}, |
| 274 | // Generally ignore whitespace. |
| 275 | {"t/s;a=1;b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 276 | {"t/s ;a=1;b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 277 | {"t/s; a=1;b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 278 | // Special case, include whitespace after param name until equals. |
| 279 | {"t/s;a =1;b=2", "t/s", {{"a ", "1"}, {"b", "2"}}}, |
| 280 | {"t/s;a= 1;b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 281 | {"t/s;a=1 ;b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 282 | {"t/s;a=1; b=2", "t/s", {{"a", "1"}, {"b", "2"}}}, |
| 283 | {"t/s; a = 1;b=2", "t/s", {{"a ", "1"}, {"b", "2"}}}, |
| 284 | // Do not trim whitespace from quoted-string param values. |
| 285 | {"t/s;a=\" 1\";b=2", "t/s", {{"a", " 1"}, {"b", "2"}}}, |
| 286 | {"t/s;a=\"1 \";b=2", "t/s", {{"a", "1 "}, {"b", "2"}}}, |
| 287 | {"t/s;a=\" 1 \";b=2", "t/s", {{"a", " 1 "}, {"b", "2"}}}, |
| 288 | // Ignore incomplete params. |
| 289 | {"t/s;a", "t/s", {}}, |
| 290 | {"t/s;a=", "t/s", {}}, |
| 291 | {"t/s;a=1;", "t/s", {{"a", "1"}}}, |
| 292 | {"t/s;a=1;b", "t/s", {{"a", "1"}}}, |
| 293 | {"t/s;a=1;b=", "t/s", {{"a", "1"}}}, |
| 294 | // Allow empty subtype. |
| 295 | {"t/", "t/", {}}, |
| 296 | {"ts/", "ts/", {}}, |
| 297 | {"t/;", "t/", {}}, |
| 298 | {"t/ s", "t/", {}}, |
| 299 | // Questionable: allow anything as long as there is a slash somewhere. |
| 300 | {"/ts", "/ts", {}}, |
| 301 | {"/s", "/s", {}}, |
| 302 | {"/", "/", {}}, |
Joel Hockey | 1864da8 | 2021-05-15 02:26:12 | [diff] [blame] | 303 | }; |
| 304 | for (const auto& test : tests) { |
| 305 | std::string mime_type; |
| 306 | base::StringPairs params; |
| 307 | EXPECT_TRUE(ParseMimeType(test.type_str, &mime_type, ¶ms)); |
| 308 | EXPECT_EQ(test.mime_type, mime_type); |
| 309 | EXPECT_EQ(test.params, params); |
| 310 | } |
| 311 | for (auto* type_str : { |
| 312 | // Must have slash in mime type. |
| 313 | "", |
| 314 | "ts", |
Joel Hockey | f89e0d4 | 2021-05-15 02:30:29 | [diff] [blame] | 315 | "t / s", |
Joel Hockey | 1864da8 | 2021-05-15 02:26:12 | [diff] [blame] | 316 | }) { |
| 317 | EXPECT_FALSE(ParseMimeType(type_str, nullptr, nullptr)); |
| 318 | } |
| 319 | } |
| 320 | |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 321 | TEST(MimeUtilTest, TestParseMimeTypeWithoutParameter) { |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 322 | std::string nonAscii("application/nonutf8"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 323 | EXPECT_TRUE(ParseMimeTypeWithoutParameter(nonAscii, nullptr, nullptr)); |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 324 | #if BUILDFLAG(IS_WIN) |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 325 | nonAscii.append(base::WideToUTF8(L"\u2603")); |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 326 | #else |
| 327 | nonAscii.append("\u2603"); // unicode snowman |
| 328 | #endif |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 329 | EXPECT_FALSE(ParseMimeTypeWithoutParameter(nonAscii, nullptr, nullptr)); |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 330 | |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 331 | std::string top_level_type; |
| 332 | std::string subtype; |
| 333 | EXPECT_TRUE(ParseMimeTypeWithoutParameter( |
| 334 | "application/mime", &top_level_type, &subtype)); |
| 335 | EXPECT_EQ("application", top_level_type); |
| 336 | EXPECT_EQ("mime", subtype); |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 337 | |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 338 | // Various allowed subtype forms. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 339 | EXPECT_TRUE( |
| 340 | ParseMimeTypeWithoutParameter("application/json", nullptr, nullptr)); |
| 341 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("application/x-suggestions+json", |
| 342 | nullptr, nullptr)); |
| 343 | EXPECT_TRUE( |
| 344 | ParseMimeTypeWithoutParameter("application/+json", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 345 | |
| 346 | // Upper case letters are allowed. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 347 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/mime", nullptr, nullptr)); |
| 348 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("TEXT/mime", nullptr, nullptr)); |
| 349 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("Text/mime", nullptr, nullptr)); |
| 350 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("TeXt/mime", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 351 | |
| 352 | // Experimental types are also considered to be valid. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 353 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("x-video/mime", nullptr, nullptr)); |
| 354 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("X-Video/mime", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 355 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 356 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text", nullptr, nullptr)); |
| 357 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/", nullptr, nullptr)); |
| 358 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/ ", nullptr, nullptr)); |
| 359 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("te(xt/ ", nullptr, nullptr)); |
| 360 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/()plain", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 361 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 362 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video", nullptr, nullptr)); |
| 363 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("x-video/", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 364 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 365 | EXPECT_FALSE( |
| 366 | ParseMimeTypeWithoutParameter("application/a/b/c", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 367 | |
Rob Buis | 6e0f522 | 2018-11-20 12:01:18 | [diff] [blame] | 368 | // Test leading and trailing whitespace |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 369 | EXPECT_TRUE(ParseMimeTypeWithoutParameter(" text/plain", nullptr, nullptr)); |
| 370 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/plain ", nullptr, nullptr)); |
| 371 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text /plain", nullptr, nullptr)); |
| 372 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text/ plain ", nullptr, nullptr)); |
Rob Buis | 6e0f522 | 2018-11-20 12:01:18 | [diff] [blame] | 373 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 374 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("\ttext/plain", nullptr, nullptr)); |
| 375 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/plain\t", nullptr, nullptr)); |
| 376 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text\t/plain", nullptr, nullptr)); |
| 377 | EXPECT_FALSE( |
| 378 | ParseMimeTypeWithoutParameter("text/\tplain ", nullptr, nullptr)); |
Rob Buis | 6e0f522 | 2018-11-20 12:01:18 | [diff] [blame] | 379 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 380 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("\vtext/plain", nullptr, nullptr)); |
| 381 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/plain\v", nullptr, nullptr)); |
| 382 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text\v/plain", nullptr, nullptr)); |
| 383 | EXPECT_FALSE( |
| 384 | ParseMimeTypeWithoutParameter("text/\vplain ", nullptr, nullptr)); |
Rob Buis | 6e0f522 | 2018-11-20 12:01:18 | [diff] [blame] | 385 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 386 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("\rtext/plain", nullptr, nullptr)); |
| 387 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/plain\r", nullptr, nullptr)); |
| 388 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text\r/plain", nullptr, nullptr)); |
| 389 | EXPECT_FALSE( |
| 390 | ParseMimeTypeWithoutParameter("text/\rplain ", nullptr, nullptr)); |
Rob Buis | 6e0f522 | 2018-11-20 12:01:18 | [diff] [blame] | 391 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 392 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("\ntext/plain", nullptr, nullptr)); |
| 393 | EXPECT_TRUE(ParseMimeTypeWithoutParameter("text/plain\n", nullptr, nullptr)); |
| 394 | EXPECT_FALSE(ParseMimeTypeWithoutParameter("text\n/plain", nullptr, nullptr)); |
| 395 | EXPECT_FALSE( |
| 396 | ParseMimeTypeWithoutParameter("text/\nplain ", nullptr, nullptr)); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 397 | } |
| 398 | |
Marcio Caroso | 57cbb3b72 | 2023-03-02 08:39:47 | [diff] [blame] | 399 | class ExtractMIMETypeTestInvalid : public testing::TestWithParam<std::string> { |
| 400 | }; |
| 401 | |
| 402 | INSTANTIATE_TEST_SUITE_P( |
| 403 | InvalidMediaTypes, |
| 404 | ExtractMIMETypeTestInvalid, |
| 405 | testing::Values( |
| 406 | // Fails because it doesn't contain '/'. |
| 407 | "a", |
| 408 | "application", |
| 409 | // Space is not HTTP token code point. |
| 410 | // https://mimesniff.spec.whatwg.org/#http-token-code-point |
| 411 | // U+2003, EM SPACE (UTF-8: E2 80 83). |
| 412 | "\xE2\x80\x83text/html", |
| 413 | "text\xE2\x80\x83/html", |
| 414 | "text / html", |
| 415 | "t e x t / h t m l", |
| 416 | "text\r\n/\nhtml", |
| 417 | "text\n/\nhtml", |
| 418 | ", text/html", |
| 419 | "; text/html")); |
| 420 | |
| 421 | TEST_P(ExtractMIMETypeTestInvalid, MustFail) { |
| 422 | // Parsing is expected to fail. |
| 423 | EXPECT_EQ(absl::nullopt, net::ExtractMimeTypeFromMediaType(GetParam(), true)); |
| 424 | } |
| 425 | |
| 426 | class ExtractMIMETypeTestValid : public testing::TestWithParam<std::string> {}; |
| 427 | |
| 428 | INSTANTIATE_TEST_SUITE_P( |
| 429 | ValidMediaTypes, |
| 430 | ExtractMIMETypeTestValid, |
| 431 | testing::Values("text/html", |
| 432 | "text/html; charset=iso-8859-1", |
| 433 | // Quoted charset parameter. |
| 434 | "text/html; charset=\"quoted\"", |
| 435 | // Multiple parameters. |
| 436 | "text/html; charset=x; foo=bar", |
| 437 | // OWSes are trimmed. |
| 438 | " text/html ", |
| 439 | "\ttext/html \t", |
| 440 | "text/html ; charset=iso-8859-1" |
| 441 | // Non-standard multiple type/subtype listing using a comma |
| 442 | // as a separator is accepted. |
| 443 | "text/html,text/plain", |
| 444 | "text/html , text/plain", |
| 445 | "text/html\t,\ttext/plain", |
| 446 | "text/html,text/plain;charset=iso-8859-1", |
| 447 | "\r\ntext/html\r\n", |
| 448 | "text/html;wow", |
| 449 | "text/html;;;;;;", |
| 450 | "text/html; = = = ")); |
| 451 | |
| 452 | TEST_P(ExtractMIMETypeTestValid, MustSucceed) { |
| 453 | // net::ExtractMIMETypeFromMediaType parses well-formed headers correctly. |
| 454 | EXPECT_EQ("text/html", |
| 455 | net::ExtractMimeTypeFromMediaType(GetParam(), true).value_or("")); |
| 456 | } |
| 457 | |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 458 | TEST(MimeUtilTest, TestIsValidTopLevelMimeType) { |
| 459 | EXPECT_TRUE(IsValidTopLevelMimeType("application")); |
| 460 | EXPECT_TRUE(IsValidTopLevelMimeType("audio")); |
| 461 | EXPECT_TRUE(IsValidTopLevelMimeType("example")); |
Kenichi Ishibashi | 9bbe262 | 2021-03-09 02:17:48 | [diff] [blame] | 462 | EXPECT_TRUE(IsValidTopLevelMimeType("font")); |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 463 | EXPECT_TRUE(IsValidTopLevelMimeType("image")); |
| 464 | EXPECT_TRUE(IsValidTopLevelMimeType("message")); |
| 465 | EXPECT_TRUE(IsValidTopLevelMimeType("model")); |
| 466 | EXPECT_TRUE(IsValidTopLevelMimeType("multipart")); |
| 467 | EXPECT_TRUE(IsValidTopLevelMimeType("text")); |
| 468 | EXPECT_TRUE(IsValidTopLevelMimeType("video")); |
| 469 | |
| 470 | EXPECT_TRUE(IsValidTopLevelMimeType("TEXT")); |
| 471 | EXPECT_TRUE(IsValidTopLevelMimeType("Text")); |
| 472 | EXPECT_TRUE(IsValidTopLevelMimeType("TeXt")); |
| 473 | |
| 474 | EXPECT_FALSE(IsValidTopLevelMimeType("mime")); |
| 475 | EXPECT_FALSE(IsValidTopLevelMimeType("")); |
| 476 | EXPECT_FALSE(IsValidTopLevelMimeType("/")); |
| 477 | EXPECT_FALSE(IsValidTopLevelMimeType(" ")); |
| 478 | |
| 479 | EXPECT_TRUE(IsValidTopLevelMimeType("x-video")); |
| 480 | EXPECT_TRUE(IsValidTopLevelMimeType("X-video")); |
| 481 | |
| 482 | EXPECT_FALSE(IsValidTopLevelMimeType("x-")); |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 485 | TEST(MimeUtilTest, TestGetExtensionsForMimeType) { |
| 486 | const struct { |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 487 | const char* const mime_type; |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 488 | size_t min_expected_size; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 489 | const char* const contained_result; |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 490 | bool no_matches; |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 491 | } tests[] = { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 492 | {"text/plain", 2, "txt"}, |
| 493 | {"text/pl", 0, nullptr, true}, |
| 494 | {"*", 0, nullptr}, |
| 495 | {"", 0, nullptr, true}, |
| 496 | {"message/*", 1, "eml"}, |
| 497 | {"MeSsAge/*", 1, "eml"}, |
| 498 | {"message/", 0, nullptr, true}, |
Wan-Teh Chang | 31b25d0 | 2020-04-17 18:06:49 | [diff] [blame] | 499 | {"image/avif", 1, "avif"}, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 500 | {"image/bmp", 1, "bmp"}, |
| 501 | {"video/*", 6, "mp4"}, |
| 502 | {"video/*", 6, "mpeg"}, |
| 503 | {"audio/*", 6, "oga"}, |
| 504 | {"aUDIo/*", 6, "wav"}, |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 505 | }; |
| 506 | |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 507 | for (const auto& test : tests) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 508 | std::vector<base::FilePath::StringType> extensions; |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 509 | GetExtensionsForMimeType(test.mime_type, &extensions); |
| 510 | ASSERT_LE(test.min_expected_size, extensions.size()); |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 511 | |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 512 | if (test.no_matches) |
| 513 | ASSERT_EQ(0u, extensions.size()); |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 514 | |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 515 | if (test.contained_result) { |
| 516 | // Convert ASCII to FilePath::StringType. |
| 517 | base::FilePath::StringType contained_result( |
| 518 | test.contained_result, |
| 519 | test.contained_result + strlen(test.contained_result)); |
| 520 | |
Jan Wilken Dörrie | 21f9de7 | 2019-06-07 10:41:53 | [diff] [blame] | 521 | bool found = base::Contains(extensions, contained_result); |
eroman | f6ef68261 | 2017-01-31 19:12:02 | [diff] [blame] | 522 | |
| 523 | ASSERT_TRUE(found) << "Must find at least the contained result within " |
| 524 | << test.mime_type; |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 525 | } |
[email protected] | 4a66fa0e | 2012-09-10 06:45:20 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
lukasza | c644f371 | 2016-01-08 19:35:04 | [diff] [blame] | 529 | TEST(MimeUtilTest, TestGenerateMimeMultipartBoundary) { |
| 530 | std::string boundary1 = GenerateMimeMultipartBoundary(); |
| 531 | std::string boundary2 = GenerateMimeMultipartBoundary(); |
| 532 | |
| 533 | // RFC 1341 says: the boundary parameter [...] consists of 1 to 70 characters. |
| 534 | EXPECT_GE(70u, boundary1.size()); |
| 535 | EXPECT_GE(70u, boundary2.size()); |
| 536 | |
| 537 | // RFC 1341 asks to: exercise care to choose a unique boundary. |
| 538 | EXPECT_NE(boundary1, boundary2); |
| 539 | ASSERT_LE(16u, boundary1.size()); |
| 540 | ASSERT_LE(16u, boundary2.size()); |
| 541 | |
| 542 | // Expect that we don't pick '\0' character from the array/string |
| 543 | // where we take the characters from. |
| 544 | EXPECT_EQ(std::string::npos, boundary1.find('\0')); |
| 545 | EXPECT_EQ(std::string::npos, boundary2.find('\0')); |
| 546 | |
| 547 | // Asserts below are not RFC 1341 requirements, but are here |
| 548 | // to improve readability of generated MIME documents and to |
| 549 | // try to preserve some aspects of the old boundary generation code. |
| 550 | EXPECT_EQ("--", boundary1.substr(0, 2)); |
| 551 | EXPECT_EQ("--", boundary2.substr(0, 2)); |
| 552 | EXPECT_NE(std::string::npos, boundary1.find("MultipartBoundary")); |
| 553 | EXPECT_NE(std::string::npos, boundary2.find("MultipartBoundary")); |
| 554 | EXPECT_EQ("--", boundary1.substr(boundary1.size() - 2, 2)); |
| 555 | EXPECT_EQ("--", boundary2.substr(boundary2.size() - 2, 2)); |
| 556 | } |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 557 | |
[email protected] | 591d361a | 2013-05-17 11:38:06 | [diff] [blame] | 558 | TEST(MimeUtilTest, TestAddMultipartValueForUpload) { |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 559 | const char ref_output[] = |
| 560 | "--boundary\r\nContent-Disposition: form-data;" |
| 561 | " name=\"value name\"\r\nContent-Type: content type" |
| 562 | "\r\n\r\nvalue\r\n" |
| 563 | "--boundary\r\nContent-Disposition: form-data;" |
| 564 | " name=\"value name\"\r\n\r\nvalue\r\n" |
| 565 | "--boundary--\r\n"; |
[email protected] | 591d361a | 2013-05-17 11:38:06 | [diff] [blame] | 566 | std::string post_data; |
| 567 | AddMultipartValueForUpload("value name", "value", "boundary", |
| 568 | "content type", &post_data); |
| 569 | AddMultipartValueForUpload("value name", "value", "boundary", |
| 570 | "", &post_data); |
| 571 | AddMultipartFinalDelimiterForUpload("boundary", &post_data); |
| 572 | EXPECT_STREQ(ref_output, post_data.c_str()); |
| 573 | } |
| 574 | |
Ethan Xu | c4740ea | 2018-11-28 21:31:20 | [diff] [blame] | 575 | TEST(MimeUtilTest, TestAddMultipartValueForUploadWithFileName) { |
| 576 | const char ref_output[] = |
| 577 | "--boundary\r\nContent-Disposition: form-data;" |
| 578 | " name=\"value name\"; filename=\"file name\"\r\nContent-Type: content " |
| 579 | "type" |
| 580 | "\r\n\r\nvalue\r\n" |
| 581 | "--boundary\r\nContent-Disposition: form-data;" |
| 582 | " name=\"value name\"; filename=\"file name\"\r\n\r\nvalue\r\n" |
| 583 | "--boundary--\r\n"; |
| 584 | std::string post_data; |
| 585 | AddMultipartValueForUploadWithFileName("value name", "file name", "value", |
| 586 | "boundary", "content type", |
| 587 | &post_data); |
| 588 | AddMultipartValueForUploadWithFileName("value name", "file name", "value", |
| 589 | "boundary", "", &post_data); |
| 590 | AddMultipartFinalDelimiterForUpload("boundary", &post_data); |
| 591 | EXPECT_STREQ(ref_output, post_data.c_str()); |
| 592 | } |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 593 | } // namespace net |