mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:00:55 +00:00
added test for buildingHealthCheckResponse
This commit is contained in:
parent
469dc7bea9
commit
e0f8c6832d
16
admin.go
16
admin.go
@ -326,8 +326,8 @@ func healthcheck(rw http.ResponseWriter, r *http.Request) {
|
|||||||
shouldReturnJSON, _ := strconv.ParseBool(jsonFlag)
|
shouldReturnJSON, _ := strconv.ParseBool(jsonFlag)
|
||||||
|
|
||||||
if shouldReturnJSON {
|
if shouldReturnJSON {
|
||||||
responseMap := buildHealthCheckResponseMap(resultList)
|
response := buildHealthCheckResponseList(resultList)
|
||||||
jsonResponse, err := json.Marshal(responseMap)
|
jsonResponse, err := json.Marshal(response)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
@ -344,15 +344,15 @@ func healthcheck(rw http.ResponseWriter, r *http.Request) {
|
|||||||
writeTemplate(rw, data, healthCheckTpl, defaultScriptsTpl)
|
writeTemplate(rw, data, healthCheckTpl, defaultScriptsTpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildHealthCheckResponseMap(resultList *[][]string) []map[string]interface{} {
|
func buildHealthCheckResponseList(healthCheckResults *[][]string) []map[string]interface{} {
|
||||||
response := make([]map[string]interface{}, len(*resultList))
|
response := make([]map[string]interface{}, len(*healthCheckResults))
|
||||||
|
|
||||||
for i, currentResult := range *resultList {
|
for i, healthCheckResult := range *healthCheckResults {
|
||||||
currentResultMap := make(map[string]interface{})
|
currentResultMap := make(map[string]interface{})
|
||||||
|
|
||||||
currentResultMap["name"] = currentResult[0]
|
currentResultMap["name"] = healthCheckResult[0]
|
||||||
currentResultMap["message"] = currentResult[1]
|
currentResultMap["message"] = healthCheckResult[1]
|
||||||
currentResultMap["status"] = currentResult[2]
|
currentResultMap["status"] = healthCheckResult[2]
|
||||||
|
|
||||||
response[i] = currentResultMap
|
response[i] = currentResultMap
|
||||||
}
|
}
|
||||||
|
@ -149,6 +149,41 @@ func TestHealthCheckHandlerDefault(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildHealthCheckResponseList(t *testing.T) {
|
||||||
|
healthCheckResults := [][]string{
|
||||||
|
[]string{
|
||||||
|
"error",
|
||||||
|
"Database",
|
||||||
|
"Error occured whie starting the db",
|
||||||
|
},
|
||||||
|
[]string{
|
||||||
|
"success",
|
||||||
|
"Cache",
|
||||||
|
"Cache started successfully",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
responseList := buildHealthCheckResponseList(&healthCheckResults)
|
||||||
|
|
||||||
|
if len(responseList) != len(healthCheckResults) {
|
||||||
|
t.Errorf("invalid response map length: got %d want %d",
|
||||||
|
len(responseList), len(healthCheckResults))
|
||||||
|
}
|
||||||
|
|
||||||
|
responseFields := []string{"name", "message", "status"}
|
||||||
|
|
||||||
|
for _, response := range responseList {
|
||||||
|
for _, field := range responseFields {
|
||||||
|
_, ok := response[field]
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("expected %s to be in the response %v", field, response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestHealthCheckHandlerReturnsJSON(t *testing.T) {
|
func TestHealthCheckHandlerReturnsJSON(t *testing.T) {
|
||||||
|
|
||||||
toolbox.AddHealthCheck("database", &SampleDatabaseCheck{})
|
toolbox.AddHealthCheck("database", &SampleDatabaseCheck{})
|
||||||
|
Loading…
Reference in New Issue
Block a user