blob: 2a1367462846e7cf9190c81232ce01d191eba38e [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Nan Zhou93b443d2022-06-21 17:46:51 +00003#include "http_utility.hpp"
George Liu647b3cd2021-07-05 12:43:56 +08004
Ed Tanousf0b59af2024-03-20 13:38:04 -07005#include <array>
6
Ed Tanous478b7ad2024-07-15 19:11:54 -07007#include <gtest/gtest.h>
George Liu647b3cd2021-07-05 12:43:56 +08008
Nan Zhouad3edd42022-06-21 17:46:44 +00009namespace http_helpers
10{
11namespace
12{
13
Ed Tanous99351cd2022-08-07 16:42:51 -070014TEST(isContentTypeAllowed, PositiveTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000015{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070016 EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
Ed Tanous99351cd2022-08-07 16:42:51 -070017 EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070018 ContentType::OctetStream, false));
19 EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070020 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070021 isContentTypeAllowed("application/json", ContentType::JSON, false));
22 EXPECT_TRUE(
23 isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
24 EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
25 ContentType::HTML, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000026}
27
Ed Tanous99351cd2022-08-07 16:42:51 -070028TEST(isContentTypeAllowed, NegativeTest)
George Liu647b3cd2021-07-05 12:43:56 +080029{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070030 EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
31 ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070032 EXPECT_FALSE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070033 isContentTypeAllowed("application/html", ContentType::JSON, false));
34 EXPECT_FALSE(
35 isContentTypeAllowed("application/json", ContentType::CBOR, false));
36 EXPECT_FALSE(
37 isContentTypeAllowed("application/cbor", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070038 EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070039 ContentType::OctetStream, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000040}
41
Ed Tanous99351cd2022-08-07 16:42:51 -070042TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
Nan Zhoua02b5862022-06-21 17:46:56 +000043{
Ed Tanous99351cd2022-08-07 16:42:51 -070044 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070045 isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
Nan Zhoua02b5862022-06-21 17:46:56 +000046}
47
Ed Tanous99351cd2022-08-07 16:42:51 -070048TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000049{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070050 EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
51 ContentType::OctetStream, true));
Gunnar Millsa3526fe2022-02-02 21:56:44 +000052}
53
Ed Tanous8ece0e42024-01-02 13:16:50 -080054TEST(getPreferredContentType, PositiveTest)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000055{
Ed Tanous99351cd2022-08-07 16:42:51 -070056 std::array<ContentType, 1> contentType{ContentType::HTML};
57 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -080058 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -070059 ContentType::HTML);
60
61 std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080062 EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070063 ContentType::HTML);
64
Ed Tanous463b2932024-07-16 17:02:42 -070065 // String the chrome gives
66 EXPECT_EQ(getPreferredContentType(
67 "text/html,"
68 "application/xhtml+xml,"
69 "application/xml;q=0.9,"
70 "image/avif,"
71 "image/webp,"
72 "image/apng,*/*;q=0.8,"
73 "application/signed-exchange;v=b3;q=0.7",
74 htmlJson),
75 ContentType::HTML);
76
Ed Tanous99351cd2022-08-07 16:42:51 -070077 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
Ed Tanous8ece0e42024-01-02 13:16:50 -080078 EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
Ed Tanous99351cd2022-08-07 16:42:51 -070079 ContentType::HTML);
80
81 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080082 EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
83 cborJson),
84 ContentType::CBOR);
Ed Tanous99351cd2022-08-07 16:42:51 -070085
Ed Tanous80e6e252024-12-11 11:28:39 -080086 EXPECT_EQ(
87 getPreferredContentType("application/json;charset=UTF-8", htmlJson),
88 ContentType::JSON);
89
90 std::array<ContentType, 1> eventStream{ContentType::EventStream};
91 EXPECT_EQ(
92 getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
93 ContentType::EventStream);
94
Ed Tanous8ece0e42024-01-02 13:16:50 -080095 EXPECT_EQ(getPreferredContentType("application/json", cborJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070096 ContentType::JSON);
Ed Tanous8ece0e42024-01-02 13:16:50 -080097 EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
Ed Tanous463b2932024-07-16 17:02:42 -070098
99 // Application types with odd characters
100 EXPECT_EQ(getPreferredContentType(
101 "application/prs.nprend, application/json", cborJson),
102 ContentType::JSON);
103
104 EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
105 cborJson),
106 ContentType::JSON);
107
108 // Q values are ignored, but should parse
109 EXPECT_EQ(getPreferredContentType(
110 "application/rdf+xml;q=0.9, application/json", cborJson),
111 ContentType::JSON);
112 EXPECT_EQ(getPreferredContentType(
113 "application/rdf+xml;q=1, application/json", cborJson),
114 ContentType::JSON);
115 EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
116 ContentType::JSON);
117 EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
118 ContentType::JSON);
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000119}
120
Ed Tanous8ece0e42024-01-02 13:16:50 -0800121TEST(getPreferredContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +0000122{
Ed Tanous99351cd2022-08-07 16:42:51 -0700123 std::array<ContentType, 1> contentType{ContentType::CBOR};
124 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800125 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -0700126 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +0800127}
Ed Tanous276ede52024-08-28 15:57:45 -0700128
129TEST(getPreferredEncoding, PositiveTest)
130{
131 std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
132 EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
133
134 std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
135 EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
136 EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
137
138 EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
139
140 EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
141 Encoding::ZSTD);
142}
143
144TEST(getPreferredEncoding, NegativeTest)
145{
146 std::array<Encoding, 2> contentType{Encoding::GZIP,
147 Encoding::UnencodedBytes};
148 EXPECT_EQ(getPreferredEncoding("noexist", contentType),
149 Encoding::UnencodedBytes);
150
151 std::array<Encoding, 1> contentType2{Encoding::GZIP};
152 EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
153}
154
Nan Zhouad3edd42022-06-21 17:46:44 +0000155} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +0000156} // namespace http_helpers