Skip to content

Commit 1551b78

Browse files
author
oninowang
committed
fix(test): add unit tests and fix some minor bugs. Fixes #3539
Signed-off-by: oninowang <[email protected]>
1 parent 6df5f7b commit 1551b78

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

contrib/sdk/httpclient/httpclient.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/gogf/gf/v2/encoding/gurl"
1616
"github.com/gogf/gf/v2/errors/gerror"
17-
"github.com/gogf/gf/v2/frame/g"
1817
"github.com/gogf/gf/v2/net/gclient"
1918
"github.com/gogf/gf/v2/net/ghttp"
2019
"github.com/gogf/gf/v2/text/gregex"
@@ -24,24 +23,21 @@ import (
2423
"github.com/gogf/gf/v2/util/gtag"
2524
)
2625

27-
// Client is an http client for SDK.
26+
// Client is a http client for SDK.
2827
type Client struct {
2928
*gclient.Client
3029
Handler
3130
}
3231

33-
// New creates and returns an http client for SDK.
32+
// New creates and returns a http client for SDK.
3433
func New(config Config) *Client {
3534
client := config.Client
3635
if client == nil {
3736
client = gclient.New()
3837
}
39-
if config.Logger == nil {
40-
config.Logger = g.Log()
41-
}
4238
handler := config.Handler
4339
if handler == nil {
44-
handler = NewDefaultHandler(config)
40+
handler = NewDefaultHandler(config.Logger, config.RawDump)
4541
}
4642
if !gstr.HasPrefix(config.URL, "http") {
4743
config.URL = fmt.Sprintf("http://%s", config.URL)
@@ -73,7 +69,8 @@ func (c *Client) Request(ctx context.Context, req, res interface{}) error {
7369

7470
// Get sends a request using GET method.
7571
func (c *Client) Get(ctx context.Context, path string, in, out interface{}) error {
76-
if urlParams := ghttp.BuildParams(in); urlParams != "" {
72+
// TODO: Path params will also be built in urlParams, not graceful now.
73+
if urlParams := ghttp.BuildParams(in); urlParams != "" && urlParams != "{}" {
7774
path += "?" + urlParams
7875
}
7976
res, err := c.ContentJson().Get(ctx, c.handlePath(path, in))

contrib/sdk/httpclient/httpclient_handler.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gogf/gf/v2/errors/gcode"
1414
"github.com/gogf/gf/v2/errors/gerror"
15+
"github.com/gogf/gf/v2/frame/g"
1516
"github.com/gogf/gf/v2/net/gclient"
1617
"github.com/gogf/gf/v2/net/ghttp"
1718
"github.com/gogf/gf/v2/os/glog"
@@ -30,10 +31,13 @@ type DefaultHandler struct {
3031
RawDump bool
3132
}
3233

33-
func NewDefaultHandler(config Config) *DefaultHandler {
34+
func NewDefaultHandler(logger *glog.Logger, rawRump bool) *DefaultHandler {
35+
if rawRump && logger == nil {
36+
logger = g.Log()
37+
}
3438
return &DefaultHandler{
35-
Logger: config.Logger,
36-
RawDump: config.RawDump,
39+
Logger: logger,
40+
RawDump: rawRump,
3741
}
3842
}
3943

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2+
//
3+
// This Source Code Form is subject to the terms of the MIT License.
4+
// If a copy of the MIT was not distributed with this file,
5+
// You can obtain one at https://github.com/gogf/gf.
6+
7+
package httpclient_test
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"testing"
13+
"time"
14+
15+
"github.com/gogf/gf/contrib/sdk/httpclient/v2"
16+
"github.com/gogf/gf/v2/errors/gcode"
17+
"github.com/gogf/gf/v2/errors/gerror"
18+
"github.com/gogf/gf/v2/frame/g"
19+
"github.com/gogf/gf/v2/net/gclient"
20+
"github.com/gogf/gf/v2/net/ghttp"
21+
"github.com/gogf/gf/v2/os/gctx"
22+
"github.com/gogf/gf/v2/test/gtest"
23+
"github.com/gogf/gf/v2/util/guid"
24+
)
25+
26+
func Test_HttpClient_With_Default_Handler(t *testing.T) {
27+
type Req struct {
28+
g.Meta `path:"/get" method:"get"`
29+
}
30+
type Res struct {
31+
Uid int
32+
Name string
33+
}
34+
35+
s := g.Server(guid.S())
36+
s.BindHandler("/get", func(r *ghttp.Request) {
37+
res := ghttp.DefaultHandlerResponse{
38+
Data: Res{
39+
Uid: 1,
40+
Name: "test",
41+
},
42+
}
43+
r.Response.WriteJson(res)
44+
})
45+
s.SetDumpRouterMap(false)
46+
s.Start()
47+
defer s.Shutdown()
48+
49+
time.Sleep(100 * time.Millisecond)
50+
51+
gtest.C(t, func(t *gtest.T) {
52+
client := httpclient.New(httpclient.Config{
53+
URL: fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()),
54+
})
55+
var (
56+
req = &Req{}
57+
res = &Res{}
58+
)
59+
err := client.Request(gctx.New(), req, res)
60+
t.AssertNil(err)
61+
t.AssertEQ(res.Uid, 1)
62+
t.AssertEQ(res.Name, "test")
63+
})
64+
}
65+
66+
type CustomHandler struct{}
67+
68+
func (c CustomHandler) HandleResponse(ctx context.Context, res *gclient.Response, out interface{}) error {
69+
defer res.Close()
70+
if pointer, ok := out.(*string); ok {
71+
*pointer = res.ReadAllString()
72+
} else {
73+
return gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", out)
74+
}
75+
return nil
76+
}
77+
78+
func Test_HttpClient_With_Custom_Handler(t *testing.T) {
79+
type Req struct {
80+
g.Meta `path:"/get" method:"get"`
81+
}
82+
83+
s := g.Server(guid.S())
84+
s.BindHandler("/get", func(r *ghttp.Request) {
85+
r.Response.WriteExit("It is a test.")
86+
})
87+
s.SetDumpRouterMap(false)
88+
s.Start()
89+
defer s.Shutdown()
90+
91+
time.Sleep(100 * time.Millisecond)
92+
93+
client := httpclient.New(httpclient.Config{
94+
URL: fmt.Sprintf("127.0.0.1:%d", s.GetListenedPort()),
95+
Handler: CustomHandler{},
96+
})
97+
req := &Req{}
98+
gtest.C(t, func(t *gtest.T) {
99+
var res = new(string)
100+
err := client.Request(gctx.New(), req, res)
101+
t.AssertNil(err)
102+
t.AssertEQ(*res, "It is a test.")
103+
})
104+
gtest.C(t, func(t *gtest.T) {
105+
var res string
106+
err := client.Request(gctx.New(), req, res)
107+
t.AssertEQ(err, gerror.NewCodef(gcode.CodeInvalidParameter, "[CustomHandler] expectedType:'*string', but realType:'%T'", res))
108+
})
109+
}

0 commit comments

Comments
 (0)