Commit 6e94b3ed authored by jaywheelT's avatar jaywheelT

Add support router HEAD request

parent feaf5a48
......@@ -227,6 +227,7 @@ public:
void options(const std::string& resource, Route::Handler handler);
void addRoute(Http::Method method, const std::string& resource, Route::Handler handler);
void removeRoute(Http::Method method, const std::string& resource);
void head(const std::string& resource, Route::Handler handler);
void addCustomHandler(Route::Handler handler);
......@@ -313,6 +314,7 @@ namespace Routes {
void Delete(Router& router, const std::string& resource, Route::Handler handler);
void Options(Router& router, const std::string& resource, Route::Handler handler);
void Remove(Router& router, Http::Method method, const std::string& resource);
void Head(Router& router, const std::string& resource, Route::Handler handler);
void NotFound(Router& router, Route::Handler handler);
......
......@@ -399,6 +399,11 @@ Router::removeRoute(Http::Method method, const std::string& resource) {
r.removeRoute(path);
}
void
Router::head(const std::string& resource, Route::Handler handler) {
addRoute(Http::Method::Head, resource, std::move(handler));
}
void
Router::addCustomHandler(Route::Handler handler) {
customHandlers.push_back(std::move(handler));
......@@ -517,6 +522,10 @@ void NotFound(Router& router, Route::Handler handler) {
router.addNotFoundHandler(std::move(handler));
}
void Head(Router& router, const std::string& resource, Route::Handler handler) {
router.head(resource, std::move(handler));
}
} // namespace Routes
} // namespace Rest
} // namespace Pistache
......@@ -204,3 +204,34 @@ TEST(router_test, test_notfound_exactly_once) {
endpoint->shutdown();
}
TEST(router_test, test_route_head_request) {
Address addr(Ipv4::any(), 0);
auto endpoint = std::make_shared<Http::Endpoint>(addr);
auto opts = Http::Endpoint::options().threads(1).maxRequestSize(4096);
endpoint->init(opts);
int count_found = 0;
Rest::Router router;
Routes::Head(router, "/moogle", [&count_found](
const Pistache::Rest::Request&,
Pistache::Http::ResponseWriter response) {
count_found++;
response.send(Pistache::Http::Code::Ok);
return Pistache::Rest::Route::Result::Ok;
});
endpoint->setHandler(router.handler());
endpoint->serveThreaded();
const auto bound_port = endpoint->getPort();
httplib::Client client("localhost", bound_port);
count_found = 0;
client.Head("/moogle");
ASSERT_EQ(count_found, 1);
endpoint->shutdown();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment