blob: 6878001e6a19c40c8dfbb2086635c45fdfce701f [file] [log] [blame]
Nan Zhou93b443d2022-06-21 17:46:51 +00001#include "http_utility.hpp"
George Liu647b3cd2021-07-05 12:43:56 +08002
Nan Zhoud5c80ad2022-07-11 01:16:31 +00003#include <gtest/gtest.h> // IWYU pragma: keep
4
5// IWYU pragma: no_include <gtest/gtest-message.h>
6// IWYU pragma: no_include <gtest/gtest-test-part.h>
7// IWYU pragma: no_include "gtest/gtest_pred_impl.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 Tanous99351cd2022-08-07 16:42:51 -070054TEST(getPreferedContentType, 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(
58 getPreferedContentType("text/html, application/json", contentType),
59 ContentType::HTML);
60
61 std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
62 EXPECT_EQ(getPreferedContentType("text/html, application/json", htmlJson),
63 ContentType::HTML);
64
65 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
66 EXPECT_EQ(getPreferedContentType("text/html, application/json", jsonHtml),
67 ContentType::HTML);
68
69 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
70 EXPECT_EQ(
71 getPreferedContentType("application/cbor, application::json", cborJson),
72 ContentType::CBOR);
73
74 EXPECT_EQ(getPreferedContentType("application/json", cborJson),
75 ContentType::JSON);
Ed Tanous4a0e1a02022-09-21 15:28:04 -070076 EXPECT_EQ(getPreferedContentType("*/*", cborJson), ContentType::ANY);
Gunnar Millsa3526fe2022-02-02 21:56:44 +000077}
78
Ed Tanous99351cd2022-08-07 16:42:51 -070079TEST(getPreferedContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000080{
Ed Tanous99351cd2022-08-07 16:42:51 -070081 std::array<ContentType, 1> contentType{ContentType::CBOR};
82 EXPECT_EQ(
83 getPreferedContentType("text/html, application/json", contentType),
84 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +080085}
Nan Zhouad3edd42022-06-21 17:46:44 +000086} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +000087} // namespace http_helpers