1 Star 0 Fork 11

熊大/tango

forked from lunny/tango 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
router_test.go 18.84 KB
一键复制 编辑 原始数据 按行查看 历史
lunny 提交于 2019-10-28 07:27 . Add more tests about any (#64)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"bytes"
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
type RouterNoMethodAction struct {
}
func TestRouter1(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/:name", new(RouterNoMethodAction))
req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusNotFound)
refute(t, len(buff.String()), 0)
}
type RouterGetAction struct {
}
func (a *RouterGetAction) Get() string {
return "get"
}
func (a *RouterGetAction) Post() string {
return "post"
}
func TestRouter2(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/:name", new(RouterGetAction))
o.Post("/:name", new(RouterGetAction))
req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "get")
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/foobar", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "post")
}
type RouterAnyAction struct {
Ctx
}
func (a *RouterAnyAction) Any() string {
return a.Req().Method
}
func TestRouter3(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Any("/", new(RouterAnyAction))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "GET")
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "POST")
}
type RouterSpecAction struct {
a string
}
func (RouterSpecAction) Method1() string {
return "1"
}
func (r *RouterSpecAction) Method2() string {
return r.a
}
func TestRouterFunc(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/", func() string {
return "func"
})
o.Post("/", func(ctx *Context) {
ctx.Write([]byte("func(*Context)"))
})
o.Put("/", func(resp http.ResponseWriter, req *http.Request) {
resp.Write([]byte("func(http.ResponseWriter, *http.Request)"))
})
o.Options("/", func(resp http.ResponseWriter) {
resp.Write([]byte("func(http.ResponseWriter)"))
})
o.Delete("/", func(req *http.Request) string {
return "func(*http.Request)"
})
// plain
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "func")
// context
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "func(*Context)")
// http
buff.Reset()
req, err = http.NewRequest("PUT", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "func(http.ResponseWriter, *http.Request)")
// response
buff.Reset()
req, err = http.NewRequest("OPTIONS", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "func(http.ResponseWriter)")
// req
buff.Reset()
req, err = http.NewRequest("DELETE", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "func(*http.Request)")
}
type Router4Action struct {
Params
}
func (r *Router4Action) Get() string {
return r.Params.Get(":name1") + "-" + r.Params.Get(":name2")
}
func TestRouter4(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/:name1-:name2", new(Router4Action))
req, err := http.NewRequest("GET", "http://localhost:8000/foobar-foobar2", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "foobar-foobar2")
}
type Router5Action struct {
}
func (r *Router5Action) Get() string {
return "router5"
}
func TestRouter5(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Route("GET", "/", new(Router5Action))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router5")
}
type Router6Action struct {
}
func (r *Router6Action) MyMethod() string {
return "router6"
}
func TestRouter6(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Route("GET:MyMethod", "/", new(Router6Action))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router6")
}
type Router7Action struct {
}
func (r *Router7Action) MyGet() string {
return "router7-get"
}
func (r *Router7Action) Post() string {
return "router7-post"
}
func TestRouter7(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Route([]string{"GET:MyGet", "POST"}, "/", new(Router7Action))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router7-get")
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router7-post")
}
type Router8Action struct {
}
func (r *Router8Action) MyGet() string {
return "router8-get"
}
func (r *Router8Action) Post() string {
return "router8-post"
}
func TestRouter8(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Route(map[string]string{"GET": "MyGet", "POST": "Post"}, "/", new(Router8Action))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router8-get")
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "router8-post")
}
type Regex1Action struct {
Params
}
func (r *Regex1Action) Get() string {
return r.Params.Get(":name")
}
func TestRouter9(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
o.Get("/(:name[a-zA-Z]+)", new(Regex1Action))
req, err := http.NewRequest("GET", "http://localhost:8000/foobar", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "foobar")
refute(t, len(buff.String()), 0)
}
var (
parsedResult = map[string][]*node{
"/": []*node{
{content: "/", tp: snode},
},
"/static/css/bootstrap.css": []*node{
{content: "/static", tp: snode},
{content: "/css", tp: snode},
{content: "/bootstrap.css", tp: snode},
},
"/:name": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
},
"/sss:name": []*node{
{content: "/sss", tp: snode},
{content: ":name", tp: nnode},
},
"/(:name)": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
},
"/(:name)/sss": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
{content: "/sss", tp: snode},
},
"/:name-:value": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
{content: "-", tp: snode},
{content: ":value", tp: nnode},
},
"/(:name)ssss(:value)": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
{content: "ssss", tp: snode},
{content: ":value", tp: nnode},
},
"/(:name[0-9]+)": []*node{
{content: "/", tp: snode},
{content: ":name", tp: rnode, regexp: regexp.MustCompile("([0-9]+)")},
},
"/*name": []*node{
{content: "/", tp: snode},
{content: "*name", tp: anode},
},
"/*name/ssss": []*node{
{content: "/", tp: snode},
{content: "*name", tp: anode},
{content: "/ssss", tp: snode},
},
"/(*name)ssss": []*node{
{content: "/", tp: snode},
{content: "*name", tp: anode},
{content: "ssss", tp: snode},
},
"/:name-(:name2[a-z]+)": []*node{
{content: "/", tp: snode},
{content: ":name", tp: nnode},
{content: "-", tp: snode},
{content: ":name2", tp: rnode, regexp: regexp.MustCompile("([a-z]+)")},
},
"/web/content/(:id3)-(:unique3)/(:filename)": []*node{
{content: "/web", tp: snode},
{content: "/content", tp: snode},
{content: "/", tp: snode},
{content: ":id3", tp: nnode},
{content: "-", tp: snode},
{content: ":unique3", tp: nnode},
{content: "/", tp: snode},
{content: ":filename", tp: nnode},
},
}
)
func TestParseNode(t *testing.T) {
for p, r := range parsedResult {
res := parseNodes(p)
if len(r) != len(res) {
t.Fatalf("%v 's result %v is not equal %v", p, r, res)
}
for i := 0; i < len(r); i++ {
if r[i].content != res[i].content ||
r[i].tp != res[i].tp {
t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
}
if r[i].tp != rnode {
if r[i].regexp != nil {
t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
}
} else {
if r[i].regexp == nil {
t.Fatalf("%v 's %d result %v is not equal %v", p, i, r[i], res[i])
}
}
}
}
}
type result struct {
url string
match bool
params Params
}
var (
matchResult = map[string][]result{
"/": []result{
{"/", true, Params{}},
{"/s", false, Params{}},
{"/123", false, Params{}},
},
"/ss/tt": []result{
{"/ss/tt", true, Params{}},
{"/s", false, Params{}},
{"/ss", false, Params{}},
},
"/:name": []result{
{"/s", true, Params{{":name", "s"}}},
{"/", false, Params{}},
{"/123/s", false, Params{}},
},
"/:name1/:name2/:name3": []result{
{"/1/2/3", true, Params{{":name1", "1"}, {":name2", "2"}, {":name3", "3"}}},
{"/1/2", false, Params{}},
{"/1/2/3/", false, Params{}},
},
"/*name": []result{
{"/s", true, Params{{"*name", "s"}}},
{"/123/s", true, Params{{"*name", "123/s"}}},
{"/", false, Params{}},
},
"/(*name)ssss": []result{
{"/sssss", true, Params{{"*name", "s"}}},
{"/123/ssss", true, Params{{"*name", "123/"}}},
{"/", false, Params{}},
{"/ss", false, Params{}},
},
"/111(*name)ssss": []result{
{"/111sssss", true, Params{{"*name", "s"}}},
{"/111/123/ssss", true, Params{{"*name", "/123/"}}},
{"/", false, Params{}},
{"/ss", false, Params{}},
},
"/(:name[0-9]+)": []result{
{"/123", true, Params{{":name", "123"}}},
{"/sss", false, Params{}},
},
"/ss(:name[0-9]+)": []result{
{"/ss123", true, Params{{":name", "123"}}},
{"/sss", false, Params{}},
},
"/ss(:name[0-9]+)tt": []result{
{"/ss123tt", true, Params{{":name", "123"}}},
{"/sss", false, Params{}},
},
"/:name1-(:name2[0-9]+)": []result{
{"/ss-123", true, Params{{":name1", "ss"}, {":name2", "123"}}},
{"/sss", false, Params{}},
},
"/(:name1)00(:name2[0-9]+)": []result{
{"/ss00123", true, Params{{":name1", "ss"}, {":name2", "123"}}},
{"/sss", false, Params{}},
},
"/(:name1)!(:name2[0-9]+)!(:name3.*)": []result{
{"/ss!123!456", true, Params{{":name1", "ss"}, {":name2", "123"}, {":name3", "456"}}},
{"/sss", false, Params{}},
},
"/web/content/(:id3)-(:unique3)/(:filename)": []result{
{"/web/content/36-0420888/website.assets_frontend.0.css", true, Params{{":id3", "36"}, {":unique3", "0420888"}, {":filename", "website.assets_frontend.0.css"}}},
},
}
)
type Action struct {
}
func (Action) Get() string {
return "get"
}
func TestRouterSingle(t *testing.T) {
for k, m := range matchResult {
r := New()
r.Route("GET", k, new(Action))
for _, res := range m {
handler, params := r.Match(res.url, "GET")
if res.match {
if handler == nil {
t.Fatal(k, res, "handler", handler, "should not be nil")
}
for i, v := range params {
if res.params[i].Name != v.Name {
t.Fatal(k, res, "params name", v, "not equal", res.params[i])
}
if res.params[i].Value != v.Value {
t.Fatal(k, res, "params value", v, "not equal", res.params[i])
}
}
} else {
if handler != nil {
t.Fatal(k, res, "handler", handler, "should be nil")
}
}
}
}
}
type testCase struct {
routers []string
results []result
}
var (
matchResult2 = []testCase{
{
[]string{"/"},
[]result{
{"/", true, Params{}},
{"/s", false, Params{}},
{"/123", false, Params{}},
},
},
{
[]string{"/admin", "/:name"},
[]result{
{"/", false, Params{}},
{"/admin", true, Params{}},
{"/s", true, Params{param{":name", "s"}}},
{"/123", true, Params{param{":name", "123"}}},
},
},
{
[]string{"/:name", "/admin"},
[]result{
{"/", false, Params{}},
{"/admin", true, Params{}},
{"/s", true, Params{param{":name", "s"}}},
{"/123", true, Params{param{":name", "123"}}},
},
},
{
[]string{"/admin", "/*name"},
[]result{
{"/", false, Params{}},
{"/admin", true, Params{}},
{"/s", true, Params{param{"*name", "s"}}},
{"/123", true, Params{param{"*name", "123"}}},
},
},
{
[]string{"/*name", "/admin"},
[]result{
{"/", false, Params{}},
{"/admin", true, Params{}},
{"/s", true, Params{param{"*name", "s"}}},
{"/123", true, Params{param{"*name", "123"}}},
},
},
{
[]string{"/*name", "/:name"},
[]result{
{"/", false, Params{}},
{"/s", true, Params{param{"*name", "s"}}},
{"/123", true, Params{param{"*name", "123"}}},
},
},
{
[]string{"/:name", "/*name"},
[]result{
{"/", false, Params{}},
{"/s", true, Params{param{":name", "s"}}},
{"/123", true, Params{param{":name", "123"}}},
{"/123/1", true, Params{param{"*name", "123/1"}}},
},
},
{
[]string{"/*name", "/*name/123"},
[]result{
{"/", false, Params{}},
{"/123", true, Params{param{"*name", "123"}}},
{"/s", true, Params{param{"*name", "s"}}},
{"/abc/123", true, Params{param{"*name", "abc"}}},
{"/name1/name2/123", true, Params{param{"*name", "name1/name2"}}},
},
},
{
[]string{"/admin/ui", "/*name", "/:name"},
[]result{
{"/", false, Params{}},
{"/admin/ui", true, Params{}},
{"/s", true, Params{param{"*name", "s"}}},
{"/123", true, Params{param{"*name", "123"}}},
},
},
{
[]string{"/(:id[0-9]+)", "/(:id[0-9]+)/edit", "/(:id[0-9]+)/del"},
[]result{
{"/1", true, Params{param{":id", "1"}}},
{"/admin/ui", false, Params{}},
{"/2/edit", true, Params{param{":id", "2"}}},
{"/3/del", true, Params{param{":id", "3"}}},
},
},
{
[]string{"/admin/ui", "/:name1/:name2"},
[]result{
{"/", false, Params{}},
{"/admin/ui", true, Params{}},
{"/s", false, Params{}},
{"/admin/ui2", true, Params{param{":name1", "admin"}, param{":name2", "ui2"}}},
{"/123/s", true, Params{param{":name1", "123"}, param{":name2", "s"}}},
},
},
{
[]string{"/(:name1)/(:name1)abc/",
"/(:name1)/(:name1)abc(:name2)abc/",
"/(:name1)/abc(:name1)abc(:name2)abc/",
"/(:name1)/(:name1)/"},
[]result{
{"/abc/abc123abc123abc", true, Params{
param{":name1", "abc"},
param{":name1", "123"},
param{":name2", "123"},
},
},
},
},
}
)
func TestRouterMultiple(t *testing.T) {
for _, kase := range matchResult2 {
r := New()
for _, k := range kase.routers {
r.Route("GET", k, new(Action))
}
for _, res := range kase.results {
handler, params := r.Match(res.url, "GET")
if res.match {
if handler == nil {
t.Fatal(kase.routers, res, "handler", handler, "should not be nil")
}
if len(res.params) != len(params) {
t.Fatal(kase.routers, res, "params", params, "not equal", res.params)
}
for i, v := range params {
if res.params[i].Name != v.Name {
t.Fatal(kase.routers, res, "params name", v, "not equal", res.params[i])
}
if res.params[i].Value != v.Value {
t.Fatal(kase.routers, res, "params value", v, "not equal", res.params[i])
}
}
} else {
if handler != nil {
t.Fatal(kase.routers, res, "handler", handler, "should be nil")
}
}
}
}
}
func TestRouter10(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
r := New()
r.Get("/", func(ctx *Context) {
ctx.Write([]byte("test"))
})
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
r.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "test")
refute(t, len(buff.String()), 0)
}
type Route11Action struct {
}
func (Route11Action) Any() string {
return "any"
}
func TestRouter11(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
r := Classic()
r.Get("/", new(Route11Action))
r.Post("/2", new(Route11Action))
req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
if err != nil {
t.Error(err)
}
r.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "any")
refute(t, len(buff.String()), 0)
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/2", nil)
if err != nil {
t.Error(err)
}
r.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "any")
refute(t, len(buff.String()), 0)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/x7h66/tango.git
git@gitee.com:x7h66/tango.git
x7h66
tango
tango
master

搜索帮助