forked from neodigm/goreq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
req_test.go
192 lines (178 loc) · 5.13 KB
/
req_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package goreq
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestMethods(t *testing.T) {
cb := func(resp *Response) *Response {
fmt.Println(resp.Text)
return resp
}
assert.NoError(t, Get("https://httpbin.org/get").SetCallback(cb).Do().Error())
assert.NoError(t, Post("https://httpbin.org/post").SetCallback(cb).Do().Error())
assert.NoError(t, Head("https://httpbin.org/head").SetCallback(cb).Do().Error())
assert.NoError(t, Put("https://httpbin.org/put").SetCallback(cb).Do().Error())
assert.NoError(t, Delete("https://httpbin.org/delete").SetCallback(cb).Do().Error())
assert.NoError(t, Connect("https://httpbin.org/connect").SetCallback(cb).Do().Error())
assert.NoError(t, Options("https://httpbin.org/options").SetCallback(cb).Do().Error())
assert.NoError(t, Trace("https://httpbin.org/trace").SetCallback(cb).Do().Error())
assert.NoError(t, Patch("https://httpbin.org/patch").SetCallback(cb).Do().Error())
}
func TestGet(t *testing.T) {
resp := Get("https://httpbin.org/get").Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
}
func TestPost(t *testing.T) {
resp := Post("https://httpbin.org/post").Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
}
func TestRequest_DoCallback(t *testing.T) {
s := make(chan struct{})
go Get("https://httpbin.org/get").SetCallback(func(resp *Response) *Response {
t.Log(resp.Text)
assert.NoError(t, resp.Err)
s <- struct{}{}
return resp
}).Do()
_ = <-s
}
func TestRequest_SetMultipartBody(t *testing.T) {
f, err := os.Open("./req.go")
assert.NoError(t, err)
resp := Post("https://httpbin.org/post").SetMultipartBody(
FormField{
Name: "AAA",
Value: "BBB",
},
FormFile{
FieldName: "CCC",
FileName: "req.go",
ContentType: "",
File: f,
},
).Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
}
func TestRequest_SetFormBody(t *testing.T) {
resp := Post("https://httpbin.org/post").SetFormBody(map[string]string{
"a": "1",
}).Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
j, _ := resp.JSON()
assert.Equal(t, "1", j.Get("form.a").String())
}
func TestRequest_SetJsonBody(t *testing.T) {
resp := Post("https://httpbin.org/post").SetJsonBody(map[string]string{
"a": "1",
}).Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
j, _ := resp.JSON()
assert.Equal(t, "1", j.Get("json.a").String())
}
func TestRequest_SetRawBody(t *testing.T) {
resp := Post("https://httpbin.org/post").SetRawBody([]byte("1")).Do()
t.Log(resp.Text)
assert.NoError(t, resp.Err)
j, _ := resp.JSON()
assert.Equal(t, "1", j.Get("data").String())
}
func TestRequest_Do(t *testing.T) {
resp := Post("https://httpbin.org/post?a=1").
AddParams(map[string]string{
"b": "2",
}).
AddHeaders(map[string]string{
"req": "golang",
}).
AddCookie(&http.Cookie{
Name: "c",
Value: "3",
}).
SetUA("goreq").
SetBasicAuth("goreq", "golang").
//SetProxy("http://127.0.0.1:1080/").
SetMultipartBody(
FormField{
Name: "d",
Value: "4",
},
FormFile{
FieldName: "e",
FileName: "e.txt",
ContentType: "",
File: bytes.NewReader([]byte("5")),
},
).
Do()
fmt.Println(resp.Text)
j, err := resp.JSON()
assert.NoError(t, err)
assert.Equal(t, "1", j.Get("args.a").String())
assert.Equal(t, "2", j.Get("args.b").String())
assert.Equal(t, "c=3", j.Get("headers.Cookie").String())
assert.Equal(t, "4", j.Get("form.d").String())
assert.Equal(t, "5", j.Get("files.e").String())
assert.Equal(t, "Basic Z29yZXE6Z29sYW5n", j.Get("headers.Authorization").String())
assert.Equal(t, "golang", j.Get("headers.Req").String())
assert.Equal(t, "goreq", j.Get("headers.User-Agent").String())
}
func setupProxy(t *testing.T) *gin.Engine {
r := gin.New()
r.GET("/:a", func(c *gin.Context) {
all, err := ioutil.ReadAll(c.Request.Body)
assert.NoError(t, err)
c.String(200, string(all))
})
return r
}
func TestProxy(t *testing.T) {
router := setupProxy(t)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()
proxyTs := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer proxyTs.Close()
txt, err := Get(ts.URL + "/login").SetRawBody([]byte(proxyTs.URL)).SetProxy(proxyTs.URL).Do().Txt()
assert.NoError(t, err)
assert.Equal(t, txt, proxyTs.URL)
}
func TestRequest_SetTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(5 * time.Second)
_, _ = fmt.Fprint(w, "hello")
}))
defer ts.Close()
err := Get(ts.URL).SetTimeout(10 * time.Second).Do().Error()
assert.NoError(t, err)
err = Get(ts.URL).SetTimeout(1 * time.Second).Do().Error()
assert.True(t, errors.Is(err, context.DeadlineExceeded))
}
func TestRequest_SetCheckRedirect(t *testing.T) {
i := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
i += 1
http.Redirect(w, r, "/", 301)
_, _ = fmt.Fprint(w, "hello")
}))
defer ts.Close()
err := Get(ts.URL).DisableRedirect().Do().Error()
assert.NoError(t, err)
assert.Equal(t, 1, i)
err = Get(ts.URL).Do().Error()
assert.Error(t, err)
assert.Equal(t, 11, i)
}