improve middleware feature for ghttp.Server; fix issue memory usage in big file downloading

This commit is contained in:
John
2019-10-09 00:33:58 +08:00
parent 977c8b7ee3
commit 41e9d35487
30 changed files with 281 additions and 173 deletions

View File

@ -47,7 +47,7 @@ func (r *Response) Write(content ...interface{}) {
if len(content) == 0 {
return
}
if r.Status == 0 && r.Request.hasServeHandler {
if r.Status == 0 {
r.Status = http.StatusOK
}
for _, v := range content {
@ -125,17 +125,18 @@ func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
// 返回HTTP Code状态码
func (r *Response) WriteStatus(status int, content ...interface{}) {
// Avoid error: http: multiple response.WriteHeader calls.
if r.Status == 0 {
r.WriteHeader(status)
}
if r.buffer.Len() == 0 {
// 状态码注册回调函数处理
if status != http.StatusOK {
if f := r.Request.Server.getStatusHandler(status, r.Request); f != nil {
// Call custom status code handler.
niceCallFunc(func() {
f(r.Request)
})
// 防止多次设置(http: multiple response.WriteHeader calls)
if r.Status == 0 {
r.WriteHeader(status)
}
return
}
}
@ -149,7 +150,6 @@ func (r *Response) WriteStatus(status int, content ...interface{}) {
r.Write(http.StatusText(status))
}
}
r.WriteHeader(status)
}
// 静态文件处理

View File

@ -21,6 +21,11 @@ type ResponseWriter struct {
buffer *bytes.Buffer // The output buffer.
}
// RawWriter returns the underlying ResponseWriter.
func (w *ResponseWriter) RawWriter() http.ResponseWriter {
return w.writer
}
// Header implements the interface function of http.ResponseWriter.Header.
func (w *ResponseWriter) Header() http.Header {
return w.writer.Header()

View File

@ -66,15 +66,6 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) {
defer func() {
// 设置请求完成时间
request.LeaveTime = gtime.Microsecond()
// 如果没有产生异常状态那么设置返回状态为200
if request.Response.Status == 0 {
if request.Middleware.served || request.Response.buffer.Len() > 0 {
request.Response.Status = http.StatusOK
} else {
request.Response.WriteStatus(http.StatusNotFound)
}
}
// error log
if request.error != nil {
s.handleErrorLog(request.error, request)
@ -84,7 +75,6 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) {
s.handleErrorLog(gerror.Newf("%v", exception), request)
}
}
// access log
s.handleAccessLog(request)
}()
@ -104,12 +94,10 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) {
}
// 动态服务检索
if serveFile == nil || serveFile.dir {
request.handlers, request.hasHookHandler, request.hasServeHandler = s.getHandlersWithCache(request)
}
request.handlers, request.hasHookHandler, request.hasServeHandler = s.getHandlersWithCache(request)
// 判断最终对该请求提供的服务方式
if serveFile != nil && serveFile.dir && request.handlers != nil {
if serveFile != nil && serveFile.dir && request.hasServeHandler {
request.isFileRequest = false
}
@ -122,7 +110,7 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) {
// 静态服务
s.serveFile(request, serveFile)
} else {
if request.hasServeHandler {
if len(request.handlers) > 0 {
// 动态服务
request.Middleware.Next()
} else {
@ -149,6 +137,16 @@ func (s *Server) handleRequest(w http.ResponseWriter, r *http.Request) {
if !request.IsExited() {
s.callHookHandler(HOOK_BEFORE_OUTPUT, request)
}
// 状态码处理如果没有产生异常状态那么设置返回状态为200
if request.Response.Status == 0 {
if request.Middleware.served || request.Response.buffer.Len() > 0 {
request.Response.Status = http.StatusOK
} else {
request.Response.WriteStatus(http.StatusNotFound)
}
}
// 设置Session Id到Cookie中
if request.Session.IsDirty() && request.Session.Id() != request.GetSessionId() {
request.Cookie.SetSessionId(request.Session.Id())
@ -253,6 +251,11 @@ func (s *Server) serveFile(r *Request, f *staticServeFile, allowIndex ...bool) {
return
}
defer file.Close()
// Clear the response buffer before file serving.
// It ignores all custom buffer content and uses the file content.
r.Response.ClearBuffer()
info, _ := file.Stat()
if info.IsDir() {
if s.config.IndexFolder || (len(allowIndex) > 0 && allowIndex[0]) {
@ -261,7 +264,7 @@ func (s *Server) serveFile(r *Request, f *staticServeFile, allowIndex ...bool) {
r.Response.WriteStatus(http.StatusForbidden)
}
} else {
http.ServeContent(r.Response.Writer, r.Request, info.Name(), info.ModTime(), file)
http.ServeContent(r.Response.Writer.RawWriter(), r.Request, info.Name(), info.ModTime(), file)
}
}

View File

@ -33,8 +33,7 @@ func Test_Cookie(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)

View File

@ -8,6 +8,9 @@ package ghttp_test
import (
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/os/gfile"
"net/http"
"testing"
"time"
@ -46,14 +49,13 @@ func Test_BindMiddleware_Basic1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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.GetContent("/test"), "1342")
gtest.Assert(client.GetContent("/test/test"), "57test86")
})
}
@ -87,15 +89,14 @@ func Test_BindMiddleware_Basic2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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"), "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")
@ -103,6 +104,111 @@ func Test_BindMiddleware_Basic2(t *testing.T) {
})
}
func Test_BindMiddleware_Must_Be_Called(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.Group("/", func(g *ghttp.RouterGroup) {
g.Middleware(func(r *ghttp.Request) {
r.Response.Write("1")
r.Middleware.Next()
})
g.Middleware(func(r *ghttp.Request) {
r.Middleware.Next()
r.Response.Write("2")
})
g.ALL("/test", func(r *ghttp.Request) {
r.Response.Write("test")
})
})
s.SetPort(p)
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * 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"), "1test2")
gtest.Assert(client.PutContent("/test/none"), "12")
})
}
func Test_Middleware_With_Static(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
s.Group("/", func(g *ghttp.RouterGroup) {
g.Middleware(func(r *ghttp.Request) {
r.Response.Write("1")
r.Middleware.Next()
r.Response.Write("2")
})
g.ALL("/user/list", func(r *ghttp.Request) {
r.Response.Write("list")
})
})
s.SetPort(p)
s.SetDumpRouteMap(false)
s.SetServerRoot(gfile.Join(gdebug.CallerDirectory(), "testdata", "static1"))
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
gtest.Assert(client.GetContent("/"), "index")
gtest.Assert(client.GetContent("/test.html"), "test")
gtest.Assert(client.GetContent("/none"), "12")
gtest.Assert(client.GetContent("/user/list"), "1list2")
})
}
func Test_Middleware_Hook_With_Static(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
a := garray.New(true)
s.Group("/", func(g *ghttp.RouterGroup) {
g.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
a.Append(1)
r.Response.Write("a")
})
g.Hook("/*", ghttp.HOOK_AFTER_SERVE, func(r *ghttp.Request) {
a.Append(1)
r.Response.Write("b")
})
g.Middleware(func(r *ghttp.Request) {
r.Response.Write("1")
r.Middleware.Next()
r.Response.Write("2")
})
g.ALL("/user/list", func(r *ghttp.Request) {
r.Response.Write("list")
})
})
s.SetPort(p)
s.SetDumpRouteMap(false)
s.SetServerRoot(gfile.Join(gdebug.CallerDirectory(), "testdata", "static1"))
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
gtest.Assert(client.GetContent("/"), "index")
gtest.Assert(a.Len(), 2)
gtest.Assert(client.GetContent("/test.html"), "test")
gtest.Assert(a.Len(), 4)
gtest.Assert(client.GetContent("/none"), "a12b")
gtest.Assert(a.Len(), 6)
gtest.Assert(client.GetContent("/user/list"), "a1list2b")
gtest.Assert(a.Len(), 8)
})
}
func Test_BindMiddleware_Status(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -117,8 +223,7 @@ func Test_BindMiddleware_Status(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -151,13 +256,12 @@ func Test_BindMiddlewareDefault_Basic1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/"), "1342")
gtest.Assert(client.GetContent("/test/test"), "13test42")
})
}
@ -183,15 +287,14 @@ func Test_BindMiddlewareDefault_Basic2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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.PutContent("/"), "Not Found")
gtest.Assert(client.GetContent("/test/test"), "Not Found")
gtest.Assert(client.GetContent("/"), "1342")
gtest.Assert(client.PutContent("/"), "1342")
gtest.Assert(client.GetContent("/test/test"), "1342")
gtest.Assert(client.PutContent("/test/test"), "13test42")
})
}
@ -215,13 +318,12 @@ func Test_BindMiddlewareDefault_Basic3(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/"), "12")
gtest.Assert(client.GetContent("/test/test"), "1test2")
})
}
@ -245,13 +347,12 @@ func Test_BindMiddlewareDefault_Basic4(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/"), "21")
gtest.Assert(client.GetContent("/test/test"), "2test1")
})
}
@ -275,13 +376,12 @@ func Test_BindMiddlewareDefault_Basic5(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/"), "12")
gtest.Assert(client.GetContent("/test/test"), "12test")
})
}
@ -300,8 +400,7 @@ func Test_BindMiddlewareDefault_Status(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -352,18 +451,17 @@ func Test_BindMiddlewareDefault_Basic6(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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"), "Not Found")
gtest.Assert(client.GetContent("/shut"), "Not Found")
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"), "Not Found")
gtest.Assert(client.GetContent("/none-exist"), "1342")
})
}
@ -400,13 +498,12 @@ func Test_Hook_Middleware_Basic1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
gtest.Assert(client.GetContent("/"), "acbd")
gtest.Assert(client.GetContent("/"), "ac1342bd")
gtest.Assert(client.GetContent("/test/test"), "ac13test42bd")
})
}
@ -438,13 +535,13 @@ func Test_Middleware_CORSAndAuth(t *testing.T) {
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/api.v2"), "Not Found")
gtest.Assert(client.GetContent("/api.v2"), "Forbidden")
gtest.Assert(client.GetContent("/api.v2/user/list"), "Forbidden")
gtest.Assert(client.GetContent("/api.v2/user/list", "token=123456"), "list")
})

View File

@ -94,8 +94,7 @@ func Test_Params_Json(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -56,8 +56,7 @@ func Test_Params_Struct(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -222,8 +222,7 @@ func Test_Params_Basic(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -40,8 +40,7 @@ func Test_Router_Basic(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -67,8 +66,7 @@ func Test_Router_Method(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -116,8 +114,7 @@ func Test_Router_Status(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -164,8 +161,7 @@ func Test_Router_CustomStatusHandler(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -191,8 +187,7 @@ func Test_Router_404(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -69,8 +69,7 @@ func Test_Router_ControllerRest(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -53,8 +53,7 @@ func Test_Router_Controller1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -84,8 +83,7 @@ func Test_Router_Controller2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -111,8 +109,7 @@ func Test_Router_ControllerMethod(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -42,8 +42,7 @@ func Test_Router_DomainBasic(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -86,8 +85,7 @@ func Test_Router_DomainMethod(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -186,8 +184,7 @@ func Test_Router_DomainStatus(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -278,8 +275,7 @@ func Test_Router_DomainCustomStatusHandler(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -316,8 +312,7 @@ func Test_Router_Domain404(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -69,8 +69,7 @@ func Test_Router_DomainControllerRest(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -51,8 +51,7 @@ func Test_Router_DomainController1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -102,8 +101,7 @@ func Test_Router_DomainController2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -156,8 +154,7 @@ func Test_Router_DomainControllerMethod(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -64,8 +64,7 @@ func Test_Router_DomainObjectRest(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -47,8 +47,7 @@ func Test_Router_DomainObject1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -97,8 +96,7 @@ func Test_Router_DomainObject2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -149,8 +147,7 @@ func Test_Router_DomainObjectMethod(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -35,8 +35,7 @@ func Test_Router_Exit(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -74,8 +73,7 @@ func Test_Router_ExitHook(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -113,8 +111,7 @@ func Test_Router_ExitAll(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -61,23 +61,23 @@ func Test_Router_Group_Group(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * 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("/api.v2"), "Not Found")
gtest.Assert(client.GetContent("/api.v2"), "12")
gtest.Assert(client.GetContent("/api.v2/test"), "1test2")
gtest.Assert(client.GetContent("/api.v2/hook"), "hook any")
gtest.Assert(client.GetContent("/api.v2/hook/name"), "hook namehook any")
gtest.Assert(client.GetContent("/api.v2/hook/name/any"), "hook any")
gtest.Assert(client.GetContent("/api.v2/hook"), "hook any12")
gtest.Assert(client.GetContent("/api.v2/hook/name"), "hook namehook any12")
gtest.Assert(client.GetContent("/api.v2/hook/name/any"), "hook any12")
gtest.Assert(client.GetContent("/api.v2/order/list"), "1list2")
gtest.Assert(client.GetContent("/api.v2/order/update"), "Not Found")
gtest.Assert(client.GetContent("/api.v2/order/update"), "12")
gtest.Assert(client.PutContent("/api.v2/order/update"), "1update2")
gtest.Assert(client.GetContent("/api.v2/user/drop"), "Not Found")
gtest.Assert(client.GetContent("/api.v2/user/drop"), "12")
gtest.Assert(client.DeleteContent("/api.v2/user/drop"), "1drop2")
gtest.Assert(client.GetContent("/api.v2/user/edit"), "Not Found")
gtest.Assert(client.GetContent("/api.v2/user/edit"), "12")
gtest.Assert(client.PostContent("/api.v2/user/edit"), "1edit2")
gtest.Assert(client.GetContent("/api.v2/user/info"), "1info2")
})

View File

@ -35,7 +35,7 @@ func Test_Router_Group_Hook1(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -64,7 +64,7 @@ func Test_Router_Group_Hook2(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -95,7 +95,7 @@ func Test_Router_Group_Hook3(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -112,7 +112,7 @@ func Test_Router_GroupRest(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -90,7 +90,7 @@ func Test_Router_GroupBasic1(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -138,7 +138,7 @@ func Test_Router_Basic2(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -174,7 +174,7 @@ func Test_Router_GroupBuildInVar(t *testing.T) {
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -33,8 +33,7 @@ func Test_Router_Hook_Basic(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -44,6 +43,50 @@ func Test_Router_Hook_Basic(t *testing.T) {
})
}
func Test_Router_Hook_Fuzzy_Router(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
i := 1000
pattern1 := "/:name/info"
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
r.SetParam("uid", i)
i++
},
})
s.BindHandler(pattern1, func(r *ghttp.Request) {
r.Response.Write(r.Get("uid"))
})
pattern2 := "/{object}/list/{page}.java"
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) {
r.Response.SetBuffer([]byte(
fmt.Sprint(r.Get("object"), "&", r.Get("page"), "&", i),
))
},
})
s.BindHandler(pattern2, func(r *ghttp.Request) {
r.Response.Write(r.Router.Uri)
})
s.SetPort(p)
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
gtest.Assert(client.GetContent("/john"), "Not Found")
gtest.Assert(client.GetContent("/john/info"), "1000")
gtest.Assert(client.GetContent("/john/info"), "1001")
gtest.Assert(client.GetContent("/john/list/1.java"), "john&1&1002")
gtest.Assert(client.GetContent("/john/list/2.java"), "john&2&1002")
})
}
func Test_Router_Hook_Priority(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -71,8 +114,7 @@ func Test_Router_Hook_Priority(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -106,8 +148,7 @@ func Test_Router_Hook_Multi(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -32,8 +32,7 @@ func Test_NameToUri_FullName(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
@ -54,8 +53,7 @@ func Test_NameToUri_AllLower(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
@ -76,8 +74,7 @@ func Test_NameToUri_Camel(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
@ -98,8 +95,7 @@ func Test_NameToUri_Default(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)

View File

@ -64,8 +64,7 @@ func Test_Router_ObjectRest(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -48,8 +48,7 @@ func Test_Router_Object1(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -79,8 +78,7 @@ func Test_Router_Object2(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -106,8 +104,7 @@ func Test_Router_ObjectMethod(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -36,8 +36,7 @@ func Test_Session_Cookie(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetBrowserMode(true)
@ -84,8 +83,7 @@ func Test_Session_Header(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -128,7 +126,7 @@ func Test_Session_StorageFile(t *testing.T) {
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -152,7 +150,7 @@ func Test_Session_StorageFile(t *testing.T) {
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -32,7 +32,7 @@ func Test_Static_ServerRoot(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -51,7 +51,7 @@ func Test_Static_ServerRoot(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -71,7 +71,7 @@ func Test_Static_Folder_Forbidden(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -93,7 +93,7 @@ func Test_Static_IndexFolder(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -116,7 +116,7 @@ func Test_Static_IndexFiles1(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -138,7 +138,7 @@ func Test_Static_IndexFiles2(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -162,7 +162,7 @@ func Test_Static_AddSearchPath1(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -186,7 +186,7 @@ func Test_Static_AddSearchPath2(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -210,7 +210,7 @@ func Test_Static_AddStaticPath(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -235,7 +235,7 @@ func Test_Static_AddStaticPath_Priority(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
@ -262,7 +262,7 @@ func Test_Static_Rewrite(t *testing.T) {
s.SetPort(p)
s.Start()
defer s.Shutdown()
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
client := ghttp.NewClient()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

View File

@ -41,8 +41,7 @@ func Test_WebSocket(t *testing.T) {
s.Start()
defer s.Shutdown()
// 等待启动完成
time.Sleep(200 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
gtest.Case(t, func() {
conn, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://127.0.0.1:%d/ws", p), nil)
gtest.Assert(err, nil)

1
net/ghttp/testdata/static1/index.html vendored Normal file
View File

@ -0,0 +1 @@
index

1
net/ghttp/testdata/static1/test.html vendored Normal file
View File

@ -0,0 +1 @@
test