Debian Codegraph @

coeurl/tests/requests_tls.cpp

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