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