Skip to content

Commit b0f502d

Browse files
committed
added HEAD support for some endpoints
1 parent da8555e commit b0f502d

6 files changed

Lines changed: 53 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.2.3] - 2023-08-25
13+
### Added
14+
- Added HEAD support for the following endpoints:
15+
- `/`, `/api/`, `/api/health`, `/api/health-check`
16+
1217
## [1.2.2] - 2023-08-24
1318
### Added
1419
- Added support for `/api/kb/details` endpoint
@@ -66,3 +71,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6671
[1.2.0]: https://github.com/scanoss/api.go/compare/v1.1.0...v1.2.0
6772
[1.2.1]: https://github.com/scanoss/api.go/compare/v1.2.0...v1.2.1
6873
[1.2.2]: https://github.com/scanoss/api.go/compare/v1.2.1...v1.2.2
74+
[1.2.3]: https://github.com/scanoss/api.go/compare/v1.2.2...v1.2.3

pkg/protocol/rest/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ func RunServer(config *myconfig.ServerConfig) error {
5858
// Set up the endpoint routing
5959
router := mux.NewRouter().StrictSlash(true)
6060
router.HandleFunc("/", service.WelcomeMsg).Methods(http.MethodGet)
61+
router.HandleFunc("/", service.HeadResponse).Methods(http.MethodHead)
6162
router.HandleFunc("/api/", service.WelcomeMsg).Methods(http.MethodGet)
63+
router.HandleFunc("/api/", service.HeadResponse).Methods(http.MethodHead)
6264
router.HandleFunc("/api/health", service.HealthCheck).Methods(http.MethodGet)
65+
router.HandleFunc("/api/health", service.HeadResponse).Methods(http.MethodHead)
6366
router.HandleFunc("/api/health-check", service.HealthCheck).Methods(http.MethodGet)
67+
router.HandleFunc("/api/health-check", service.HeadResponse).Methods(http.MethodHead)
6468
router.HandleFunc("/api/metrics/{type}", service.MetricsHandler).Methods(http.MethodGet)
6569
router.HandleFunc("/api/file_contents/{md5}", apiService.FileContents).Methods(http.MethodGet)
6670
router.HandleFunc("/api/kb/details", apiService.KBDetails).Methods(http.MethodGet)

pkg/service/utils_service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func HealthCheck(w http.ResponseWriter, r *http.Request) {
9595
printResponse(w, fmt.Sprintln(`{"alive": true}`), zlog.S, true)
9696
}
9797

98+
// HeadResponse responds with the HEAD OK Status for the requested path.
99+
func HeadResponse(w http.ResponseWriter, r *http.Request) {
100+
zlog.S.Debugf("%v HEAD request from %v", r.URL.Path, r.RemoteAddr)
101+
w.WriteHeader(http.StatusOK)
102+
}
103+
98104
// MetricsHandler responds with the metrics for the requested type.
99105
func MetricsHandler(w http.ResponseWriter, r *http.Request) {
100106
vars := mux.Vars(r)

pkg/service/utils_service_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,23 @@ func TestApiService(t *testing.T) {
193193

194194
printResponse(w, "test message", zs, false)
195195
}
196+
197+
func TestHeadResponse(t *testing.T) {
198+
err := zlog.NewSugaredDevLogger()
199+
if err != nil {
200+
t.Fatalf("an error '%s' was not expected when opening a sugared logger", err)
201+
}
202+
defer zlog.SyncZap()
203+
req := httptest.NewRequest(http.MethodHead, "http://localhost/api/health", nil)
204+
w := httptest.NewRecorder()
205+
HeadResponse(w, req)
206+
resp := w.Result()
207+
body, err := io.ReadAll(resp.Body)
208+
if err != nil {
209+
t.Fatalf("an error was not expected when reading from request: %v", err)
210+
}
211+
assert.Equal(t, http.StatusOK, resp.StatusCode)
212+
fmt.Println("Status: ", resp.StatusCode)
213+
fmt.Println("Type: ", resp.Header.Get("Content-Type"))
214+
fmt.Println("Body: ", string(body))
215+
}

tests/curl_commands.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ curl -X GET http://localhost:5443/api/license/obligations/MIT > obligations.txt
2020

2121
curl -X GET http://localhost:5443/api/api/health-check
2222

23+
curl --head http://localhost:5443/api/health-check
24+
2325
# Service Metrics
2426

2527
curl -X GET http://localhost:5443/api/api/metrics/all

tests/healthcheck_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ func (s *E2EHealthSuite) TestHappyWelcomeMsg() {
4848
fmt.Println("Status: ", resp.StatusCode)
4949
fmt.Println("Type: ", resp.Header.Get("Content-Type"))
5050
fmt.Println("Body: ", bodyStr)
51+
52+
resp2, err := c.Head(fmt.Sprintf("%v/", hostPort))
53+
if err != nil {
54+
s.Failf("an error was not expected when sending request.", "error: %v", err)
55+
}
56+
s.Equal(http.StatusOK, resp2.StatusCode)
57+
fmt.Println("Status: ", resp.StatusCode)
5158
}
5259

5360
func (s *E2EHealthSuite) TestHappyHealthcheck() {
@@ -66,6 +73,14 @@ func (s *E2EHealthSuite) TestHappyHealthcheck() {
6673
fmt.Println("Status: ", resp.StatusCode)
6774
fmt.Println("Type: ", resp.Header.Get("Content-Type"))
6875
fmt.Println("Body: ", bodyStr)
76+
77+
// Test the HEAD call also
78+
resp2, err := c.Head(fmt.Sprintf("%v/api/health-check", hostPort))
79+
if err != nil {
80+
s.Failf("an error was not expected when sending request.", "error: %v", err)
81+
}
82+
s.Equal(http.StatusOK, resp2.StatusCode)
83+
fmt.Println("Status: ", resp.StatusCode)
6984
}
7085

7186
func (s *E2EHealthSuite) TestMetrics() {

0 commit comments

Comments
 (0)