1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-27 16:51:28 +00:00
Beego/router_test.go

733 lines
20 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-09-11 07:49:12 +00:00
package beego
import (
"bytes"
2013-09-11 07:49:12 +00:00
"net/http"
"net/http/httptest"
"strings"
2013-09-11 07:49:12 +00:00
"testing"
2014-06-08 12:24:01 +00:00
"github.com/astaxie/beego/context"
2016-03-25 02:31:48 +00:00
"github.com/astaxie/beego/logs"
2013-09-11 07:49:12 +00:00
)
type TestController struct {
Controller
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) Get() {
tc.Data["Username"] = "astaxie"
tc.Ctx.Output.Body([]byte("ok"))
2013-09-11 07:49:12 +00:00
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) Post() {
tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name")))
2014-04-10 14:33:32 +00:00
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) Param() {
tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Query(":name")))
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) List() {
tc.Ctx.Output.Body([]byte("i am list"))
2013-09-11 07:49:12 +00:00
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) Params() {
2015-12-16 15:11:03 +00:00
tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param("0") + tc.Ctx.Input.Param("1") + tc.Ctx.Input.Param("2")))
2013-12-18 02:00:52 +00:00
}
2014-11-05 14:40:31 +00:00
func (tc *TestController) Myext() {
tc.Ctx.Output.Body([]byte(tc.Ctx.Input.Param(":ext")))
2013-09-22 03:12:37 +00:00
}
2015-09-08 14:05:38 +00:00
func (tc *TestController) GetURL() {
2015-09-08 13:41:38 +00:00
tc.Ctx.Output.Body([]byte(tc.URLFor(".Myext")))
}
2015-09-08 14:05:38 +00:00
func (tc *TestController) GetParams() {
tc.Ctx.WriteString(tc.Ctx.Input.Query(":last") + "+" +
tc.Ctx.Input.Query(":first") + "+" + tc.Ctx.Input.Query("learn"))
2014-05-19 10:52:48 +00:00
}
2015-09-08 14:05:38 +00:00
func (tc *TestController) GetManyRouter() {
tc.Ctx.WriteString(tc.Ctx.Input.Query(":id") + tc.Ctx.Input.Query(":page"))
2014-05-19 10:52:48 +00:00
}
2016-02-12 03:27:59 +00:00
func (tc *TestController) GetEmptyBody() {
var res []byte
tc.Ctx.Output.Body(res)
}
2015-09-08 14:05:38 +00:00
type JSONController struct {
2013-11-26 09:09:23 +00:00
Controller
}
2015-09-08 14:05:38 +00:00
func (jc *JSONController) Prepare() {
jc.Data["json"] = "prepare"
jc.ServeJSON(true)
2013-11-26 09:09:23 +00:00
}
2015-09-08 14:05:38 +00:00
func (jc *JSONController) Get() {
jc.Data["Username"] = "astaxie"
jc.Ctx.Output.Body([]byte("ok"))
2013-11-26 09:09:23 +00:00
}
func TestUrlFor(t *testing.T) {
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
handler.Add("/api/list", &TestController{}, "*:List")
handler.Add("/person/:last/:first", &TestController{}, "*:Param")
2015-09-08 14:05:38 +00:00
if a := handler.URLFor("TestController.List"); a != "/api/list" {
2016-03-25 02:31:48 +00:00
logs.Info(a)
t.Errorf("TestController.List must equal to /api/list")
}
2015-09-08 14:05:38 +00:00
if a := handler.URLFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" {
2014-06-23 07:28:53 +00:00
t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a)
}
2014-06-23 07:28:53 +00:00
}
func TestUrlFor3(t *testing.T) {
handler := NewControllerRegister()
handler.AddAuto(&TestController{})
2015-09-08 14:05:38 +00:00
if a := handler.URLFor("TestController.Myext"); a != "/test/myext" && a != "/Test/Myext" {
2014-06-23 07:28:53 +00:00
t.Errorf("TestController.Myext must equal to /test/myext, but get " + a)
}
2015-09-08 14:05:38 +00:00
if a := handler.URLFor("TestController.GetURL"); a != "/test/geturl" && a != "/Test/GetURL" {
t.Errorf("TestController.GetURL must equal to /test/geturl, but get " + a)
}
}
func TestUrlFor2(t *testing.T) {
handler := NewControllerRegister()
handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List")
2015-09-08 14:05:38 +00:00
handler.Add("/v1/:username/edit", &TestController{}, "get:GetURL")
handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param")
2014-06-18 15:32:47 +00:00
handler.Add("/:year:int/:month:int/:title/:entid", &TestController{})
2015-09-08 14:05:38 +00:00
if handler.URLFor("TestController.GetURL", ":username", "astaxie") != "/v1/astaxie/edit" {
2016-03-25 02:31:48 +00:00
logs.Info(handler.URLFor("TestController.GetURL"))
2014-07-03 15:40:21 +00:00
t.Errorf("TestController.List must equal to /v1/astaxie/edit")
}
2015-09-08 14:05:38 +00:00
if handler.URLFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") !=
"/v1/za/cms_12_123.html" {
2016-03-25 02:31:48 +00:00
logs.Info(handler.URLFor("TestController.List"))
t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html")
}
2015-09-08 14:05:38 +00:00
if handler.URLFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") !=
"/v1/za_cms/ttt_12_123.html" {
2016-03-25 02:31:48 +00:00
logs.Info(handler.URLFor("TestController.Param"))
t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html")
}
2015-09-08 14:05:38 +00:00
if handler.URLFor("TestController.Get", ":year", "1111", ":month", "11",
2014-06-18 15:32:47 +00:00
":title", "aaaa", ":entid", "aaaa") !=
"/1111/11/aaaa/aaaa" {
2016-03-25 02:31:48 +00:00
logs.Info(handler.URLFor("TestController.Get"))
2014-06-18 15:32:47 +00:00
t.Errorf("TestController.Get must equal to /1111/11/aaaa/aaaa")
}
}
2013-09-11 07:49:12 +00:00
func TestUserFunc(t *testing.T) {
r, _ := http.NewRequest("GET", "/api/list", nil)
w := httptest.NewRecorder()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-09-11 07:49:12 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2014-04-10 14:33:32 +00:00
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)
2014-07-03 15:40:21 +00:00
w := httptest.NewRecorder()
handler := NewControllerRegister()
handler.AddAuto(&TestController{})
handler.ServeHTTP(w, r)
if w.Body.String() != "i am list" {
t.Errorf("user define func can't run")
}
}
func TestAutoFunc2(t *testing.T) {
r, _ := http.NewRequest("GET", "/Test/List", nil)
2013-09-11 07:49:12 +00:00
w := httptest.NewRecorder()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-09-11 07:49:12 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-12-18 02:00:52 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-09-22 03:12:37 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2014-05-19 10:52:48 +00:00
handler.Add("/person/:last/:first", &TestController{}, "get:GetParams")
2013-09-11 07:49:12 +00:00
handler.ServeHTTP(w, r)
2014-05-19 10:52:48 +00:00
body := w.Body.String()
if body != "anderson+thomas+kungfu" {
t.Errorf("url param set to [%s];", body)
2013-09-11 07:49:12 +00:00
}
}
func TestManyRoute(t *testing.T) {
r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
w := httptest.NewRecorder()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2014-05-19 10:52:48 +00:00
handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}, "get:GetManyRouter")
handler.ServeHTTP(w, r)
2014-05-19 10:52:48 +00:00
body := w.Body.String()
2014-05-19 10:52:48 +00:00
if body != "3212" {
t.Errorf("url param set to [%s];", body)
}
}
2016-02-12 03:27:59 +00:00
// Test for issue #1669
func TestEmptyResponse(t *testing.T) {
r, _ := http.NewRequest("GET", "/beego-empty.html", nil)
w := httptest.NewRecorder()
handler := NewControllerRegister()
handler.Add("/beego-empty.html", &TestController{}, "get:GetEmptyBody")
handler.ServeHTTP(w, r)
if body := w.Body.String(); body != "" {
t.Error("want empty body")
}
}
2013-09-11 07:49:12 +00:00
func TestNotFound(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-09-11 07:49:12 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2013-09-11 07:49:12 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2015-09-08 14:05:38 +00:00
handler.Add("/json/list", &JSONController{})
2013-11-26 09:09:23 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
2014-01-01 09:57:57 +00:00
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
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()
2014-06-10 12:12:57 +00:00
handler := NewControllerRegister()
handler.Handler("/sayhi", http.HandlerFunc(sayhello))
handler.ServeHTTP(w, r)
if w.Body.String() != "sayhello" {
t.Errorf("TestRouterHandler can't run")
}
}
2014-05-20 10:20:44 +00:00
2015-10-12 13:26:18 +00:00
func TestRouterHandlerAll(t *testing.T) {
r, _ := http.NewRequest("POST", "/sayhi/a/b/c", nil)
w := httptest.NewRecorder()
handler := NewControllerRegister()
handler.Handler("/sayhi", http.HandlerFunc(sayhello), true)
handler.ServeHTTP(w, r)
if w.Body.String() != "sayhello" {
t.Errorf("TestRouterHandler can't run")
}
}
2014-05-20 10:20:44 +00:00
//
// Benchmarks NewApp:
//
func beegoFilterFunc(ctx *context.Context) {
ctx.WriteString("hello")
}
type AdminController struct {
Controller
}
func (a *AdminController) Get() {
a.Ctx.WriteString("hello")
}
2014-06-09 02:11:37 +00:00
func TestRouterFunc(t *testing.T) {
2014-06-10 12:12:57 +00:00
mux := NewControllerRegister()
2014-06-09 02:11:37 +00:00
mux.Get("/action", beegoFilterFunc)
mux.Post("/action", beegoFilterFunc)
rw, r := testRequest("GET", "/action")
mux.ServeHTTP(rw, r)
if rw.Body.String() != "hello" {
t.Errorf("TestRouterFunc can't run")
}
}
2014-05-20 10:20:44 +00:00
func BenchmarkFunc(b *testing.B) {
2014-06-10 12:12:57 +00:00
mux := NewControllerRegister()
2014-05-20 10:20:44 +00:00
mux.Get("/action", beegoFilterFunc)
rw, r := testRequest("GET", "/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mux.ServeHTTP(rw, r)
}
}
func BenchmarkController(b *testing.B) {
2014-06-10 12:12:57 +00:00
mux := NewControllerRegister()
2014-05-20 10:20:44 +00:00
mux.Add("/action", &AdminController{})
rw, r := testRequest("GET", "/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mux.ServeHTTP(rw, r)
}
}
func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
request, _ := http.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
return recorder, request
}
// Expectation: A Filter with the correct configuration should be created given
// specific parameters.
func TestInsertFilter(t *testing.T) {
testName := "TestInsertFilter"
mux := NewControllerRegister()
mux.InsertFilter("*", BeforeRouter, func(*context.Context) {})
if !mux.filters[BeforeRouter][0].returnOnOutput {
t.Errorf(
"%s: passing no variadic params should set returnOnOutput to true",
testName)
}
if mux.filters[BeforeRouter][0].resetParams {
t.Errorf(
"%s: passing no variadic params should set resetParams to false",
testName)
}
mux = NewControllerRegister()
mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, false)
if mux.filters[BeforeRouter][0].returnOnOutput {
t.Errorf(
"%s: passing false as 1st variadic param should set returnOnOutput to false",
testName)
}
mux = NewControllerRegister()
mux.InsertFilter("*", BeforeRouter, func(*context.Context) {}, true, true)
if !mux.filters[BeforeRouter][0].resetParams {
t.Errorf(
"%s: passing true as 2nd variadic param should set resetParams to true",
testName)
}
}
// Expectation: the second variadic arg should cause the execution of the filter
// to preserve the parameters from before its execution.
func TestParamResetFilter(t *testing.T) {
testName := "TestParamResetFilter"
route := "/beego/*" // splat
path := "/beego/routes/routes"
mux := NewControllerRegister()
mux.InsertFilter("*", BeforeExec, beegoResetParams, true, true)
mux.Get(route, beegoHandleResetParams)
rw, r := testRequest("GET", path)
mux.ServeHTTP(rw, r)
// The two functions, `beegoResetParams` and `beegoHandleResetParams` add
// a response header of `Splat`. The expectation here is that that Header
// value should match what the _request's_ router set, not the filter's.
2019-01-22 11:09:57 +00:00
headers := rw.Result().Header
if len(headers["Splat"]) != 1 {
t.Errorf(
"%s: There was an error in the test. Splat param not set in Header",
testName)
}
if headers["Splat"][0] != "routes/routes" {
t.Errorf(
"%s: expected `:splat` param to be [routes/routes] but it was [%s]",
testName, headers["Splat"][0])
}
}
// Execution point: BeforeRouter
// expectation: only BeforeRouter function is executed, notmatch output as router doesn't handle
func TestFilterBeforeRouter(t *testing.T) {
testName := "TestFilterBeforeRouter"
url := "/beforeRouter"
mux := NewControllerRegister()
mux.InsertFilter(url, BeforeRouter, beegoBeforeRouter1)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "BeforeRouter1") {
t.Errorf(testName + " BeforeRouter did not run")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " BeforeRouter did not return properly")
}
}
// Execution point: BeforeExec
// expectation: only BeforeExec function is executed, match as router determines route only
func TestFilterBeforeExec(t *testing.T) {
testName := "TestFilterBeforeExec"
url := "/beforeExec"
mux := NewControllerRegister()
mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
mux.InsertFilter(url, BeforeExec, beegoBeforeExec1)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "BeforeExec1") {
t.Errorf(testName + " BeforeExec did not run")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " BeforeExec did not return properly")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "BeforeRouter") {
t.Errorf(testName + " BeforeRouter ran in error")
}
}
// Execution point: AfterExec
// expectation: only AfterExec function is executed, match as router handles
func TestFilterAfterExec(t *testing.T) {
testName := "TestFilterAfterExec"
url := "/afterExec"
mux := NewControllerRegister()
mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput)
2015-06-13 04:47:01 +00:00
mux.InsertFilter(url, AfterExec, beegoAfterExec1, false)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "AfterExec1") {
t.Errorf(testName + " AfterExec did not run")
}
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " handler did not run properly")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "BeforeRouter") {
t.Errorf(testName + " BeforeRouter ran in error")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "BeforeExec") {
t.Errorf(testName + " BeforeExec ran in error")
}
}
// Execution point: FinishRouter
// expectation: only FinishRouter function is executed, match as router handles
func TestFilterFinishRouter(t *testing.T) {
testName := "TestFilterFinishRouter"
url := "/finishRouter"
mux := NewControllerRegister()
mux.InsertFilter(url, BeforeRouter, beegoFilterNoOutput)
mux.InsertFilter(url, BeforeExec, beegoFilterNoOutput)
mux.InsertFilter(url, AfterExec, beegoFilterNoOutput)
mux.InsertFilter(url, FinishRouter, beegoFinishRouter1)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "FinishRouter1") {
t.Errorf(testName + " FinishRouter did not run")
}
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " handler did not run properly")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "AfterExec1") {
t.Errorf(testName + " AfterExec ran in error")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "BeforeRouter") {
t.Errorf(testName + " BeforeRouter ran in error")
}
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "BeforeExec") {
t.Errorf(testName + " BeforeExec ran in error")
}
}
// Execution point: FinishRouter
// expectation: only first FinishRouter function is executed, match as router handles
func TestFilterFinishRouterMultiFirstOnly(t *testing.T) {
testName := "TestFilterFinishRouterMultiFirstOnly"
url := "/finishRouterMultiFirstOnly"
mux := NewControllerRegister()
2015-06-13 04:47:01 +00:00
mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false)
mux.InsertFilter(url, FinishRouter, beegoFinishRouter2)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "FinishRouter1") {
t.Errorf(testName + " FinishRouter1 did not run")
}
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " handler did not run properly")
}
// not expected in body
2017-03-17 17:24:45 +00:00
if strings.Contains(rw.Body.String(), "FinishRouter2") {
t.Errorf(testName + " FinishRouter2 did run")
}
}
// Execution point: FinishRouter
// expectation: both FinishRouter functions execute, match as router handles
func TestFilterFinishRouterMulti(t *testing.T) {
testName := "TestFilterFinishRouterMulti"
url := "/finishRouterMulti"
mux := NewControllerRegister()
mux.InsertFilter(url, FinishRouter, beegoFinishRouter1, false)
2015-06-13 04:47:01 +00:00
mux.InsertFilter(url, FinishRouter, beegoFinishRouter2, false)
mux.Get(url, beegoFilterFunc)
rw, r := testRequest("GET", url)
mux.ServeHTTP(rw, r)
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "FinishRouter1") {
t.Errorf(testName + " FinishRouter1 did not run")
}
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "hello") {
t.Errorf(testName + " handler did not run properly")
}
2017-03-17 17:24:45 +00:00
if !strings.Contains(rw.Body.String(), "FinishRouter2") {
t.Errorf(testName + " FinishRouter2 did not run properly")
}
}
func beegoFilterNoOutput(ctx *context.Context) {
}
func beegoBeforeRouter1(ctx *context.Context) {
ctx.WriteString("|BeforeRouter1")
}
func beegoBeforeExec1(ctx *context.Context) {
ctx.WriteString("|BeforeExec1")
}
func beegoAfterExec1(ctx *context.Context) {
ctx.WriteString("|AfterExec1")
}
func beegoFinishRouter1(ctx *context.Context) {
ctx.WriteString("|FinishRouter1")
}
func beegoFinishRouter2(ctx *context.Context) {
ctx.WriteString("|FinishRouter2")
}
func beegoResetParams(ctx *context.Context) {
ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat"))
}
func beegoHandleResetParams(ctx *context.Context) {
ctx.ResponseWriter.Header().Set("splat", ctx.Input.Param(":splat"))
}
2018-05-31 11:35:23 +00:00
// YAML
type YAMLController struct {
Controller
}
func (jc *YAMLController) Prepare() {
jc.Data["yaml"] = "prepare"
jc.ServeYAML()
}
func (jc *YAMLController) Get() {
jc.Data["Username"] = "astaxie"
jc.Ctx.Output.Body([]byte("ok"))
}
func TestYAMLPrepare(t *testing.T) {
r, _ := http.NewRequest("GET", "/yaml/list", nil)
w := httptest.NewRecorder()
handler := NewControllerRegister()
handler.Add("/yaml/list", &YAMLController{})
handler.ServeHTTP(w, r)
if strings.TrimSpace(w.Body.String()) != "prepare" {
t.Errorf(w.Body.String())
}
}
func TestRouterEntityTooLargeCopyBody(t *testing.T) {
_MaxMemory := BConfig.MaxMemory
_CopyRequestBody := BConfig.CopyRequestBody
BConfig.CopyRequestBody = true
BConfig.MaxMemory = 20
b := bytes.NewBuffer([]byte("barbarbarbarbarbarbarbarbarbar"))
r, _ := http.NewRequest("POST", "/user/123", b)
w := httptest.NewRecorder()
handler := NewControllerRegister()
handler.Post("/user/:id", func(ctx *context.Context) {
ctx.Output.Body([]byte(ctx.Input.Param(":id")))
})
handler.ServeHTTP(w, r)
BConfig.CopyRequestBody = _CopyRequestBody
BConfig.MaxMemory = _MaxMemory
if w.Code != http.StatusRequestEntityTooLarge {
t.Errorf("TestRouterRequestEntityTooLarge can't run")
}
}