2019-08-03 23:57:20 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-09 00:33:58 +08:00
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2019-09-14 22:53:28 +08:00
|
|
|
"net/http"
|
2019-08-03 23:57:20 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/net/ghttp"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
)
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_BindMiddleware_Basic1(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindMiddleware("/test", func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
}, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindMiddleware("/test/:name", func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("5")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("6")
|
|
|
|
}, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("7")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("8")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
//s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test"), "1342")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "57test86")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_BindMiddleware_Basic2(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-11-26 14:56:05 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
s.BindMiddleware("/*", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("1")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-11-26 14:56:05 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-26 14:56:05 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "12")
|
|
|
|
t.Assert(client.GetContent("/test"), "12")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "1test2")
|
2019-11-26 14:56:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_BindMiddleware_Basic3(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindMiddleware("PUT:/test", func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
}, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindMiddleware("POST:/test/:name", func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("5")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("6")
|
|
|
|
}, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("7")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("8")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test"), "Not Found")
|
|
|
|
t.Assert(client.PutContent("/test"), "1342")
|
|
|
|
t.Assert(client.PostContent("/test"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "test")
|
|
|
|
t.Assert(client.PutContent("/test/test"), "test")
|
|
|
|
t.Assert(client.PostContent("/test/test"), "57test86")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-04 10:03:03 +08:00
|
|
|
func Test_BindMiddleware_Basic4(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-09 00:33:58 +08:00
|
|
|
s := g.Server(p)
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("1")
|
|
|
|
r.Middleware.Next()
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.ALL("/test", func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-10-09 00:33:58 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-09 00:33:58 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test"), "1test2")
|
|
|
|
t.Assert(client.PutContent("/test/none"), "Not Found")
|
2019-10-09 00:33:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Middleware_With_Static(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-09 00:33:58 +08:00
|
|
|
s := g.Server(p)
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("1")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.ALL("/user/list", func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("list")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2020-03-21 19:41:05 +08:00
|
|
|
s.SetServerRoot(gdebug.TestDataPath("static1"))
|
2019-10-09 00:33:58 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-09 00:33:58 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "index")
|
|
|
|
t.Assert(client.GetContent("/test.html"), "test")
|
|
|
|
t.Assert(client.GetContent("/none"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/user/list"), "1list2")
|
2019-10-09 00:33:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-09 15:26:50 +08:00
|
|
|
func Test_Middleware_Status(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-09 15:26:50 +08:00
|
|
|
s := g.Server(p)
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
2019-10-09 15:26:50 +08:00
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.WriteOver(r.Response.Status)
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.ALL("/user/list", func(r *ghttp.Request) {
|
2019-10-09 15:26:50 +08:00
|
|
|
r.Response.Write("list")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-10-09 15:26:50 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-09 15:26:50 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/user/list"), "200")
|
2019-10-09 15:26:50 +08:00
|
|
|
|
|
|
|
resp, err := client.Get("/")
|
|
|
|
defer resp.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp.StatusCode, 404)
|
2019-10-09 15:26:50 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
func Test_Middleware_Hook_With_Static(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-09 00:33:58 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
a := garray.New(true)
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
a.Append(1)
|
2019-10-11 23:33:21 +08:00
|
|
|
fmt.Println("HOOK_BEFORE_SERVE")
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("a")
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.Hook("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
a.Append(1)
|
2019-10-11 23:33:21 +08:00
|
|
|
fmt.Println("HOOK_AFTER_SERVE")
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("b")
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("1")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-12-02 23:00:48 +08:00
|
|
|
group.ALL("/user/list", func(r *ghttp.Request) {
|
2019-10-09 00:33:58 +08:00
|
|
|
r.Response.Write("list")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
//s.SetDumpRouterMap(false)
|
2020-03-21 19:41:05 +08:00
|
|
|
s.SetServerRoot(gdebug.TestDataPath("static1"))
|
2019-10-09 00:33:58 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-09 00:33:58 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2019-10-13 22:40:23 +08:00
|
|
|
// The length assert sometimes fails, so I added time.Sleep here for debug purpose.
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "index")
|
2019-10-13 22:40:23 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(a.Len(), 2)
|
2019-10-13 22:40:23 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/test.html"), "test")
|
2019-10-13 22:40:23 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(a.Len(), 4)
|
2019-10-13 22:40:23 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/none"), "ab")
|
2019-10-13 22:40:23 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(a.Len(), 6)
|
2019-10-13 22:40:23 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/user/list"), "a1list2b")
|
2019-10-13 22:40:23 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(a.Len(), 8)
|
2019-10-09 00:33:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-10 17:46:11 +08:00
|
|
|
func Test_BindMiddleware_Status(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-10 17:46:11 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
s.BindMiddleware("/test/*any", func(r *ghttp.Request) {
|
|
|
|
r.Middleware.Next()
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-10 17:46:11 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-10 17:46:11 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "test")
|
|
|
|
t.Assert(client.GetContent("/test/test/test"), "Not Found")
|
2019-08-10 17:46:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic1(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "1342")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "13test42")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic2(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("PUT:/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "1342")
|
|
|
|
t.Assert(client.PutContent("/"), "1342")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "1342")
|
|
|
|
t.Assert(client.PutContent("/test/test"), "13test42")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic3(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "12")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "1test2")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic4(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "21")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "2test1")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic5(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "12")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "12test")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Status(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-10 17:46:11 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-10 17:46:11 +08:00
|
|
|
r.Middleware.Next()
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-10 17:46:11 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-10 17:46:11 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "test")
|
2019-08-10 17:46:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
type ObjectMiddleware struct{}
|
2019-08-03 23:57:20 +08:00
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func (o *ObjectMiddleware) Init(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("100")
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func (o *ObjectMiddleware) Shut(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("200")
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func (o *ObjectMiddleware) Index(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("Object Index")
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func (o *ObjectMiddleware) Show(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("Object Show")
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func (o *ObjectMiddleware) Info(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("Object Info")
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:19:07 +08:00
|
|
|
func Test_BindMiddlewareDefault_Basic6(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindObject("/", new(ObjectMiddleware))
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "13100Object Index20042")
|
|
|
|
t.Assert(client.GetContent("/init"), "1342")
|
|
|
|
t.Assert(client.GetContent("/shut"), "1342")
|
|
|
|
t.Assert(client.GetContent("/index"), "13100Object Index20042")
|
|
|
|
t.Assert(client.GetContent("/show"), "13100Object Show20042")
|
|
|
|
t.Assert(client.GetContent("/none-exist"), "1342")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_Hook_Middleware_Basic1(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-08-03 23:57:20 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("a")
|
|
|
|
})
|
|
|
|
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("b")
|
|
|
|
})
|
|
|
|
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("c")
|
|
|
|
})
|
|
|
|
s.BindHookHandler("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("d")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
2019-09-19 20:19:07 +08:00
|
|
|
s.BindMiddlewareDefault(func(r *ghttp.Request) {
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("3")
|
2019-08-06 20:40:04 +08:00
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("4")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-08-03 23:57:20 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "ac1342bd")
|
|
|
|
t.Assert(client.GetContent("/test/test"), "ac13test42bd")
|
2019-08-03 23:57:20 +08:00
|
|
|
})
|
|
|
|
}
|
2019-09-14 22:53:28 +08:00
|
|
|
|
|
|
|
func MiddlewareAuth(r *ghttp.Request) {
|
|
|
|
token := r.Get("token")
|
|
|
|
if token == "123456" {
|
|
|
|
r.Middleware.Next()
|
|
|
|
} else {
|
|
|
|
r.Response.WriteStatus(http.StatusForbidden)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiddlewareCORS(r *ghttp.Request) {
|
|
|
|
r.Response.CORSDefault()
|
|
|
|
r.Middleware.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Middleware_CORSAndAuth(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-09-14 22:53:28 +08:00
|
|
|
s := g.Server(p)
|
2020-03-04 22:52:56 +08:00
|
|
|
s.Use(MiddlewareCORS)
|
2019-12-02 23:00:48 +08:00
|
|
|
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
|
2020-03-04 22:52:56 +08:00
|
|
|
group.Middleware(MiddlewareAuth)
|
2020-02-16 13:32:06 +08:00
|
|
|
group.POST("/user/list", func(r *ghttp.Request) {
|
2019-09-14 22:53:28 +08:00
|
|
|
r.Response.Write("list")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-09-14 22:53:28 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-14 22:53:28 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2020-02-16 13:32:06 +08:00
|
|
|
// Common Checks.
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/api.v2"), "Not Found")
|
2020-02-16 13:32:06 +08:00
|
|
|
// Auth Checks.
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.PostContent("/api.v2/user/list"), "Forbidden")
|
|
|
|
t.Assert(client.PostContent("/api.v2/user/list", "token=123456"), "list")
|
2020-02-16 13:32:06 +08:00
|
|
|
// CORS Checks.
|
|
|
|
resp, err := client.Post("/api.v2/user/list", "token=123456")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(len(resp.Header["Access-Control-Allow-Headers"]), 1)
|
|
|
|
t.Assert(resp.Header["Access-Control-Allow-Headers"][0], "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With")
|
|
|
|
t.Assert(resp.Header["Access-Control-Allow-Methods"][0], "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE")
|
|
|
|
t.Assert(resp.Header["Access-Control-Allow-Origin"][0], "*")
|
|
|
|
t.Assert(resp.Header["Access-Control-Max-Age"][0], "3628800")
|
2020-02-16 13:32:06 +08:00
|
|
|
resp.Close()
|
2019-09-14 22:53:28 +08:00
|
|
|
})
|
|
|
|
}
|
2019-12-17 21:06:34 +08:00
|
|
|
|
|
|
|
func MiddlewareScope1(r *ghttp.Request) {
|
|
|
|
r.Response.Write("a")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("b")
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiddlewareScope2(r *ghttp.Request) {
|
|
|
|
r.Response.Write("c")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("d")
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiddlewareScope3(r *ghttp.Request) {
|
|
|
|
r.Response.Write("e")
|
|
|
|
r.Middleware.Next()
|
|
|
|
r.Response.Write("f")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Middleware_Scope(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-12-17 21:06:34 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(MiddlewareScope1)
|
|
|
|
group.ALL("/scope1", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("1")
|
|
|
|
})
|
|
|
|
group.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(MiddlewareScope2)
|
|
|
|
group.ALL("/scope2", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
group.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(MiddlewareScope3)
|
|
|
|
group.ALL("/scope3", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("3")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-17 21:06:34 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
t.Assert(client.GetContent("/scope1"), "a1b")
|
|
|
|
t.Assert(client.GetContent("/scope2"), "ac2db")
|
|
|
|
t.Assert(client.GetContent("/scope3"), "ae3fb")
|
2019-12-17 21:06:34 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-04 22:52:56 +08:00
|
|
|
|
|
|
|
func Test_Middleware_Panic(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-03-04 22:52:56 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
i := 0
|
|
|
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Group("/", func(group *ghttp.RouterGroup) {
|
|
|
|
group.Middleware(func(r *ghttp.Request) {
|
|
|
|
i++
|
|
|
|
panic("error")
|
|
|
|
r.Middleware.Next()
|
|
|
|
}, func(r *ghttp.Request) {
|
|
|
|
i++
|
|
|
|
r.Middleware.Next()
|
|
|
|
})
|
|
|
|
group.ALL("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(i)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
//s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-04 22:52:56 +08:00
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(client.GetContent("/"), "error")
|
2020-03-04 22:52:56 +08:00
|
|
|
})
|
|
|
|
}
|