Skip to content

Commit

Permalink
Add head support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Baranick committed May 2, 2018
1 parent 6b0a047 commit a693222
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Started Consulate server on :8080

## Routes

All routes accept the following query string parameters:
All routes respond to both GET and HEAD requests. They accept the following query string parameters:

1. `pretty`: when present, pretty prints json responses
1. `verbose`: when present, additional details are include in responses
Expand Down Expand Up @@ -666,6 +666,9 @@ Error Set:

## Changelog

### 0.0.3
* Add HEAD support for all methods.

### 0.0.2
* Adjust status code semantics to follow Consul health check semantics. This results in some changes to the status codes returned.
* Made all status code configurable.
Expand Down
19 changes: 12 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ func (r *server) createRouter() *gin.Engine {
router.Use(gin.Logger())
router.Use(gin.Recovery())
r.attachPrometheusMiddleware(router)
router.GET(aboutRoute, r.about)
router.GET(healthRoute, r.health)
router.GET(verifyAllChecksRoute, r.verifyAllChecks)
router.GET(verifyCheckIdRoute, r.verifyCheckId)
router.GET(verifyCheckNameRoute, r.verifyCheckName)
router.GET(verifyServiceIdRoute, r.verifyServiceId)
router.GET(verifyServiceNameRoute, r.verifyServiceName)
r.handle(router, aboutRoute, r.about)
r.handle(router, healthRoute, r.health)
r.handle(router, verifyAllChecksRoute, r.verifyAllChecks)
r.handle(router, verifyCheckIdRoute, r.verifyCheckId)
r.handle(router, verifyCheckNameRoute, r.verifyCheckName)
r.handle(router, verifyServiceIdRoute, r.verifyServiceId)
r.handle(router, verifyServiceNameRoute, r.verifyServiceName)
return router
}

Expand All @@ -149,6 +149,11 @@ func (r *server) attachPrometheusMiddleware(engine *gin.Engine) {
prometheusMiddleware.Use(engine)
}

func (r *server) handle(router *gin.Engine, relativePath string, handler gin.HandlerFunc) {
router.GET(relativePath, handler)
router.HEAD(relativePath, handler)
}

func (r *server) createClient() {
r.httpClient = *client.CreateClient(&r.config.ClientConfig)
}
Expand Down
15 changes: 15 additions & 0 deletions server_test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ func TestApi(t *testing.T) {
}

func verifyApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
verifyHeadApiCall(t, s, d)
verifyGetApiCall(t, s, d)
}

func verifyHeadApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
r, err := s.Client().Head(s.Url(d.path))
if err != nil {
t.Error(err)
}
if r.StatusCode != d.statusCode {
t.Errorf("%q => StatusCode: %v, want %v", d.path, r.StatusCode, d.statusCode)
}
}

func verifyGetApiCall(t *testing.T, s *testutil.WrappedTestServer, d apiTestData) {
r, err := s.Client().Get(s.Url(d.path))
if r != nil && r.Body != nil {
defer r.Body.Close()
Expand Down

0 comments on commit a693222

Please sign in to comment.