mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 15:40:55 +00:00
276 lines
7.0 KiB
Go
276 lines
7.0 KiB
Go
// Beego (http://beego.me/)
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
|
// @authors astaxie
|
|
|
|
package beego
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/astaxie/beego/context"
|
|
)
|
|
|
|
type TestController struct {
|
|
Controller
|
|
}
|
|
|
|
func (this *TestController) Get() {
|
|
this.Data["Username"] = "astaxie"
|
|
this.Ctx.Output.Body([]byte("ok"))
|
|
}
|
|
|
|
func (this *TestController) Post() {
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Query(":name")))
|
|
}
|
|
|
|
func (this *TestController) List() {
|
|
this.Ctx.Output.Body([]byte("i am list"))
|
|
}
|
|
|
|
func (this *TestController) Params() {
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Params["0"] + this.Ctx.Input.Params["1"] + this.Ctx.Input.Params["2"]))
|
|
}
|
|
|
|
func (this *TestController) Myext() {
|
|
this.Ctx.Output.Body([]byte(this.Ctx.Input.Param(":ext")))
|
|
}
|
|
|
|
func (this *TestController) GetUrl() {
|
|
this.Ctx.Output.Body([]byte(this.UrlFor(".Myext")))
|
|
}
|
|
|
|
func (t *TestController) GetParams() {
|
|
t.Ctx.WriteString(t.Ctx.Input.Query(":last") + "+" +
|
|
t.Ctx.Input.Query(":first") + "+" + t.Ctx.Input.Query("learn"))
|
|
}
|
|
|
|
func (t *TestController) GetManyRouter() {
|
|
t.Ctx.WriteString(t.Ctx.Input.Query(":id") + t.Ctx.Input.Query(":page"))
|
|
}
|
|
|
|
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"))
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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{}, "get:GetParams")
|
|
handler.ServeHTTP(w, r)
|
|
body := w.Body.String()
|
|
if body != "anderson+thomas+kungfu" {
|
|
t.Errorf("url param set to [%s];", body)
|
|
}
|
|
}
|
|
|
|
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{}, "get:GetManyRouter")
|
|
handler.ServeHTTP(w, r)
|
|
|
|
body := w.Body.String()
|
|
|
|
if body != "3212" {
|
|
t.Errorf("url param set to [%s];", body)
|
|
}
|
|
}
|
|
|
|
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) {
|
|
r, _ := http.NewRequest("GET", "/static/js/jquery.js", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler := NewControllerRegistor()
|
|
handler.ServeHTTP(w, r)
|
|
|
|
if w.Code != 404 {
|
|
t.Errorf("handler.Static failed to serve file")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestRouterGet(t *testing.T) {
|
|
r, _ := http.NewRequest("GET", "/user", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler := NewControllerRegistor()
|
|
handler.Get("/user", func(ctx *context.Context) {
|
|
ctx.Output.Body([]byte("Get userlist"))
|
|
})
|
|
handler.ServeHTTP(w, r)
|
|
if w.Body.String() != "Get userlist" {
|
|
t.Errorf("TestRouterGet can't run")
|
|
}
|
|
}
|
|
|
|
func TestRouterPost(t *testing.T) {
|
|
r, _ := http.NewRequest("POST", "/user/123", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler := NewControllerRegistor()
|
|
handler.Post("/user/:id", func(ctx *context.Context) {
|
|
ctx.Output.Body([]byte(ctx.Input.Param(":id")))
|
|
})
|
|
handler.ServeHTTP(w, r)
|
|
if w.Body.String() != "123" {
|
|
t.Errorf("TestRouterPost can't run")
|
|
}
|
|
}
|
|
|
|
func sayhello(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("sayhello"))
|
|
}
|
|
|
|
func TestRouterHandler(t *testing.T) {
|
|
r, _ := http.NewRequest("POST", "/sayhi", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler := NewControllerRegistor()
|
|
handler.Handler("/sayhi", http.HandlerFunc(sayhello))
|
|
handler.ServeHTTP(w, r)
|
|
if w.Body.String() != "sayhello" {
|
|
t.Errorf("TestRouterHandler can't run")
|
|
}
|
|
}
|