diff options
| author | Stefan Boberg <[email protected]> | 2025-10-24 19:32:01 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2025-10-24 19:32:01 +0200 |
| commit | 4cba4eb3f122c7a1a49b629b1c0656d7f817f001 (patch) | |
| tree | e184821073167f6e81a75193efca91013d7b359b /thirdparty/cpr/test/async_tests.cpp | |
| parent | fixed progress bar when scanning changed local files (#608) (diff) | |
| download | zen-4cba4eb3f122c7a1a49b629b1c0656d7f817f001.tar.xz zen-4cba4eb3f122c7a1a49b629b1c0656d7f817f001.zip | |
move cpr in-tree (#605)
* added cpr 1.10.5 in-tree to allow updates to vcpkg without breaking the build
* added asio 1.29.0 in-tree to remove one more vcpkg dependency
* bumped vcpkg to 2024.06.15 to address failure to build due to use of deprecated binaries in vcpkg (404 error: `https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-pkgconf-1~2.1.0-1-any.pkg.tar.zst` during build)
Diffstat (limited to 'thirdparty/cpr/test/async_tests.cpp')
| -rw-r--r-- | thirdparty/cpr/test/async_tests.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/thirdparty/cpr/test/async_tests.cpp b/thirdparty/cpr/test/async_tests.cpp new file mode 100644 index 000000000..026bdf0c6 --- /dev/null +++ b/thirdparty/cpr/test/async_tests.cpp @@ -0,0 +1,81 @@ +#include <gtest/gtest.h> + +#include <string> +#include <vector> + +#include <cpr/cpr.h> +#include <cpr/filesystem.h> + +#include "cpr/api.h" +#include "cpr/response.h" +#include "httpServer.hpp" + +using namespace cpr; + +static HttpServer* server = new HttpServer(); + +bool write_data(std::string /*data*/, intptr_t /*userdata*/) { + return true; +} + +TEST(AsyncTests, AsyncGetTest) { + Url url{server->GetBaseUrl() + "/hello.html"}; + cpr::AsyncResponse future = cpr::GetAsync(url); + std::string expected_text{"Hello world!"}; + cpr::Response response = future.get(); + EXPECT_EQ(expected_text, response.text); + EXPECT_EQ(url, response.url); + EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); + EXPECT_EQ(200, response.status_code); +} + +TEST(AsyncTests, AsyncGetMultipleTest) { + Url url{server->GetBaseUrl() + "/hello.html"}; + std::vector<AsyncResponse> responses; + for (size_t i = 0; i < 10; ++i) { + responses.emplace_back(cpr::GetAsync(url)); + } + for (cpr::AsyncResponse& future : responses) { + std::string expected_text{"Hello world!"}; + cpr::Response response = future.get(); + EXPECT_EQ(expected_text, response.text); + EXPECT_EQ(url, response.url); + EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); + EXPECT_EQ(200, response.status_code); + } +} + +TEST(AsyncTests, AsyncGetMultipleReflectTest) { + Url url{server->GetBaseUrl() + "/hello.html"}; + std::vector<AsyncResponse> responses; + for (size_t i = 0; i < 100; ++i) { + Parameters p{{"key", std::to_string(i)}}; + responses.emplace_back(cpr::GetAsync(url, p)); + } + int i = 0; + for (cpr::AsyncResponse& future : responses) { + std::string expected_text{"Hello world!"}; + cpr::Response response = future.get(); + EXPECT_EQ(expected_text, response.text); + Url expected_url{url + "?key=" + std::to_string(i)}; + EXPECT_EQ(expected_url, response.url); + EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); + EXPECT_EQ(200, response.status_code); + ++i; + } +} + +TEST(AsyncTests, AsyncDownloadTest) { + cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"}; + cpr::AsyncResponse future = cpr::DownloadAsync(fs::path{"/tmp/aync_download"}, url, cpr::Header{{"Accept-Encoding", "gzip"}}, cpr::WriteCallback{write_data, 0}); + cpr::Response response = future.get(); + EXPECT_EQ(url, response.url); + EXPECT_EQ(200, response.status_code); + EXPECT_EQ(cpr::ErrorCode::OK, response.error.code); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + ::testing::AddGlobalTestEnvironment(server); + return RUN_ALL_TESTS(); +} |