Skip to content

Commit

Permalink
Closes labstack#314
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Mar 11, 2016
1 parent 300d578 commit 09f3d30
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 8 additions & 18 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type (
}

HTTPError struct {
code int
message string
Code int
Message string
}

Middleware interface {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
6 changes: 3 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 09f3d30

Please sign in to comment.