mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
parent
300d57894d
commit
09f3d3061f
@ -253,7 +253,7 @@ func testBindError(t *testing.T, c Context, ct string) {
|
||||
switch ct {
|
||||
case ApplicationJSON, ApplicationXML:
|
||||
if assert.IsType(t, new(HTTPError), err) {
|
||||
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).code)
|
||||
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code)
|
||||
}
|
||||
default:
|
||||
if assert.IsType(t, new(HTTPError), err) {
|
||||
|
26
echo.go
26
echo.go
@ -41,8 +41,8 @@ type (
|
||||
}
|
||||
|
||||
HTTPError struct {
|
||||
code int
|
||||
message string
|
||||
Code int
|
||||
Message string
|
||||
}
|
||||
|
||||
Middleware interface {
|
||||
@ -240,8 +240,8 @@ func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
|
||||
code := http.StatusInternalServerError
|
||||
msg := http.StatusText(code)
|
||||
if he, ok := err.(*HTTPError); ok {
|
||||
code = he.code
|
||||
msg = he.message
|
||||
code = he.Code
|
||||
msg = he.Message
|
||||
}
|
||||
if e.debug {
|
||||
msg = err.Error()
|
||||
@ -426,27 +426,17 @@ func (e *Echo) Run(eng engine.Engine) {
|
||||
}
|
||||
|
||||
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||
he := &HTTPError{code: code, message: http.StatusText(code)}
|
||||
he := &HTTPError{Code: code, Message: http.StatusText(code)}
|
||||
if len(msg) > 0 {
|
||||
m := msg[0]
|
||||
he.message = m
|
||||
he.Message = m
|
||||
}
|
||||
return he
|
||||
}
|
||||
|
||||
// SetCode sets code.
|
||||
func (e *HTTPError) SetCode(code int) {
|
||||
e.code = code
|
||||
}
|
||||
|
||||
// Code returns code.
|
||||
func (e *HTTPError) Code() int {
|
||||
return e.code
|
||||
}
|
||||
|
||||
// Error returns message.
|
||||
// Error makes it compatible with `error` interface.
|
||||
func (e *HTTPError) Error() string {
|
||||
return e.message
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (binder) Bind(i interface{}, c Context) (err error) {
|
||||
|
@ -263,7 +263,7 @@ func TestEchoMethodNotAllowed(t *testing.T) {
|
||||
func TestEchoHTTPError(t *testing.T) {
|
||||
m := http.StatusText(http.StatusBadRequest)
|
||||
he := NewHTTPError(http.StatusBadRequest, m)
|
||||
assert.Equal(t, http.StatusBadRequest, he.Code())
|
||||
assert.Equal(t, http.StatusBadRequest, he.Code)
|
||||
assert.Equal(t, m, he.Error())
|
||||
}
|
||||
|
||||
|
6
glide.lock
generated
6
glide.lock
generated
@ -1,5 +1,5 @@
|
||||
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
||||
updated: 2016-03-06T08:57:09.146198268-08:00
|
||||
updated: 2016-03-11T07:51:13.670742959-08:00
|
||||
imports:
|
||||
- name: github.com/klauspost/compress
|
||||
version: 2d3d403f37d2e70b722590bc286076a17422e1f2
|
||||
@ -21,9 +21,9 @@ imports:
|
||||
- name: github.com/mattn/go-isatty
|
||||
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
|
||||
- name: github.com/valyala/fasthttp
|
||||
version: 57df0ba8a413b7f236b6f64aa6b8b5419ffe828f
|
||||
version: ca2c5535a325ab129d46891de0bbe84261822b3c
|
||||
- name: golang.org/x/net
|
||||
version: 08f168e593b5aab61849054b77981de812666697
|
||||
version: e7da8edaa52631091740908acaf2c2d4c9b3ce90
|
||||
subpackages:
|
||||
- context
|
||||
- websocket
|
||||
|
@ -38,19 +38,19 @@ func TestBasicAuth(t *testing.T) {
|
||||
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
||||
req.Header().Set(echo.Authorization, auth)
|
||||
he := h.Handle(c).(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
||||
|
||||
// Empty Authorization header
|
||||
req.Header().Set(echo.Authorization, "")
|
||||
he = h.Handle(c).(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
||||
|
||||
// Invalid Authorization header
|
||||
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
|
||||
req.Header().Set(echo.Authorization, auth)
|
||||
he = h.Handle(c).(*echo.HTTPError)
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
||||
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
||||
assert.Equal(t, basic+" realm=Restricted", res.Header().Get(echo.WWWAuthenticate))
|
||||
}
|
||||
|
@ -411,7 +411,7 @@ func TestRouterMultiRoute(t *testing.T) {
|
||||
c = NewContext(nil, nil, e)
|
||||
r.Find(GET, "/user", c)
|
||||
he := c.Handle(c).(*HTTPError)
|
||||
assert.Equal(t, http.StatusNotFound, he.code)
|
||||
assert.Equal(t, http.StatusNotFound, he.Code)
|
||||
}
|
||||
|
||||
func TestRouterPriority(t *testing.T) {
|
||||
@ -514,7 +514,7 @@ func TestRouterPriorityNotFound(t *testing.T) {
|
||||
c = NewContext(nil, nil, e)
|
||||
r.Find(GET, "/abc/def", c)
|
||||
he := c.Handle(c).(*HTTPError)
|
||||
assert.Equal(t, http.StatusNotFound, he.Code())
|
||||
assert.Equal(t, http.StatusNotFound, he.Code)
|
||||
}
|
||||
|
||||
func TestRouterParamNames(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user