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-05-31 00:20:36 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-04-01 17:05:54 +02:00
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
var (
|
2015-06-06 00:08:32 +02:00
|
|
|
api = []Route{
|
2015-04-09 23:59:31 +02:00
|
|
|
// OAuth Authorizations
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/authorizations", nil},
|
|
|
|
{"GET", "/authorizations/:id", nil},
|
|
|
|
{"POST", "/authorizations", nil},
|
|
|
|
//{"PUT", "/authorizations/clients/:client_id", nil},
|
|
|
|
//{"PATCH", "/authorizations/:id", nil},
|
|
|
|
{"DELETE", "/authorizations/:id", nil},
|
|
|
|
{"GET", "/applications/:client_id/tokens/:access_token", nil},
|
|
|
|
{"DELETE", "/applications/:client_id/tokens", nil},
|
|
|
|
{"DELETE", "/applications/:client_id/tokens/:access_token", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Activity
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/events", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/events", nil},
|
|
|
|
{"GET", "/networks/:owner/:repo/events", nil},
|
|
|
|
{"GET", "/orgs/:org/events", nil},
|
|
|
|
{"GET", "/users/:user/received_events", nil},
|
|
|
|
{"GET", "/users/:user/received_events/public", nil},
|
|
|
|
{"GET", "/users/:user/events", nil},
|
|
|
|
{"GET", "/users/:user/events/public", nil},
|
|
|
|
{"GET", "/users/:user/events/orgs/:org", nil},
|
|
|
|
{"GET", "/feeds", nil},
|
|
|
|
{"GET", "/notifications", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/notifications", nil},
|
|
|
|
{"PUT", "/notifications", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/notifications", nil},
|
|
|
|
{"GET", "/notifications/threads/:id", nil},
|
|
|
|
//{"PATCH", "/notifications/threads/:id", nil},
|
|
|
|
{"GET", "/notifications/threads/:id/subscription", nil},
|
|
|
|
{"PUT", "/notifications/threads/:id/subscription", nil},
|
|
|
|
{"DELETE", "/notifications/threads/:id/subscription", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stargazers", nil},
|
|
|
|
{"GET", "/users/:user/starred", nil},
|
|
|
|
{"GET", "/user/starred", nil},
|
|
|
|
{"GET", "/user/starred/:owner/:repo", nil},
|
|
|
|
{"PUT", "/user/starred/:owner/:repo", nil},
|
|
|
|
{"DELETE", "/user/starred/:owner/:repo", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/subscribers", nil},
|
|
|
|
{"GET", "/users/:user/subscriptions", nil},
|
|
|
|
{"GET", "/user/subscriptions", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/subscription", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/subscription", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/subscription", nil},
|
|
|
|
{"GET", "/user/subscriptions/:owner/:repo", nil},
|
|
|
|
{"PUT", "/user/subscriptions/:owner/:repo", nil},
|
|
|
|
{"DELETE", "/user/subscriptions/:owner/:repo", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Gists
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/users/:user/gists", nil},
|
|
|
|
{"GET", "/gists", nil},
|
|
|
|
//{"GET", "/gists/public", nil},
|
|
|
|
//{"GET", "/gists/starred", nil},
|
|
|
|
{"GET", "/gists/:id", nil},
|
|
|
|
{"POST", "/gists", nil},
|
|
|
|
//{"PATCH", "/gists/:id", nil},
|
|
|
|
{"PUT", "/gists/:id/star", nil},
|
|
|
|
{"DELETE", "/gists/:id/star", nil},
|
|
|
|
{"GET", "/gists/:id/star", nil},
|
|
|
|
{"POST", "/gists/:id/forks", nil},
|
|
|
|
{"DELETE", "/gists/:id", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Git Data
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/repos/:owner/:repo/git/blobs/:sha", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/blobs", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/commits/:sha", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/commits", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/git/refs/*ref", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/refs", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/refs", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/git/refs/*ref", nil},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/git/refs/*ref", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/tags/:sha", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/tags", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/git/trees/:sha", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/git/trees", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Issues
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/issues", nil},
|
|
|
|
{"GET", "/user/issues", nil},
|
|
|
|
{"GET", "/orgs/:org/issues", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/issues/:number", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/assignees", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/assignees/:assignee", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/comments", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/comments", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/comments/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues/:number/comments", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/issues/comments/:id", nil},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/issues/comments/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/events", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/events", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/issues/events/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/labels", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/labels/:name", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/labels", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/labels/:name", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/labels/:name", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/issues/:number/labels", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/issues/:number/labels", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/issues/:number/labels/:name", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/issues/:number/labels", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/issues/:number/labels", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones/:number/labels", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/milestones/:number", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/milestones", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/milestones/:number", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/milestones/:number", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Miscellaneous
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/emojis", nil},
|
|
|
|
{"GET", "/gitignore/templates", nil},
|
|
|
|
{"GET", "/gitignore/templates/:name", nil},
|
|
|
|
{"POST", "/markdown", nil},
|
|
|
|
{"POST", "/markdown/raw", nil},
|
|
|
|
{"GET", "/meta", nil},
|
|
|
|
{"GET", "/rate_limit", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Organizations
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/users/:user/orgs", nil},
|
|
|
|
{"GET", "/user/orgs", nil},
|
|
|
|
{"GET", "/orgs/:org", nil},
|
|
|
|
//{"PATCH", "/orgs/:org", nil},
|
|
|
|
{"GET", "/orgs/:org/members", nil},
|
|
|
|
{"GET", "/orgs/:org/members/:user", nil},
|
|
|
|
{"DELETE", "/orgs/:org/members/:user", nil},
|
|
|
|
{"GET", "/orgs/:org/public_members", nil},
|
|
|
|
{"GET", "/orgs/:org/public_members/:user", nil},
|
|
|
|
{"PUT", "/orgs/:org/public_members/:user", nil},
|
|
|
|
{"DELETE", "/orgs/:org/public_members/:user", nil},
|
|
|
|
{"GET", "/orgs/:org/teams", nil},
|
|
|
|
{"GET", "/teams/:id", nil},
|
|
|
|
{"POST", "/orgs/:org/teams", nil},
|
|
|
|
//{"PATCH", "/teams/:id", nil},
|
|
|
|
{"DELETE", "/teams/:id", nil},
|
|
|
|
{"GET", "/teams/:id/members", nil},
|
|
|
|
{"GET", "/teams/:id/members/:user", nil},
|
|
|
|
{"PUT", "/teams/:id/members/:user", nil},
|
|
|
|
{"DELETE", "/teams/:id/members/:user", nil},
|
|
|
|
{"GET", "/teams/:id/repos", nil},
|
|
|
|
{"GET", "/teams/:id/repos/:owner/:repo", nil},
|
|
|
|
{"PUT", "/teams/:id/repos/:owner/:repo", nil},
|
|
|
|
{"DELETE", "/teams/:id/repos/:owner/:repo", nil},
|
|
|
|
{"GET", "/user/teams", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Pull Requests
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/repos/:owner/:repo/pulls", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/pulls", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/pulls/:number", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/commits", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/files", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/merge", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/pulls/:number/merge", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/pulls/:number/comments", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/pulls/comments", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/pulls/comments/:number", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/pulls/:number/comments", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/pulls/comments/:number", nil},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/pulls/comments/:number", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Repositories
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/user/repos", nil},
|
|
|
|
{"GET", "/users/:user/repos", nil},
|
|
|
|
{"GET", "/orgs/:org/repos", nil},
|
|
|
|
{"GET", "/repositories", nil},
|
|
|
|
{"POST", "/user/repos", nil},
|
|
|
|
{"POST", "/orgs/:org/repos", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/contributors", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/languages", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/teams", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/tags", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/branches", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/branches/:branch", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/collaborators", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/collaborators/:user", nil},
|
|
|
|
{"PUT", "/repos/:owner/:repo/collaborators/:user", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/collaborators/:user", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/comments", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits/:sha/comments", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/commits/:sha/comments", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/comments/:id", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/comments/:id", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/comments/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/commits/:sha", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/readme", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/contents/*path", nil},
|
|
|
|
//{"PUT", "/repos/:owner/:repo/contents/*path", nil},
|
|
|
|
//{"DELETE", "/repos/:owner/:repo/contents/*path", nil},
|
|
|
|
//{"GET", "/repos/:owner/:repo/:archive_format/:ref", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/keys", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/keys/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/keys", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/keys/:id", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/keys/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/downloads", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/downloads/:id", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/downloads/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/forks", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/forks", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/hooks", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/hooks/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/hooks", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/hooks/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/hooks/:id/tests", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/hooks/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/merges", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases/:id", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/releases", nil},
|
|
|
|
//{"PATCH", "/repos/:owner/:repo/releases/:id", nil},
|
|
|
|
{"DELETE", "/repos/:owner/:repo/releases/:id", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/releases/:id/assets", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/contributors", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/commit_activity", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/code_frequency", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/participation", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/stats/punch_card", nil},
|
|
|
|
{"GET", "/repos/:owner/:repo/statuses/:ref", nil},
|
|
|
|
{"POST", "/repos/:owner/:repo/statuses/:ref", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Search
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/search/repositories", nil},
|
|
|
|
{"GET", "/search/code", nil},
|
|
|
|
{"GET", "/search/issues", nil},
|
|
|
|
{"GET", "/search/users", nil},
|
|
|
|
{"GET", "/legacy/issues/search/:owner/:repository/:state/:keyword", nil},
|
|
|
|
{"GET", "/legacy/repos/search/:keyword", nil},
|
|
|
|
{"GET", "/legacy/user/search/:keyword", nil},
|
|
|
|
{"GET", "/legacy/user/email/:email", nil},
|
2015-04-06 06:00:14 +02:00
|
|
|
|
2015-04-09 23:59:31 +02:00
|
|
|
// Users
|
2015-06-01 09:07:53 +02:00
|
|
|
{"GET", "/users/:user", nil},
|
|
|
|
{"GET", "/user", nil},
|
|
|
|
//{"PATCH", "/user", nil},
|
|
|
|
{"GET", "/users", nil},
|
|
|
|
{"GET", "/user/emails", nil},
|
|
|
|
{"POST", "/user/emails", nil},
|
|
|
|
{"DELETE", "/user/emails", nil},
|
|
|
|
{"GET", "/users/:user/followers", nil},
|
|
|
|
{"GET", "/user/followers", nil},
|
|
|
|
{"GET", "/users/:user/following", nil},
|
|
|
|
{"GET", "/user/following", nil},
|
|
|
|
{"GET", "/user/following/:user", nil},
|
|
|
|
{"GET", "/users/:user/following/:target_user", nil},
|
|
|
|
{"PUT", "/user/following/:user", nil},
|
|
|
|
{"DELETE", "/user/following/:user", nil},
|
|
|
|
{"GET", "/users/:user/keys", nil},
|
|
|
|
{"GET", "/user/keys", nil},
|
|
|
|
{"GET", "/user/keys/:id", nil},
|
|
|
|
{"POST", "/user/keys", nil},
|
|
|
|
//{"PATCH", "/user/keys/:id", nil},
|
|
|
|
{"DELETE", "/user/keys/:id", nil},
|
2015-04-09 23:59:31 +02:00
|
|
|
}
|
|
|
|
)
|
2015-04-06 05:08:52 +02:00
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterStatic(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-04-12 22:04:41 +02:00
|
|
|
path := "/folders/a/files/echo.gif"
|
2015-05-31 00:20:36 +02:00
|
|
|
r.Add(GET, path, func(c *Context) error {
|
|
|
|
c.Set("path", path)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
|
|
|
h, _ := r.Find(GET, path, c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, path, c.Get("path"))
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterParam(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
|
|
|
h, _ := r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterTwoParam(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:uid/files/:fid", func(*Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
2015-04-24 16:44:30 +02:00
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ := r.Find(GET, "/users/1/files/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
|
|
|
assert.Equal(t, "1", c.P(1))
|
2015-05-04 22:01:02 +02:00
|
|
|
}
|
2015-04-29 08:09:30 +02:00
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Nil(t, h)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:09:30 +02:00
|
|
|
func TestRouterMatchAny(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/*", func(*Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
2015-04-26 18:48:49 +02:00
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ := r.Find(GET, "/users/", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "", c.P(0))
|
2015-04-26 21:44:38 +02:00
|
|
|
}
|
|
|
|
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
2015-03-07 07:55:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterMicroParam(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/:a/:b/:c", func(c *Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
|
|
|
h, _ := r.Find(GET, "/1/2/3", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
|
|
|
assert.Equal(t, "2", c.P(1))
|
|
|
|
assert.Equal(t, "3", c.P(2))
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-31 17:26:00 +02:00
|
|
|
|
2015-04-13 22:12:30 +02:00
|
|
|
func TestRouterMultiRoute(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-04-12 22:04:41 +02:00
|
|
|
|
2015-04-13 22:12:30 +02:00
|
|
|
// Routes
|
2015-05-31 00:20:36 +02:00
|
|
|
r.Add(GET, "/users", func(c *Context) error {
|
|
|
|
c.Set("path", "/users")
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
2015-04-13 22:12:30 +02:00
|
|
|
|
|
|
|
// Route > /users
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ := r.Find(GET, "/users", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, "/users", c.Get("path"))
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:id
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "1", c.P(0))
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:12:30 +02:00
|
|
|
// Route > /user
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/user", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Nil(t, h)
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
func TestRouterPriority(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-04-13 22:12:30 +02:00
|
|
|
|
|
|
|
// Routes
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("a", 1)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/new", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("b", 2)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("c", 3)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/dew", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("d", 4)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:id/files", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("e", 5)
|
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/newsee", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("f", 6)
|
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/*", func(c *Context) error {
|
2015-05-10 07:06:13 +02:00
|
|
|
c.Set("g", 7)
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
2015-04-13 22:12:30 +02:00
|
|
|
|
|
|
|
// Route > /users
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ := r.Find(GET, "/users", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 1, c.Get("a"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Route > /users/new
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/new", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 2, c.Get("b"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:id
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 3, c.Get("c"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/dew
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/dew", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 4, c.Get("d"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:id/files
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1/files", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 5, c.Get("e"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:id
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/news", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 3, c.Get("c"))
|
2015-04-13 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/*
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/joe/books", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, 7, c.Get("g"))
|
2015-06-04 01:19:03 +02:00
|
|
|
assert.Equal(t, "joe/books", c.Param("_name"))
|
2015-04-12 22:04:41 +02:00
|
|
|
}
|
2015-04-11 22:52:40 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 16:44:30 +02:00
|
|
|
func TestRouterParamNames(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
2015-04-24 16:44:30 +02:00
|
|
|
|
|
|
|
// Routes
|
2015-05-31 00:20:36 +02:00
|
|
|
r.Add(GET, "/users", func(c *Context) error {
|
|
|
|
c.Set("path", "/users")
|
2015-04-24 16:44:30 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:id", func(c *Context) error {
|
2015-04-24 16:44:30 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users/:uid/files/:fid", func(c *Context) error {
|
2015-04-24 16:44:30 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
c := NewContext(nil, nil, e)
|
2015-04-24 16:44:30 +02:00
|
|
|
|
|
|
|
// Route > /users
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ := r.Find(GET, "/users", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
|
|
|
assert.Equal(t, "/users", c.Get("path"))
|
2015-04-24 16:44:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:id
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "id", c.pnames[0])
|
|
|
|
assert.Equal(t, "1", c.P(0))
|
2015-04-24 16:44:30 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 07:06:13 +02:00
|
|
|
// Route > /users/:uid/files/:fid
|
2015-06-04 00:18:27 +02:00
|
|
|
h, _ = r.Find(GET, "/users/1/files/1", c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-04 00:18:27 +02:00
|
|
|
assert.Equal(t, "uid", c.pnames[0])
|
|
|
|
assert.Equal(t, "1", c.P(0))
|
|
|
|
assert.Equal(t, "fid", c.pnames[1])
|
|
|
|
assert.Equal(t, "1", c.P(1))
|
2015-04-24 16:44:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
func TestRouterAPI(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
|
|
|
|
2015-04-06 06:00:14 +02:00
|
|
|
for _, route := range api {
|
2015-06-01 09:07:53 +02:00
|
|
|
r.Add(route.Method, route.Path, func(c *Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
|
|
|
}
|
|
|
|
c := NewContext(nil, nil, e)
|
|
|
|
for _, route := range api {
|
|
|
|
h, _ := r.Find(route.Method, route.Path, c)
|
2015-05-31 00:20:36 +02:00
|
|
|
if assert.NotNil(t, h) {
|
2015-06-06 00:08:32 +02:00
|
|
|
for i, n := range c.pnames {
|
|
|
|
if assert.NotEmpty(t, n) {
|
|
|
|
assert.Equal(t, ":"+n, c.P(i))
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 00:18:27 +02:00
|
|
|
h(c)
|
2015-04-06 06:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-06 05:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterServeHTTP(t *testing.T) {
|
2015-06-04 00:18:27 +02:00
|
|
|
e := New()
|
|
|
|
r := e.router
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
r.Add(GET, "/users", func(*Context) error {
|
2015-04-19 01:47:48 +02:00
|
|
|
return nil
|
2015-06-04 00:18:27 +02:00
|
|
|
}, e)
|
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)
|
2015-05-31 00:38:02 +02:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-04-06 05:08:52 +02:00
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
// Not found
|
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-05-31 00:38:02 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-04-06 05:08:52 +02:00
|
|
|
}
|
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func (n *node) printTree(pfx string, tail bool) {
|
|
|
|
p := prefix(tail, pfx, "└── ", "├── ")
|
2015-05-01 03:47:13 +02:00
|
|
|
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.typ, n.parent, n.handler)
|
2015-04-01 17:05:54 +02:00
|
|
|
|
2015-04-14 06:57:36 +02:00
|
|
|
children := n.children
|
|
|
|
l := len(children)
|
2015-04-01 17:05:54 +02:00
|
|
|
p = prefix(tail, pfx, " ", "│ ")
|
|
|
|
for i := 0; i < l-1; i++ {
|
2015-04-14 06:57:36 +02:00
|
|
|
children[i].printTree(p, false)
|
2015-04-01 17:05:54 +02:00
|
|
|
}
|
|
|
|
if l > 0 {
|
2015-04-14 06:57:36 +02:00
|
|
|
children[l-1].printTree(p, true)
|
2015-04-01 17:05:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|