1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-01 10:53:27 +00:00

make router test passed

This commit is contained in:
astaxie 2015-09-08 22:05:38 +08:00
parent c11740b647
commit b2048e8653

View File

@ -52,17 +52,17 @@ func (tc *TestController) Myext() {
tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param(":ext"))) tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param(":ext")))
} }
func (tc *TestController) GetUrl() { func (tc *TestController) GetURL() {
tc.Ctx.Output.Body([]byte(tc.URLFor(".Myext"))) tc.Ctx.Output.Body([]byte(tc.URLFor(".Myext")))
} }
func (t *TestController) GetParams() { func (tc *TestController) GetParams() {
t.Ctx.WriteString(t.Ctx.Input.Query(":last") + "+" + tc.Ctx.WriteString(tc.Ctx.Input.Query(":last") + "+" +
t.Ctx.Input.Query(":first") + "+" + t.Ctx.Input.Query("learn")) tc.Ctx.Input.Query(":first") + "+" + tc.Ctx.Input.Query("learn"))
} }
func (t *TestController) GetManyRouter() { func (tc *TestController) GetManyRouter() {
t.Ctx.WriteString(t.Ctx.Input.Query(":id") + t.Ctx.Input.Query(":page")) tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page"))
} }
type ResStatus struct { type ResStatus struct {
@ -70,29 +70,29 @@ type ResStatus struct {
Msg string Msg string
} }
type JsonController struct { type JSONController struct {
Controller Controller
} }
func (this *JsonController) Prepare() { func (jc *JSONController) Prepare() {
this.Data["json"] = "prepare" jc.Data["json"] = "prepare"
this.ServeJSON(true) jc.ServeJSON(true)
} }
func (this *JsonController) Get() { func (jc *JSONController) Get() {
this.Data["Username"] = "astaxie" jc.Data["Username"] = "astaxie"
this.Ctx.Output.Body([]byte("ok")) jc.Ctx.Output.Body([]byte("ok"))
} }
func TestUrlFor(t *testing.T) { func TestUrlFor(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.Add("/api/list", &TestController{}, "*:List") handler.Add("/api/list", &TestController{}, "*:List")
handler.Add("/person/:last/:first", &TestController{}, "*:Param") handler.Add("/person/:last/:first", &TestController{}, "*:Param")
if a := handler.UrlFor("TestController.List"); a != "/api/list" { if a := handler.URLFor("TestController.List"); a != "/api/list" {
Info(a) Info(a)
t.Errorf("TestController.List must equal to /api/list") t.Errorf("TestController.List must equal to /api/list")
} }
if a := handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" { if a := handler.URLFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" {
t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a) t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a)
} }
} }
@ -100,39 +100,39 @@ func TestUrlFor(t *testing.T) {
func TestUrlFor3(t *testing.T) { func TestUrlFor3(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.AddAuto(&TestController{}) handler.AddAuto(&TestController{})
if a := handler.UrlFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" { if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" {
t.Errorf("TestController.Myext must equal to /test/myext, but get " + a) t.Errorf("TestController.Myext must equal to /test/myext, but get " + a)
} }
if a := handler.UrlFor("TestController.GetUrl"); a != "/test/geturl" && a != "/Test/GetUrl" { if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" {
t.Errorf("TestController.GetUrl must equal to /test/geturl, but get " + a) t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a)
} }
} }
func TestUrlFor2(t *testing.T) { func TestUrlFor2(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List") handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List")
handler.Add("/v1/:username/edit", &TestController{}, "get:GetUrl") handler.Add("/v1/:username/edit", &TestController{}, "get:GetURL")
handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param") handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param")
handler.Add("/:year:int/:month:int/:title/:entid", &TestController{}) handler.Add("/:year:int/:month:int/:title/:entid", &TestController{})
if handler.UrlFor("TestController.GetUrl", ":username", "astaxie") != "/v1/astaxie/edit" { if handler.URLFor("TestController.GetURL", ":username", "astaxie") != "/v1/astaxie/edit" {
Info(handler.UrlFor("TestController.GetUrl")) Info(handler.URLFor("TestController.GetURL"))
t.Errorf("TestController.List must equal to /v1/astaxie/edit") t.Errorf("TestController.List must equal to /v1/astaxie/edit")
} }
if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") != if handler.URLFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") !=
"/v1/za/cms_12_123.html" { "/v1/za/cms_12_123.html" {
Info(handler.UrlFor("TestController.List")) Info(handler.URLFor("TestController.List"))
t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html") t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html")
} }
if handler.UrlFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") != if handler.URLFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") !=
"/v1/za_cms/ttt_12_123.html" { "/v1/za_cms/ttt_12_123.html" {
Info(handler.UrlFor("TestController.Param")) Info(handler.URLFor("TestController.Param"))
t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html") t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html")
} }
if handler.UrlFor("TestController.Get", ":year", "1111", ":month", "11", if handler.URLFor("TestController.Get", ":year", "1111", ":month", "11",
":title", "aaaa", ":entid", "aaaa") != ":title", "aaaa", ":entid", "aaaa") !=
"/1111/11/aaaa/aaaa" { "/1111/11/aaaa/aaaa" {
Info(handler.UrlFor("TestController.Get")) Info(handler.URLFor("TestController.Get"))
t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa") t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa")
} }
} }
@ -270,7 +270,7 @@ func TestPrepare(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
handler := NewControllerRegister() handler := NewControllerRegister()
handler.Add("/json/list", &JsonController{}) handler.Add("/json/list", &JSONController{})
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
if w.Body.String() != `"prepare"` { if w.Body.String() != `"prepare"` {
t.Errorf(w.Body.String() + "user define func can't run") t.Errorf(w.Body.String() + "user define func can't run")