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