gf/net/gclient/gclient_z_unit_request_obj_test.go
John Guo b1611fee1b
improve port listening for ghttp.Server (#2175)
* version updates

* fix issue #2172

* improve port listening for ghttp.Server

* UT cases update

* UT cases update

* add GetListenedPort/GetListenedAddress for gtcp.Server

* UT cases update for package gudp

* up
2022-10-08 21:45:21 +08:00

80 lines
1.9 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 gclient_test
import (
"fmt"
"testing"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)
func Test_Client_DoRequestObj(t *testing.T) {
type UserCreateReq struct {
g.Meta `path:"/user" method:"post"`
Id int
Name string
}
type UserCreateRes struct {
Id int
}
type UserQueryReq struct {
g.Meta `path:"/user/{id}" method:"get"`
Id int
}
type UserQueryRes struct {
Id int
Name string
}
s := g.Server(guid.S())
s.Group("/user", func(group *ghttp.RouterGroup) {
group.GET("/{id}", func(r *ghttp.Request) {
r.Response.WriteJson(g.Map{"id": r.Get("id").Int(), "name": "john"})
})
group.POST("/", func(r *ghttp.Request) {
r.Response.WriteJson(g.Map{"id": r.Get("Id")})
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
client := g.Client().SetPrefix(url).ContentJson()
var (
createRes *UserCreateRes
createReq = UserCreateReq{
Id: 1,
Name: "john",
}
)
err := client.DoRequestObj(ctx, createReq, &createRes)
t.AssertNil(err)
t.Assert(createRes.Id, 1)
})
gtest.C(t, func(t *gtest.T) {
url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
client := g.Client().SetPrefix(url).ContentJson()
var (
queryRes *UserQueryRes
queryReq = UserQueryReq{
Id: 1,
}
)
err := client.DoRequestObj(ctx, queryReq, &queryRes)
t.AssertNil(err)
t.Assert(queryRes.Id, 1)
t.Assert(queryRes.Name, "john")
})
}