-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfind_test.go
176 lines (142 loc) · 4.53 KB
/
find_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package headless_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/nextdotid/proof_server/headless"
)
func newValidRequest(location string, matchType string) headless.FindRequest {
switch matchType {
case "regexp":
return headless.FindRequest{
Location: location,
Timeout: "2s",
Match: headless.Match{
Type: "regexp",
MatchRegExp: &headless.MatchRegExp{
Selector: "*",
Value: "match-this-text",
},
},
}
case "xpath":
return headless.FindRequest{
Location: location,
Timeout: "2s",
Match: headless.Match{
Type: "xpath",
MatchXPath: &headless.MatchXPath{
Selector: "//text()[contains(.,'match-this-text')]",
},
},
}
case "js":
return headless.FindRequest{
Location: location,
Timeout: "2s",
Match: headless.Match{
Type: "js",
MatchJS: &headless.MatchJS{
Value: "() => [].filter.call(document.querySelectorAll('*'), (el) => el.textContent === 'match-this-text')[0]",
},
},
}
}
return headless.FindRequest{}
}
func Test_Find(t *testing.T) {
apiTs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// To simulate network latency
time.Sleep(time.Duration(300) * time.Millisecond)
w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{ "content": "match-this-text" }`))
}))
defer apiTs.Close()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
html := fmt.Sprintf(`
<html>
<head>
<script>
fetch('%s')
.then(response => response.json())
.then(({ content }) => {
document.body.innerHTML = '<h1>' + content + '</h1>';
});
</script>
</head>
<body>
</body>
</html>`,
apiTs.URL)
w.Header().Add("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(html))
}))
defer ts.Close()
t.Run("success", func(t *testing.T) {
// using regexp
req := newValidRequest(ts.URL, "regexp")
res := headless.FindRespond{}
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Equal(t, true, res.Found)
assert.Equal(t, "", res.Message)
// using xpath
req = newValidRequest(ts.URL, "xpath")
res = headless.FindRespond{}
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Equal(t, true, res.Found)
assert.Equal(t, "", res.Message)
// using js
req = newValidRequest(ts.URL, "js")
res = headless.FindRespond{}
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Equal(t, true, res.Found)
assert.Equal(t, "", res.Message)
})
t.Run("error ", func(t *testing.T) {
// invalid location
req := newValidRequest(ts.URL, "regexp")
res := headless.FindRespond{}
req.Location = ""
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "location")
// invalid timeout
req = newValidRequest(ts.URL, "regexp")
res = headless.FindRespond{}
req.Timeout = "invalid"
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "timeout")
// invalid match type
req = newValidRequest(ts.URL, "regexp")
res = headless.FindRespond{}
req.Match.Type = "invalid"
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "match.type")
// missing regexp value
req = newValidRequest(ts.URL, "regexp")
res = headless.FindRespond{}
req.Match.MatchRegExp.Value = ""
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "match.regexp.value")
// missing xpath selector
req = newValidRequest(ts.URL, "xpath")
res = headless.FindRespond{}
req.Match.MatchXPath.Selector = ""
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "match.xpath.selector")
// missing js value
req = newValidRequest(ts.URL, "js")
res = headless.FindRespond{}
req.Match.MatchJS.Value = ""
APITestCall(headless.Engine, "POST", "/v1/find", req, &res)
assert.Contains(t, res.Message, "match.js.value")
// target text is not found
req = newValidRequest(ts.URL, "regexp")
success := headless.FindRespond{}
req.Match.MatchRegExp.Value = "unknown-text"
APITestCall(headless.Engine, "POST", "/v1/find", req, &success)
assert.Equal(t, success.Found, false)
})
}