|
6 | 6 | #include <string>
|
7 | 7 | #include <string_view>
|
8 | 8 | #include <thread>
|
| 9 | +#include <unordered_map> |
9 | 10 | #include <vector>
|
10 | 11 |
|
11 | 12 | #include <Catch2/single_include/catch2/catch.hpp>
|
12 | 13 | #include <curl/curl.h>
|
| 14 | +#include <fmt/core.h> |
| 15 | +#include <json/single_include/nlohmann/json.hpp> |
13 | 16 |
|
14 | 17 | #include "../src/clp/Array.hpp"
|
15 | 18 | #include "../src/clp/CurlDownloadHandler.hpp"
|
@@ -188,3 +191,58 @@ TEST_CASE("network_reader_illegal_offset", "[NetworkReader]") {
|
188 | 191 | size_t pos{};
|
189 | 192 | REQUIRE((clp::ErrorCode_Failure == reader.try_get_pos(pos)));
|
190 | 193 | }
|
| 194 | + |
| 195 | +TEST_CASE("network_reader_with_valid_http_header_kv_pairs", "[NetworkReader]") { |
| 196 | + std::unordered_map<std::string, std::string> valid_http_header_kv_pairs; |
| 197 | + // We use httpbin (https://httpbin.org/) to test the user-specified headers. On success, it is |
| 198 | + // supposed to respond all the user-specified headers as key-value pairs in JSON form. |
| 199 | + constexpr int cNumHttpHeaderKeyValuePairs{10}; |
| 200 | + for (size_t i{0}; i < cNumHttpHeaderKeyValuePairs; ++i) { |
| 201 | + valid_http_header_kv_pairs.emplace( |
| 202 | + fmt::format("Unit-Test-Key{}", i), |
| 203 | + fmt::format("Unit-Test-Value{}", i) |
| 204 | + ); |
| 205 | + } |
| 206 | + clp::NetworkReader reader{ |
| 207 | + "https://httpbin.org/headers", |
| 208 | + 0, |
| 209 | + false, |
| 210 | + clp::CurlDownloadHandler::cDefaultOverallTimeout, |
| 211 | + clp::CurlDownloadHandler::cDefaultConnectionTimeout, |
| 212 | + clp::NetworkReader::cDefaultBufferPoolSize, |
| 213 | + clp::NetworkReader::cDefaultBufferSize, |
| 214 | + valid_http_header_kv_pairs |
| 215 | + }; |
| 216 | + auto const content = nlohmann::json::parse(get_content(reader)); |
| 217 | + auto const& headers{content.at("headers")}; |
| 218 | + REQUIRE(assert_curl_error_code(CURLE_OK, reader)); |
| 219 | + for (auto const& [key, value] : valid_http_header_kv_pairs) { |
| 220 | + REQUIRE((value == headers.at(key).get<std::string_view>())); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +TEST_CASE("network_reader_with_illegal_http_header_kv_pairs", "[NetworkReader]") { |
| 225 | + auto illegal_header_kv_pairs = GENERATE( |
| 226 | + // The following headers are determined by offset and disable_cache, which should not be |
| 227 | + // overridden by user-defined headers. |
| 228 | + std::unordered_map<std::string, std::string>{{"Range", "bytes=100-"}}, |
| 229 | + std::unordered_map<std::string, std::string>{{"RAnGe", "bytes=100-"}}, |
| 230 | + std::unordered_map<std::string, std::string>{{"Cache-Control", "no-cache"}}, |
| 231 | + std::unordered_map<std::string, std::string>{{"Pragma", "no-cache"}}, |
| 232 | + // The CRLF-terminated headers should be rejected. |
| 233 | + std::unordered_map<std::string, std::string>{{"Legal-Name", "CRLF\r\n"}} |
| 234 | + ); |
| 235 | + clp::NetworkReader reader{ |
| 236 | + "https://httpbin.org/headers", |
| 237 | + 0, |
| 238 | + false, |
| 239 | + clp::CurlDownloadHandler::cDefaultOverallTimeout, |
| 240 | + clp::CurlDownloadHandler::cDefaultConnectionTimeout, |
| 241 | + clp::NetworkReader::cDefaultBufferPoolSize, |
| 242 | + clp::NetworkReader::cDefaultBufferSize, |
| 243 | + illegal_header_kv_pairs |
| 244 | + }; |
| 245 | + auto const content = get_content(reader); |
| 246 | + REQUIRE(content.empty()); |
| 247 | + REQUIRE(assert_curl_error_code(CURLE_BAD_FUNCTION_ARGUMENT, reader)); |
| 248 | +} |
0 commit comments