Repository navigation
Expand file tree
/
Copy pathnew_response_types.cpp
More file actions
181 lines (164 loc) · 6.02 KB
/
Copy pathnew_response_types.cpp
File metadata and controls
181 lines (164 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <curl/curl.h>
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <io.h>
#include <fcntl.h>
#else
#include <unistd.h>
#endif
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
#include "./httpserver.hpp"
#include "./littletest.hpp"
#include "./test_utils.hpp"
using std::shared_ptr;
using std::string;
using std::vector;
using httpserver::http_resource;
using httpserver::http_request;
using httpserver::http_response;
using httpserver::iovec_entry;
using httpserver::webserver;
using httpserver::create_webserver;
size_t writefunc(void *ptr, size_t size, size_t nmemb, string *s) {
s->append(reinterpret_cast<char*>(ptr), size*nmemb);
return size*nmemb;
}
class empty_resource : public http_resource {
public:
http_response render_get(const http_request&) {
return http_response::empty();
}
};
class pipe_resource : public http_resource {
public:
http_response render_get(const http_request&) {
int pipefd[2];
#if defined(_WIN32) && !defined(__CYGWIN__)
if (_pipe(pipefd, 4096, _O_BINARY) != 0) {
#else
if (pipe(pipefd) != 0) {
#endif
return
http_response::empty().with_status(500);
}
const char* msg = "hello from pipe";
write(pipefd[1], msg, strlen(msg));
close(pipefd[1]);
return http_response::pipe(pipefd[0]);
}
}; // NOLINT(readability/braces)
// v2 iovec uses borrowed buffers. The static parts must outlive the response;
// here they live for the program's duration as static storage.
static const char kHello[] = "Hello";
static const char kSpace[] = " ";
static const char kWorld[] = "World";
class iovec_resource : public http_resource {
public:
http_response render_get(const http_request&) {
std::vector<iovec_entry> parts = {
{ kHello, sizeof(kHello) - 1 },
{ kSpace, sizeof(kSpace) - 1 },
{ kWorld, sizeof(kWorld) - 1 },
};
return
http_response::iovec(parts)
.with_header("Content-Type", "text/plain");
}
};
static webserver* ws_ptr = nullptr;
LT_BEGIN_SUITE(response_types_suite)
void set_up() {
ws_ptr = new webserver(create_webserver(0));
ws_ptr->register_path("empty", std::make_shared<empty_resource>());
ws_ptr->register_path("pipe", std::make_shared<pipe_resource>());
ws_ptr->register_path("iovec", std::make_shared<iovec_resource>());
ws_ptr->start(false);
}
void tear_down() {
ws_ptr->stop();
delete ws_ptr;
ws_ptr = nullptr;
}
LT_END_SUITE(response_types_suite)
LT_BEGIN_AUTO_TEST(response_types_suite, empty_response_test)
curl_global_init(CURL_GLOBAL_ALL);
string s;
CURL *curl = curl_easy_init();
CURLcode res;
long http_code = 0; // NOLINT(runtime/int)
const uint16_t port = ws_ptr->get_bound_port();
const string empty_url = "localhost:" + std::to_string(port) + "/empty";
curl_easy_setopt(curl, CURLOPT_URL, empty_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(http_code, 204);
LT_CHECK_EQ(s, "");
curl_easy_cleanup(curl);
curl_global_cleanup();
LT_END_AUTO_TEST(empty_response_test)
LT_BEGIN_AUTO_TEST(response_types_suite, pipe_response_test)
curl_global_init(CURL_GLOBAL_ALL);
string s;
CURL *curl = curl_easy_init();
CURLcode res;
long http_code = 0; // NOLINT(runtime/int)
const uint16_t port = ws_ptr->get_bound_port();
const string pipe_url = "localhost:" + std::to_string(port) + "/pipe";
curl_easy_setopt(curl, CURLOPT_URL, pipe_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(http_code, 200);
LT_CHECK_EQ(s, "hello from pipe");
curl_easy_cleanup(curl);
curl_global_cleanup();
LT_END_AUTO_TEST(pipe_response_test)
LT_BEGIN_AUTO_TEST(response_types_suite, iovec_response_test)
curl_global_init(CURL_GLOBAL_ALL);
string s;
CURL *curl = curl_easy_init();
CURLcode res;
long http_code = 0; // NOLINT(runtime/int)
const uint16_t port = ws_ptr->get_bound_port();
const string iovec_url = "localhost:" + std::to_string(port) + "/iovec";
curl_easy_setopt(curl, CURLOPT_URL, iovec_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(http_code, 200);
LT_CHECK_EQ(s, "Hello World");
curl_easy_cleanup(curl);
curl_global_cleanup();
LT_END_AUTO_TEST(iovec_response_test)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()