Skip to content

Commit 5cd8c6a

Browse files
authored
feat(api): add HEAD method for health route (gotenberg#963)
1 parent a3647fe commit 5cd8c6a

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

pkg/modules/api/api.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,22 @@ func (a *Api) Start() error {
453453
)
454454
}
455455

456-
// Let's not forget the health check route...
456+
// Let's not forget the health check routes...
457+
checks := append(a.healthChecks, health.WithTimeout(a.timeout))
458+
checker := health.NewChecker(checks...)
459+
healthCheckHandler := health.NewHandler(checker)
460+
457461
a.srv.GET(
458462
fmt.Sprintf("%s%s", a.rootPath, "health"),
459463
func() echo.HandlerFunc {
460-
checks := append(a.healthChecks, health.WithTimeout(a.timeout))
461-
checker := health.NewChecker(checks...)
462-
return echo.WrapHandler(health.NewHandler(checker))
464+
return echo.WrapHandler(healthCheckHandler)
465+
}(),
466+
hardTimeoutMiddleware(hardTimeout),
467+
)
468+
a.srv.HEAD(
469+
fmt.Sprintf("%s%s", a.rootPath, "health"),
470+
func() echo.HandlerFunc {
471+
return echo.WrapHandler(healthCheckHandler)
463472
}(),
464473
hardTimeoutMiddleware(hardTimeout),
465474
)

pkg/modules/api/api_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,18 +839,23 @@ func TestApi_Start(t *testing.T) {
839839
return
840840
}
841841

842-
// health request.
842+
// health requests.
843843
recorder := httptest.NewRecorder()
844-
healthRequest := httptest.NewRequest(http.MethodGet, "/health", nil)
845844

846-
mod.srv.ServeHTTP(recorder, healthRequest)
845+
healthGetRequest := httptest.NewRequest(http.MethodGet, "/health", nil)
846+
mod.srv.ServeHTTP(recorder, healthGetRequest)
847+
if recorder.Code != http.StatusOK {
848+
t.Errorf("expected %d status code but got %d", http.StatusOK, recorder.Code)
849+
}
850+
851+
healthHeadRequest := httptest.NewRequest(http.MethodHead, "/health", nil)
852+
mod.srv.ServeHTTP(recorder, healthHeadRequest)
847853
if recorder.Code != http.StatusOK {
848854
t.Errorf("expected %d status code but got %d", http.StatusOK, recorder.Code)
849855
}
850856

851857
// version request.
852858
versionRequest := httptest.NewRequest(http.MethodGet, "/version", nil)
853-
854859
mod.srv.ServeHTTP(recorder, versionRequest)
855860
if recorder.Code != http.StatusOK {
856861
t.Errorf("expected %d status code but got %d", http.StatusOK, recorder.Code)
@@ -859,7 +864,6 @@ func TestApi_Start(t *testing.T) {
859864
// "multipart/form-data" request.
860865
multipartRequest := func(url string) *http.Request {
861866
body := &bytes.Buffer{}
862-
863867
writer := multipart.NewWriter(body)
864868

865869
defer func() {

0 commit comments

Comments
 (0)