blob: 018a5f45f00c3dab6fc2d0329fc82ac14f6e702f [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 Tanous80e6e252024-12-11 11:28:39 -080084 EXPECT_EQ(
85 getPreferredContentType("application/json;charset=UTF-8", htmlJson),
86 ContentType::JSON);
87
88 std::array<ContentType, 1> eventStream{ContentType::EventStream};
89 EXPECT_EQ(
90 getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
91 ContentType::EventStream);
92
Ed Tanous8ece0e42024-01-02 13:16:50 -080093 EXPECT_EQ(getPreferredContentType("application/json", cborJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070094 ContentType::JSON);
Ed Tanous8ece0e42024-01-02 13:16:50 -080095 EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
Ed Tanous463b2932024-07-16 17:02:42 -070096
97 // Application types with odd characters
98 EXPECT_EQ(getPreferredContentType(
99 "application/prs.nprend, application/json", cborJson),
100 ContentType::JSON);
101
102 EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
103 cborJson),
104 ContentType::JSON);
105
106 // Q values are ignored, but should parse
107 EXPECT_EQ(getPreferredContentType(
108 "application/rdf+xml;q=0.9, application/json", cborJson),
109 ContentType::JSON);
110 EXPECT_EQ(getPreferredContentType(
111 "application/rdf+xml;q=1, application/json", cborJson),
112 ContentType::JSON);
113 EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
114 ContentType::JSON);
115 EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
116 ContentType::JSON);
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000117}
118
Ed Tanous8ece0e42024-01-02 13:16:50 -0800119TEST(getPreferredContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +0000120{
Ed Tanous99351cd2022-08-07 16:42:51 -0700121 std::array<ContentType, 1> contentType{ContentType::CBOR};
122 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800123 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -0700124 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +0800125}
Ed Tanous276ede52024-08-28 15:57:45 -0700126
127TEST(getPreferredEncoding, PositiveTest)
128{
129 std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
130 EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
131
132 std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
133 EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
134 EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
135
136 EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
137
138 EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
139 Encoding::ZSTD);
140}
141
142TEST(getPreferredEncoding, NegativeTest)
143{
144 std::array<Encoding, 2> contentType{Encoding::GZIP,
145 Encoding::UnencodedBytes};
146 EXPECT_EQ(getPreferredEncoding("noexist", contentType),
147 Encoding::UnencodedBytes);
148
149 std::array<Encoding, 1> contentType2{Encoding::GZIP};
150 EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
151}
152
Nan Zhouad3edd42022-06-21 17:46:44 +0000153} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000154} // namespace http_helpers