diff --git a/components/core/tests/test-NetworkReader.cpp b/components/core/tests/test-NetworkReader.cpp index f2995f141..552775ea8 100644 --- a/components/core/tests/test-NetworkReader.cpp +++ b/components/core/tests/test-NetworkReader.cpp @@ -196,26 +196,36 @@ TEST_CASE("network_reader_with_valid_http_header_kv_pairs", "[NetworkReader]") { std::unordered_map valid_http_header_kv_pairs; // We use httpbin (https://httpbin.org/) to test the user-specified headers. On success, it is // supposed to respond all the user-specified headers as key-value pairs in JSON form. - constexpr int cNumHttpHeaderKeyValuePairs{10}; + constexpr size_t cNumHttpHeaderKeyValuePairs{10}; for (size_t i{0}; i < cNumHttpHeaderKeyValuePairs; ++i) { valid_http_header_kv_pairs.emplace( fmt::format("Unit-Test-Key{}", i), fmt::format("Unit-Test-Value{}", i) ); } - clp::NetworkReader reader{ - "https://httpbin.org/headers", - 0, - false, - clp::CurlDownloadHandler::cDefaultOverallTimeout, - clp::CurlDownloadHandler::cDefaultConnectionTimeout, - clp::NetworkReader::cDefaultBufferPoolSize, - clp::NetworkReader::cDefaultBufferSize, - valid_http_header_kv_pairs - }; - auto const content{get_content(reader)}; - REQUIRE(assert_curl_error_code(CURLE_OK, reader)); - auto const parsed_content = nlohmann::json::parse(content); + std::optional> optional_content; + // Retry the unit test a limited number of times to handle transient server-side HTTP errors. + // This ensures the test is not marked as failed due to temporary issues beyond our control. + constexpr size_t cNumMaxTrials{10}; + for (size_t i{0}; i < cNumMaxTrials; ++i) { + clp::NetworkReader reader{ + "https://httpbin.org/headers", + 0, + false, + clp::CurlDownloadHandler::cDefaultOverallTimeout, + clp::CurlDownloadHandler::cDefaultConnectionTimeout, + clp::NetworkReader::cDefaultBufferPoolSize, + clp::NetworkReader::cDefaultBufferSize, + valid_http_header_kv_pairs + }; + auto const content = get_content(reader); + if (assert_curl_error_code(CURLE_OK, reader)) { + optional_content.emplace(content); + break; + } + } + REQUIRE(optional_content.has_value()); + auto const parsed_content = nlohmann::json::parse(optional_content.value()); auto const& headers{parsed_content.at("headers")}; for (auto const& [key, value] : valid_http_header_kv_pairs) { REQUIRE((value == headers.at(key).get()));