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