-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhttp_test.go
73 lines (66 loc) · 1.97 KB
/
http_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
package http
import (
"fmt"
"testing"
"time"
)
func TestGet(t *testing.T) {
http := New().UseRequest(func(req Request) Request {
fmt.Printf("http 发送 %s %s header=%+v data=%+v\n", req.SuperAgent.Method, req.SuperAgent.Url, req.SuperAgent.Header, req.SuperAgent.Data)
return req
}).UseResponse(func(req Request, res *Response) *Response {
fmt.Printf("http 接收 %s %s\n", req.SuperAgent.Method, req.SuperAgent.Url)
return res
}).Timeout(time.Second * 10).
BaseURL("https://api.github.com").
BaseURL("/repos")
res, err := http.Get("/eyasliu/blog/issues", map[string]interface{}{
"per_page": 1,
})
if err != nil {
panic(err)
}
s := []struct {
URL string `json:"url"`
Title string
}{}
err = res.JSON(&s)
if err != nil {
panic(err)
}
if len(s) > 0 {
t.Logf("get res struct: %+v", s)
}
}
func TestError(t *testing.T) {
h := New().UseRequest(func(req Request) Request {
fmt.Printf("http 发送 %s %s header=%+v data=%+v\n", req.SuperAgent.Method, req.SuperAgent.Url, req.SuperAgent.Header, req.SuperAgent.Data)
return req
}).UseResponse(func(req Request, res *Response) *Response {
fmt.Printf("http 接收 %s %s\n", req.SuperAgent.Method, req.SuperAgent.Url)
return res
})
res, err := h.Header("just-test", "1234").Get("https://api.github.com/repos/eyasliu/blog/issuesx", nil)
if err != nil {
t.Logf("get error success: statusCode=%d body=%s error=%s", res.Status(), res.String(), err.Error())
} else {
t.Fatalf("res: statusCode=%d body=%s", res.Status(), res.String())
panic("should get 404 error")
}
res, err = h.Get("", nil)
if err != nil {
t.Logf("success empty url, statusCode=%d body=%s error=%s", res.Status(), res.String(), err.Error())
} else {
panic("should error")
}
}
// func TestProxy(t *testing.T) {
// h := Proxy("http://127.0.0.1:1080")
// res, err := h.Get("https://www.google.com", map[string]string{
// "hl": "zh-Hans",
// })
// if err != nil {
// panic(err)
// }
// t.Logf("google html: %s", res.String())
// }