1
0
mirror of https://github.com/labstack/echo.git synced 2025-04-11 11:42:01 +02:00

Added tests for Group

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-29 16:00:02 -07:00
parent 76e9b44190
commit df43772757
2 changed files with 119 additions and 11 deletions

View File

@ -7,6 +7,9 @@ import (
"net/http/httptest"
"testing"
"reflect"
"strings"
"golang.org/x/net/websocket"
)
@ -235,18 +238,75 @@ func TestEchoGroup(t *testing.T) {
}
}
func TestEchoMethod(t *testing.T) {
func TestEchoConnect(t *testing.T) {
e := New()
h := func(*Context) error { return nil }
e.Connect("/", h)
e.Delete("/", h)
e.Get("/", h)
e.Head("/", h)
e.Options("/", h)
e.Patch("/", h)
e.Post("/", h)
e.Put("/", h)
e.Trace("/", h)
testMethod(CONNECT, "/", e, nil, t)
}
func TestEchoDelete(t *testing.T) {
e := New()
testMethod(DELETE, "/", e, nil, t)
}
func TestEchoGet(t *testing.T) {
e := New()
testMethod(GET, "/", e, nil, t)
}
func TestEchoHead(t *testing.T) {
e := New()
testMethod(HEAD, "/", e, nil, t)
}
func TestEchoOptions(t *testing.T) {
e := New()
testMethod(OPTIONS, "/", e, nil, t)
}
func TestEchoPatch(t *testing.T) {
e := New()
testMethod(PATCH, "/", e, nil, t)
}
func TestEchoPost(t *testing.T) {
e := New()
testMethod(POST, "/", e, nil, t)
}
func TestEchoPut(t *testing.T) {
e := New()
testMethod(PUT, "/", e, nil, t)
}
func TestEchoTrace(t *testing.T) {
e := New()
testMethod(TRACE, "/", e, nil, t)
}
func testMethod(method, path string, e *Echo, g *Group, t *testing.T) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
p := reflect.ValueOf(path)
h := reflect.ValueOf(func(c *Context) error {
c.String(http.StatusOK, method)
return nil
})
i := interface{}(e)
if g != nil {
path = e.prefix + path
i = g
}
reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h})
_, body := request(method, path, e)
if body != method {
t.Errorf("expected body `%s`, got %s.", method, body)
}
}
func request(method, path string, e *Echo) (int, string) {
r, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
e.ServeHTTP(w, r)
return w.Code, w.Body.String()
}
func TestWebSocket(t *testing.T) {

48
group_test.go Normal file
View File

@ -0,0 +1,48 @@
package echo
import "testing"
func TestGroupConnect(t *testing.T) {
g := New().Group("/group")
testMethod(CONNECT, "/", &g.echo, g, t)
}
func TestGroupDelete(t *testing.T) {
g := New().Group("/group")
testMethod(DELETE, "/", &g.echo, g, t)
}
func TestGroupGet(t *testing.T) {
g := New().Group("/group")
testMethod(GET, "/", &g.echo, g, t)
}
func TestGroupHead(t *testing.T) {
g := New().Group("/group")
testMethod(HEAD, "/", &g.echo, g, t)
}
func TestGroupOptions(t *testing.T) {
g := New().Group("/group")
testMethod(OPTIONS, "/", &g.echo, g, t)
}
func TestGroupPatch(t *testing.T) {
g := New().Group("/group")
testMethod(PATCH, "/", &g.echo, g, t)
}
func TestGroupPost(t *testing.T) {
g := New().Group("/group")
testMethod(POST, "/", &g.echo, g, t)
}
func TestGroupPut(t *testing.T) {
g := New().Group("/group")
testMethod(PUT, "/", &g.echo, g, t)
}
func TestGroupTrace(t *testing.T) {
g := New().Group("/group")
testMethod(TRACE, "/", &g.echo, g, t)
}