requests.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <thread>
  2. #include "coeurl/client.hpp"
  3. #include "coeurl/request.hpp"
  4. #if __has_include(<doctest.h>)
  5. #include <doctest.h>
  6. #else
  7. #include <doctest/doctest.h>
  8. #endif
  9. using namespace coeurl;
  10. TEST_CASE("Basic request") {
  11. Client g{};
  12. g.get("http://localhost:5000/", [](const Request &r) {
  13. CHECK(r.url() == "http://localhost:5000/");
  14. CHECK(r.response_code() == 200);
  15. CHECK(r.response() == "OK");
  16. CHECK(r.response_headers()["content-type"] == "text/plain; charset=utf-8");
  17. });
  18. }
  19. TEST_CASE("Basic manual request") {
  20. Client g{};
  21. auto r = std::make_shared<Request>(&g, Request::Method::Get, "http://localhost:5000/");
  22. r->on_complete([](const Request &r) {
  23. CHECK(r.url() == "http://localhost:5000/");
  24. CHECK(r.response_code() == 200);
  25. CHECK(r.response() == "OK");
  26. CHECK(r.response_headers()["content-type"] == "text/plain; charset=utf-8");
  27. });
  28. g.submit_request(r);
  29. }
  30. TEST_CASE("Basic redirect") {
  31. Client g{};
  32. g.get(
  33. "http://localhost:5000/redirect",
  34. [](const Request &r) {
  35. CHECK(r.url() == "http://localhost:5000/redirect");
  36. CHECK(r.response_code() == 200);
  37. CHECK(r.response() == "OK");
  38. },
  39. {}, 1);
  40. }
  41. TEST_CASE("No redirect") {
  42. Client g{};
  43. g.get(
  44. "http://localhost:5000/redirect",
  45. [](const Request &r) {
  46. CHECK(r.url() == "http://localhost:5000/redirect");
  47. CHECK(r.response_code() == 302);
  48. },
  49. {}, 0);
  50. }
  51. TEST_CASE("Max redirects") {
  52. Client g{};
  53. g.get(
  54. "http://localhost:5000/double_redirect",
  55. [](const Request &r) {
  56. CHECK(r.url() == "http://localhost:5000/double_redirect");
  57. CHECK(r.response_code() == 302);
  58. },
  59. {}, 1);
  60. }
  61. TEST_CASE("Basic manual POST request") {
  62. Client g{};
  63. auto r = std::make_shared<Request>(&g, Request::Method::Post, "http://localhost:5000/post");
  64. r->request("ABCD");
  65. r->on_complete([](const Request &r) {
  66. CHECK(r.url() == "http://localhost:5000/post");
  67. CHECK(r.response_code() == 200);
  68. CHECK(r.response() == "ABCD");
  69. });
  70. g.submit_request(r);
  71. }
  72. TEST_CASE("Basic manual PUT request") {
  73. Client g{};
  74. auto r = std::make_shared<Request>(&g, Request::Method::Put, "http://localhost:5000/put");
  75. r->request("ABCD");
  76. r->on_complete([](const Request &r) {
  77. CHECK(r.url() == "http://localhost:5000/put");
  78. CHECK(r.response_code() == 200);
  79. CHECK(r.response() == "ABCD");
  80. });
  81. g.submit_request(r);
  82. }
  83. TEST_CASE("Basic manual HEAD request") {
  84. Client g{};
  85. auto r = std::make_shared<Request>(&g, Request::Method::Head, "http://localhost:5000/");
  86. r->on_complete([](const Request &r) {
  87. CHECK(r.url() == "http://localhost:5000/");
  88. CHECK(r.response_code() == 200);
  89. CHECK(r.response().empty());
  90. });
  91. g.submit_request(r);
  92. }
  93. TEST_CASE("Basic manual OPTIONS request") {
  94. Client g{};
  95. auto r = std::make_shared<Request>(&g, Request::Method::Options, "http://localhost:5000/");
  96. r->on_complete([](const Request &r) {
  97. CHECK(r.url() == "http://localhost:5000/");
  98. CHECK(r.response_code() == 200);
  99. CHECK(r.response_headers()["allow"].find("HEAD") != std::string::npos);
  100. CHECK(r.response_headers()["allow"].find("OPTIONS") != std::string::npos);
  101. CHECK(r.response_headers()["allow"].find("GET") != std::string::npos);
  102. });
  103. g.submit_request(r);
  104. }
  105. TEST_CASE("Basic manual DELETE request") {
  106. Client g{};
  107. auto r = std::make_shared<Request>(&g, Request::Method::Delete, "http://localhost:5000/delete");
  108. r->on_complete([](const Request &r) {
  109. CHECK(r.url() == "http://localhost:5000/delete");
  110. CHECK(r.response_code() == 200);
  111. });
  112. g.submit_request(r);
  113. }
  114. TEST_CASE("Basic simple POST request") {
  115. Client g{};
  116. g.post("http://localhost:5000/post", "ABCD", "text/plain", [](const Request &r) {
  117. CHECK(r.url() == "http://localhost:5000/post");
  118. CHECK(r.response_code() == 200);
  119. CHECK(r.response() == "ABCD");
  120. });
  121. }
  122. TEST_CASE("Basic simple PUT request") {
  123. Client g{};
  124. g.put("http://localhost:5000/put", "ABCD", "text/plain", [](const Request &r) {
  125. CHECK(r.url() == "http://localhost:5000/put");
  126. CHECK(r.response_code() == 200);
  127. CHECK(r.response() == "ABCD");
  128. });
  129. }
  130. TEST_CASE("Basic simple HEAD request") {
  131. Client g{};
  132. g.head("http://localhost:5000/", [](const Request &r) {
  133. CHECK(r.url() == "http://localhost:5000/");
  134. CHECK(r.response_code() == 200);
  135. CHECK(r.response().empty());
  136. });
  137. }
  138. TEST_CASE("Basic simple OPTIONS request") {
  139. Client g{};
  140. g.options("http://localhost:5000/", [](const Request &r) {
  141. CHECK(r.url() == "http://localhost:5000/");
  142. CHECK(r.response_code() == 200);
  143. CHECK(r.response_headers()["allow"].find("HEAD") != std::string::npos);
  144. CHECK(r.response_headers()["allow"].find("OPTIONS") != std::string::npos);
  145. CHECK(r.response_headers()["allow"].find("GET") != std::string::npos);
  146. });
  147. }
  148. TEST_CASE("Basic simple DELETE request") {
  149. Client g{};
  150. g.delete_("http://localhost:5000/delete", [](const Request &r) {
  151. CHECK(r.url() == "http://localhost:5000/delete");
  152. CHECK(r.response_code() == 200);
  153. });
  154. }
  155. TEST_CASE("Basic simple DELETE request with body") {
  156. Client g{};
  157. g.delete_("http://localhost:5000/delete", "hello", "text/plain", [](const Request &r) {
  158. CHECK(r.url() == "http://localhost:5000/delete");
  159. CHECK(r.response_code() == 200);
  160. CHECK(r.response() == "hello");
  161. });
  162. }