-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
69 lines (60 loc) · 1.3 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
package httpgo
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"testing"
"time"
)
func TestGet(t *testing.T) {
t.Log(Get("https://qq.com").Do())
}
func TestGetTimeout(t *testing.T) {
t.Log(Get("https://voocel.com").SetTimeout(1 * time.Second).Do())
}
func TestPost(t *testing.T) {
t.Log(Post("127.0.0.1:3333/post").SetForm(map[string]string{"name": "peter", "address": "unknown"}).Do())
}
func TestPostJSON(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
m := map[string]string{
"name": fmt.Sprint("tony-", i),
"addr": "unknown",
}
b, _ := json.Marshal(m)
t.Log(Post("127.0.0.1:3333/post").SetJSON(string(b)).Do())
}(i)
}
wg.Wait()
}
func TestHeader(t *testing.T) {
r, err := Post("https://qq.com").SetUA("test-ua").
AddCookie(&http.Cookie{
Name: "http_go_cookie",
Value: "http_go",
}).
AddHeader("test", "value").Do()
t.Log(r, err)
}
func TestAsync(t *testing.T) {
ch := make(chan *AsyncResponse)
AsyncGet("https://qq.com", ch)
timer := time.NewTimer(5 * time.Second)
for {
select {
case res := <-ch:
fmt.Println(res.Resp)
case <-timer.C:
return
}
}
}
func TestRequest_SetFile(t *testing.T) {
r, err := Post("http://127.0.0.1:3333/file").SetFile("file", "abc.txt").Do()
t.Log(r, err)
}