2013-09-11 07:49:12 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestController struct {
|
|
|
|
Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TestController) Get() {
|
|
|
|
this.Data["Username"] = "astaxie"
|
|
|
|
this.Ctx.Output.Body([]byte("ok"))
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:33:32 +00:00
|
|
|
func (this *TestController) Post() {
|
|
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Query(":name")))
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:49:12 +00:00
|
|
|
func (this *TestController) List() {
|
|
|
|
this.Ctx.Output.Body([]byte("i am list"))
|
|
|
|
}
|
|
|
|
|
2013-12-18 02:00:52 +00:00
|
|
|
func (this *TestController) Params() {
|
|
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Params["0"] + this.Ctx.Input.Params["1"] + this.Ctx.Input.Params["2"]))
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:12:37 +00:00
|
|
|
func (this *TestController) Myext() {
|
2013-11-26 06:13:23 +00:00
|
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Param(":ext")))
|
2013-09-22 03:12:37 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 15:05:07 +00:00
|
|
|
func (this *TestController) GetUrl() {
|
|
|
|
this.Ctx.Output.Body([]byte(this.UrlFor(".Myext")))
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:09:23 +00:00
|
|
|
type ResStatus struct {
|
|
|
|
Code int
|
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
type JsonController struct {
|
|
|
|
Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *JsonController) Prepare() {
|
|
|
|
this.Data["json"] = "prepare"
|
|
|
|
this.ServeJson(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *JsonController) Get() {
|
|
|
|
this.Data["Username"] = "astaxie"
|
|
|
|
this.Ctx.Output.Body([]byte("ok"))
|
|
|
|
}
|
|
|
|
|
2013-11-10 15:05:07 +00:00
|
|
|
func TestUrlFor(t *testing.T) {
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/api/list", &TestController{}, "*:List")
|
|
|
|
handler.Add("/person/:last/:first", &TestController{})
|
|
|
|
handler.AddAuto(&TestController{})
|
|
|
|
if handler.UrlFor("TestController.List") != "/api/list" {
|
|
|
|
t.Errorf("TestController.List must equal to /api/list")
|
|
|
|
}
|
|
|
|
if handler.UrlFor("TestController.Get", ":last", "xie", ":first", "asta") != "/person/xie/asta" {
|
|
|
|
t.Errorf("TestController.Get must equal to /person/xie/asta")
|
|
|
|
}
|
|
|
|
if handler.UrlFor("TestController.Myext") != "/Test/Myext" {
|
|
|
|
t.Errorf("TestController.Myext must equal to /Test/Myext")
|
|
|
|
}
|
|
|
|
if handler.UrlFor("TestController.GetUrl") != "/Test/GetUrl" {
|
|
|
|
t.Errorf("TestController.GetUrl must equal to /Test/GetUrl")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:49:12 +00:00
|
|
|
func TestUserFunc(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/api/list", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/api/list", &TestController{}, "*:List")
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "i am list" {
|
|
|
|
t.Errorf("user define func can't run")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:33:32 +00:00
|
|
|
func TestPostFunc(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("POST", "/astaxie", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/:name", &TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "astaxie" {
|
|
|
|
t.Errorf("post func should astaxie")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:49:12 +00:00
|
|
|
func TestAutoFunc(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/test/list", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.AddAuto(&TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "i am list" {
|
|
|
|
t.Errorf("user define func can't run")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 02:00:52 +00:00
|
|
|
func TestAutoFuncParams(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/test/params/2009/11/12", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.AddAuto(&TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "20091112" {
|
|
|
|
t.Errorf("user define func can't run")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 03:12:37 +00:00
|
|
|
func TestAutoExtFunc(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/test/myext.json", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.AddAuto(&TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "json" {
|
|
|
|
t.Errorf("user define func can't run")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:49:12 +00:00
|
|
|
func TestRouteOk(t *testing.T) {
|
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/person/:last/:first", &TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
lastNameParam := r.URL.Query().Get(":last")
|
|
|
|
firstNameParam := r.URL.Query().Get(":first")
|
|
|
|
learnParam := r.URL.Query().Get("learn")
|
|
|
|
|
|
|
|
if lastNameParam != "anderson" {
|
|
|
|
t.Errorf("url param set to [%s]; want [%s]", lastNameParam, "anderson")
|
|
|
|
}
|
|
|
|
if firstNameParam != "thomas" {
|
|
|
|
t.Errorf("url param set to [%s]; want [%s]", firstNameParam, "thomas")
|
|
|
|
}
|
|
|
|
if learnParam != "kungfu" {
|
|
|
|
t.Errorf("url param set to [%s]; want [%s]", learnParam, "kungfu")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 03:22:14 +00:00
|
|
|
func TestManyRoute(t *testing.T) {
|
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
id := r.URL.Query().Get(":id")
|
|
|
|
page := r.URL.Query().Get(":page")
|
|
|
|
|
|
|
|
if id != "32" {
|
|
|
|
t.Errorf("url param set to [%s]; want [%s]", id, "32")
|
|
|
|
}
|
|
|
|
if page != "12" {
|
|
|
|
t.Errorf("url param set to [%s]; want [%s]", page, "12")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 07:49:12 +00:00
|
|
|
func TestNotFound(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
|
|
t.Errorf("Code set to [%v]; want [%v]", w.Code, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStatic tests the ability to serve static
|
|
|
|
// content from the filesystem
|
|
|
|
func TestStatic(t *testing.T) {
|
2013-11-10 15:26:28 +00:00
|
|
|
r, _ := http.NewRequest("GET", "/static/js/jquery.js", nil)
|
2013-09-11 07:49:12 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
|
2013-11-10 15:26:28 +00:00
|
|
|
if w.Code != 404 {
|
2013-09-11 07:49:12 +00:00
|
|
|
t.Errorf("handler.Static failed to serve file")
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 09:09:23 +00:00
|
|
|
|
|
|
|
func TestPrepare(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/json/list", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.Add("/json/list", &JsonController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != `"prepare"` {
|
|
|
|
t.Errorf(w.Body.String() + "user define func can't run")
|
|
|
|
}
|
|
|
|
}
|
2014-01-01 09:57:57 +00:00
|
|
|
|
|
|
|
func TestAutoPrefix(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/admin/test/list", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler := NewControllerRegistor()
|
|
|
|
handler.AddAutoPrefix("/admin", &TestController{})
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Body.String() != "i am list" {
|
|
|
|
t.Errorf("TestAutoPrefix can't run")
|
|
|
|
}
|
|
|
|
}
|