-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
188 lines (182 loc) · 5.17 KB
/
api_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
177
178
179
180
181
182
183
184
185
186
187
188
package zenrows_test
import (
"net/url"
"testing"
"github.com/renatoaraujo/go-zenrows"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestScrapeOptions(t *testing.T) {
tests := []struct {
name string
option zenrows.ScrapeOptions
expected url.Values
}{
{
"WithJSRender",
zenrows.WithJSRender(),
url.Values{"js_render": []string{"true"}},
},
{
"WithJSInstructions",
zenrows.WithJSInstructions(`[{"click": ".selector"}, {"wait": 500}]`),
url.Values{"js_render": []string{"true"}, "js_instructions": []string{`[{"click":".selector"},{"wait":500}]`}},
},
{
"WithCustomHeaders",
zenrows.WithCustomHeaders(true),
url.Values{"custom_headers": []string{"true"}},
},
{
"WithPremiumProxy",
zenrows.WithPremiumProxy(),
url.Values{"premium_proxy": []string{"true"}},
},
{
"WithProxyCountry",
zenrows.WithProxyCountry("US"),
url.Values{"premium_proxy": []string{"true"}, "proxy_country": []string{"US"}},
},
{
"WithBlockResources",
zenrows.WithBlockResources("image"),
url.Values{"js_render": []string{"true"}, "block_resources": []string{"image"}},
},
{
"WithJSONResponse",
zenrows.WithJSONResponse(true),
url.Values{"js_render": []string{"true"}, "json_response": []string{"true"}},
},
{
"WithWindowWidth",
zenrows.WithWindowWidth(1920),
url.Values{"js_render": []string{"true"}, "window_width": []string{"1920"}},
},
{
"WithWindowHeight",
zenrows.WithWindowHeight(1080),
url.Values{"js_render": []string{"true"}, "window_height": []string{"1080"}},
},
{
"WithCSSExtractor",
zenrows.WithCSSExtractor(".content"),
url.Values{"css_extractor": []string{".content"}},
},
{
"WithAutoparse",
zenrows.WithAutoparse(true),
url.Values{"autoparse": []string{"true"}},
},
{
"WithResolveCaptcha",
zenrows.WithResolveCaptcha(true),
url.Values{"js_render": []string{"true"}, "resolve_captcha": []string{"true"}},
},
{
"WithDevice",
zenrows.WithDevice("desktop"),
url.Values{"device": []string{"desktop"}},
},
{
"WithOriginalStatus",
zenrows.WithOriginalStatus(true),
url.Values{"original_status": []string{"true"}},
},
{
"WithWaitFor",
zenrows.WithWaitFor(".selector"),
url.Values{"js_render": []string{"true"}, "wait_for": []string{".selector"}},
},
{
"WithWait",
zenrows.WithWait(500),
url.Values{"js_render": []string{"true"}, "wait": []string{"500"}},
},
{
"WithSessionID",
zenrows.WithSessionID(12345),
url.Values{"session_id": []string{"12345"}},
},
{
"WithAIAntiBot",
zenrows.WithAIAntiBot(),
url.Values{"js_render": []string{"true"}, "antibot": []string{"true"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values := url.Values{}
tt.option(values)
assert.Equal(t, tt.expected, values)
})
}
}
func TestApplyParameters(t *testing.T) {
tests := []struct {
name string
options []zenrows.ScrapeOptions
expected url.Values
}{
{
"Single Option - WithJSRender",
[]zenrows.ScrapeOptions{zenrows.WithJSRender()},
url.Values{"js_render": []string{"true"}},
},
{
"Multiple Options - WithJSInstructions and WithCustomHeaders",
[]zenrows.ScrapeOptions{
zenrows.WithJSInstructions(`[{"click": ".selector"}, {"wait": 500}]`),
zenrows.WithCustomHeaders(true),
},
url.Values{"js_render": []string{"true"}, "js_instructions": []string{`[{"click":".selector"},{"wait":500}]`}, "custom_headers": []string{"true"}},
},
{
"Multiple Options - WithPremiumProxy and WithProxyCountry",
[]zenrows.ScrapeOptions{
zenrows.WithPremiumProxy(),
zenrows.WithProxyCountry("US"),
},
url.Values{"premium_proxy": []string{"true"}, "proxy_country": []string{"US"}},
},
{
"Multiple Options - WithBlockResources and WithJSONResponse",
[]zenrows.ScrapeOptions{
zenrows.WithBlockResources("image"),
zenrows.WithJSONResponse(true),
},
url.Values{"js_render": []string{"true"}, "block_resources": []string{"image"}, "json_response": []string{"true"}},
},
{
"Multiple Options - WithWindowWidth and WithWindowHeight",
[]zenrows.ScrapeOptions{
zenrows.WithWindowWidth(1920),
zenrows.WithWindowHeight(1080),
},
url.Values{"js_render": []string{"true"}, "window_width": []string{"1920"}, "window_height": []string{"1080"}},
},
{
"Multiple Options - WithCSSExtractor and WithAutoparse",
[]zenrows.ScrapeOptions{
zenrows.WithCSSExtractor(".content"),
zenrows.WithAutoparse(true),
},
url.Values{"autoparse": []string{"true"}, "css_extractor": []string{".content"}},
},
{
"Multiple Options - WithResolveCaptcha and WithJSInstructions",
[]zenrows.ScrapeOptions{
zenrows.WithResolveCaptcha(true),
zenrows.WithJSInstructions(`[{"click": ".button"}]`),
},
url.Values{"js_render": []string{"true"}, "js_instructions": []string{`[{"click":".button"}]`}, "resolve_captcha": []string{"true"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseURL, err := url.Parse("https://example.com")
require.NoError(t, err)
resultURL := zenrows.ApplyParameters(baseURL, tt.options...)
assert.Equal(t, tt.expected, resultURL.Query())
})
}
}