download_tests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <cstddef>
  2. #include <gtest/gtest.h>
  3. #include <string>
  4. #include <cpr/cpr.h>
  5. #include "cpr/api.h"
  6. #include "cpr/callback.h"
  7. #include "cpr/cprtypes.h"
  8. #include "cpr/session.h"
  9. #include "httpServer.hpp"
  10. static cpr::HttpServer* server = new cpr::HttpServer();
  11. bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
  12. return true;
  13. }
  14. TEST(DownloadTests, DownloadGzip) {
  15. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  16. cpr::Session session;
  17. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  18. session.SetUrl(url);
  19. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  20. EXPECT_EQ(url, response.url);
  21. EXPECT_EQ(200, response.status_code);
  22. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  23. }
  24. TEST(DownloadTests, RangeTestWholeFile) {
  25. const int64_t download_size = 9;
  26. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  27. cpr::Session session;
  28. session.SetUrl(url);
  29. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  30. session.SetRange(cpr::Range{std::nullopt, std::nullopt});
  31. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  32. EXPECT_EQ(200, response.status_code);
  33. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  34. EXPECT_EQ(download_size, response.downloaded_bytes);
  35. }
  36. TEST(DownloadTests, RangeTestLowerLimit) {
  37. const int64_t download_size = 8;
  38. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  39. cpr::Session session;
  40. session.SetUrl(url);
  41. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  42. session.SetRange(cpr::Range{1, std::nullopt});
  43. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  44. EXPECT_EQ(206, response.status_code);
  45. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  46. EXPECT_EQ(download_size, response.downloaded_bytes);
  47. }
  48. TEST(DownloadTests, RangeTestUpperLimit) {
  49. const int64_t download_size = 6;
  50. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  51. cpr::Session session;
  52. session.SetUrl(url);
  53. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  54. session.SetRange(cpr::Range{std::nullopt, download_size - 1});
  55. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  56. EXPECT_EQ(206, response.status_code);
  57. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  58. EXPECT_EQ(download_size, response.downloaded_bytes);
  59. }
  60. TEST(DownloadTests, RangeTestLowerAndUpperLimit) {
  61. const int64_t download_size = 2;
  62. const int64_t start_from = 2;
  63. const int64_t finish_at = start_from + download_size - 1;
  64. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  65. cpr::Session session;
  66. session.SetUrl(url);
  67. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  68. session.SetRange(cpr::Range{start_from, finish_at});
  69. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  70. EXPECT_EQ(206, response.status_code);
  71. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  72. EXPECT_EQ(download_size, response.downloaded_bytes);
  73. }
  74. TEST(DownloadTests, RangeTestMultipleRangesSet) {
  75. const int64_t num_parts = 2;
  76. const int64_t download_size = num_parts * (26 /*content range*/ + 4 /*\n*/) + ((num_parts + 1) * 15 /*boundary*/) + 2 /*--*/ + 6 /*data*/;
  77. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  78. cpr::Session session;
  79. session.SetUrl(url);
  80. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  81. session.SetMultiRange(cpr::MultiRange{cpr::Range{std::nullopt, 3}, cpr::Range{5, 6}});
  82. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  83. EXPECT_EQ(206, response.status_code);
  84. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  85. EXPECT_EQ(download_size, response.downloaded_bytes);
  86. }
  87. TEST(DownloadTests, RangeTestMultipleRangesOption) {
  88. const int64_t num_parts = 3;
  89. const int64_t download_size = num_parts * (26 /*content range*/ + 4 /*\n*/) + ((num_parts + 1) * 15 /*boundary*/) + 2 /*--*/ + 7 /*data*/;
  90. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  91. cpr::Session session;
  92. session.SetUrl(url);
  93. session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  94. session.SetOption(cpr::MultiRange{cpr::Range{std::nullopt, 2}, cpr::Range{4, 5}, cpr::Range{7, 8}});
  95. cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
  96. EXPECT_EQ(206, response.status_code);
  97. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  98. EXPECT_EQ(download_size, response.downloaded_bytes);
  99. }
  100. int main(int argc, char** argv) {
  101. ::testing::InitGoogleTest(&argc, argv);
  102. ::testing::AddGlobalTestEnvironment(server);
  103. return RUN_ALL_TESTS();
  104. }