|
|
|
|
#include <ServerContext.hpp>
|
|
|
|
|
|
|
|
|
|
std::string TestResponse::string() {
|
|
|
|
|
buffer = str();
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
std::string TestResponse::message() {
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
string();
|
|
|
|
|
auto msg = buffer.substr(13, buffer.find('\r') - 13);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
std::string TestResponse::code() {
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
string();
|
|
|
|
|
return buffer.substr(9, 3);
|
|
|
|
|
}
|
|
|
|
|
std::string TestResponse::content() {
|
|
|
|
|
if (buffer.empty())
|
|
|
|
|
string();
|
|
|
|
|
auto start = buffer.find("\r\n\r\n") + 4;
|
|
|
|
|
return buffer.substr(buffer.find("\r\n\r\n") + 4, buffer.size() - start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::unordered_map<std::string, std::string> &TestResponse::headers() {
|
|
|
|
|
size_t end = buffer.find("\r\n\r\n") + 4;
|
|
|
|
|
size_t start = buffer.find("\r\n") + 2;
|
|
|
|
|
std::string header;
|
|
|
|
|
std::string header_content;
|
|
|
|
|
for (size_t c = start; c < end; c++) {
|
|
|
|
|
if (buffer[c] == ':') {
|
|
|
|
|
header = buffer.substr(start, c - start);
|
|
|
|
|
c += 2;
|
|
|
|
|
start = c;
|
|
|
|
|
}
|
|
|
|
|
if (buffer[c] == '\r') {
|
|
|
|
|
header_content = buffer.substr(start, c - start);
|
|
|
|
|
c += 2;
|
|
|
|
|
start = c;
|
|
|
|
|
headers_.emplace(header, header_content);
|
|
|
|
|
header = "";
|
|
|
|
|
header_content = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return headers_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void header_present(std::shared_ptr<TestResponse> response, const std::string &header, const std::string &content) {
|
|
|
|
|
const auto loc_itr = response->headers().find(header);
|
|
|
|
|
const auto end = response->headers().end();
|
|
|
|
|
const auto header_found = loc_itr != end;
|
|
|
|
|
REQUIRE(header_found);
|
|
|
|
|
REQUIRE(loc_itr->first == header);
|
|
|
|
|
REQUIRE(loc_itr->second == content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void header_present(std::shared_ptr<TestResponse> response, const std::string &header) {
|
|
|
|
|
const auto loc_itr = response->headers().find(header);
|
|
|
|
|
const auto end = response->headers().end();
|
|
|
|
|
const auto header_found = loc_itr != end;
|
|
|
|
|
REQUIRE(header_found);
|
|
|
|
|
REQUIRE(loc_itr->first == header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void is_json_request(std::shared_ptr<TestResponse> response) {
|
|
|
|
|
const auto content = response->content();
|
|
|
|
|
auto obj = nlohmann::json::parse(content);
|
|
|
|
|
REQUIRE(obj.is_object());
|
|
|
|
|
header_present(response, "Content-Type", "application/json");
|
|
|
|
|
header_present(response, "Content-Length", std::to_string(content.length()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommonResponse::service_unavailable(std::shared_ptr<TestResponse> response) {
|
|
|
|
|
REQUIRE(response->code() == "503");
|
|
|
|
|
REQUIRE(response->message() == "Service Unavailable");
|
|
|
|
|
is_json_request(response);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data) {
|
|
|
|
|
std::string data_dump(data.dump());
|
|
|
|
|
REQUIRE(response->code() == "200");
|
|
|
|
|
REQUIRE(response->message() == "OK");
|
|
|
|
|
REQUIRE(response->content() == data_dump);
|
|
|
|
|
is_json_request(response);
|
|
|
|
|
};
|
|
|
|
|
void CommonResponse::bad_request(std::shared_ptr<TestResponse> response) {
|
|
|
|
|
REQUIRE(response->code() == "400");
|
|
|
|
|
REQUIRE(response->message() == "Bad Request");
|
|
|
|
|
is_json_request(response);
|
|
|
|
|
};
|
|
|
|
|
void CommonResponse::accepted(std::shared_ptr<TestResponse> response) {
|
|
|
|
|
REQUIRE(response->code() == "202");
|
|
|
|
|
REQUIRE(response->message() == "Accepted");
|
|
|
|
|
is_json_request(response);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void CommonResponse::created(std::shared_ptr<TestResponse> response, std::shared_ptr<TestRequest> request, const std::string &location) {
|
|
|
|
|
REQUIRE(response->code() == "201");
|
|
|
|
|
REQUIRE(response->message() == "Created");
|
|
|
|
|
is_json_request(response);
|
|
|
|
|
header_present(response, "Location", location);
|
|
|
|
|
};
|