requests_tls.cpp 6.2 KB

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