From b65641350ea44f3e0c22bf4a7950c25babe5e448 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 31 May 2015 09:37:26 -0700 Subject: [PATCH] Fixed #37 Signed-off-by: Vishal Rana --- echo.go | 3 ++- echo_test.go | 50 ++++++++++++++++---------------------------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/echo.go b/echo.go index 228e503a..c10cdc73 100644 --- a/echo.go +++ b/echo.go @@ -293,7 +293,8 @@ func (e *Echo) WebSocket(path string, h HandlerFunc) { } func (e *Echo) add(method, path string, h Handler) { - e.router.Add(method, e.prefix+path, wrapHandler(h), e) + path = e.prefix + path + e.router.Add(method, path, wrapHandler(h), e) key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name() e.uris[key] = path } diff --git a/echo_test.go b/echo_test.go index 81c6e06b..1a0b90e6 100644 --- a/echo_test.go +++ b/echo_test.go @@ -307,58 +307,40 @@ func TestEchoWebSocket(t *testing.T) { origin := "http://localhost" url := fmt.Sprintf("ws://%s/ws", addr) ws, err := websocket.Dial(url, "", origin) - if err != nil { - t.Fatal(err) - } - ws.Write([]byte("test")) - defer ws.Close() - buf := new(bytes.Buffer) - buf.ReadFrom(ws) - s := buf.String() - if s != "test" { - t.Errorf("expected `test`, got %s.", s) + if assert.NoError(t, err) { + ws.Write([]byte("test")) + defer ws.Close() + buf := new(bytes.Buffer) + buf.ReadFrom(ws) + assert.Equal(t, "test", buf.String()) } } func TestEchoURL(t *testing.T) { e := New() + static := func(*Context) error { return nil } getUser := func(*Context) error { return nil } getFile := func(*Context) error { return nil } + e.Get("/static/file", static) e.Get("/users/:id", getUser) - e.Get("/users/:uid/files/:fid", getFile) + g := e.Group("/group") + g.Get("/users/:uid/files/:fid", getFile) - if e.URL(static) != "/static/file" { - t.Error("uri should be /static/file") - } - if e.URI(static) != "/static/file" { - t.Error("uri should be /static/file") - } - if e.URI(getUser) != "/users/:id" { - t.Error("uri should be /users/:id") - } - if e.URI(getUser, "1") != "/users/1" { - t.Error("uri should be /users/1") - } - if e.URI(getFile, "1") != "/users/1/files/:fid" { - t.Error("uri should be /users/1/files/:fid") - } - if e.URI(getFile, "1", "1") != "/users/1/files/1" { - t.Error("uri should be /users/1/files/1") - } + assert.Equal(t, "/static/file", e.URL(static)) + assert.Equal(t, "/users/:id", e.URL(getUser)) + assert.Equal(t, "/users/1", e.URL(getUser, "1")) + assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1")) + assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1")) } func TestEchoNotFound(t *testing.T) { e := New() - - // Default NotFound handler r, _ := http.NewRequest(GET, "/files", nil) w := httptest.NewRecorder() e.ServeHTTP(w, r) - if w.Code != http.StatusNotFound { - t.Errorf("status code should be 404, found %d", w.Code) - } + assert.Equal(t, http.StatusNotFound, w.Code) } func TestEchoHTTPError(t *testing.T) {