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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
//===-- llvm/unittest/Support/HTTPServer.cpp - unit tests -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Debuginfod/HTTPClient.h"
#include "llvm/Debuginfod/HTTPServer.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace llvm;
#ifdef LLVM_ENABLE_HTTPLIB
TEST(HTTPServer, IsAvailable) { EXPECT_TRUE(HTTPServer::isAvailable()); }
HTTPResponse Response = {200u, "text/plain", "hello, world\n"};
std::string UrlPathPattern = R"(/(.*))";
std::string InvalidUrlPathPattern = R"(/(.*)";
HTTPRequestHandler Handler = [](HTTPServerRequest &Request) {
Request.setResponse(Response);
};
HTTPRequestHandler DelayHandler = [](HTTPServerRequest &Request) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
Request.setResponse(Response);
};
HTTPRequestHandler StreamingHandler = [](HTTPServerRequest &Request) {
Request.setResponse({200, "text/plain", Response.Body.size(),
[=](size_t Offset, size_t Length) -> StringRef {
return Response.Body.substr(Offset, Length);
}});
};
TEST(HTTPServer, InvalidUrlPath) {
// test that we can bind to any address
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(InvalidUrlPathPattern, Handler),
Failed<StringError>());
EXPECT_THAT_EXPECTED(Server.bind(), Succeeded());
}
TEST(HTTPServer, bind) {
// test that we can bind to any address
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, Handler), Succeeded());
EXPECT_THAT_EXPECTED(Server.bind(), Succeeded());
}
TEST(HTTPServer, ListenBeforeBind) {
// test that we can bind to any address
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, Handler), Succeeded());
EXPECT_THAT_ERROR(Server.listen(), Failed<StringError>());
}
#ifdef LLVM_ENABLE_CURL
// Test the client and server against each other.
// Test fixture to initialize and teardown the HTTP client for each
// client-server test
class HTTPClientServerTest : public ::testing::Test {
protected:
void SetUp() override { HTTPClient::initialize(); }
void TearDown() override { HTTPClient::cleanup(); }
};
/// A simple handler which writes returned data to a string.
struct StringHTTPResponseHandler final : public HTTPResponseHandler {
std::string ResponseBody = "";
/// These callbacks store the body and status code in an HTTPResponseBuffer
/// allocated based on Content-Length. The Content-Length header must be
/// handled by handleHeaderLine before any calls to handleBodyChunk.
Error handleBodyChunk(StringRef BodyChunk) override {
ResponseBody = ResponseBody + BodyChunk.str();
return Error::success();
}
};
TEST_F(HTTPClientServerTest, Hello) {
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, Handler), Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, Response.Body);
EXPECT_EQ(Client.responseCode(), Response.Code);
Server.stop();
}
TEST_F(HTTPClientServerTest, LambdaHandlerHello) {
HTTPServer Server;
HTTPResponse LambdaResponse = {200u, "text/plain",
"hello, world from a lambda\n"};
EXPECT_THAT_ERROR(Server.get(UrlPathPattern,
[LambdaResponse](HTTPServerRequest &Request) {
Request.setResponse(LambdaResponse);
}),
Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, LambdaResponse.Body);
EXPECT_EQ(Client.responseCode(), LambdaResponse.Code);
Server.stop();
}
// Test the streaming response.
TEST_F(HTTPClientServerTest, StreamingHello) {
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, StreamingHandler), Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, Response.Body);
EXPECT_EQ(Client.responseCode(), Response.Code);
Server.stop();
}
// Writes a temporary file and streams it back using streamFile.
HTTPRequestHandler TempFileStreamingHandler = [](HTTPServerRequest Request) {
int FD;
SmallString<64> TempFilePath;
sys::fs::createTemporaryFile("http-stream-file-test", "temp", FD,
TempFilePath);
raw_fd_ostream OS(FD, true, /*unbuffered=*/true);
OS << Response.Body;
OS.close();
streamFile(Request, TempFilePath);
};
// Test streaming back chunks of a file.
TEST_F(HTTPClientServerTest, StreamingFileResponse) {
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, TempFileStreamingHandler),
Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, Response.Body);
EXPECT_EQ(Client.responseCode(), Response.Code);
Server.stop();
}
// Deletes the temporary file before streaming it back, should give a 404 not
// found status code.
HTTPRequestHandler MissingTempFileStreamingHandler =
[](HTTPServerRequest Request) {
int FD;
SmallString<64> TempFilePath;
sys::fs::createTemporaryFile("http-stream-file-test", "temp", FD,
TempFilePath);
raw_fd_ostream OS(FD, true, /*unbuffered=*/true);
OS << Response.Body;
OS.close();
// delete the file
sys::fs::remove(TempFilePath);
streamFile(Request, TempFilePath);
};
// Streaming a missing file should give a 404.
TEST_F(HTTPClientServerTest, StreamingMissingFileResponse) {
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, MissingTempFileStreamingHandler),
Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Client.responseCode(), 404u);
Server.stop();
}
TEST_F(HTTPClientServerTest, ClientTimeout) {
HTTPServer Server;
EXPECT_THAT_ERROR(Server.get(UrlPathPattern, DelayHandler), Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port);
HTTPClient Client;
// Timeout below 50ms, request should fail
Client.setTimeout(std::chrono::milliseconds(40));
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Failed<StringError>());
Server.stop();
}
// Check that Url paths are dispatched to the first matching handler and provide
// the correct path pattern match components.
TEST_F(HTTPClientServerTest, PathMatching) {
HTTPServer Server;
EXPECT_THAT_ERROR(
Server.get(R"(/abc/(.*)/(.*))",
[&](HTTPServerRequest &Request) {
EXPECT_EQ(Request.UrlPath, "/abc/1/2");
ASSERT_THAT(Request.UrlPathMatches,
testing::ElementsAre("1", "2"));
Request.setResponse({200u, "text/plain", Request.UrlPath});
}),
Succeeded());
EXPECT_THAT_ERROR(Server.get(UrlPathPattern,
[&](HTTPServerRequest &Request) {
llvm_unreachable(
"Should not reach this handler");
Handler(Request);
}),
Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port) + "/abc/1/2";
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, "/abc/1/2");
EXPECT_EQ(Client.responseCode(), 200u);
Server.stop();
}
TEST_F(HTTPClientServerTest, FirstPathMatched) {
HTTPServer Server;
EXPECT_THAT_ERROR(
Server.get(UrlPathPattern,
[&](HTTPServerRequest Request) { Handler(Request); }),
Succeeded());
EXPECT_THAT_ERROR(
Server.get(R"(/abc/(.*)/(.*))",
[&](HTTPServerRequest Request) {
EXPECT_EQ(Request.UrlPathMatches.size(), 2u);
llvm_unreachable("Should not reach this handler");
Request.setResponse({200u, "text/plain", Request.UrlPath});
}),
Succeeded());
Expected<unsigned> PortOrErr = Server.bind();
EXPECT_THAT_EXPECTED(PortOrErr, Succeeded());
unsigned Port = *PortOrErr;
ThreadPool Pool(hardware_concurrency(1));
Pool.async([&]() { EXPECT_THAT_ERROR(Server.listen(), Succeeded()); });
std::string Url = "http://localhost:" + utostr(Port) + "/abc/1/2";
HTTPRequest Request(Url);
StringHTTPResponseHandler Handler;
HTTPClient Client;
EXPECT_THAT_ERROR(Client.perform(Request, Handler), Succeeded());
EXPECT_EQ(Handler.ResponseBody, Response.Body);
EXPECT_EQ(Client.responseCode(), Response.Code);
Server.stop();
}
#endif
#else
TEST(HTTPServer, IsAvailable) { EXPECT_FALSE(HTTPServer::isAvailable()); }
#endif // LLVM_ENABLE_HTTPLIB
|