1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <brillo/mime_utils.h>
6
7 #include <gtest/gtest.h>
8
9 namespace brillo {
10
TEST(MimeUtils,Combine)11 TEST(MimeUtils, Combine) {
12 std::string mime_string = mime::Combine(mime::types::kText, "xml");
13 EXPECT_EQ(mime::text::kXml, mime_string);
14 EXPECT_EQ(
15 "application/json; charset=utf-8",
16 mime::Combine(mime::types::kApplication, "json", {{"charset", "utf-8"}}));
17 }
18
TEST(MimeUtils,Split)19 TEST(MimeUtils, Split) {
20 std::string s1, s2;
21 EXPECT_TRUE(mime::Split(mime::image::kJpeg, &s1, &s2));
22 EXPECT_EQ(mime::types::kImage, s1);
23 EXPECT_EQ("jpeg", s2);
24
25 mime::Parameters parameters;
26 EXPECT_TRUE(
27 mime::Split("application/json;charset=utf-8", &s1, &s2, ¶meters));
28 EXPECT_EQ(mime::types::kApplication, s1);
29 EXPECT_EQ("json", s2);
30 EXPECT_EQ(mime::application::kJson, mime::Combine(s1, s2));
31 EXPECT_EQ(1, parameters.size());
32 EXPECT_EQ(mime::parameters::kCharset, parameters.front().first);
33 EXPECT_EQ("utf-8", parameters.front().second);
34 EXPECT_EQ("application/json; charset=utf-8",
35 mime::Combine(s1, s2, parameters));
36 }
37
TEST(MimeUtils,ExtractParts)38 TEST(MimeUtils, ExtractParts) {
39 mime::Parameters parameters;
40
41 EXPECT_EQ(mime::types::kText, mime::GetType(mime::text::kPlain));
42 EXPECT_EQ("plain", mime::GetSubtype(mime::text::kPlain));
43
44 parameters = mime::GetParameters("text/plain; charset=iso-8859-1;foo=bar");
45 EXPECT_EQ(2, parameters.size());
46 EXPECT_EQ(mime::parameters::kCharset, parameters[0].first);
47 EXPECT_EQ("iso-8859-1", parameters[0].second);
48 EXPECT_EQ("foo", parameters[1].first);
49 EXPECT_EQ("bar", parameters[1].second);
50 }
51
TEST(MimeUtils,AppendRemoveParams)52 TEST(MimeUtils, AppendRemoveParams) {
53 std::string mime_string = mime::AppendParameter(
54 mime::text::kXml, mime::parameters::kCharset, "utf-8");
55 EXPECT_EQ("text/xml; charset=utf-8", mime_string);
56 mime_string = mime::AppendParameter(mime_string, "foo", "bar");
57 EXPECT_EQ("text/xml; charset=utf-8; foo=bar", mime_string);
58 EXPECT_EQ("utf-8",
59 mime::GetParameterValue(mime_string, mime::parameters::kCharset));
60 EXPECT_EQ("bar", mime::GetParameterValue(mime_string, "foo"));
61 EXPECT_EQ("", mime::GetParameterValue(mime_string, "baz"));
62 mime_string = mime::RemoveParameters(mime_string);
63 EXPECT_EQ(mime::text::kXml, mime_string);
64 }
65
66 } // namespace brillo
67