Skip to content

Commit

Permalink
Merge pull request #616 from jaywheelT/router-head-request
Browse files Browse the repository at this point in the history
Add support router HEAD request
  • Loading branch information
dennisjenkins75 authored Oct 15, 2019
2 parents feaf5a4 + 6e94b3e commit 1c9d71a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/pistache/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Router {
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);

Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/server/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions tests/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 1c9d71a

Please sign in to comment.