|
| 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