1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-31 09:37:26 -07:00
parent 6d4864ac4f
commit b65641350e
2 changed files with 18 additions and 35 deletions

View File

@ -293,7 +293,8 @@ func (e *Echo) WebSocket(path string, h HandlerFunc) {
} }
func (e *Echo) add(method, path string, h Handler) { 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() key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
e.uris[key] = path e.uris[key] = path
} }

View File

@ -307,58 +307,40 @@ func TestEchoWebSocket(t *testing.T) {
origin := "http://localhost" origin := "http://localhost"
url := fmt.Sprintf("ws://%s/ws", addr) url := fmt.Sprintf("ws://%s/ws", addr)
ws, err := websocket.Dial(url, "", origin) ws, err := websocket.Dial(url, "", origin)
if err != nil { if assert.NoError(t, err) {
t.Fatal(err) ws.Write([]byte("test"))
} defer ws.Close()
ws.Write([]byte("test")) buf := new(bytes.Buffer)
defer ws.Close() buf.ReadFrom(ws)
buf := new(bytes.Buffer) assert.Equal(t, "test", buf.String())
buf.ReadFrom(ws)
s := buf.String()
if s != "test" {
t.Errorf("expected `test`, got %s.", s)
} }
} }
func TestEchoURL(t *testing.T) { func TestEchoURL(t *testing.T) {
e := New() e := New()
static := func(*Context) error { return nil } static := func(*Context) error { return nil }
getUser := func(*Context) error { return nil } getUser := func(*Context) error { return nil }
getFile := func(*Context) error { return nil } getFile := func(*Context) error { return nil }
e.Get("/static/file", static) e.Get("/static/file", static)
e.Get("/users/:id", getUser) 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" { assert.Equal(t, "/static/file", e.URL(static))
t.Error("uri should be /static/file") assert.Equal(t, "/users/:id", e.URL(getUser))
} assert.Equal(t, "/users/1", e.URL(getUser, "1"))
if e.URI(static) != "/static/file" { assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1"))
t.Error("uri should be /static/file") assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1"))
}
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")
}
} }
func TestEchoNotFound(t *testing.T) { func TestEchoNotFound(t *testing.T) {
e := New() e := New()
// Default NotFound handler
r, _ := http.NewRequest(GET, "/files", nil) r, _ := http.NewRequest(GET, "/files", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
e.ServeHTTP(w, r) e.ServeHTTP(w, r)
if w.Code != http.StatusNotFound { assert.Equal(t, http.StatusNotFound, w.Code)
t.Errorf("status code should be 404, found %d", w.Code)
}
} }
func TestEchoHTTPError(t *testing.T) { func TestEchoHTTPError(t *testing.T) {