Skip to content

Commit

Permalink
Zhaojkun httperror message (labstack#959)
Browse files Browse the repository at this point in the history
* Change echo http error message

* Add test case

* Fixed test cases

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr authored Jun 20, 2017
1 parent 7676f85 commit 687f054
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func NewHTTPError(code int, message ...interface{}) *HTTPError {

// Error makes it compatible with `error` interface.
func (he *HTTPError) Error() string {
return fmt.Sprintf("code=%d, message=%s", he.Code, he.Message)
return fmt.Sprintf("code=%d, message=%v", he.Code, he.Message)
}

// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
Expand Down
33 changes: 20 additions & 13 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestEchoURL(t *testing.T) {

func TestEchoRoutes(t *testing.T) {
e := New()
routes := []Route{
routes := []*Route{
{GET, "/users/:user/events", ""},
{GET, "/users/:user/events/public", ""},
{POST, "/repos/:owner/:repo/git/refs", ""},
Expand All @@ -290,16 +290,18 @@ func TestEchoRoutes(t *testing.T) {
})
}

for _, r := range e.Routes() {
found := false
for _, rr := range routes {
if r.Method == rr.Method && r.Path == rr.Path {
found = true
break
if assert.Equal(t, len(routes), len(e.Routes())) {
for _, r := range e.Routes() {
found := false
for _, rr := range routes {
if r.Method == rr.Method && r.Path == rr.Path {
found = true
break
}
}
if !found {
t.Errorf("Route %s %s not found", r.Method, r.Path)
}
}
if !found {
t.Errorf("Route %s : %s not found", r.Method, r.Path)
}
}
}
Expand Down Expand Up @@ -423,9 +425,7 @@ func testMethod(t *testing.T, method, path string, e *Echo) {
i := interface{}(e)
reflect.ValueOf(i).MethodByName(method).Call([]reflect.Value{p, h})
_, body := request(method, path, e)
if body != method {
t.Errorf("expected body `%s`, got %s", method, body)
}
assert.Equal(t, method, body)
}

func request(method, path string, e *Echo) (int, string) {
Expand All @@ -434,3 +434,10 @@ func request(method, path string, e *Echo) (int, string) {
e.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}

func TestHTTPError(t *testing.T) {
err := NewHTTPError(400, map[string]interface{}{
"code": 12,
})
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
}
14 changes: 7 additions & 7 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
staticRoutes = []Route{
staticRoutes = []*Route{
{"GET", "/", ""},
{"GET", "/cmd.html", ""},
{"GET", "/code.html", ""},
Expand Down Expand Up @@ -170,7 +170,7 @@ var (
{"GET", "/progs/update.bash", ""},
}

gitHubAPI = []Route{
gitHubAPI = []*Route{
// OAuth Authorizations
{"GET", "/authorizations", ""},
{"GET", "/authorizations/:id", ""},
Expand Down Expand Up @@ -433,7 +433,7 @@ var (
{"DELETE", "/user/keys/:id", ""},
}

parseAPI = []Route{
parseAPI = []*Route{
// Objects
{"POST", "/1/classes/:className", ""},
{"GET", "/1/classes/:className/:objectId", ""},
Expand Down Expand Up @@ -477,7 +477,7 @@ var (
{"POST", "/1/functions", ""},
}

googlePlusAPI = []Route{
googlePlusAPI = []*Route{
// People
{"GET", "/people/:userId", ""},
{"GET", "/people", ""},
Expand Down Expand Up @@ -839,7 +839,7 @@ func TestRouterStaticDynamicConflict(t *testing.T) {
assert.Equal(t, 3, c.Get("c"))
}

func testRouterAPI(t *testing.T, api []Route) {
func testRouterAPI(t *testing.T, api []*Route) {
e := New()
r := e.router

Expand All @@ -866,15 +866,15 @@ func TestRouterGitHubAPI(t *testing.T) {

// Issue #729
func TestRouterParamAlias(t *testing.T) {
api := []Route{
api := []*Route{
{GET, "/users/:userID/following", ""},
{GET, "/users/:userID/followedBy", ""},
{GET, "/users/:userID/follow", ""},
}
testRouterAPI(t, api)
}

func benchmarkRouterRoutes(b *testing.B, routes []Route) {
func benchmarkRouterRoutes(b *testing.B, routes []*Route) {
e := New()
r := e.router
b.ReportAllocs()
Expand Down

0 comments on commit 687f054

Please sign in to comment.