-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
98 lines (78 loc) · 1.9 KB
/
session_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
package quick
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func RunServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("quick", "hd")
_, _ = w.Write([]byte("quick"))
}))
}
func TestSession_Get(t *testing.T) {
ser := RunServer()
defer ser.Close()
resp, err := NewSession().Get(ser.URL)
if err != nil {
t.Fatal(err)
}
asserts := assert.New(t)
asserts.Equal(resp.StatusCode, 200)
asserts.Equal(resp.Body.String(), "quick")
asserts.Equal(resp.Header.Get("quick"), "hd")
}
func BenchmarkSession_Get(b *testing.B) {
ser := RunServer()
defer ser.Close()
session := NewSession()
for i := 0; i < b.N; i++ {
_, _ = session.Get(ser.URL)
}
}
func BenchmarkSession_Get_Parallel(b *testing.B) {
ser := RunServer()
defer ser.Close()
session := NewSession()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = session.Get(ser.URL)
}
})
}
func TestSession_Post(t *testing.T) {
asserts := assert.New(t)
ser := RunServer()
defer ser.Close()
cookieJar, _ := NewCookieJar()
session := NewSession()
session.SetCookieJar(cookieJar)
req := NewRequest().SetMethod(http.MethodGet).SetUrl(ser.URL)
req.SetCookies(NewCookiesWithString("test=111111"))
resp, err := session.Suck(req)
if err != nil {
t.Fatal(err)
}
asserts.Equal(resp.StatusCode, 200)
}
func TestSession_SetBaseURL(t *testing.T) {
session := NewSession().SetBaseURL("https://httpbin.org/")
resp, err := session.Get("/get?a=1#12")
if err != nil {
t.Fatal(err)
}
fmt.Println(resp.Status)
req, _ := http.NewRequest("GET", "/get?a=1#12", nil)
resp, err = session.Do(req)
if err != nil {
t.Fatal(err)
}
fmt.Println(resp)
}
func TestSession_EnableTrace(t *testing.T) {
session := NewSession()
resp, _ := session.EnableTrace().Get("https://httpbin.org/get")
fmt.Println(resp.TraceInfo())
}