1
0

resolve_tests.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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(ResolveTests, HelloWorldTest) {
  8. Url url{"http://www.example.com:" + std::to_string(server->GetPort()) + "/hello.html"};
  9. Resolve resolve{"www.example.com", "127.0.0.1", {server->GetPort()}};
  10. Response response = cpr::Get(url, resolve);
  11. std::string expected_text{"Hello world!"};
  12. EXPECT_EQ(expected_text, response.text);
  13. EXPECT_EQ(url, response.url);
  14. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  15. EXPECT_EQ(200, response.status_code);
  16. EXPECT_EQ(ErrorCode::OK, response.error.code);
  17. }
  18. TEST(ResolveTests, RedirectMultiple) {
  19. Url url1{"http://www.example0.com:" + std::to_string(server->GetPort()) + "/resolve_permanent_redirect.html"};
  20. Url url2{"http://www.example1.com:" + std::to_string(server->GetPort()) + "/hello.html"};
  21. Resolve resolve1{"www.example0.com", "127.0.0.1", {server->GetPort()}};
  22. Resolve resolve2{"www.example1.com", "127.0.0.1", {server->GetPort()}};
  23. Response response = cpr::Get(url1, std::vector<Resolve>{resolve1, resolve2}, Header{{"RedirectLocation", url2.str()}});
  24. std::string expected_text{"Hello world!"};
  25. EXPECT_EQ(expected_text, response.text);
  26. EXPECT_EQ(url2, response.url);
  27. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  28. EXPECT_EQ(200, response.status_code);
  29. EXPECT_EQ(ErrorCode::OK, response.error.code);
  30. }
  31. int main(int argc, char** argv) {
  32. ::testing::InitGoogleTest(&argc, argv);
  33. ::testing::AddGlobalTestEnvironment(server);
  34. return RUN_ALL_TESTS();
  35. }