|
|
|
@ -15,29 +15,80 @@ std::string TestResponse::code() { |
|
|
|
string(); |
|
|
|
string(); |
|
|
|
return buffer.substr(9, 3); |
|
|
|
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 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) { |
|
|
|
void CommonResponse::service_unavailable(std::shared_ptr<TestResponse> response) { |
|
|
|
const std::string service_unavailable_json = "HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\nContent-Length: 43\r\n\r\n{\"code\":503,\"status\":\"Service Unavailable\"}"; |
|
|
|
|
|
|
|
REQUIRE(response->string() == service_unavailable_json); |
|
|
|
|
|
|
|
REQUIRE(response->code() == "503"); |
|
|
|
REQUIRE(response->code() == "503"); |
|
|
|
REQUIRE(response->message() == "Service Unavailable"); |
|
|
|
REQUIRE(response->message() == "Service Unavailable"); |
|
|
|
|
|
|
|
is_json_request(response); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data) { |
|
|
|
void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data) { |
|
|
|
std::string str(data.dump()); |
|
|
|
std::string data_dump(data.dump()); |
|
|
|
const std::string res = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " + std::to_string(str.size()) + "\r\n\r\n" + str; |
|
|
|
|
|
|
|
REQUIRE(response->code() == "200"); |
|
|
|
REQUIRE(response->code() == "200"); |
|
|
|
REQUIRE(response->message() == "OK"); |
|
|
|
REQUIRE(response->message() == "OK"); |
|
|
|
REQUIRE(response->string() == res); |
|
|
|
REQUIRE(response->content() == data_dump); |
|
|
|
|
|
|
|
is_json_request(response); |
|
|
|
}; |
|
|
|
}; |
|
|
|
void CommonResponse::bad_request(std::shared_ptr<TestResponse> response) { |
|
|
|
void CommonResponse::bad_request(std::shared_ptr<TestResponse> response) { |
|
|
|
const std::string bad_request_json = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 35\r\n\r\n{\"code\":400,\"status\":\"Bad Request\"}"; |
|
|
|
|
|
|
|
REQUIRE(response->string() == bad_request_json); |
|
|
|
|
|
|
|
REQUIRE(response->code() == "400"); |
|
|
|
REQUIRE(response->code() == "400"); |
|
|
|
REQUIRE(response->message() == "Bad Request"); |
|
|
|
REQUIRE(response->message() == "Bad Request"); |
|
|
|
|
|
|
|
is_json_request(response); |
|
|
|
}; |
|
|
|
}; |
|
|
|
void CommonResponse::accepted(std::shared_ptr<TestResponse> response) { |
|
|
|
void CommonResponse::accepted(std::shared_ptr<TestResponse> response) { |
|
|
|
const std::string accepted = "HTTP/1.1 202 Accepted\r\nContent-Type: application/json\r\nContent-Length: 32\r\n\r\n{\"code\":202,\"status\":\"Accepted\"}"; |
|
|
|
|
|
|
|
REQUIRE(response->string() == accepted); |
|
|
|
|
|
|
|
REQUIRE(response->code() == "202"); |
|
|
|
REQUIRE(response->code() == "202"); |
|
|
|
REQUIRE(response->message() == "Accepted"); |
|
|
|
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", "/session/torrents/hash"); |
|
|
|
|
|
|
|
}; |