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"
|
|
|
|
"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) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/test"), "1342")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "57test86")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_BindMiddleware_Basic2(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/test"), "Not Found")
|
|
|
|
gtest.Assert(client.PutContent("/test"), "1342")
|
|
|
|
gtest.Assert(client.PostContent("/test"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "test")
|
|
|
|
gtest.Assert(client.PutContent("/test/test"), "test")
|
|
|
|
gtest.Assert(client.PostContent("/test/test"), "57test86")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic1(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "1342")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "13test42")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic2(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("PUT:/test/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "1342")
|
|
|
|
gtest.Assert(client.PutContent("/"), "1342")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "1342")
|
|
|
|
gtest.Assert(client.PutContent("/test/test"), "13test42")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic3(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(func(r *ghttp.Request) {
|
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("2")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "12")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "1test2")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic4(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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.AddMiddleware(func(r *ghttp.Request) {
|
|
|
|
r.Middleware.Next()
|
2019-08-03 23:57:20 +08:00
|
|
|
r.Response.Write("1")
|
|
|
|
})
|
2019-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "21")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "2test1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic5(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "12")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "12test")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-08-06 20:40:04 +08:00
|
|
|
func Test_AddMiddleware_Basic6(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
2019-08-06 20:40:04 +08:00
|
|
|
s.BindObject("/", new(ObjectMiddleware))
|
|
|
|
s.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "13100Object Index20042")
|
|
|
|
gtest.Assert(client.GetContent("/init"), "1342")
|
|
|
|
gtest.Assert(client.GetContent("/shut"), "1342")
|
|
|
|
gtest.Assert(client.GetContent("/index"), "13100Object Index20042")
|
|
|
|
gtest.Assert(client.GetContent("/show"), "13100Object Show20042")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "1342")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:40:04 +08:00
|
|
|
func Test_Hook_Middleware_Basic1(t *testing.T) {
|
2019-08-03 23:57:20 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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-08-06 20:40:04 +08:00
|
|
|
s.AddMiddleware(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)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "ac1342bd")
|
|
|
|
gtest.Assert(client.GetContent("/test/test"), "ac13test42bd")
|
|
|
|
})
|
|
|
|
}
|