blob: d4823da6490d91224ecefc6695ba7b758c24b245 [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
63 std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML};
Ed Tanous8ece0e42024-01-02 13:16:50 -080064 EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml),
Ed Tanous99351cd2022-08-07 16:42:51 -070065 ContentType::HTML);
66
67 std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON};
Ed Tanous8ece0e42024-01-02 13:16:50 -080068 EXPECT_EQ(getPreferredContentType("application/cbor, application::json",
69 cborJson),
70 ContentType::CBOR);
Ed Tanous99351cd2022-08-07 16:42:51 -070071
Ed Tanous8ece0e42024-01-02 13:16:50 -080072 EXPECT_EQ(getPreferredContentType("application/json", cborJson),
Ed Tanous99351cd2022-08-07 16:42:51 -070073 ContentType::JSON);
Ed Tanous8ece0e42024-01-02 13:16:50 -080074 EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY);
Gunnar Millsa3526fe2022-02-02 21:56:44 +000075}
76
Ed Tanous8ece0e42024-01-02 13:16:50 -080077TEST(getPreferredContentType, NegativeTest)
Nan Zhoua02b5862022-06-21 17:46:56 +000078{
Ed Tanous99351cd2022-08-07 16:42:51 -070079 std::array<ContentType, 1> contentType{ContentType::CBOR};
80 EXPECT_EQ(
Ed Tanous8ece0e42024-01-02 13:16:50 -080081 getPreferredContentType("text/html, application/json", contentType),
Ed Tanous99351cd2022-08-07 16:42:51 -070082 ContentType::NoMatch);
George Liu647b3cd2021-07-05 12:43:56 +080083}
Nan Zhouad3edd42022-06-21 17:46:44 +000084} // namespace
Gunnar Millsa3526fe2022-02-02 21:56:44 +000085} // namespace http_helpers