1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#include "http_utility.hpp"
#include <array>
#include <gtest/gtest.h>
namespace http_helpers
{
namespace
{
TEST(isContentTypeAllowed, PositiveTest)
{
EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
ContentType::OctetStream, false));
EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
EXPECT_TRUE(
isContentTypeAllowed("application/json", ContentType::JSON, false));
EXPECT_TRUE(
isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
ContentType::HTML, false));
}
TEST(isContentTypeAllowed, NegativeTest)
{
EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
ContentType::HTML, false));
EXPECT_FALSE(
isContentTypeAllowed("application/html", ContentType::JSON, false));
EXPECT_FALSE(
isContentTypeAllowed("application/json", ContentType::CBOR, false));
EXPECT_FALSE(
isContentTypeAllowed("application/cbor", ContentType::HTML, false));
EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
ContentType::OctetStream, false));
}
TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
{
EXPECT_TRUE(
isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
}
TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
{
EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
ContentType::OctetStream, true));
}
TEST(getPreferredContentType, PositiveTest)
{
std::array<ContentType, 1> contentType{ContentType::HTML};
EXPECT_EQ(
getPreferredContentType("text/html, application/json", contentType),
ContentType::HTML);
std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
ContentType::HTML);
// String the chrome gives
EXPECT_EQ(getPreferredContentType(
"text/html,"
"application/xhtml+xml,"
"application/xml;q=0.9,"
"image/avif,"
"image/webp,"
"image/apng,*/*;q=0.8,"
"application/signed-exchange;v=b3;q=0.7",
htmlJson),
ContentType::HTML);
std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
ContentType::HTML);
std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
cborJson),
ContentType::CBOR);
EXPECT_EQ(
getPreferredContentType("application/json;charset=UTF-8", htmlJson),
ContentType::JSON);
std::array<ContentType, 1> eventStream{ContentType::EventStream};
EXPECT_EQ(
getPreferredContentType("text/event-stream;charset=UTF-8", eventStream),
ContentType::EventStream);
EXPECT_EQ(getPreferredContentType("application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
// Application types with odd characters
EXPECT_EQ(getPreferredContentType(
"application/prs.nprend, application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json",
cborJson),
ContentType::JSON);
// Q values are ignored, but should parse
EXPECT_EQ(getPreferredContentType(
"application/rdf+xml;q=0.9, application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType(
"application/rdf+xml;q=1, application/json", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson),
ContentType::JSON);
EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson),
ContentType::JSON);
}
TEST(getPreferredContentType, NegativeTest)
{
std::array<ContentType, 1> contentType{ContentType::CBOR};
EXPECT_EQ(
getPreferredContentType("text/html, application/json", contentType),
ContentType::NoMatch);
}
TEST(getPreferredEncoding, PositiveTest)
{
std::array<Encoding, 1> encodingsGzip{Encoding::GZIP};
EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP);
std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD};
EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP);
EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD);
EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP);
EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd),
Encoding::ZSTD);
}
TEST(getPreferredEncoding, NegativeTest)
{
std::array<Encoding, 2> contentType{Encoding::GZIP,
Encoding::UnencodedBytes};
EXPECT_EQ(getPreferredEncoding("noexist", contentType),
Encoding::UnencodedBytes);
std::array<Encoding, 1> contentType2{Encoding::GZIP};
EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch);
}
} // namespace
} // namespace http_helpers
|