-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreq_test.go
115 lines (107 loc) · 2.88 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
package req
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"testing"
)
func init() {
Debug = true
}
func TestMethods(t *testing.T) {
cb := func(resp *Response) *Response {
fmt.Println(resp.Text)
return resp
}
assert.Nil(t, Get("https://httpbin.org/get").SetCallback(cb).Do().Error())
assert.Nil(t, Post("https://httpbin.org/post").SetCallback(cb).Do().Error())
assert.Nil(t, Head("https://httpbin.org/head").SetCallback(cb).Do().Error())
assert.Nil(t, Put("https://httpbin.org/put").SetCallback(cb).Do().Error())
assert.Nil(t, Delete("https://httpbin.org/delete").SetCallback(cb).Do().Error())
assert.Nil(t, Connect("https://httpbin.org/connect").SetCallback(cb).Do().Error())
assert.Nil(t, Options("https://httpbin.org/options").SetCallback(cb).Do().Error())
assert.Nil(t, Trace("https://httpbin.org/trace").SetCallback(cb).Do().Error())
assert.Nil(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)
if resp.Err != nil {
t.Error(resp.Err)
}
}
func TestPost(t *testing.T) {
resp := Post("https://httpbin.org/post").Do()
t.Log(resp.Text)
assert.Nil(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.Nil(t, resp.Err)
s <- struct{}{}
return resp
}).Do()
_ = <-s
}
func TestRequest_SetMultipartBody(t *testing.T) {
f, err := os.Open("./req.go")
assert.Nil(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.Nil(t, resp.Err)
}
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.Nil(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())
}