diff --git a/echo_test.go b/echo_test.go index 7b007e91..a7e0f8b0 100644 --- a/echo_test.go +++ b/echo_test.go @@ -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) { diff --git a/group_test.go b/group_test.go new file mode 100644 index 00000000..f78d63c1 --- /dev/null +++ b/group_test.go @@ -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) +}