mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 06:00:54 +00:00
added tests for health check endpoints
This commit is contained in:
parent
db547a7c84
commit
8d1a9bc92e
@ -2,11 +2,30 @@ package beego
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego/toolbox"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SampleDatabaseCheck struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type SampleCacheCheck struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dc *SampleDatabaseCheck) Check() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *SampleCacheCheck) Check() error {
|
||||||
|
return errors.New("no cache detected")
|
||||||
|
}
|
||||||
|
|
||||||
func TestList_01(t *testing.T) {
|
func TestList_01(t *testing.T) {
|
||||||
m := make(M)
|
m := make(M)
|
||||||
list("BConfig", BConfig, m)
|
list("BConfig", BConfig, m)
|
||||||
@ -101,3 +120,57 @@ func TestExecJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHealthCheckHandlerDefault(t *testing.T) {
|
||||||
|
endpointPath := "/healthcheck"
|
||||||
|
|
||||||
|
toolbox.AddHealthCheck("database", &SampleDatabaseCheck{})
|
||||||
|
toolbox.AddHealthCheck("cache", &SampleCacheCheck{})
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", endpointPath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(healthcheck)
|
||||||
|
|
||||||
|
handler.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
if status := w.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
if !strings.Contains(w.Body.String(), "database") {
|
||||||
|
t.Errorf("Expected 'database' in generated template.")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHealthCheckHandlerReturnsJSON(t *testing.T) {
|
||||||
|
|
||||||
|
toolbox.AddHealthCheck("database", &SampleDatabaseCheck{})
|
||||||
|
toolbox.AddHealthCheck("cache", &SampleCacheCheck{})
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "/healthcheck?json=true", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(healthcheck)
|
||||||
|
|
||||||
|
handler.ServeHTTP(w, req)
|
||||||
|
if status := w.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedResponseBody := `[{"message":"database","name":"success","status":"OK"},{"message":"cache","name":"error","status":"no cache detected"}]`
|
||||||
|
if w.Body.String() != expectedResponseBody {
|
||||||
|
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||||
|
w.Body.String(), expectedResponseBody)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user