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 | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 16 | EXPECT_TRUE(isContentTypeAllowed("*/*, application/octet-stream", |
| 17 | ContentType::OctetStream)); |
| 18 | EXPECT_TRUE(isContentTypeAllowed("application/octet-stream", |
| 19 | ContentType::OctetStream)); |
| 20 | EXPECT_TRUE(isContentTypeAllowed("text/html", ContentType::HTML)); |
| 21 | EXPECT_TRUE(isContentTypeAllowed("application/json", ContentType::JSON)); |
| 22 | EXPECT_TRUE(isContentTypeAllowed("application/cbor", ContentType::CBOR)); |
| 23 | EXPECT_TRUE( |
| 24 | isContentTypeAllowed("application/json, text/html", ContentType::HTML)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 27 | TEST(isContentTypeAllowed, NegativeTest) |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 28 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 29 | EXPECT_FALSE( |
| 30 | isContentTypeAllowed("application/octet-stream", ContentType::HTML)); |
| 31 | EXPECT_FALSE(isContentTypeAllowed("application/html", ContentType::JSON)); |
| 32 | EXPECT_FALSE(isContentTypeAllowed("application/json", ContentType::CBOR)); |
| 33 | EXPECT_FALSE(isContentTypeAllowed("application/cbor", ContentType::HTML)); |
| 34 | EXPECT_FALSE(isContentTypeAllowed("application/json, text/html", |
| 35 | ContentType::OctetStream)); |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 38 | TEST(isContentTypeAllowed, ContainsAnyMimeTypeReturnsTrue) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 39 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 40 | EXPECT_TRUE( |
| 41 | isContentTypeAllowed("text/html, */*", ContentType::OctetStream)); |
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, ContainsQFactorWeightingReturnsTrue) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 45 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 46 | EXPECT_TRUE( |
| 47 | isContentTypeAllowed("text/html, */*;q=0.8", ContentType::OctetStream)); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 50 | TEST(getPreferedContentType, PositiveTest) |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 51 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 52 | std::array<ContentType, 1> contentType{ContentType::HTML}; |
| 53 | EXPECT_EQ( |
| 54 | getPreferedContentType("text/html, application/json", contentType), |
| 55 | ContentType::HTML); |
| 56 | |
| 57 | std::array<ContentType, 2> htmlJson{ContentType::HTML, ContentType::JSON}; |
| 58 | EXPECT_EQ(getPreferedContentType("text/html, application/json", htmlJson), |
| 59 | ContentType::HTML); |
| 60 | |
| 61 | std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML}; |
| 62 | EXPECT_EQ(getPreferedContentType("text/html, application/json", jsonHtml), |
| 63 | ContentType::HTML); |
| 64 | |
| 65 | std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON}; |
| 66 | EXPECT_EQ( |
| 67 | getPreferedContentType("application/cbor, application::json", cborJson), |
| 68 | ContentType::CBOR); |
| 69 | |
| 70 | EXPECT_EQ(getPreferedContentType("application/json", cborJson), |
| 71 | ContentType::JSON); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 74 | TEST(getPreferedContentType, NegativeTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 75 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame^] | 76 | std::array<ContentType, 1> contentType{ContentType::CBOR}; |
| 77 | EXPECT_EQ( |
| 78 | getPreferedContentType("text/html, application/json", contentType), |
| 79 | ContentType::NoMatch); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 80 | } |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 81 | } // namespace |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 82 | } // namespace http_helpers |