-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmock_http_server.cpp
More file actions
206 lines (186 loc) · 7.86 KB
/
Copy pathmock_http_server.cpp
File metadata and controls
206 lines (186 loc) · 7.86 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "mock/mock_http_server.hpp"
#include <gtest/gtest.h> // EXPECT_*
#include <httplib.h>
#include <cstddef>
#include <filesystem>
#include <vector>
#include "databento/constants.hpp"
#include "databento/dbn.hpp"
#include "databento/dbn_encoder.hpp"
#include "databento/detail/buffer.hpp"
#include "databento/detail/zstd_stream.hpp"
#include "databento/file_stream.hpp"
#include "databento/record.hpp"
using databento::tests::mock::MockHttpServer;
int MockHttpServer::ListenOnThread() {
listen_thread_ = detail::ScopedThread{[this] { this->server_.listen_after_bind(); }};
// Wait until the server is accepting so a later stop() in the destructor can't
// race the listen thread's startup, which would otherwise hang
server_.wait_until_ready();
return port_;
}
void MockHttpServer::MockBadPostRequest(const std::string& path,
const nlohmann::json& json) {
server_.Post(path, [json](const httplib::Request&, httplib::Response& resp) {
resp.status = 400;
resp.body = json.dump();
});
}
void MockHttpServer::MockGetJson(const std::string& path, const nlohmann::json& json) {
this->MockGetJson(path, {}, json);
}
void MockHttpServer::MockGetJson(const std::string& path,
const std::map<std::string, std::string>& params,
const nlohmann::json& json) {
this->MockGetJson(path, params, json, {});
}
void MockHttpServer::MockGetJson(const std::string& path,
const std::map<std::string, std::string>& params,
const nlohmann::json& json,
const nlohmann::json& warnings) {
server_.Get(path, [json, params, warnings](const httplib::Request& req,
httplib::Response& resp) {
if (!req.has_header("Authorization")) {
resp.status = 401;
return;
}
[[maybe_unused]] auto _auth = req.get_header_value("Authorization");
CheckParams(params, req);
if (!warnings.empty()) {
resp.set_header("X-Warning", warnings.dump());
}
resp.set_content(json.dump(), "application/json");
resp.status = 200;
});
}
void MockHttpServer::MockPostJson(const std::string& path,
const std::map<std::string, std::string>& form_params,
const nlohmann::json& json) {
server_.Post(
path, [json, form_params](const httplib::Request& req, httplib::Response& resp) {
if (!req.has_header("Authorization")) {
resp.status = 401;
return;
}
[[maybe_unused]] auto _auth = req.get_header_value("Authorization");
CheckFormParams(form_params, req);
resp.set_content(json.dump(), "application/json");
resp.status = 200;
});
}
void MockHttpServer::MockPostDbn(const std::string& path,
const std::map<std::string, std::string>& params,
const std::string& dbn_path) {
constexpr std::size_t kChunkSize = 32;
// Read contents into buffer
auto buffer = EncodeToBuffer(dbn_path);
// Serve
server_.Post(path, MakeDbnStreamHandler(params, std::move(buffer), kChunkSize));
}
void MockHttpServer::MockPostDbn(const std::string& path,
const std::map<std::string, std::string>& params,
Record record, std::size_t count,
std::size_t chunk_size) {
MockPostDbn(path, params, record, count, 0, chunk_size);
}
void MockHttpServer::MockPostDbn(const std::string& path,
const std::map<std::string, std::string>& params,
Record record, std::size_t count,
std::size_t extra_bytes, std::size_t chunk_size) {
// Needs to be copy-constructable for use in server
auto buffer = std::make_shared<detail::Buffer>();
{
detail::ZstdCompressStream zstd_stream{buffer.get()};
DbnEncoder encoder{Metadata{
kDbnVersion,
ToString(Dataset::IfusImpact),
{Schema::Mbp1},
},
&zstd_stream};
for (std::size_t i = 0; i < count; ++i) {
encoder.EncodeRecord(record);
}
if (extra_bytes > sizeof(RecordHeader)) {
std::vector<std::byte> empty(extra_bytes - sizeof(RecordHeader));
// write the header so it looks like the start of a valid record
zstd_stream.WriteAll(reinterpret_cast<const std::byte*>(&record.Header()),
sizeof(RecordHeader));
zstd_stream.WriteAll(empty.data(), empty.size());
}
}
server_.Post(path, MakeDbnStreamHandler(params, std::move(buffer), chunk_size));
}
void MockHttpServer::MockGetDbnFile(const std::string& path,
const std::string& dbn_path) {
server_.Get(path, [dbn_path](const httplib::Request& req, httplib::Response& resp) {
if (!req.has_header("Authorization")) {
resp.status = 401;
return;
}
resp.set_file_content(dbn_path, "application/octet-stream");
});
}
void MockHttpServer::CheckParams(const std::map<std::string, std::string>& params,
const httplib::Request& req) {
for (const auto& param : params) {
EXPECT_TRUE(req.has_param(param.first)) << "Missing query param " << param.first;
EXPECT_EQ(req.get_param_value(param.first), param.second)
<< "Incorrect query param value for " << param.first << ". Expected "
<< param.second << ", found " << req.get_param_value(param.first);
}
}
void MockHttpServer::CheckFormParams(const std::map<std::string, std::string>& params,
const httplib::Request& req) {
EXPECT_EQ(req.get_header_value("content-type"), "application/x-www-form-urlencoded")
<< "Request body is not form data";
httplib::Params form_params;
httplib::detail::parse_query_text(req.body, form_params);
for (const auto& param : params) {
const auto param_it = form_params.find(param.first);
if (param_it == form_params.end()) {
EXPECT_NE(param_it, form_params.end()) << "Missing for mparam " << param.first;
} else {
EXPECT_EQ(param_it->second, param.second)
<< "Incorrect form param value for " << param.first << ". Expected "
<< param.second << ", found " << param_it->second;
}
}
}
MockHttpServer::SharedConstBuffer MockHttpServer::EncodeToBuffer(
const std::string& dbn_path) {
detail::Buffer buffer{};
InFileStream input_file{dbn_path};
while (auto read_size =
input_file.ReadSome(buffer.WriteBegin(), buffer.WriteCapacity())) {
buffer.Fill(read_size);
if (buffer.WriteCapacity() < 1024) {
buffer.Reserve(buffer.Capacity() * 2);
}
}
return std::make_shared<const databento::detail::Buffer>(std::move(buffer));
}
httplib::Server::Handler MockHttpServer::MakeDbnStreamHandler(
const std::map<std::string, std::string>& params, SharedConstBuffer&& buffer,
std::size_t chunk_size) {
return [buffer = std::move(buffer), chunk_size, params](const httplib::Request& req,
httplib::Response& resp) {
if (!req.has_header("Authorization")) {
resp.status = 401;
return;
}
CheckParams(params, req);
resp.status = 200;
resp.set_header("Content-Disposition", "attachment; filename=test.dbn.zst");
resp.set_content_provider(
"application/octet-stream",
[buffer, chunk_size](const std::size_t offset, httplib::DataSink& sink) {
if (offset < buffer->ReadCapacity()) {
sink.write(reinterpret_cast<const char*>(&buffer->ReadBegin()[offset]),
std::min(chunk_size, buffer->ReadCapacity() - offset));
} else {
sink.done();
}
return true;
});
};
}