88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package routes_test
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.deepak.science/deepak/gogmagog/routes"
|
|
"github.com/stretchr/testify/assert"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestEmptyHeatlhErrorWriter(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
|
|
m := getEmptyModel()
|
|
|
|
router := routes.NewRouter(m)
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
rr := NewBadWriter()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusInternalServerError, status)
|
|
|
|
}
|
|
|
|
func TestEmptyHealth(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
m := getEmptyModel()
|
|
router := routes.NewRouter(m)
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusOK, status)
|
|
expected := `[
|
|
{
|
|
"name": "Store",
|
|
"healthy": true,
|
|
"message": ""
|
|
}
|
|
]`
|
|
|
|
assert.JSONEq(expected, rr.Body.String())
|
|
contentType := rr.Header().Get("Content-Type")
|
|
assert.Equal("application/json", contentType)
|
|
}
|
|
|
|
func TestUnhealthyDB(t *testing.T) {
|
|
// set up
|
|
assert := assert.New(t)
|
|
errorMsg := "error"
|
|
m := getErrorModel(errorMsg)
|
|
router := routes.NewRouter(m)
|
|
req, _ := http.NewRequest("GET", "/health", nil)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// function under test
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// check results
|
|
status := rr.Code
|
|
assert.Equal(http.StatusInternalServerError, status)
|
|
expected := fmt.Sprintf(`[
|
|
{
|
|
"name": "Store",
|
|
"healthy": false,
|
|
"message": "%s"
|
|
}
|
|
]`, errorMsg)
|
|
|
|
assert.JSONEq(expected, rr.Body.String())
|
|
contentType := rr.Header().Get("Content-Type")
|
|
assert.Equal("application/json", contentType)
|
|
}
|