blob: d2060c16fbfa1d9a4056c43c9361c096cb1abbac [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
Nan Zhoud5c80ad2022-07-11 01:16:31 +00005#include <gtest/gtest.h> // IWYU pragma: keep
6
7// IWYU pragma: no_include <gtest/gtest-message.h>
8// IWYU pragma: no_include <gtest/gtest-test-part.h>
9// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
George Liu647b3cd2021-07-05 12:43:56 +080010
Nan Zhouad3edd42022-06-21 17:46:44 +000011namespace http_helpers
12{
13namespace
14{
15
Ed Tanous99351cd2022-08-07 16:42:51 -070016TEST(isContentTypeAllowed, PositiveTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000017{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070018 EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true));
Ed Tanous99351cd2022-08-07 16:42:51 -070019 EXPECT_TRUE(isContentTypeAllowed("application/octet-stream",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070020 ContentType::OctetStream, false));
21 EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070022 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070023 isContentTypeAllowed("application/json", ContentType::JSON, false));
24 EXPECT_TRUE(
25 isContentTypeAllowed("application/cbor", ContentType::CBOR, false));
26 EXPECT_TRUE(isContentTypeAllowed("application/json, text/html",
27 ContentType::HTML, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000028}
29
Ed Tanous99351cd2022-08-07 16:42:51 -070030TEST(isContentTypeAllowed, NegativeTest)
George Liu647b3cd2021-07-05 12:43:56 +080031{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070032 EXPECT_FALSE(isContentTypeAllowed("application/octet-stream",
33 ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070034 EXPECT_FALSE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070035 isContentTypeAllowed("application/html", ContentType::JSON, false));
36 EXPECT_FALSE(
37 isContentTypeAllowed("application/json", ContentType::CBOR, false));
38 EXPECT_FALSE(
39 isContentTypeAllowed("application/cbor", ContentType::HTML, false));
Ed Tanous99351cd2022-08-07 16:42:51 -070040 EXPECT_FALSE(isContentTypeAllowed("application/json, text/html",
Ed Tanous4a0e1a02022-09-21 15:28:04 -070041 ContentType::OctetStream, false));
Nan Zhoua02b5862022-06-21 17:46:56 +000042}
43
Ed Tanous99351cd2022-08-07 16:42:51 -070044TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue)
Nan Zhoua02b5862022-06-21 17:46:56 +000045{
Ed Tanous99351cd2022-08-07 16:42:51 -070046 EXPECT_TRUE(
Ed Tanous4a0e1a02022-09-21 15:28:04 -070047 isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true));
Nan Zhoua02b5862022-06-21 17:46:56 +000048}
49
Ed Tanous99351cd2022-08-07 16:42:51 -070050TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000051{
Ed Tanous4a0e1a02022-09-21 15:28:04 -070052 EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8",
53 ContentType::OctetStream, true));
Gunnar Millsa3526fe2022-02-02 21:56:44 +000054}
55
Ed Tanous8ece0e42024-01-02 13:16:50 -080056TEST(getPreferredContentType, PositiveTest)
Gunnar Millsa3526fe2022-02-02 21:56:44 +000057{
Ed Tanous99351cd2022-08-07 16:42:51 -070058 std::array<ContentType, 1> contentType{ContentType::HTML};
59 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -080060 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -070061 ContentType::HTML);
62
63 std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080064 EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070065 ContentType::HTML);
66
67 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
Ed Tanous8ece0e42024-01-02 13:16:50 -080068 EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
Ed Tanous99351cd2022-08-07 16:42:51 -070069 ContentType::HTML);
70
71 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080072 EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
73 cborJson),
74 ContentType::CBOR);
Ed Tanous99351cd2022-08-07 16:42:51 -070075
Ed Tanous8ece0e42024-01-02 13:16:50 -080076 EXPECT_EQ(getPreferredContentType("application/json", cborJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070077 ContentType::JSON);
Ed Tanous8ece0e42024-01-02 13:16:50 -080078 EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
Gunnar Millsa3526fe2022-02-02 21:56:44 +000079}
80
Ed Tanous8ece0e42024-01-02 13:16:50 -080081TEST(getPreferredContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000082{
Ed Tanous99351cd2022-08-07 16:42:51 -070083 std::array<ContentType, 1> contentType{ContentType::CBOR};
84 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -080085 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -070086 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +080087}
Nan Zhouad3edd42022-06-21 17:46:44 +000088} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +000089} // namespace http_helpers