-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab_test.go
160 lines (140 loc) · 3.79 KB
/
tab_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package vBrowser
import (
"net/http"
"testing"
)
const testURL = "http://www.msftconnecttest.com/redirect"
var tab = b.NewTab()
type testTransport struct {
actual http.RoundTripper
testFunc func(req *http.Request, resp *http.Response)
}
func (t *testTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
resp, err = t.actual.RoundTrip(req)
if t.testFunc != nil {
t.testFunc(req, resp)
}
return resp, err
}
type testFunc func(req *http.Request, resp *http.Response)
func hookTransport(client *http.Client, f testFunc) {
client.Transport = &testTransport{
actual: tab.httpClient.Transport,
testFunc: f,
}
}
func unhookTransport(client *http.Client) {
if trans, ok := client.Transport.(*testTransport); ok {
tab.httpClient.Transport = trans.actual
}
}
func TestTab_Open(t *testing.T) {
// hook the transport
var ok bool
c := tab.httpClient.CheckRedirect
tab.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
} // stop auto redirect(because it cause referer change)
testFunc := func(req *http.Request, resp *http.Response) {
referer := req.Header.Get("Referer")
t.Log(referer)
ok = referer == ""
}
hookTransport(tab.httpClient, testFunc)
hookTransport(tab.ajaxHttpClient, testFunc)
// test
err := tab.Open(testURL)
if err != nil {
t.Error(err)
t.Fail()
}
if !ok {
t.Error("Test Referer Failed")
t.Fail()
}
// resume the transport
tab.httpClient.CheckRedirect = c
unhookTransport(tab.httpClient)
unhookTransport(tab.ajaxHttpClient)
}
func TestTab_Jump(t *testing.T) {
// hook the transport
var ok bool
lastURL := tab.Location
t.Log("lastURL:", lastURL)
c := tab.httpClient.CheckRedirect
tab.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
} // stop auto redirect(because it cause referer change)
testFunc := func(req *http.Request, resp *http.Response) {
referer := req.Header.Get("Referer")
t.Log(referer)
ok = referer == lastURL.String()
}
hookTransport(tab.httpClient, testFunc)
hookTransport(tab.ajaxHttpClient, testFunc)
// test
err := tab.Jump(testURL)
if err != nil {
t.Error(err)
t.Fail()
}
if !ok {
t.Error("Test Referer Failed")
t.Fail()
}
// resume the transport
tab.httpClient.CheckRedirect = c
if trans, ok := tab.httpClient.Transport.(*testTransport); ok {
tab.httpClient.Transport = trans.actual
}
unhookTransport(tab.httpClient)
unhookTransport(tab.ajaxHttpClient)
}
func TestTab_Request(t *testing.T) {
_, err := tab.Request("1", "1", nil)
if err == nil || err.Error() != `1 1: unsupported protocol scheme ""` {
t.Log(err)
t.Fail()
}
_, err = tab.Request("1", "http://1.1", nil)
if err == nil || err.Error() != `1 http://1.1: dial tcp: lookup 1.1: no such host` {
t.Log(err)
t.Fail()
}
// hook the transport
var ok bool
c := tab.httpClient.CheckRedirect
tab.httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
} // stop auto redirect(because it cause referer change)
testFunc := func(req *http.Request, resp *http.Response) {
referer := req.Header.Get("Referer")
method := req.Method
url := req.URL.String()
t.Log(method, url, referer)
t.Log("referer", referer)
t.Log("method", method)
t.Log("url", url)
ok = referer == tab.Location.String() && method == http.MethodPost && url == testURL
}
hookTransport(tab.httpClient, testFunc)
hookTransport(tab.ajaxHttpClient, testFunc)
// test
resp, err := tab.Request(http.MethodPost, testURL, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
t.Error(err)
t.Fail()
}
if !ok {
t.Error("not ok")
t.Fail()
}
// resume the transport
tab.httpClient.CheckRedirect = c
unhookTransport(tab.httpClient)
unhookTransport(tab.ajaxHttpClient)
}