Skip to content

Commit

Permalink
Tests for HEAD /health
Browse files Browse the repository at this point in the history
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
  • Loading branch information
eternal-flame-AD committed Sep 20, 2024
1 parent d13cc40 commit 447d361
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
39 changes: 33 additions & 6 deletions api/health.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package api

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/render"
"github.com/gotify/server/v2/model"
)

Expand Down Expand Up @@ -32,15 +35,39 @@ type HealthAPI struct {
// schema:
// $ref: "#/definitions/Health"
func (a *HealthAPI) Health(ctx *gin.Context) {
var response *model.Health
if err := a.DB.Ping(); err != nil {
ctx.JSON(500, model.Health{
response = &model.Health{
Health: model.StatusOrange,
Database: model.StatusRed,
})
}
} else {
response = &model.Health{
Health: model.StatusGreen,
Database: model.StatusGreen,
}
}

status := 500
if response.Database == model.StatusGreen || response.Health == model.StatusGreen {
status = 200
}

renderer := render.JSON{Data: response}

// in case this is called from a non http request
if ctx.Request == nil {
ctx.Render(status, renderer)
return
}
ctx.JSON(200, model.Health{
Health: model.StatusGreen,
Database: model.StatusGreen,
})

switch ctx.Request.Method {
case "HEAD":
renderer.WriteContentType(ctx.Writer)
ctx.Status(status)
case "GET":
ctx.Render(status, renderer)
default:
ctx.AbortWithStatus(http.StatusMethodNotAllowed)
}
}
42 changes: 42 additions & 0 deletions api/health_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"net/http"
"net/http/httptest"
"testing"

Expand All @@ -9,9 +10,19 @@ import (
"github.com/gotify/server/v2/model"
"github.com/gotify/server/v2/test"
"github.com/gotify/server/v2/test/testdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func testConsistentHead(t *testing.T, head http.Header, get http.Header) {

Check failure on line 17 in api/health_test.go

View workflow job for this annotation

GitHub Actions / gotify

File is not `gofumpt`-ed with `-extra` (gofumpt)
// The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request method had been GET.
// However, a server MAY omit header fields for which a value is determined only while generating the content.
assert.Empty(t, head.Get("Content-Length"), "Content-Length should be empty")
assert.Equal(t, get.Get("Content-Type"), head.Get("Content-Type"), "Content-Type should be the same")
assert.Equal(t, get.Get("Transfer-Encoding"), head.Get("Transfer-Encoding"), "Transfer-Encoding should be the same")
assert.Equal(t, get.Get("Connection"), head.Get("Connection"), "Connection should be the same")
}

func TestHealthSuite(t *testing.T) {
suite.Run(t, new(HealthSuite))
}
Expand All @@ -38,12 +49,43 @@ func (s *HealthSuite) AfterTest(suiteName, testName string) {
}

func (s *HealthSuite) TestHealthSuccess() {
head, err := http.NewRequest("HEAD", "/health", nil)
if err != nil {
s.T().Fatal(err)
}
s.ctx.Request = head
s.a.Health(s.ctx)
headHeaders := s.recorder.Header().Clone()

request, err := http.NewRequest("GET", "/health", nil)
if err != nil {
s.T().Fatal(err)
}
s.ctx.Request = request
s.a.Health(s.ctx)

testConsistentHead(s.T(), headHeaders, s.recorder.Header())
test.BodyEquals(s.T(), model.Health{Health: model.StatusGreen, Database: model.StatusGreen}, s.recorder)
}

func (s *HealthSuite) TestDatabaseFailure() {
s.db.Close()

head, err := http.NewRequest("HEAD", "/health", nil)
if err != nil {
s.T().Fatal(err)
}
s.ctx.Request = head
s.a.Health(s.ctx)
headHeaders := s.recorder.Header().Clone()

request, err := http.NewRequest("GET", "/health", nil)
if err != nil {
s.T().Fatal(err)
}
s.ctx.Request = request
s.a.Health(s.ctx)

testConsistentHead(s.T(), headHeaders, s.recorder.Header())
test.BodyEquals(s.T(), model.Health{Health: model.StatusOrange, Database: model.StatusRed}, s.recorder)
}

0 comments on commit 447d361

Please sign in to comment.