mirror of
https://gitee.com/johng/gf
synced 2026-07-03 11:51:04 +08:00
add middleware feature for ghttp.Server
This commit is contained in:
@ -1,147 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
type XinYanModel struct {
|
||||
Success bool `json:"success"`
|
||||
Data Data `json:"data"`
|
||||
ErrorCode interface{} `json:"errorCode"`
|
||||
ErrorMsg interface{} `json:"errorMsg"`
|
||||
}
|
||||
type ApplyReportDetail struct {
|
||||
ApplyScore string `json:"apply_score"`
|
||||
ApplyCredibility string `json:"apply_credibility"`
|
||||
QueryOrgCount string `json:"apply_query_org_count"`
|
||||
QueryFinanceCount string `json:"apply_query_finance_count"`
|
||||
QueryCashCount string `json:"apply_query_cash_count"`
|
||||
QuerySumCount string `json:"apply_query_sum_count"`
|
||||
LatestQueryTime string `json:"apply_latest_query_time"`
|
||||
LatestOneMonth string `json:"apply_latest_one_month"`
|
||||
LatestThreeMonth string `json:"apply_latest_three_month"`
|
||||
LatestSixMonth string `json:"apply_latest_six_month"`
|
||||
}
|
||||
type BehaviorReportDetail struct {
|
||||
LoansScore string `json:"behavior_report_detailloans_score"`
|
||||
LoansCredibility string `json:"behavior_report_detailloans_credibility"`
|
||||
LoansCount string `json:"behavior_report_detailloans_count"`
|
||||
LoansSettleCount string `json:"behavior_report_detailloans_settle_count"`
|
||||
LoansOverdueCount string `json:"behavior_report_detailloans_overdue_count"`
|
||||
LoansOrgCount string `json:"behavior_report_detailloans_org_count"`
|
||||
ConsfinOrgCount string `json:"behavior_report_detailconsfin_org_count"`
|
||||
LoansCashCount string `json:"behavior_report_detailloans_cash_count"`
|
||||
LatestOneMonth string `json:"behavior_report_detaillatest_one_month"`
|
||||
LatestThreeMonth string `json:"behavior_report_detaillatest_three_month"`
|
||||
LatestSixMonth string `json:"behavior_report_detaillatest_six_month"`
|
||||
HistorySucFee string `json:"behavior_report_detailhistory_suc_fee"`
|
||||
HistoryFailFee string `json:"behavior_report_detailhistory_fail_fee"`
|
||||
LatestOneMonthSuc string `json:"behavior_report_detaillatest_one_month_suc"`
|
||||
LatestOneMonthFail string `json:"behavior_report_detaillatest_one_month_fail"`
|
||||
LoansLongTime string `json:"behavior_report_detailloans_long_time"`
|
||||
LoansLatestTime string `json:"behavior_report_detailloans_latest_time"`
|
||||
}
|
||||
type CurrentReportDetail struct {
|
||||
LoansCreditLimit string `json:"current_report_detailloans_credit_limit"`
|
||||
LoansCredibility string `json:"current_report_detailloans_credibility"`
|
||||
LoansOrgCount string `json:"current_report_detailloans_org_count"`
|
||||
LoansProductCount string `json:"current_report_detailloans_product_count"`
|
||||
LoansMaxLimit string `json:"current_report_detailloans_max_limit"`
|
||||
LoansAvgLimit string `json:"current_report_detailloans_avg_limit"`
|
||||
ConsfinCreditLimit string `json:"current_report_detailconsfin_credit_limit"`
|
||||
ConsfinCredibility string `json:"current_report_detailconsfin_credibility"`
|
||||
ConsfinOrgCount string `json:"current_report_detailconsfin_org_count"`
|
||||
ConsfinProductCount string `json:"current_report_detailconsfin_product_count"`
|
||||
ConsfinMaxLimit string `json:"current_report_detailconsfin_max_limit"`
|
||||
ConsfinAvgLimit string `json:"current_report_detailconsfin_avg_limit"`
|
||||
}
|
||||
type ResultDetail struct {
|
||||
ApplyReportDetail ApplyReportDetail `json:"apply_report_detail"`
|
||||
BehaviorReportDetail BehaviorReportDetail `json:"behavior_report_detail"`
|
||||
CurrentReportDetail CurrentReportDetail `json:"current_report_detail"`
|
||||
}
|
||||
type Data struct {
|
||||
Code string `json:"code"`
|
||||
Desc string `json:"desc1"`
|
||||
TransID string `json:"trans_id"`
|
||||
TradeNo string `json:"trade_no"`
|
||||
Fee string `json:"fee"`
|
||||
IDNo string `json:"id_no"`
|
||||
IDName string `json:"id_name"`
|
||||
Versions string `json:"versions"`
|
||||
ResultDetail ResultDetail `json:"result_detail"`
|
||||
}
|
||||
|
||||
var data = `{
|
||||
"success": true,
|
||||
"data": {
|
||||
"code": "0",
|
||||
"desc": "查询成功",
|
||||
"trans_id": "14910304379231213",
|
||||
"trade_no": "201704011507240100057329",
|
||||
"fee": "Y",
|
||||
"id_no": "0783231bcc39f4957e99907e02ae401c",
|
||||
"id_name": "dd67a5943781369ddd7c594e231e9e70 ",
|
||||
"versions": "1.0.0",
|
||||
"result_detail":{
|
||||
"apply_report_detail": {
|
||||
"apply_score": "189",
|
||||
"apply_credibility": "84",
|
||||
"query_org_count": "7",
|
||||
"query_finance_count": "2",
|
||||
"query_cash_count": "2",
|
||||
"query_sum_count": "13",
|
||||
"latest_query_time": "2017-09-03",
|
||||
"latest_one_month": "1",
|
||||
"latest_three_month": "5",
|
||||
"latest_six_month": "12"
|
||||
},
|
||||
"behavior_report_detail": {
|
||||
"loans_score": "199",
|
||||
"loans_credibility": "90",
|
||||
"loans_count": "300",
|
||||
"loans_settle_count": "280",
|
||||
"loans_overdue_count": "20",
|
||||
"loans_org_count": "5",
|
||||
"consfin_org_count": "3",
|
||||
"loans_cash_count": "2",
|
||||
"latest_one_month": "3",
|
||||
"latest_three_month": "20",
|
||||
"latest_six_month": "23",
|
||||
"history_suc_fee": "30",
|
||||
"history_fail_fee": "25",
|
||||
"latest_one_month_suc": "5",
|
||||
"latest_one_month_fail": "20",
|
||||
"loans_long_time": "130",
|
||||
"loans_latest_time": "2017-09-16"
|
||||
},
|
||||
"current_report_detail": {
|
||||
"loans_credit_limit": "1400",
|
||||
"loans_credibility": "80",
|
||||
"loans_org_count": "7",
|
||||
"loans_product_count": "8",
|
||||
"loans_max_limit": "2000",
|
||||
"loans_avg_limit": "1000",
|
||||
"consfin_credit_limit": "1500",
|
||||
"consfin_credibility": "90",
|
||||
"consfin_org_count": "8",
|
||||
"consfin_product_count": "5",
|
||||
"consfin_max_limit": "5000",
|
||||
"consfin_avg_limit": "3000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"errorCode": null,
|
||||
"errorMsg": null
|
||||
}`
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
xinyan := new(XinYanModel)
|
||||
glog.Debug(xinyan)
|
||||
j, _ := gjson.DecodeToJson(data)
|
||||
err := j.ToStruct(xinyan)
|
||||
glog.Debug(err)
|
||||
|
||||
glog.Debug(xinyan)
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Println(i)
|
||||
switch i {
|
||||
case 5:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,8 +163,8 @@ func (r *Request) GetMap(def ...map[string]string) map[string]string {
|
||||
|
||||
// 将所有的request参数映射到struct属性上,参数pointer应当为一个struct对象的指针,
|
||||
// mapping为非必需参数,自定义参数与属性的映射关系
|
||||
func (r *Request) GetToStruct(pointer interface{}, mapping ...map[string]string) {
|
||||
r.GetRequestToStruct(pointer, mapping...)
|
||||
func (r *Request) GetToStruct(pointer interface{}, mapping ...map[string]string) error {
|
||||
return r.GetRequestToStruct(pointer, mapping...)
|
||||
}
|
||||
|
||||
// 仅退出当前逻辑执行函数, 如:服务函数、HOOK函数
|
||||
|
||||
@ -100,12 +100,12 @@ type (
|
||||
)
|
||||
|
||||
const (
|
||||
SERVER_STATUS_STOPPED = 0 // Server状态:停止
|
||||
SERVER_STATUS_RUNNING = 1 // Server状态:运行
|
||||
HOOK_BEFORE_SERVE = "BeforeServe" // 回调事件,在执行服务前
|
||||
HOOK_AFTER_SERVE = "AfterServe" // 回调事件,在执行服务后
|
||||
HOOK_BEFORE_OUTPUT = "BeforeOutput" // 回调事件,在输出结果前
|
||||
HOOK_AFTER_OUTPUT = "AfterOutput" // 回调事件,在输出结果后
|
||||
SERVER_STATUS_STOPPED = 0 // Server状态:停止
|
||||
SERVER_STATUS_RUNNING = 1 // Server状态:运行
|
||||
HOOK_BEFORE_SERVE = "HOOK_BEFORE_SERVE" // 回调事件,在执行服务前
|
||||
HOOK_AFTER_SERVE = "HOOK_AFTER_SERVE" // 回调事件,在执行服务后
|
||||
HOOK_BEFORE_OUTPUT = "HOOK_BEFORE_OUTPUT" // 回调事件,在输出结果前
|
||||
HOOK_AFTER_OUTPUT = "HOOK_AFTER_OUTPUT" // 回调事件,在输出结果后
|
||||
HTTP_METHODS = "GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE"
|
||||
gDEFAULT_SERVER = "default"
|
||||
gDEFAULT_DOMAIN = "default"
|
||||
@ -281,35 +281,48 @@ func (s *Server) DumpRoutesMap() {
|
||||
// 获得路由表(格式化字符串)
|
||||
func (s *Server) GetRouteMap() string {
|
||||
type tableItem struct {
|
||||
hook string
|
||||
domain string
|
||||
method string
|
||||
route string
|
||||
handler string
|
||||
priority int
|
||||
middleware string
|
||||
domain string
|
||||
method string
|
||||
route string
|
||||
handler *handlerItem
|
||||
priority int
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
table := tablewriter.NewWriter(buf)
|
||||
table.SetHeader([]string{"SERVER", "ADDRESS", "DOMAIN", "METHOD", "P", "ROUTE", "HANDLER", "HOOK"})
|
||||
table.SetHeader([]string{"SERVER", "ADDRESS", "DOMAIN", "METHOD", "P", "ROUTE", "HANDLER", "MIDDLEWARE"})
|
||||
table.SetRowLine(true)
|
||||
table.SetBorder(false)
|
||||
table.SetCenterSeparator("|")
|
||||
table.SetColumnAlignment([]int{
|
||||
tablewriter.ALIGN_CENTER,
|
||||
tablewriter.ALIGN_CENTER,
|
||||
tablewriter.ALIGN_CENTER,
|
||||
tablewriter.ALIGN_CENTER,
|
||||
tablewriter.ALIGN_CENTER,
|
||||
tablewriter.ALIGN_LEFT,
|
||||
tablewriter.ALIGN_LEFT,
|
||||
tablewriter.ALIGN_CENTER,
|
||||
})
|
||||
|
||||
m := make(map[string]*garray.SortedArray)
|
||||
for k, registeredItems := range s.routesMap {
|
||||
array, _ := gregex.MatchString(`(.*?)%([A-Z]+):(.+)@(.+)`, k)
|
||||
for index, registeredItem := range registeredItems {
|
||||
item := &tableItem{
|
||||
hook: array[1],
|
||||
domain: array[4],
|
||||
method: array[2],
|
||||
route: array[3],
|
||||
handler: registeredItem.handler.itemName,
|
||||
priority: len(registeredItems) - index - 1,
|
||||
middleware: array[1],
|
||||
domain: array[4],
|
||||
method: array[2],
|
||||
route: array[3],
|
||||
handler: registeredItem.handler,
|
||||
priority: len(registeredItems) - index - 1,
|
||||
}
|
||||
if item.handler.itemType == gHANDLER_TYPE_MIDDLEWARE {
|
||||
item.middleware = "YES"
|
||||
}
|
||||
if _, ok := m[item.domain]; !ok {
|
||||
// 注意排序函数的逻辑
|
||||
// 注意排序函数的逻辑,从小到达排序
|
||||
m[item.domain] = garray.NewSortedArraySize(100, func(v1, v2 interface{}) int {
|
||||
item1 := v1.(*tableItem)
|
||||
item2 := v2.(*tableItem)
|
||||
@ -317,7 +330,11 @@ func (s *Server) GetRouteMap() string {
|
||||
if r = strings.Compare(item1.domain, item2.domain); r == 0 {
|
||||
if r = strings.Compare(item1.route, item2.route); r == 0 {
|
||||
if r = strings.Compare(item1.method, item2.method); r == 0 {
|
||||
if r = strings.Compare(item1.hook, item2.hook); r == 0 {
|
||||
if item1.handler.itemType == gHANDLER_TYPE_MIDDLEWARE && item2.handler.itemType != gHANDLER_TYPE_MIDDLEWARE {
|
||||
return -1
|
||||
} else if item1.handler.itemType == gHANDLER_TYPE_MIDDLEWARE && item2.handler.itemType == gHANDLER_TYPE_MIDDLEWARE {
|
||||
return 1
|
||||
} else if r = strings.Compare(item1.middleware, item2.middleware); r == 0 {
|
||||
r = item2.priority - item1.priority
|
||||
}
|
||||
}
|
||||
@ -346,8 +363,8 @@ func (s *Server) GetRouteMap() string {
|
||||
data[3] = item.method
|
||||
data[4] = gconv.String(len(strings.Split(item.route, "/")) - 1 + item.priority)
|
||||
data[5] = item.route
|
||||
data[6] = item.handler
|
||||
data[7] = item.hook
|
||||
data[6] = item.handler.itemName
|
||||
data[7] = item.middleware
|
||||
table.Append(data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,9 +11,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/internal/debug"
|
||||
|
||||
"github.com/gogf/gf/container/glist"
|
||||
"github.com/gogf/gf/internal/debug"
|
||||
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
@ -132,6 +131,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 上面循环后得到的lists是该路由规则一路匹配下来相关的模糊匹配链表(注意不是这棵树所有的链表)。
|
||||
// 下面从头开始遍历每个节点的模糊匹配链表,将该路由项插入进去(按照优先级高的放在lists链表的前面)
|
||||
item := (*handlerItem)(nil)
|
||||
@ -147,7 +147,7 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
strings.EqualFold(handler.router.Uri, item.router.Uri) {
|
||||
e.Value = handler
|
||||
pushed = true
|
||||
break
|
||||
goto ForBreak
|
||||
}
|
||||
fallthrough
|
||||
|
||||
@ -156,10 +156,11 @@ func (s *Server) setHandler(pattern string, handler *handlerItem) {
|
||||
if s.compareRouterPriority(handler, item) {
|
||||
l.InsertBefore(handler, e)
|
||||
pushed = true
|
||||
break
|
||||
}
|
||||
goto ForBreak
|
||||
}
|
||||
}
|
||||
ForBreak:
|
||||
if !pushed {
|
||||
l.PushBack(handler)
|
||||
}
|
||||
@ -277,19 +278,15 @@ func (s *Server) patternToRegRule(rule string) (regrule string, names []string)
|
||||
if len(v) > 1 {
|
||||
regrule += `/([^/]+)`
|
||||
names = append(names, v[1:])
|
||||
break
|
||||
} else {
|
||||
regrule += `/[^/]+`
|
||||
break
|
||||
}
|
||||
case '*':
|
||||
if len(v) > 1 {
|
||||
regrule += `/{0,1}(.*)`
|
||||
names = append(names, v[1:])
|
||||
break
|
||||
} else {
|
||||
regrule += `/{0,1}.*`
|
||||
break
|
||||
}
|
||||
default:
|
||||
// 特殊字符替换
|
||||
|
||||
@ -163,7 +163,8 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
if item.hookName != "" {
|
||||
switch item.itemType {
|
||||
case gHANDLER_TYPE_HOOK:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (%s)`,
|
||||
@ -173,15 +174,30 @@ func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
||||
item.hookName,
|
||||
),
|
||||
)
|
||||
case gHANDLER_TYPE_MIDDLEWARE:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s (MIDDLEWARE)`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
),
|
||||
)
|
||||
default:
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
),
|
||||
)
|
||||
}
|
||||
return json.Marshal(
|
||||
fmt.Sprintf(
|
||||
`%s %s:%s`,
|
||||
item.router.Uri,
|
||||
item.router.Domain,
|
||||
item.router.Method,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (item *handlerParsedItem) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(item.handler)
|
||||
}
|
||||
|
||||
// 生成回调方法查询的Key
|
||||
|
||||
@ -36,7 +36,7 @@ func Test_Cookie(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
|
||||
359
net/ghttp/ghttp_unit_middleware_test.go
Normal file
359
net/ghttp/ghttp_unit_middleware_test.go
Normal file
@ -0,0 +1,359 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func Test_BindMiddleWare_Basic1(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.BindMiddleWare("/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
}, func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("4")
|
||||
})
|
||||
s.BindMiddleWare("/test/:name", func(r *ghttp.Request) {
|
||||
r.Response.Write("5")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("6")
|
||||
}, func(r *ghttp.Request) {
|
||||
r.Response.Write("7")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_BindMiddleWare_Basic2(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.BindMiddleWare("PUT:/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
}, func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("4")
|
||||
})
|
||||
s.BindMiddleWare("POST:/test/:name", func(r *ghttp.Request) {
|
||||
r.Response.Write("5")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("6")
|
||||
}, func(r *ghttp.Request) {
|
||||
r.Response.Write("7")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic1(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic2(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("PUT:/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic3(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic4(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("1")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.MiddleWare.Next()
|
||||
})
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic5(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("2")
|
||||
r.MiddleWare.Next()
|
||||
})
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
type ObjectMiddleWare struct{}
|
||||
|
||||
func (o *ObjectMiddleWare) Init(r *ghttp.Request) {
|
||||
r.Response.Write("100")
|
||||
}
|
||||
|
||||
func (o *ObjectMiddleWare) Shut(r *ghttp.Request) {
|
||||
r.Response.Write("200")
|
||||
}
|
||||
|
||||
func (o *ObjectMiddleWare) Index(r *ghttp.Request) {
|
||||
r.Response.Write("Object Index")
|
||||
}
|
||||
|
||||
func (o *ObjectMiddleWare) Show(r *ghttp.Request) {
|
||||
r.Response.Write("Object Show")
|
||||
}
|
||||
|
||||
func (o *ObjectMiddleWare) Info(r *ghttp.Request) {
|
||||
r.Response.Write("Object Info")
|
||||
}
|
||||
|
||||
func Test_AddMiddleWare_Basic6(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindObject("/", new(ObjectMiddleWare))
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Hook_MiddleWare_Basic1(t *testing.T) {
|
||||
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")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("3")
|
||||
r.MiddleWare.Next()
|
||||
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")
|
||||
})
|
||||
}
|
||||
@ -95,7 +95,7 @@ func Test_Params_Json(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -55,7 +55,7 @@ func Test_Params_Struct(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -127,7 +127,7 @@ func Test_Params_Basic(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -41,7 +41,7 @@ func Test_Router_Basic(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -68,7 +68,7 @@ func Test_Router_Method(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -117,7 +117,7 @@ func Test_Router_Status(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -160,7 +160,7 @@ func Test_Router_CustomStatusHandler(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -187,7 +187,7 @@ func Test_Router_404(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -70,7 +70,7 @@ func Test_Router_ControllerRest(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -54,7 +54,7 @@ func Test_Router_Controller1(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -85,7 +85,7 @@ func Test_Router_Controller2(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -112,7 +112,7 @@ func Test_Router_ControllerMethod(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -43,7 +43,7 @@ func Test_Router_DomainBasic(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -87,7 +87,7 @@ func Test_Router_DomainMethod(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -187,7 +187,7 @@ func Test_Router_DomainStatus(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -279,7 +279,7 @@ func Test_Router_DomainCustomStatusHandler(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -317,7 +317,7 @@ func Test_Router_Domain404(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -70,7 +70,7 @@ func Test_Router_DomainControllerRest(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -52,7 +52,7 @@ func Test_Router_DomainController1(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -103,7 +103,7 @@ func Test_Router_DomainController2(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -157,7 +157,7 @@ func Test_Router_DomainControllerMethod(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -65,7 +65,7 @@ func Test_Router_DomainObjectRest(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -48,7 +48,7 @@ func Test_Router_DomainObject1(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -98,7 +98,7 @@ func Test_Router_DomainObject2(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -150,7 +150,7 @@ func Test_Router_DomainObjectMethod(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -38,7 +38,7 @@ func Test_Router_Exit(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -77,7 +77,7 @@ func Test_Router_ExitHook(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -116,7 +116,7 @@ func Test_Router_ExitAll(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -35,7 +35,7 @@ func Test_Router_Group_HOOK1(t *testing.T) {
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * 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(time.Second)
|
||||
time.Sleep(200 * 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(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -112,7 +112,7 @@ func Test_Router_GroupRest(t *testing.T) {
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -90,7 +90,7 @@ func Test_Router_GroupBasic1(t *testing.T) {
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * 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(time.Second)
|
||||
time.Sleep(200 * 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(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -34,7 +34,7 @@ func Test_Router_Hook_Basic(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -72,7 +72,7 @@ func Test_Router_Hook_Priority(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -107,7 +107,7 @@ func Test_Router_Hook_Multi(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -1,44 +0,0 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
func Test_Router_MiddleWare_Basic1(t *testing.T) {
|
||||
p := ports.PopRand()
|
||||
s := g.Server(p)
|
||||
s.BindHandler("/test/test", func(r *ghttp.Request) {
|
||||
r.Response.Write("test")
|
||||
})
|
||||
s.AddMiddleWare(func(r *ghttp.Request) {
|
||||
r.Response.Write("1")
|
||||
r.MiddleWare.Next()
|
||||
r.Response.Write("2")
|
||||
})
|
||||
s.SetPort(p)
|
||||
s.SetDumpRouteMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
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")
|
||||
})
|
||||
}
|
||||
@ -33,7 +33,7 @@ func Test_NameToUri_FullName(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
@ -55,7 +55,7 @@ func Test_NameToUri_AllLower(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
@ -77,7 +77,7 @@ func Test_NameToUri_Camel(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
@ -99,7 +99,7 @@ func Test_NameToUri_Default(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
|
||||
@ -65,7 +65,7 @@ func Test_Router_ObjectRest(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -49,7 +49,7 @@ func Test_Router_Object1(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -80,7 +80,7 @@ func Test_Router_Object2(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
@ -107,7 +107,7 @@ func Test_Router_ObjectMethod(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -38,7 +38,7 @@ func Test_Session(t *testing.T) {
|
||||
defer s.Shutdown()
|
||||
|
||||
// 等待启动完成
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Case(t, func() {
|
||||
client := ghttp.NewClient()
|
||||
client.SetBrowserMode(true)
|
||||
|
||||
@ -30,7 +30,7 @@ func Test_Static_ServerRoot(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -49,7 +49,7 @@ func Test_Static_ServerRoot(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -69,7 +69,7 @@ func Test_Static_Folder_Forbidden(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -91,7 +91,7 @@ func Test_Static_IndexFolder(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -113,7 +113,7 @@ func Test_Static_IndexFiles1(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -135,7 +135,7 @@ func Test_Static_IndexFiles2(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -159,7 +159,7 @@ func Test_Static_AddSearchPath1(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -183,7 +183,7 @@ func Test_Static_AddSearchPath2(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -207,7 +207,7 @@ func Test_Static_AddStaticPath(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -232,7 +232,7 @@ func Test_Static_AddStaticPath_Priority(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
@ -259,7 +259,7 @@ func Test_Static_Rewrite(t *testing.T) {
|
||||
s.SetPort(p)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(time.Second)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
client := ghttp.NewClient()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
|
||||
@ -32,7 +32,7 @@ func Export(i ...interface{}) string {
|
||||
if b, ok := v.([]byte); ok {
|
||||
buffer.Write(b)
|
||||
} else {
|
||||
if m := gconv.Map(v); m != nil {
|
||||
if m := gconv.Map(v); m != nil && len(m) > 0 {
|
||||
v = m
|
||||
}
|
||||
encoder := json.NewEncoder(buffer)
|
||||
|
||||
Reference in New Issue
Block a user