Skip to content

Commit

Permalink
test header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dhconnelly committed Nov 23, 2024
1 parent 9dc9bb2 commit 22a255d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
72 changes: 53 additions & 19 deletions src/http/server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#include <memory>
#include <stdexcept>

#include "utils/logging.h"

using testing::AllOf;
using testing::Eq;
using testing::Field;
using testing::HasSubstr;
using testing::Pair;
using testing::UnorderedElementsAre;

namespace gabby {
namespace http {
Expand Down Expand Up @@ -81,26 +84,57 @@ std::string Call(int port, Method method, const std::string_view path,
return sock.ReadAll();
}

HttpServer TestServer(Handler h) {
SetGlobalLogLevel(LogLevel::DEBUG);
HttpServer server(kTestConfig, h);
return std::move(server);
class TestServer {
public:
TestServer(Handler h) : server_(kTestConfig, h) { server_.Start(); }

int port() { return server_.port(); }

~TestServer() {
server_.Stop();
server_.Wait();
}

private:
HttpServer server_;
};

TEST(HttpServer, ParseRequest) {
std::shared_ptr<std::atomic<bool>> done(new std::atomic(false));
Request received;
auto server =
TestServer([&received, done](const Request& req, ResponseWriter& resp) {
*done = true;
received = req;
resp.WriteStatus(StatusCode::OK);
});
auto result = Call(server.port(), Method::GET, "/foo", {});
EXPECT_THAT(result, HasSubstr("HTTP/1.1 200 OK"));
EXPECT_THAT(received, AllOf(Field(&Request::method, Method::GET),
Field(&Request::path, "/foo")));
}

TEST(HttpServer, Handle) {
TEST(HttpServer, ParseHeaders) {
std::shared_ptr<std::atomic<bool>> done(new std::atomic(false));
auto server = TestServer([done](const Request& req, ResponseWriter& resp) {
*done = true;
resp.WriteStatus(StatusCode::OK);
});
LOG(DEBUG) << "created test server, starting it";
server.Start();
LOG(DEBUG) << "calling test server";
auto resp = Call(server.port(), Method::GET, "/", {});
EXPECT_THAT(resp, HasSubstr("HTTP/1.1 200 OK"));
LOG(DEBUG) << "got response: " << resp;
server.Stop();
server.Wait();
Request received;
auto server =
TestServer([&received, done](const Request& req, ResponseWriter& resp) {
*done = true;
received = req;
resp.WriteStatus(StatusCode::OK);
});
auto result = Call(server.port(), Method::GET, "/foo",
{
{"a", "b"},
{"1", "2"},
});
EXPECT_THAT(result, HasSubstr("HTTP/1.1 200 OK"));
EXPECT_THAT(
received,
AllOf(Field(&Request::method, Method::GET),
Field(&Request::path, "/foo"),
Field(&Request::headers,
UnorderedElementsAre(Pair("a", "b"), Pair("1", "2")))));
}

} // namespace http
Expand Down
18 changes: 18 additions & 0 deletions src/http/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ std::string to_string(Method method) {
}
}

std::ostream& operator<<(std::ostream& os, Method method) {
return os << to_string(method);
}

std::ostream& operator<<(std::ostream& os, const Request& req) {
os << "{ ";
os << std::format("addr: {}", req.addr);
os << std::format(", method: {}", to_string(req.method));
os << std::format(", path: {}", req.path);
os << ", headers: { ";
for (const auto& [k, v] : req.headers) {
os << std::format("{}: {}", k, v);
}
os << " }";
os << " }";
return os;
}

void ResponseWriter::Write(std::string_view s) {
LOG(DEBUG) << "sending " << s.size() << " bytes";
if (s.size() == 0) return;
Expand Down
4 changes: 3 additions & 1 deletion src/http/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum class Method {
POST,
};

std::ostream& operator<<(std::ostream&, Method method);
std::string to_string(StatusCode code);
std::string to_string(Method method);

Expand All @@ -63,10 +64,11 @@ struct Request {
Method method;
std::string path;
std::unordered_map<std::string, std::string> headers;
std::unordered_map<std::string, std::string> params;
FILE* stream;
};

std::ostream& operator<<(std::ostream& os, const Request& req);

class ResponseWriter {
public:
explicit ResponseWriter(FILE* stream) : stream_(stream) {}
Expand Down

0 comments on commit 22a255d

Please sign in to comment.