options_tests.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <cpr/cpr.h>
  4. #include "httpServer.hpp"
  5. using namespace cpr;
  6. static HttpServer* server = new HttpServer();
  7. TEST(OptionsTests, BaseUrlTest) {
  8. Url url{server->GetBaseUrl() + "/"};
  9. Response response = cpr::Options(url);
  10. std::string expected_text{""};
  11. EXPECT_EQ(expected_text, response.text);
  12. EXPECT_EQ(url, response.url);
  13. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, response.header["Access-Control-Allow-Methods"]);
  14. EXPECT_EQ(200, response.status_code);
  15. EXPECT_EQ(ErrorCode::OK, response.error.code);
  16. }
  17. TEST(OptionsTests, SpecificUrlTest) {
  18. Url url{server->GetBaseUrl() + "/hello.html"};
  19. Response response = cpr::Options(url);
  20. std::string expected_text{""};
  21. EXPECT_EQ(expected_text, response.text);
  22. EXPECT_EQ(url, response.url);
  23. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, response.header["Access-Control-Allow-Methods"]);
  24. EXPECT_EQ(200, response.status_code);
  25. EXPECT_EQ(ErrorCode::OK, response.error.code);
  26. }
  27. TEST(OptionsTests, AsyncBaseUrlTest) {
  28. Url url{server->GetBaseUrl() + "/"};
  29. std::vector<AsyncResponse> responses;
  30. for (size_t i = 0; i < 10; ++i) {
  31. responses.emplace_back(cpr::OptionsAsync(url));
  32. }
  33. for (cpr::AsyncResponse& future_response : responses) {
  34. cpr::Response response = future_response.get();
  35. std::string expected_text{""};
  36. EXPECT_EQ(expected_text, response.text);
  37. EXPECT_EQ(url, response.url);
  38. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, response.header["Access-Control-Allow-Methods"]);
  39. EXPECT_EQ(200, response.status_code);
  40. EXPECT_EQ(ErrorCode::OK, response.error.code);
  41. }
  42. }
  43. TEST(OptionsTests, AsyncSpecificUrlTest) {
  44. Url url{server->GetBaseUrl() + "/hello.html"};
  45. std::vector<AsyncResponse> responses;
  46. for (size_t i = 0; i < 10; ++i) {
  47. responses.emplace_back(cpr::OptionsAsync(url));
  48. }
  49. for (cpr::AsyncResponse& future_response : responses) {
  50. cpr::Response response = future_response.get();
  51. std::string expected_text{""};
  52. EXPECT_EQ(expected_text, response.text);
  53. EXPECT_EQ(url, response.url);
  54. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, response.header["Access-Control-Allow-Methods"]);
  55. EXPECT_EQ(200, response.status_code);
  56. EXPECT_EQ(ErrorCode::OK, response.error.code);
  57. }
  58. }
  59. int main(int argc, char** argv) {
  60. ::testing::InitGoogleTest(&argc, argv);
  61. ::testing::AddGlobalTestEnvironment(server);
  62. return RUN_ALL_TESTS();
  63. }