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 | |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 63 | // String the chrome gives |
| 64 | EXPECT_EQ(getPreferredContentType( |
| 65 | "text/html," |
| 66 | "application/xhtml+xml," |
| 67 | "application/xml;q=0.9," |
| 68 | "image/avif," |
| 69 | "image/webp," |
| 70 | "image/apng,*/*;q=0.8," |
| 71 | "application/signed-exchange;v=b3;q=0.7", |
| 72 | htmlJson), |
| 73 | ContentType::HTML); |
| 74 | |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 75 | std::array<ContentType, 2> jsonHtml{ContentType::JSON, ContentType::HTML}; |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 76 | EXPECT_EQ(getPreferredContentType("text/html, application/json", jsonHtml), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 77 | ContentType::HTML); |
| 78 | |
| 79 | std::array<ContentType, 2> cborJson{ContentType::CBOR, ContentType::JSON}; |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 80 | EXPECT_EQ(getPreferredContentType("application/cbor, application::json", |
| 81 | cborJson), |
| 82 | ContentType::CBOR); |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 83 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 84 | EXPECT_EQ(getPreferredContentType("application/json", cborJson), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 85 | ContentType::JSON); |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 86 | EXPECT_EQ(getPreferredContentType("*/*", cborJson), ContentType::ANY); |
Ed Tanous | 463b293 | 2024-07-16 17:02:42 -0700 | [diff] [blame] | 87 | |
| 88 | // Application types with odd characters |
| 89 | EXPECT_EQ(getPreferredContentType( |
| 90 | "application/prs.nprend, application/json", cborJson), |
| 91 | ContentType::JSON); |
| 92 | |
| 93 | EXPECT_EQ(getPreferredContentType("application/rdf+xml, application/json", |
| 94 | cborJson), |
| 95 | ContentType::JSON); |
| 96 | |
| 97 | // Q values are ignored, but should parse |
| 98 | EXPECT_EQ(getPreferredContentType( |
| 99 | "application/rdf+xml;q=0.9, application/json", cborJson), |
| 100 | ContentType::JSON); |
| 101 | EXPECT_EQ(getPreferredContentType( |
| 102 | "application/rdf+xml;q=1, application/json", cborJson), |
| 103 | ContentType::JSON); |
| 104 | EXPECT_EQ(getPreferredContentType("application/json;q=0.9", cborJson), |
| 105 | ContentType::JSON); |
| 106 | EXPECT_EQ(getPreferredContentType("application/json;q=1", cborJson), |
| 107 | ContentType::JSON); |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 110 | TEST(getPreferredContentType, NegativeTest) |
Nan Zhou | a02b586 | 2022-06-21 17:46:56 +0000 | [diff] [blame] | 111 | { |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 112 | std::array<ContentType, 1> contentType{ContentType::CBOR}; |
| 113 | EXPECT_EQ( |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 114 | getPreferredContentType("text/html, application/json", contentType), |
Ed Tanous | 99351cd | 2022-08-07 16:42:51 -0700 | [diff] [blame] | 115 | ContentType::NoMatch); |
George Liu | 647b3cd | 2021-07-05 12:43:56 +0800 | [diff] [blame] | 116 | } |
Ed Tanous | 276ede5 | 2024-08-28 15:57:45 -0700 | [diff] [blame] | 117 | |
| 118 | TEST(getPreferredEncoding, PositiveTest) |
| 119 | { |
| 120 | std::array<Encoding, 1> encodingsGzip{Encoding::GZIP}; |
| 121 | EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzip), Encoding::GZIP); |
| 122 | |
| 123 | std::array<Encoding, 2> encodingsGzipZstd{Encoding::GZIP, Encoding::ZSTD}; |
| 124 | EXPECT_EQ(getPreferredEncoding("gzip", encodingsGzipZstd), Encoding::GZIP); |
| 125 | EXPECT_EQ(getPreferredEncoding("zstd", encodingsGzipZstd), Encoding::ZSTD); |
| 126 | |
| 127 | EXPECT_EQ(getPreferredEncoding("*", encodingsGzipZstd), Encoding::GZIP); |
| 128 | |
| 129 | EXPECT_EQ(getPreferredEncoding("zstd, gzip;q=1.0", encodingsGzipZstd), |
| 130 | Encoding::ZSTD); |
| 131 | } |
| 132 | |
| 133 | TEST(getPreferredEncoding, NegativeTest) |
| 134 | { |
| 135 | std::array<Encoding, 2> contentType{Encoding::GZIP, |
| 136 | Encoding::UnencodedBytes}; |
| 137 | EXPECT_EQ(getPreferredEncoding("noexist", contentType), |
| 138 | Encoding::UnencodedBytes); |
| 139 | |
| 140 | std::array<Encoding, 1> contentType2{Encoding::GZIP}; |
| 141 | EXPECT_EQ(getPreferredEncoding("zstd", contentType2), Encoding::NoMatch); |
| 142 | } |
| 143 | |
Nan Zhou | ad3edd4 | 2022-06-21 17:46:44 +0000 | [diff] [blame] | 144 | } // namespace |
Gunnar Mills | a3526fe | 2022-02-02 21:56:44 +0000 | [diff] [blame] | 145 | } // namespace http_helpers |