mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 23:10:49 +00:00
add error test
This commit is contained in:
parent
0859ec570c
commit
b2098266a3
@ -77,6 +77,7 @@ func (ctx *Context) Redirect(status int, localurl string) {
|
||||
// Abort stops this request.
|
||||
// if beego.ErrorMaps exists, panic body.
|
||||
func (ctx *Context) Abort(status int, body string) {
|
||||
ctx.Output.SetStatus(status)
|
||||
panic(body)
|
||||
}
|
||||
|
||||
|
73
error_test.go
Normal file
73
error_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package beego
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type errorTestController struct {
|
||||
Controller
|
||||
}
|
||||
|
||||
func (ec *errorTestController) Get() {
|
||||
errorCode, err := ec.GetInt("code")
|
||||
if err != nil {
|
||||
ec.Abort("parse code error")
|
||||
}
|
||||
if errorCode != 0 {
|
||||
ec.CustomAbort(errorCode, ec.GetString("code"))
|
||||
}
|
||||
ec.Abort("404")
|
||||
}
|
||||
|
||||
func TestErrorCode_01(t *testing.T) {
|
||||
registerDefaultErrorHandler()
|
||||
for k, _ := range ErrorMaps {
|
||||
r, _ := http.NewRequest("GET", "/error?code="+k, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler := NewControllerRegister()
|
||||
handler.Add("/error", &errorTestController{})
|
||||
handler.ServeHTTP(w, r)
|
||||
code, _ := strconv.Atoi(k)
|
||||
if w.Code != code {
|
||||
t.Fail()
|
||||
}
|
||||
if !strings.Contains(string(w.Body.Bytes()), http.StatusText(code)) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorCode_02(t *testing.T) {
|
||||
registerDefaultErrorHandler()
|
||||
r, _ := http.NewRequest("GET", "/error?code=0", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler := NewControllerRegister()
|
||||
handler.Add("/error", &errorTestController{})
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != 404 {
|
||||
t.Fail()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorCode_03(t *testing.T) {
|
||||
registerDefaultErrorHandler()
|
||||
r, _ := http.NewRequest("GET", "/error?code=crash", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler := NewControllerRegister()
|
||||
handler.Add("/error", &errorTestController{})
|
||||
handler.ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Fail()
|
||||
}
|
||||
if string(w.Body.Bytes()) != "parse code error" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user