Nan Zhou | 93b443d | 2022-06-21 17:46:51 +0000 | [diff] [blame] | 1 | #include "http_utility.hpp" |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 2 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 3 | #include <array> |
| 4 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 5 | #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 Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 10 | |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 11 | namespace http_helpers |
| 12 | { |
| 13 | namespace |
| 14 | { |
| 15 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 16 | TEST(isContentTypeAllowed, PositiveTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 17 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 18 | EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 19 | EXPECT_TRUE(isContentTypeAllowed("application/octet-stream", |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 20 | ContentType::OctetStream, false)); |
| 21 | EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 22 | EXPECT_TRUE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 23 | 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 Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 30 | TEST(isContentTypeAllowed, NegativeTest) |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 31 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 32 | EXPECT_FALSE(isContentTypeAllowed("application/octet-stream", |
| 33 | ContentType::HTML, false)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 34 | EXPECT_FALSE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 35 | 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 Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 40 | EXPECT_FALSE(isContentTypeAllowed("application/json, text/html", |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 41 | ContentType::OctetStream, false)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 44 | TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 45 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 46 | EXPECT_TRUE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 47 | isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 50 | TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 51 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame] | 52 | EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8", |
| 53 | ContentType::OctetStream, true)); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 56 | TEST(getPreferredContentType, PositiveTest) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 57 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 58 | std::array<ContentType, 1> contentType{ContentType::HTML}; |
| 59 | EXPECT_EQ( |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 60 | getPreferredContentType("text/html, application/json", contentType), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 61 | ContentType::HTML); |
| 62 | |
| 63 | std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON}; |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 64 | EXPECT_EQ(getPreferredContentType("text/html, application/json", htmlJson), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 65 | ContentType::HTML); |
| 66 | |
| 67 | std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML}; |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 68 | EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 69 | ContentType::HTML); |
| 70 | |
| 71 | std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON}; |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 72 | EXPECT_EQ(getPreferredContentType("application/cbor, application::json", |
| 73 | cborJson), |
| 74 | ContentType::CBOR); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 75 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 76 | EXPECT_EQ(getPreferredContentType("application/json", cborJson), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 77 | ContentType::JSON); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 78 | EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 81 | TEST(getPreferredContentType, NegativeTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 82 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 83 | std::array<ContentType, 1> contentType{ContentType::CBOR}; |
| 84 | EXPECT_EQ( |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 85 | getPreferredContentType("text/html, application/json", contentType), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 86 | ContentType::NoMatch); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 87 | } |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 88 | } // namespace |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 89 | } // namespace http_helpers |