-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests_test.go
More file actions
123 lines (106 loc) · 2.4 KB
/
requests_test.go
File metadata and controls
123 lines (106 loc) · 2.4 KB
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
package requests
import (
"io/ioutil"
"net/http"
"testing"
)
const (
getUrl = "http://httpbin.org/get"
postUrl = "http://httpbin.org/post"
putUrl = "http://httpbin.org/put"
deleteUrl = "http://httpbin.org/delete"
)
type RespData struct {
Args map[string]string `json:"args"`
Data string `json:"data"`
Headers struct {
Accept string `json:"Accept"`
AcceptEncoding string `json:"Accept-Encoding"`
AcceptLanguage string `json:"Accept-Language"`
Host string `json:"Host"`
UpgradeInsecureRequests string `json:"Upgrade-Insecure-Requests"`
UserAgent string `json:"User-Agent"`
XAmznTraceId string `json:"X-Amzn-Trace-Id"`
JWT string `json:"JWT"`
} `json:"headers"`
Origin string `json:"origin"`
Json map[string]string `json:"json"`
Url string `json:"url"`
}
func TestGetAndParse(t *testing.T) {
var resp RespData
_, err := GetAndParse(getUrl, &resp)
if err != nil {
t.Fatal(err)
return
}
t.Logf("%+v", resp)
}
func TestGetAndParse2(t *testing.T) {
c := NewClient()
c.AddQuery("search", "mysql").AddQuery("order", "id")
c.AddHeader("JWT", "1qasdfsddf")
var resp RespData
if _, err := c.GetAndParseJson(getUrl, &resp); err != nil {
t.Fatal(err)
}
t.Logf("%+v", resp)
}
func TestPost(t *testing.T) {
//data := map[string]string{
// "name": "golang",
//}
data := struct {
Name string
}{
Name: "golang",
}
var resp RespData
_, err := PostAndParse(postUrl, &data, &resp)
if err != nil {
t.Fatal(err)
return
}
t.Logf("%+v", resp)
}
func TestPut(t *testing.T) {
data := struct {
Name string
}{
Name: "python",
}
var resp RespData
_, err := PutAndParse(putUrl, &data, &resp)
if err != nil {
t.Fatal(err)
return
}
t.Logf("%+v", resp)
}
func TestDelete(t *testing.T) {
c := NewClient()
c.AddQuery("search", "mysql").AddQuery("action", "delete")
c.AddHeader("JWT", "1qasdfsddf")
var resp RespData
_, err := c.DeleteAndParseJson(deleteUrl, &resp)
if err != nil {
t.Fatal(err)
return
}
t.Logf("%+v", resp)
}
func TestCustomClient(t *testing.T) {
// custom client
httpClient := &http.Client{}
c := NewNativeClient(httpClient)
resp, err := c.Get(getUrl)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(bytes))
}