async_tests.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <vector>
  4. #include <cpr/cpr.h>
  5. #include <cpr/filesystem.h>
  6. #include "httpServer.hpp"
  7. using namespace cpr;
  8. static HttpServer* server = new HttpServer();
  9. bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
  10. return true;
  11. }
  12. TEST(UrlEncodedPostTests, AsyncGetTest) {
  13. Url url{server->GetBaseUrl() + "/hello.html"};
  14. cpr::AsyncResponse future = cpr::GetAsync(url);
  15. std::string expected_text{"Hello world!"};
  16. cpr::Response response = future.get();
  17. EXPECT_EQ(expected_text, response.text);
  18. EXPECT_EQ(url, response.url);
  19. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  20. EXPECT_EQ(200, response.status_code);
  21. }
  22. TEST(UrlEncodedPostTests, AsyncGetMultipleTest) {
  23. Url url{server->GetBaseUrl() + "/hello.html"};
  24. std::vector<AsyncResponse> responses;
  25. for (size_t i = 0; i < 10; ++i) {
  26. responses.emplace_back(cpr::GetAsync(url));
  27. }
  28. for (cpr::AsyncResponse& future : responses) {
  29. std::string expected_text{"Hello world!"};
  30. cpr::Response response = future.get();
  31. EXPECT_EQ(expected_text, response.text);
  32. EXPECT_EQ(url, response.url);
  33. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  34. EXPECT_EQ(200, response.status_code);
  35. }
  36. }
  37. TEST(UrlEncodedPostTests, AsyncGetMultipleReflectTest) {
  38. Url url{server->GetBaseUrl() + "/hello.html"};
  39. std::vector<AsyncResponse> responses;
  40. for (size_t i = 0; i < 100; ++i) {
  41. Parameters p{{"key", std::to_string(i)}};
  42. responses.emplace_back(cpr::GetAsync(url, p));
  43. }
  44. int i = 0;
  45. for (cpr::AsyncResponse& future : responses) {
  46. std::string expected_text{"Hello world!"};
  47. cpr::Response response = future.get();
  48. EXPECT_EQ(expected_text, response.text);
  49. Url expected_url{url + "?key=" + std::to_string(i)};
  50. EXPECT_EQ(expected_url, response.url);
  51. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  52. EXPECT_EQ(200, response.status_code);
  53. ++i;
  54. }
  55. }
  56. TEST(UrlEncodedPostTests, AsyncDownloadTest) {
  57. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  58. cpr::AsyncResponse future = cpr::DownloadAsync(fs::path{"/tmp/aync_download"}, url, cpr::Header{{"Accept-Encoding", "gzip"}}, cpr::WriteCallback{write_data, 0});
  59. cpr::Response response = future.get();
  60. EXPECT_EQ(url, response.url);
  61. EXPECT_EQ(200, response.status_code);
  62. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  63. }
  64. int main(int argc, char** argv) {
  65. ::testing::InitGoogleTest(&argc, argv);
  66. ::testing::AddGlobalTestEnvironment(server);
  67. return RUN_ALL_TESTS();
  68. }