-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreq_test.go
138 lines (110 loc) · 2.75 KB
/
greq_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
130
131
132
133
134
135
136
137
138
package greq_test
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/netutil/httpreq"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/greq"
)
var testDoer = httpreq.DoerFunc(func(req *http.Request) (*http.Response, error) {
tw := httptest.NewRecorder()
dump.P("CORE++")
_, err := tw.WriteString(req.RequestURI + " > ")
if err != nil {
return nil, err
}
if req.Body != nil {
_, err = tw.Write(fsutil.MustReadReader(req.Body))
}
dump.P("CORE--")
return tw.Result(), err
})
var testBaseURL string
func TestMain(m *testing.M) {
// create server
s := testutil.NewEchoServer()
defer s.Close()
testBaseURL = "http://" + s.Listener.Addr().String()
fmt.Println("Test server listen on:", testBaseURL)
// with base url
greq.BaseURL(testBaseURL)
// do something
m.Run()
}
func TestHReq_Doer(t *testing.T) {
buf := &bytes.Buffer{}
mid0 := greq.MiddleFunc(func(r *http.Request, next greq.HandleFunc) (*greq.Response, error) {
dump.P("MID0++")
w, err := next(r)
dump.P("MID0--")
return w, err
})
resp, err := greq.New(testBaseURL).
Doer(testDoer).
Use(mid0).
UserAgent("custom-cli/1.0").
Get("/get").
Do()
assert.NoErr(t, err)
assert.True(t, resp.IsOK())
err = resp.Write(buf)
assert.NoErr(t, err)
dump.P(buf.String())
}
func TestHReq_Send(t *testing.T) {
resp, err := greq.New(testBaseURL).
UserAgent("custom-cli/1.0").
Send("GET", "/get")
assert.NoErr(t, err)
assert.True(t, resp.IsOK())
assert.False(t, resp.IsFail())
retMp := make(map[string]any)
err = resp.Decode(&retMp)
assert.NoErr(t, err)
dump.P(retMp)
assert.Contains(t, retMp, "headers")
headers := retMp["headers"].(map[string]any)
assert.Contains(t, headers, "User-Agent")
assert.Eq(t, "custom-cli/1.0", headers["User-Agent"])
}
func TestHReq_GetDo(t *testing.T) {
resp, err := greq.New(testBaseURL).
JSONType().
GetDo("/get")
assert.NoErr(t, err)
assert.True(t, resp.IsOK())
assert.True(t, resp.IsSuccessful())
retMp := make(map[string]any)
err = resp.Decode(&retMp)
assert.NoErr(t, err)
dump.P(retMp)
}
func TestHReq_PostDo(t *testing.T) {
resp, err := greq.New(testBaseURL).
UserAgent(greq.AgentCURL).
JSONType().
PostDo("/post", `{"name": "inhere"}`)
assert.NoErr(t, err)
assert.True(t, resp.IsOK())
assert.True(t, resp.IsSuccessful())
retMp := make(map[string]any)
err = resp.Decode(&retMp)
assert.NoErr(t, err)
dump.P(retMp)
}
func TestHReq_String(t *testing.T) {
str := greq.New(testBaseURL).
UserAgent("some-cli/1.0").
BasicAuth("inhere", "some string").
JSONType().
StringBody("hi, with body").
Post("/post", `{"name": "inhere"}`).
String()
fmt.Println(str)
}