Skip to content

Commit

Permalink
fix(test): add unit tests and fix some minor bugs. Fixes gogf#3539
Browse files Browse the repository at this point in the history
Signed-off-by: oninowang <oninowang@tencent.com>
  • Loading branch information
oninowang committed Apr 29, 2024
1 parent 6df5f7b commit 1551b78
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 11 deletions.
13 changes: 5 additions & 8 deletions contrib/sdk/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/gogf/gf/v2/encoding/gurl"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/text/gregex"
Expand All @@ -24,24 +23,21 @@ import (
"github.com/gogf/gf/v2/util/gtag"
)

// Client is an http client for SDK.
// Client is a http client for SDK.
type Client struct {
*gclient.Client
Handler
}

// New creates and returns an http client for SDK.
// New creates and returns a http client for SDK.
func New(config Config) *Client {
client := config.Client
if client == nil {
client = gclient.New()
}
if config.Logger == nil {
config.Logger = g.Log()
}
handler := config.Handler
if handler == nil {
handler = NewDefaultHandler(config)
handler = NewDefaultHandler(config.Logger, config.RawDump)
}
if !gstr.HasPrefix(config.URL, "http") {
config.URL = fmt.Sprintf("http://%s", config.URL)
Expand Down Expand Up @@ -73,7 +69,8 @@ func (c *Client) Request(ctx context.Context, req, res interface{}) error {

// Get sends a request using GET method.
func (c *Client) Get(ctx context.Context, path string, in, out interface{}) error {
if urlParams := ghttp.BuildParams(in); urlParams != "" {
// TODO: Path params will also be built in urlParams, not graceful now.
if urlParams := ghttp.BuildParams(in); urlParams != "" && urlParams != "{}" {
path += "?" + urlParams
}
res, err := c.ContentJson().Get(ctx, c.handlePath(path, in))
Expand Down
10 changes: 7 additions & 3 deletions contrib/sdk/httpclient/httpclient_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/glog"
Expand All @@ -30,10 +31,13 @@ type DefaultHandler struct {
RawDump bool
}

func NewDefaultHandler(config Config) *DefaultHandler {
func NewDefaultHandler(logger *glog.Logger, rawRump bool) *DefaultHandler {
if rawRump && logger == nil {
logger = g.Log()
}
return &DefaultHandler{
Logger: config.Logger,
RawDump: config.RawDump,
Logger: logger,
RawDump: rawRump,
}
}

Expand Down
109 changes: 109 additions & 0 deletions contrib/sdk/httpclient/httpclient_z_unit_feature_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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 httpclient_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/gogf/gf/contrib/sdk/httpclient/v2"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)

func Test_HttpClient_With_Default_Handler(t *testing.T) {
type Req struct {
g.Meta `path:"/get" method:"get"`
}
type Res struct {
Uid int
Name string
}

s := g.Server(guid.S())
s.BindHandler("/get", func(r *ghttp.Request) {
res := ghttp.DefaultHandlerResponse{
Data: Res{
Uid: 1,
Name: "test",
},
}
r.Response.WriteJson(res)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
client := httpclient.New(httpclient.Config{
URL: fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()),
})
var (
req = &Req{}
res = &Res{}
)
err := client.Request(gctx.New(), req, res)
t.AssertNil(err)
t.AssertEQ(res.Uid, 1)
t.AssertEQ(res.Name, "test")
})
}

type CustomHandler struct{}

func (c CustomHandler) HandleResponse(ctx context.Context, res *gclient.Response, out interface{}) error {
defer res.Close()
if pointer, ok := out.(*string); ok {
*pointer = res.ReadAllString()
} else {
return gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", out)
}
return nil
}

func Test_HttpClient_With_Custom_Handler(t *testing.T) {
type Req struct {
g.Meta `path:"/get" method:"get"`
}

s := g.Server(guid.S())
s.BindHandler("/get", func(r *ghttp.Request) {
r.Response.WriteExit("It is a test.")
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

client := httpclient.New(httpclient.Config{
URL: fmt.Sprintf("127.0.0.1:%d", s.GetListenedPort()),
Handler: CustomHandler{},
})
req := &Req{}
gtest.C(t, func(t *gtest.T) {
var res = new(string)
err := client.Request(gctx.New(), req, res)
t.AssertNil(err)
t.AssertEQ(*res, "It is a test.")
})
gtest.C(t, func(t *gtest.T) {
var res string
err := client.Request(gctx.New(), req, res)
t.AssertEQ(err, gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", res))
})
}

0 comments on commit 1551b78

Please sign in to comment.