blob: 72c47595ef9e925faf04533a135fb4ec3db6d9a5 [file] [log] [blame]
Nan Zhou93b443d2022-06-21 17:46:51 +00001#include "http_utility.hpp"
George Liu647b3cd2021-07-05 12:43:56 +08002
Ed Tanousf0b59af2024-03-20 13:38:04 -07003#include <array>
4
Ed Tanous478b7ad2024-07-15 19:11:54 -07005#include <gtest/gtest.h>
George Liu647b3cd2021-07-05 12:43:56 +08006
Nan Zhouad3edd42022-06-21 17:46:44 +00007namespace http_helpers
8{
9namespace
10{
11
Ed Tanous99351cd2022-08-07 16:42:51 -070012TEST(isContentTypeAllowed, PositiveTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000013{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070014 EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
Ed Tanous99351cd2022-08-07 16:42:51 -070015 EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070016 ContentType::OctetStream, false));
17 EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070018 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070019 isContentTypeAllowed("application/json", ContentType::JSON, false));
20 EXPECT_TRUE(
21 isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
22 EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
23 ContentType::HTML, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000024}
25
Ed Tanous99351cd2022-08-07 16:42:51 -070026TEST(isContentTypeAllowed, NegativeTest)
George Liu647b3cd2021-07-05 12:43:56 +080027{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070028 EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
29 ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070030 EXPECT_FALSE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070031 isContentTypeAllowed("application/html", ContentType::JSON, false));
32 EXPECT_FALSE(
33 isContentTypeAllowed("application/json", ContentType::CBOR, false));
34 EXPECT_FALSE(
35 isContentTypeAllowed("application/cbor", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070036 EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070037 ContentType::OctetStream, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000038}
39
Ed Tanous99351cd2022-08-07 16:42:51 -070040TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
Nan Zhoua02b5862022-06-21 17:46:56 +000041{
Ed Tanous99351cd2022-08-07 16:42:51 -070042 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070043 isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
Nan Zhoua02b5862022-06-21 17:46:56 +000044}
45
Ed Tanous99351cd2022-08-07 16:42:51 -070046TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000047{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070048 EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
49 ContentType::OctetStream, true));
Gunnar Millsa3526fe2022-02-02 21:56:44 +000050}
51
Ed Tanous8ece0e42024-01-02 13:16:50 -080052TEST(getPreferredContentType, PositiveTest)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000053{
Ed Tanous99351cd2022-08-07 16:42:51 -070054 std::array<ContentType, 1> contentType{ContentType::HTML};
55 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -080056 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -070057 ContentType::HTML);
58
59 std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080060 EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070061 ContentType::HTML);
62
Ed Tanous463b2932024-07-16 17:02:42 -070063 // String the chrome gives
64 EXPECT_EQ(getPreferredContentType(
65 "text/html,"
66 "application/xhtml+xml,"
67 "application/xml;q=0.9,"
68 "image/avif,"
69 "image/webp,"
70 "image/apng,*/*;q=0.8,"
71 "application/signed-exchange;v=b3;q=0.7",
72 htmlJson),
73 ContentType::HTML);
74
Ed Tanous99351cd2022-08-07 16:42:51 -070075 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
Ed Tanous8ece0e42024-01-02 13:16:50 -080076 EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
Ed Tanous99351cd2022-08-07 16:42:51 -070077 ContentType::HTML);
78
79 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080080 EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
81 cborJson),
82 ContentType::CBOR);
Ed Tanous99351cd2022-08-07 16:42:51 -070083
Ed Tanous8ece0e42024-01-02 13:16:50 -080084 EXPECT_EQ(getPreferredContentType("application/json", cborJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070085 ContentType::JSON);
Ed Tanous8ece0e42024-01-02 13:16:50 -080086 EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
Ed Tanous463b2932024-07-16 17:02:42 -070087
88 // Application types with odd characters
89 EXPECT_EQ(getPreferredContentType(
90 "application/prs.nprend, application/json", cborJson),
91 ContentType::JSON);
92
93 EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
94 cborJson),
95 ContentType::JSON);
96
97 // Q values are ignored, but should parse
98 EXPECT_EQ(getPreferredContentType(
99 "application/rdf+xml;q=0.9, application/json", cborJson),
100 ContentType::JSON);
101 EXPECT_EQ(getPreferredContentType(
102 "application/rdf+xml;q=1, application/json", cborJson),
103 ContentType::JSON);
104 EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
105 ContentType::JSON);
106 EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
107 ContentType::JSON);
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000108}
109
Ed Tanous8ece0e42024-01-02 13:16:50 -0800110TEST(getPreferredContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +0000111{
Ed Tanous99351cd2022-08-07 16:42:51 -0700112 std::array<ContentType, 1> contentType{ContentType::CBOR};
113 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800114 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -0700115 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +0800116}
Nan Zhouad3edd42022-06-21 17:46:44 +0000117} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000118} // namespace http_helpers