forked from mozillazg/request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
133 lines (122 loc) · 2.77 KB
/
example_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
package request_test
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/melonwool/request"
)
func ExampleRequest_Get() {
c := new(http.Client)
req := request.NewRequest(c)
url := "https://httpbin.org/get"
resp, _ := req.Get(url)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(resp.Ok())
fmt.Println(d.Get("url").MustString())
// Output:
//true
//https://httpbin.org/get
}
func ExampleGet() {
c := new(http.Client)
args := request.NewArgs(c)
url := "https://httpbin.org/get"
resp, _ := request.Get(url, args)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(resp.Ok())
fmt.Println(d.Get("url").MustString())
// Output:
//true
//https://httpbin.org/get
}
func ExampleRequest_Get_params() {
c := new(http.Client)
req := request.NewRequest(c)
req.Params = map[string]string{
"a": "1",
"b": "2",
}
url := "https://httpbin.org/get"
resp, _ := req.Get(url)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(d.Get("url").MustString())
// Output:
//https://httpbin.org/get?a=1&b=2
}
func ExampleRequest_Get_customHeaders() {
c := new(http.Client)
req := request.NewRequest(c)
req.Headers = map[string]string{
"X-Abc": "abc",
"User-Agent": "go-request-test",
}
url := "https://httpbin.org/get"
resp, _ := req.Get(url)
d, _ := resp.Json()
defer resp.Body.Close()
fmt.Println(d.Get("headers").Get("User-Agent").MustString())
fmt.Println(d.Get("headers").Get("X-Abc").MustString())
// Output:
//go-request-test
//abc
}
func ExampleRequest_Post() {
c := new(http.Client)
req := request.NewRequest(c)
req.Data = map[string]string{
"a": "1",
"b": "2",
}
url := "https://httpbin.org/post"
resp, _ := req.Post(url)
defer resp.Body.Close()
}
func ExamplePost() {
c := new(http.Client)
args := request.NewArgs(c)
args.Data = map[string]string{
"a": "1",
"b": "2",
}
url := "https://httpbin.org/post"
resp, _ := request.Post(url, args)
defer resp.Body.Close()
}
func ExampleRequest_Get_cookies() {
c := new(http.Client)
req := request.NewRequest(c)
req.Cookies = map[string]string{
"name": "value",
"foo": "bar",
}
url := "https://httpbin.org/cookies"
resp, _ := req.Get(url)
defer resp.Body.Close()
}
func ExampleRequest_Post_files() {
c := new(http.Client)
req := request.NewRequest(c)
f, _ := os.Open("test.txt")
defer f.Close()
req.Files = []request.FileField{
{FieldName: "abc", FileName: "abc.txt", File: f},
}
url := "https://httpbin.org/post"
resp, _ := req.Post(url)
defer resp.Body.Close()
}
func ExampleRequest_Post_rawBody() {
c := new(http.Client)
req := request.NewRequest(c)
req.Body = strings.NewReader("a=1&b=2&foo=bar")
req.Headers = map[string]string{
"Content-Type": request.DefaultContentType,
}
url := "https://httpbin.org/post"
resp, _ := req.Post(url)
defer resp.Body.Close()
}