-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathhttpreq_test.go
91 lines (78 loc) · 2.42 KB
/
httpreq_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
package httpreq_test
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/netutil/httpreq"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
var testSrvAddr string
func TestMain(m *testing.M) {
s := testutil.NewEchoServer()
defer s.Close()
testSrvAddr = "http://" + s.Listener.Addr().String()
fmt.Println("Test server listen on:", testSrvAddr)
m.Run()
}
func TestStdClient(t *testing.T) {
assert.NotNil(t, httpreq.Std())
httpreq.SetTimeout(300)
httpreq.Config(func(hc *http.Client) {
hc.Timeout = 400 * time.Millisecond
})
t.Run("head", func(t *testing.T) {
resp, err := httpreq.Send("head", testSrvAddr+"/head")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseRespToReply(resp)
// dump.P(rr)
assert.Equal(t, "HEAD", rr.Method)
})
t.Run("options", func(t *testing.T) {
resp := httpreq.MustSend("options", testSrvAddr+"/options")
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseRespToReply(resp)
dump.P(rr)
assert.Eq(t, "OPTIONS", rr.Method)
})
t.Run("get", func(t *testing.T) {
resp := httpreq.MustResp(httpreq.Get(testSrvAddr + "/get"))
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseBodyToReply(resp.Body)
assert.Equal(t, "GET", rr.Method)
})
t.Run("post", func(t *testing.T) {
resp, err := httpreq.Post(testSrvAddr+"/post", "hi")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseBodyToReply(resp.Body)
assert.Equal(t, "POST", rr.Method)
assert.Equal(t, "hi", rr.Body)
})
t.Run("post JSON", func(t *testing.T) {
resp, err := httpreq.PostJSON(testSrvAddr+"/post", map[string]string{"name": "inhere"})
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseBodyToReply(resp.Body)
assert.Equal(t, "POST", rr.Method)
assert.StrContains(t, rr.Body, `{"name":"inhere"}`)
})
t.Run("put", func(t *testing.T) {
resp, err := httpreq.Put(testSrvAddr+"/put", "hi")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseBodyToReply(resp.Body)
assert.Equal(t, "PUT", rr.Method)
assert.Equal(t, "hi", rr.Body)
})
t.Run("delete", func(t *testing.T) {
resp, err := httpreq.Delete(testSrvAddr + "/delete")
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
rr := testutil.ParseBodyToReply(resp.Body)
assert.Equal(t, "DELETE", rr.Method)
})
}