-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathclient_test.go
129 lines (110 loc) · 3.74 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package httpreq_test
import (
"io"
"net/http"
"strings"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/jsonutil"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/netutil/httpreq"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestClient_Send(t *testing.T) {
resp, err := httpreq.New(testSrvAddr).
ContentType(httpctype.JSON).
DefaultMethod(http.MethodPost).
DefaultHeaderMap(map[string]string{"coustom1": "value1"}).
Send("POST", "/json", func(opt *httpreq.Option) {
opt.Body = io.NopCloser(strings.NewReader(`{"name": "inhere"}`))
})
assert.NoErr(t, err)
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
assert.False(t, httpreq.IsRedirect(sc))
assert.False(t, httpreq.IsForbidden(sc))
assert.False(t, httpreq.IsNotFound(sc))
assert.False(t, httpreq.IsClientError(sc))
assert.False(t, httpreq.IsServerError(sc))
retMp := make(map[string]any)
err = jsonutil.DecodeReader(resp.Body, &retMp)
assert.NoErr(t, err)
dump.P(retMp)
}
func TestClient_RSET(t *testing.T) {
cli := httpreq.New(testSrvAddr).
ContentType(httpctype.JSON).
DefaultHeader("custom1", "value1").
DefaultHeaderMap(map[string]string{
"custom2": "value2",
})
assert.NotNil(t, cli.Doer())
t.Run("Get", func(t *testing.T) {
resp, err := cli.Get("/get", httpreq.WithData("name=inhere&age=18"))
assert.NoErr(t, err)
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
rr := testutil.ParseRespToReply(resp)
assert.Equal(t, "GET", rr.Method)
assert.Equal(t, "value1", rr.Headers["Custom1"])
assert.Equal(t, "value2", rr.Headers["Custom2"])
assert.Equal(t, "inhere", rr.Query["name"])
// dump.P(rr)
})
t.Run("Post", func(t *testing.T) {
resp, err := cli.Post("/post", `{"name": "inhere"}`, httpreq.WithJSONType)
assert.NoErr(t, err)
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
rr := testutil.ParseRespToReply(resp)
assert.Equal(t, "POST", rr.Method)
assert.Equal(t, "value1", rr.Headers["Custom1"])
assert.StrContains(t, rr.Headers["Content-Type"].(string), httpctype.MIMEJSON)
assert.Eq(t, `{"name": "inhere"}`, rr.Body)
dump.P(rr)
})
t.Run("Put", func(t *testing.T) {
resp, err := cli.Put("/put", `{"name": "inhere"}`, httpreq.WithJSONType)
assert.NoErr(t, err)
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
rr := testutil.ParseRespToReply(resp)
assert.Equal(t, "PUT", rr.Method)
assert.Equal(t, "value1", rr.Headers["Custom1"])
assert.StrContains(t, rr.Headers["Content-Type"].(string), httpctype.MIMEJSON)
assert.Eq(t, `{"name": "inhere"}`, rr.Body)
// dump.P(rr)
})
t.Run("Delete", func(t *testing.T) {
resp, err := cli.Delete("/delete", httpreq.WithData("name=inhere&age=18"))
assert.NoErr(t, err)
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
rr := testutil.ParseRespToReply(resp)
assert.Equal(t, "DELETE", rr.Method)
assert.Equal(t, "value1", rr.Headers["Custom1"])
assert.Equal(t, "value2", rr.Headers["Custom2"])
assert.Equal(t, "inhere", rr.Query["name"])
// dump.P(rr)
})
}
func TestHttpReq_MustSend(t *testing.T) {
cli := httpreq.New().OnBeforeSend(func(req *http.Request) {
assert.Eq(t, http.MethodPost, req.Method)
}).OnAfterSend(func(resp *http.Response, err error) {
bodyStr, _ := io.ReadAll(resp.Body)
assert.StrContains(t, string(bodyStr), "hi,goutil")
})
resp := cli.BaseURL(testSrvAddr).
BytesBody([]byte("hi,goutil")).
MustSend("POST", "/post")
sc := resp.StatusCode
assert.True(t, httpreq.IsOK(sc))
assert.True(t, httpreq.IsSuccessful(sc))
}