/
response_test.go
104 lines (89 loc) · 3.28 KB
/
response_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
package requests
import (
"fmt"
"testing"
)
func TestHttpResponse_Bytes(t *testing.T) {
resp, err := NewHttpRequest("GET", "https://www.baidu.com/s").
WithParam("wd", "golang").
WithHeader("Referer", "https://www.baidu.com").
WithHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").
WithHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7").
WithHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").
WithDebug(true).
GetHttpResponse()
if err != nil {
t.Fatal(err)
} else {
b, err := resp.Bytes()
t.Log(resp.StatusCode, err, string(b))
}
}
func TestHttpResponse_SaveFile(t *testing.T) {
resp, err := NewHttpRequest("GET", "http://f.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494eee5a348a020f5e0fe98257e81.jpg").
WithHeader("Referer", "https://www.baidu.com").
WithHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").
WithHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7").
WithHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").
WithDebug(true).
GetHttpResponse()
if err != nil {
t.Fatal(err)
} else {
err := resp.SaveFile("./f2deb48f8c5494eee5a348a020f5e0fe98257e81.jpg")
t.Log(resp.StatusCode, err)
}
}
func TestHttpResponse_ToJson(t *testing.T) {
resp, err := NewHttpRequest("GET", "https://api.apiopen.top/searchPoetry").
WithParam("name", "古风二首 二").
WithReferer("https://www.baidu.com").
WithHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").
WithHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7").
WithUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").
WithDebug(true).
GetHttpResponse()
if err != nil {
t.Fatal(err)
} else {
type Data struct {
Title string `json:"title"`
Content string `json:"content"`
Authors string `json:"authors"`
}
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Result []*Data
}
var result Result
err := resp.ToJson(&result)
t.Log(resp.StatusCode, err, fmt.Sprintf("%v", result))
}
}
func TestHttpRequest_Error(t *testing.T) {
req := NewHttpRequest("GET", "https://www.baidu.com/s").
WithParam("wd", "golang").
WithHeader("Referer", "https://www.baidu.com").
WithHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").
WithHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7").
WithHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").
WithDebug(true)
if err := req.Error(); err != nil {
t.Log(err)
}
}
func TestHttpRequest_WithFiles(t *testing.T) {
req := NewHttpRequest("POST", "https://www.baidu.com").
WithFile("requests.go", "./requests.go")
if err := req.Error(); err != nil {
t.Log(err)
} else {
resp, err := req.GetHttpResponse()
if err != nil {
t.Log(err)
} else {
t.Log(resp.String())
}
}
}