Files
gf/net/ghttp/ghttp_unit_router_handler_extended_test.go
2021-08-04 20:50:45 +08:00

83 lines
2.0 KiB
Go

// Copyright GoFrame Author(https://goframe.org). 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 (
"context"
"fmt"
"github.com/gogf/gf/errors/gerror"
"testing"
"time"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/test/gtest"
)
func Test_Router_Handler_Extended_Handler_Basic(t *testing.T) {
p, _ := ports.PopRand()
s := g.Server(p)
s.BindHandler("/test", func(ctx context.Context) {
r := g.RequestFromCtx(ctx)
r.Response.Write("test")
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
t.Assert(client.GetContent("/test"), "test")
})
}
func Test_Router_Handler_Extended_Handler_WithObject(t *testing.T) {
type TestReq struct {
Age int
Name string
}
type TestRes struct {
Id int
Age int
Name string
}
p, _ := ports.PopRand()
s := g.Server(p)
s.Use(ghttp.MiddlewareHandlerResponse)
s.BindHandler("/test", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
return &TestRes{
Id: 1,
Age: req.Age,
Name: req.Name,
}, nil
})
s.BindHandler("/test/error", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
return &TestRes{
Id: 1,
Age: req.Age,
Name: req.Name,
}, gerror.New("error")
})
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
t.Assert(client.GetContent("/test?age=18&name=john"), `{"code":0,"message":"","data":{"Id":1,"Age":18,"Name":"john"}}`)
t.Assert(client.GetContent("/test/error"), `{"code":50,"message":"error","data":null}`)
})
}