2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-06 05:08:52 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-04-01 17:05:54 +02:00
|
|
|
"testing"
|
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
type route struct {
|
|
|
|
method string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
var (
|
|
|
|
params = make(Params, 5)
|
|
|
|
api = []route{
|
|
|
|
// OAuth Authorizations
|
|
|
|
{"GET", "/authorizations"},
|
|
|
|
{"GET", "/authorizations/:id"},
|
|
|
|
{"POST", "/authorizations"},
|
|
|
|
//{"PUT", "/authorizations/clients/:client_id"},
|
|
|
|
//{"PATCH", "/authorizations/:id"},
|
|
|
|
{"DELETE", "/authorizations/:id"},
|
|
|
|
{"GET", "/applications/:client_id/tokens/:access_token"},
|
|
|
|
{"DELETE", "/applications/:client_id/tokens"},
|
|
|
|
{"DELETE", "/applications/:client_id/tokens/:access_token"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Activity
|
|
|
|
{"GET", "/events"},
|
|
|
|
{"GET", "/repos/:owner/:repo/events"},
|
|
|
|
{"GET", "/networks/:owner/:repo/events"},
|
|
|
|
{"GET", "/orgs/:org/events"},
|
|
|
|
{"GET", "/users/:user/received_events"},
|
|
|
|
{"GET", "/users/:user/received_events/public"},
|
|
|
|
{"GET", "/users/:user/events"},
|
|
|
|
{"GET", "/users/:user/events/public"},
|
|
|
|
{"GET", "/users/:user/events/orgs/:org"},
|
|
|
|
{"GET", "/feeds"},
|
|
|
|
{"GET", "/notifications"},
|
|
|
|
{"GET", "/repos/:owner/:repo/notifications"},
|
|
|
|
{"PUT", "/notifications"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/notifications"},
|
|
|
|
{"GET", "/notifications/threads/:id"},
|
|
|
|
//{"PATCH", "/notifications/threads/:id"},
|
|
|
|
{"GET", "/notifications/threads/:id/subscription"},
|
|
|
|
{"PUT", "/notifications/threads/:id/subscription"},
|
|
|
|
{"DELETE", "/notifications/threads/:id/subscription"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stargazers"},
|
|
|
|
{"GET", "/users/:user/starred"},
|
|
|
|
{"GET", "/user/starred"},
|
|
|
|
{"GET", "/user/starred/:owner/:repo"},
|
|
|
|
{"PUT", "/user/starred/:owner/:repo"},
|
|
|
|
{"DELETE", "/user/starred/:owner/:repo"},
|
|
|
|
{"GET", "/repos/:owner/:repo/subscribers"},
|
|
|
|
{"GET", "/users/:user/subscriptions"},
|
|
|
|
{"GET", "/user/subscriptions"},
|
|
|
|
{"GET", "/repos/:owner/:repo/subscription"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/subscription"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/subscription"},
|
|
|
|
{"GET", "/user/subscriptions/:owner/:repo"},
|
|
|
|
{"PUT", "/user/subscriptions/:owner/:repo"},
|
|
|
|
{"DELETE", "/user/subscriptions/:owner/:repo"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Gists
|
|
|
|
{"GET", "/users/:user/gists"},
|
|
|
|
{"GET", "/gists"},
|
|
|
|
//{"GET", "/gists/public"},
|
|
|
|
//{"GET", "/gists/starred"},
|
|
|
|
{"GET", "/gists/:id"},
|
|
|
|
{"POST", "/gists"},
|
|
|
|
//{"PATCH", "/gists/:id"},
|
|
|
|
{"PUT", "/gists/:id/star"},
|
|
|
|
{"DELETE", "/gists/:id/star"},
|
|
|
|
{"GET", "/gists/:id/star"},
|
|
|
|
{"POST", "/gists/:id/forks"},
|
|
|
|
{"DELETE", "/gists/:id"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Git Data
|
|
|
|
{"GET", "/repos/:owner/:repo/git/blobs/:sha"},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/blobs"},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/commits/:sha"},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/commits"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/git/refs/*ref"},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/refs"},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/refs"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/git/refs/*ref"},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/git/refs/*ref"},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/tags/:sha"},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/tags"},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/trees/:sha"},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/trees"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Issues
|
|
|
|
{"GET", "/issues"},
|
|
|
|
{"GET", "/user/issues"},
|
|
|
|
{"GET", "/orgs/:org/issues"},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues"},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number"},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/issues/:number"},
|
|
|
|
{"GET", "/repos/:owner/:repo/assignees"},
|
|
|
|
{"GET", "/repos/:owner/:repo/assignees/:assignee"},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/comments"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/comments"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/comments/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues/:number/comments"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/issues/comments/:id"},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/issues/comments/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/events"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/events"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/events/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/labels"},
|
|
|
|
{"GET", "/repos/:owner/:repo/labels/:name"},
|
|
|
|
{"POST", "/repos/:owner/:repo/labels"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/labels/:name"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/labels/:name"},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/labels"},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues/:number/labels"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/issues/:number/labels/:name"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/issues/:number/labels"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/issues/:number/labels"},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones/:number/labels"},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones"},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones/:number"},
|
|
|
|
{"POST", "/repos/:owner/:repo/milestones"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/milestones/:number"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/milestones/:number"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Miscellaneous
|
|
|
|
{"GET", "/emojis"},
|
|
|
|
{"GET", "/gitignore/templates"},
|
|
|
|
{"GET", "/gitignore/templates/:name"},
|
|
|
|
{"POST", "/markdown"},
|
|
|
|
{"POST", "/markdown/raw"},
|
|
|
|
{"GET", "/meta"},
|
|
|
|
{"GET", "/rate_limit"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Organizations
|
|
|
|
{"GET", "/users/:user/orgs"},
|
|
|
|
{"GET", "/user/orgs"},
|
|
|
|
{"GET", "/orgs/:org"},
|
|
|
|
//{"PATCH", "/orgs/:org"},
|
|
|
|
{"GET", "/orgs/:org/members"},
|
|
|
|
{"GET", "/orgs/:org/members/:user"},
|
|
|
|
{"DELETE", "/orgs/:org/members/:user"},
|
|
|
|
{"GET", "/orgs/:org/public_members"},
|
|
|
|
{"GET", "/orgs/:org/public_members/:user"},
|
|
|
|
{"PUT", "/orgs/:org/public_members/:user"},
|
|
|
|
{"DELETE", "/orgs/:org/public_members/:user"},
|
|
|
|
{"GET", "/orgs/:org/teams"},
|
|
|
|
{"GET", "/teams/:id"},
|
|
|
|
{"POST", "/orgs/:org/teams"},
|
|
|
|
//{"PATCH", "/teams/:id"},
|
|
|
|
{"DELETE", "/teams/:id"},
|
|
|
|
{"GET", "/teams/:id/members"},
|
|
|
|
{"GET", "/teams/:id/members/:user"},
|
|
|
|
{"PUT", "/teams/:id/members/:user"},
|
|
|
|
{"DELETE", "/teams/:id/members/:user"},
|
|
|
|
{"GET", "/teams/:id/repos"},
|
|
|
|
{"GET", "/teams/:id/repos/:owner/:repo"},
|
|
|
|
{"PUT", "/teams/:id/repos/:owner/:repo"},
|
|
|
|
{"DELETE", "/teams/:id/repos/:owner/:repo"},
|
|
|
|
{"GET", "/user/teams"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Pull Requests
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls"},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number"},
|
|
|
|
{"POST", "/repos/:owner/:repo/pulls"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/pulls/:number"},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/commits"},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/files"},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/merge"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/pulls/:number/merge"},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/comments"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/pulls/comments"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/pulls/comments/:number"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/pulls/:number/comments"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/pulls/comments/:number"},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/pulls/comments/:number"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Repositories
|
|
|
|
{"GET", "/user/repos"},
|
|
|
|
{"GET", "/users/:user/repos"},
|
|
|
|
{"GET", "/orgs/:org/repos"},
|
|
|
|
{"GET", "/repositories"},
|
|
|
|
{"POST", "/user/repos"},
|
|
|
|
{"POST", "/orgs/:org/repos"},
|
|
|
|
{"GET", "/repos/:owner/:repo"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo"},
|
|
|
|
{"GET", "/repos/:owner/:repo/contributors"},
|
|
|
|
{"GET", "/repos/:owner/:repo/languages"},
|
|
|
|
{"GET", "/repos/:owner/:repo/teams"},
|
|
|
|
{"GET", "/repos/:owner/:repo/tags"},
|
|
|
|
{"GET", "/repos/:owner/:repo/branches"},
|
|
|
|
{"GET", "/repos/:owner/:repo/branches/:branch"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo"},
|
|
|
|
{"GET", "/repos/:owner/:repo/collaborators"},
|
|
|
|
{"GET", "/repos/:owner/:repo/collaborators/:user"},
|
|
|
|
{"PUT", "/repos/:owner/:repo/collaborators/:user"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/collaborators/:user"},
|
|
|
|
{"GET", "/repos/:owner/:repo/comments"},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits/:sha/comments"},
|
|
|
|
{"POST", "/repos/:owner/:repo/commits/:sha/comments"},
|
|
|
|
{"GET", "/repos/:owner/:repo/comments/:id"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/comments/:id"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/comments/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits"},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits/:sha"},
|
|
|
|
{"GET", "/repos/:owner/:repo/readme"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/contents/*path"},
|
|
|
|
//{"PUT", "/repos/:owner/:repo/contents/*path"},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/contents/*path"},
|
|
|
|
//{"GET", "/repos/:owner/:repo/:archive_format/:ref"},
|
|
|
|
{"GET", "/repos/:owner/:repo/keys"},
|
|
|
|
{"GET", "/repos/:owner/:repo/keys/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/keys"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/keys/:id"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/keys/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/downloads"},
|
|
|
|
{"GET", "/repos/:owner/:repo/downloads/:id"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/downloads/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/forks"},
|
|
|
|
{"POST", "/repos/:owner/:repo/forks"},
|
|
|
|
{"GET", "/repos/:owner/:repo/hooks"},
|
|
|
|
{"GET", "/repos/:owner/:repo/hooks/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/hooks"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/hooks/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/hooks/:id/tests"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/hooks/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/merges"},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases"},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases/:id"},
|
|
|
|
{"POST", "/repos/:owner/:repo/releases"},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/releases/:id"},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/releases/:id"},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases/:id/assets"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/contributors"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/commit_activity"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/code_frequency"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/participation"},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/punch_card"},
|
|
|
|
{"GET", "/repos/:owner/:repo/statuses/:ref"},
|
|
|
|
{"POST", "/repos/:owner/:repo/statuses/:ref"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Search
|
|
|
|
{"GET", "/search/repositories"},
|
|
|
|
{"GET", "/search/code"},
|
|
|
|
{"GET", "/search/issues"},
|
|
|
|
{"GET", "/search/users"},
|
|
|
|
{"GET", "/legacy/issues/search/:owner/:repository/:state/:keyword"},
|
|
|
|
{"GET", "/legacy/repos/search/:keyword"},
|
|
|
|
{"GET", "/legacy/user/search/:keyword"},
|
|
|
|
{"GET", "/legacy/user/email/:email"},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Users
|
|
|
|
{"GET", "/users/:user"},
|
|
|
|
{"GET", "/user"},
|
|
|
|
//{"PATCH", "/user"},
|
|
|
|
{"GET", "/users"},
|
|
|
|
{"GET", "/user/emails"},
|
|
|
|
{"POST", "/user/emails"},
|
|
|
|
{"DELETE", "/user/emails"},
|
|
|
|
{"GET", "/users/:user/followers"},
|
|
|
|
{"GET", "/user/followers"},
|
|
|
|
{"GET", "/users/:user/following"},
|
|
|
|
{"GET", "/user/following"},
|
|
|
|
{"GET", "/user/following/:user"},
|
|
|
|
{"GET", "/users/:user/following/:target_user"},
|
|
|
|
{"PUT", "/user/following/:user"},
|
|
|
|
{"DELETE", "/user/following/:user"},
|
|
|
|
{"GET", "/users/:user/keys"},
|
|
|
|
{"GET", "/user/keys"},
|
|
|
|
{"GET", "/user/keys/:id"},
|
|
|
|
{"POST", "/user/keys"},
|
|
|
|
//{"PATCH", "/user/keys/:id"},
|
|
|
|
{"DELETE", "/user/keys/:id"},
|
|
|
|
}
|
|
|
|
)
|
2015-04-06 05:08:52 +02:00
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterStatic(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-08 23:40:49 +02:00
|
|
|
r.Add(GET, "/folders/files/echo.gif", func(*Context) {}, nil)
|
2015-04-09 23:59:31 +02:00
|
|
|
h, _ := r.Find(GET, "/folders/files/echo.gif", params)
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterParam(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-09 23:59:31 +02:00
|
|
|
r.Add(GET, "/users/:id", func(c *Context) {
|
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param id should be 1")
|
|
|
|
}
|
|
|
|
}, nil)
|
|
|
|
h, _ := r.Find(GET, "/users/1", make(Params, 5))
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterTwoParam(t *testing.T) {
|
|
|
|
r := New().Router
|
2015-04-09 23:59:31 +02:00
|
|
|
r.Add(GET, "/users/:uid/files/:fid", func(c *Context) {
|
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param uid should be 1")
|
|
|
|
}
|
|
|
|
if c.P(1) != "1" {
|
|
|
|
t.Error("param fid should be 1")
|
|
|
|
}
|
|
|
|
}, nil)
|
|
|
|
h, _ := r.Find(GET, "/users/1/files/1", params)
|
2015-04-04 19:44:48 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterCatchAll(t *testing.T) {
|
2015-03-07 07:55:51 +02:00
|
|
|
r := New().Router
|
2015-04-08 23:40:49 +02:00
|
|
|
r.Add(GET, "/static/*", func(*Context) {}, nil)
|
2015-04-09 23:59:31 +02:00
|
|
|
h, _ := r.Find(GET, "/static/*", params)
|
2015-03-07 07:55:51 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterMicroParam(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-09 23:59:31 +02:00
|
|
|
r.Add(GET, "/:a/:b/:c", func(c *Context) {
|
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param a should be 1")
|
|
|
|
}
|
|
|
|
if c.P(1) != "2" {
|
|
|
|
t.Error("param b should be 2")
|
|
|
|
}
|
|
|
|
if c.P(2) != "3" {
|
|
|
|
t.Error("param c should be 3")
|
|
|
|
}
|
|
|
|
}, nil)
|
|
|
|
h, _ := r.Find(GET, "/1/2/3", params)
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 17:26:00 +02:00
|
|
|
|
2015-04-11 22:52:40 +02:00
|
|
|
func TestRouterConflict(t *testing.T) {
|
|
|
|
r := New().Router
|
2015-04-12 17:05:29 +02:00
|
|
|
r.Add(GET, "/new", func(*Context) {
|
|
|
|
println("/new")
|
|
|
|
}, nil)
|
|
|
|
r.Add(GET, "/new/:id", func(*Context) {
|
|
|
|
println("/new/:id")
|
|
|
|
}, nil)
|
|
|
|
r.Add(GET, "/new/name", func(*Context) {
|
|
|
|
println("/new/name")
|
|
|
|
}, nil)
|
|
|
|
r.Add(GET, "/new/name/joe", func(*Context) {
|
|
|
|
println("/new/name/joe")
|
|
|
|
}, nil)
|
|
|
|
r.Add(GET, "/new/name/:id", func(*Context) {
|
|
|
|
println("/new/name/:id")
|
|
|
|
}, nil)
|
|
|
|
// h, _ := r.Find(GET, "/users/new", params)
|
|
|
|
// h(&Context{})
|
2015-04-11 22:52:40 +02:00
|
|
|
n := r.trees[GET]
|
|
|
|
n.printTree("", true)
|
|
|
|
}
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
func TestRouterAPI(t *testing.T) {
|
2015-04-06 06:00:14 +02:00
|
|
|
r := New().Router
|
|
|
|
for _, route := range api {
|
2015-04-09 23:59:31 +02:00
|
|
|
r.Add(route.method, route.path, func(c *Context) {
|
|
|
|
for _, p := range c.params {
|
|
|
|
if p.Name != "" {
|
|
|
|
if ":"+p.Name != p.Value {
|
|
|
|
t.Errorf("param not found, method=%s, path=%s", route.method, route.path)
|
|
|
|
}
|
2015-04-06 06:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-09 23:59:31 +02:00
|
|
|
}, nil)
|
|
|
|
h, _ := r.Find(route.method, route.path, params)
|
|
|
|
if h == nil {
|
|
|
|
t.Errorf("handler not found, method=%s, path=%s", route.method, route.path)
|
2015-04-06 06:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-06 05:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterServeHTTP(t *testing.T) {
|
|
|
|
r := New().Router
|
2015-04-08 23:40:49 +02:00
|
|
|
r.Add(GET, "/users", func(*Context) {}, nil)
|
2015-04-06 05:08:52 +02:00
|
|
|
|
|
|
|
// OK
|
2015-04-06 06:49:55 +02:00
|
|
|
req, _ := http.NewRequest(GET, "/users", nil)
|
2015-04-06 05:08:52 +02:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
// NotFound handler
|
2015-04-06 06:49:55 +02:00
|
|
|
req, _ = http.NewRequest(GET, "/files", nil)
|
2015-04-06 05:08:52 +02:00
|
|
|
w = httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func (n *node) printTree(pfx string, tail bool) {
|
|
|
|
p := prefix(tail, pfx, "└── ", "├── ")
|
2015-04-12 17:05:29 +02:00
|
|
|
fmt.Printf("%s%s has=%d, echo=%v\n", p, n.prefix, n.handler, n.echo)
|
2015-04-01 17:05:54 +02:00
|
|
|
|
|
|
|
nodes := n.edges
|
|
|
|
l := len(nodes)
|
|
|
|
p = prefix(tail, pfx, " ", "│ ")
|
|
|
|
for i := 0; i < l-1; i++ {
|
|
|
|
nodes[i].printTree(p, false)
|
|
|
|
}
|
|
|
|
if l > 0 {
|
|
|
|
nodes[l-1].printTree(p, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefix(tail bool, p, on, off string) string {
|
|
|
|
if tail {
|
|
|
|
return fmt.Sprintf("%s%s", p, on)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s", p, off)
|
2015-03-31 17:26:00 +02:00
|
|
|
}
|