mirror of
				https://github.com/labstack/echo.git
				synced 2025-10-30 23:57:38 +02:00 
			
		
		
		
	Zhaojkun httperror message (#959)
* Change echo http error message * Add test case * Fixed test cases Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
		
							
								
								
									
										2
									
								
								echo.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								echo.go
									
									
									
									
									
								
							| @@ -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`. | ||||
|   | ||||
							
								
								
									
										33
									
								
								echo_test.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								echo_test.go
									
									
									
									
									
								
							| @@ -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", ""}, | ||||
| @@ -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) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -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) { | ||||
| @@ -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()) | ||||
| } | ||||
|   | ||||
| @@ -10,7 +10,7 @@ import ( | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	staticRoutes = []Route{ | ||||
| 	staticRoutes = []*Route{ | ||||
| 		{"GET", "/", ""}, | ||||
| 		{"GET", "/cmd.html", ""}, | ||||
| 		{"GET", "/code.html", ""}, | ||||
| @@ -170,7 +170,7 @@ var ( | ||||
| 		{"GET", "/progs/update.bash", ""}, | ||||
| 	} | ||||
|  | ||||
| 	gitHubAPI = []Route{ | ||||
| 	gitHubAPI = []*Route{ | ||||
| 		// OAuth Authorizations | ||||
| 		{"GET", "/authorizations", ""}, | ||||
| 		{"GET", "/authorizations/:id", ""}, | ||||
| @@ -433,7 +433,7 @@ var ( | ||||
| 		{"DELETE", "/user/keys/:id", ""}, | ||||
| 	} | ||||
|  | ||||
| 	parseAPI = []Route{ | ||||
| 	parseAPI = []*Route{ | ||||
| 		// Objects | ||||
| 		{"POST", "/1/classes/:className", ""}, | ||||
| 		{"GET", "/1/classes/:className/:objectId", ""}, | ||||
| @@ -477,7 +477,7 @@ var ( | ||||
| 		{"POST", "/1/functions", ""}, | ||||
| 	} | ||||
|  | ||||
| 	googlePlusAPI = []Route{ | ||||
| 	googlePlusAPI = []*Route{ | ||||
| 		// People | ||||
| 		{"GET", "/people/:userId", ""}, | ||||
| 		{"GET", "/people", ""}, | ||||
| @@ -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 | ||||
|  | ||||
| @@ -866,7 +866,7 @@ 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", ""}, | ||||
| @@ -874,7 +874,7 @@ func TestRouterParamAlias(t *testing.T) { | ||||
| 	testRouterAPI(t, api) | ||||
| } | ||||
|  | ||||
| func benchmarkRouterRoutes(b *testing.B, routes []Route) { | ||||
| func benchmarkRouterRoutes(b *testing.B, routes []*Route) { | ||||
| 	e := New() | ||||
| 	r := e.router | ||||
| 	b.ReportAllocs() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user