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 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 3 | #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 Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 8 | |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 9 | namespace http_helpers |
| 10 | { |
| 11 | namespace |
| 12 | { |
| 13 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 14 | TEST(isContentTypeAllowed, PositiveTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 15 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 16 | EXPECT_TRUE(isContentTypeAllowed("*/*", ContentType::HTML, true)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 17 | EXPECT_TRUE(isContentTypeAllowed("application/octet-stream", |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 18 | ContentType::OctetStream, false)); |
| 19 | EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML, false)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 20 | EXPECT_TRUE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 21 | 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 Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 28 | TEST(isContentTypeAllowed, NegativeTest) |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 29 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 30 | EXPECT_FALSE(isContentTypeAllowed("application/octet-stream", |
| 31 | ContentType::HTML, false)); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 32 | EXPECT_FALSE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 33 | 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 Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 38 | EXPECT_FALSE(isContentTypeAllowed("application/json, text/html", |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 39 | ContentType::OctetStream, false)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 42 | TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 43 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 44 | EXPECT_TRUE( |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 45 | isContentTypeAllowed("text/html, */*", ContentType::OctetStream, true)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 48 | TEST(isContentTypeAllowed, ContainsQFactorWeightingReturnsTrue) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 49 | { |
Ed Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 50 | EXPECT_TRUE(isContentTypeAllowed("text/html, */*;q=0.8", |
| 51 | ContentType::OctetStream, true)); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 54 | TEST(getPreferedContentType, PositiveTest) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 55 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 56 | 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 Tanous | 4a0e1a0 | 2022-09-21 15:28:04 -0700 | [diff] [blame^] | 76 | EXPECT_EQ(getPreferedContentType("*/*", cborJson), ContentType::ANY); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 79 | TEST(getPreferedContentType, NegativeTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 80 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 81 | std::array<ContentType, 1> contentType{ContentType::CBOR}; |
| 82 | EXPECT_EQ( |
| 83 | getPreferedContentType("text/html, application/json", contentType), |
| 84 | ContentType::NoMatch); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 85 | } |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 86 | } // namespace |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 87 | } // namespace http_helpers |