-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture_test.go
More file actions
541 lines (469 loc) · 14.7 KB
/
capture_test.go
File metadata and controls
541 lines (469 loc) · 14.7 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package capture
import (
"net/http"
"testing"
)
func TestNew(t *testing.T) {
key := "test_key"
secret := "test_secret"
c := New(key, secret)
if c.Key != key {
t.Errorf("Expected key %s, got %s", key, c.Key)
}
if c.Secret != secret {
t.Errorf("Expected secret %s, got %s", secret, c.Secret)
}
if c.UseEdge {
t.Error("Expected UseEdge to be false by default")
}
if c.Client == nil {
t.Error("Expected HTTP client to be initialized")
}
}
func TestNewWithEdge(t *testing.T) {
c := New("key", "secret", WithEdge())
if !c.UseEdge {
t.Error("Expected UseEdge to be true when using WithEdge option")
}
}
func TestNewWithHTTPClient(t *testing.T) {
customClient := &http.Client{}
c := New("key", "secret", WithHTTPClient(customClient))
if c.Client != customClient {
t.Error("Expected custom HTTP client to be set")
}
}
func TestGenerateToken(t *testing.T) {
c := New("key", "secret")
secret := "test_secret"
query := "url=example.com&width=1920"
token := c.generateToken(secret, query)
if token == "" {
t.Error("Expected non-empty token")
}
// Token should be consistent for same inputs
token2 := c.generateToken(secret, query)
if token != token2 {
t.Error("Expected consistent token generation")
}
}
func TestToQueryString(t *testing.T) {
c := New("key", "secret")
tests := []struct {
name string
options RequestOptions
expected string
}{
{
name: "empty options",
options: RequestOptions{},
expected: "",
},
{
name: "basic options",
options: RequestOptions{
"width": 1920,
"height": 1080,
},
expected: "height=1080&width=1920",
},
{
name: "with format",
options: RequestOptions{
"width": 1920,
"format": "png",
},
expected: "format=png&width=1920",
},
{
name: "with empty values (should be ignored)",
options: RequestOptions{
"width": 1920,
"empty": "",
"zero": 0,
"false": false,
},
expected: "false=false&width=1920&zero=0",
},
{
name: "with special characters",
options: RequestOptions{
"userAgent": "Custom Agent (v1.0)",
},
expected: "userAgent=Custom+Agent+%28v1.0%29",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := c.toQueryString(tt.options)
if result != tt.expected {
t.Errorf("Expected %s, got %s", tt.expected, result)
}
})
}
}
func TestBuildURL(t *testing.T) {
targetURL := "https://example.com"
tests := []struct {
name string
requestType RequestType
options RequestOptions
useEdge bool
expectError bool
}{
{
name: "image capture",
requestType: RequestTypeImage,
options: RequestOptions{"width": 1920},
useEdge: false,
expectError: false,
},
{
name: "pdf capture",
requestType: RequestTypePDF,
options: RequestOptions{"format": "A4"},
useEdge: false,
expectError: false,
},
{
name: "content capture",
requestType: RequestTypeContent,
options: RequestOptions{},
useEdge: false,
expectError: false,
},
{
name: "metadata capture",
requestType: RequestTypeMetadata,
options: RequestOptions{},
useEdge: false,
expectError: false,
},
{
name: "edge mode",
requestType: RequestTypeImage,
options: RequestOptions{},
useEdge: true,
expectError: false,
},
{
name: "missing key",
requestType: RequestTypeImage,
options: RequestOptions{},
useEdge: false,
expectError: true,
},
{
name: "missing secret",
requestType: RequestTypeImage,
options: RequestOptions{},
useEdge: false,
expectError: true,
},
{
name: "empty url",
requestType: RequestTypeImage,
options: RequestOptions{},
useEdge: false,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new client for each test to avoid state pollution
testClient := New("test_key", "test_secret")
if tt.useEdge {
testClient.UseEdge = true
}
// Handle special cases for error testing
if tt.name == "missing key" {
testClient.Key = ""
} else if tt.name == "missing secret" {
testClient.Secret = ""
} else if tt.name == "empty url" {
targetURL = ""
}
url, err := testClient.buildURL(tt.requestType, targetURL, tt.options)
if tt.expectError {
if err == nil {
t.Error("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if url == "" {
t.Error("Expected non-empty URL")
}
// Verify URL structure
expectedBase := testClient.APIURL
if tt.useEdge {
expectedBase = testClient.EdgeURL
}
if !contains(url, expectedBase) {
t.Errorf("URL should contain base URL %s, got %s", expectedBase, url)
}
if !contains(url, string(tt.requestType)) {
t.Errorf("URL should contain request type %s, got %s", tt.requestType, url)
}
})
}
}
func TestBuildImageURL(t *testing.T) {
c := New("test_key", "test_secret")
url, err := c.BuildImageURL("https://example.com", RequestOptions{"width": 1920})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL")
}
if !contains(url, string(RequestTypeImage)) {
t.Errorf("URL should contain image type, got %s", url)
}
}
func TestBuildPDFURL(t *testing.T) {
c := New("test_key", "test_secret")
url, err := c.BuildPDFURL("https://example.com", RequestOptions{"format": "A4"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL")
}
if !contains(url, string(RequestTypePDF)) {
t.Errorf("URL should contain PDF type, got %s", url)
}
}
func TestBuildContentURL(t *testing.T) {
c := New("test_key", "test_secret")
url, err := c.BuildContentURL("https://example.com", RequestOptions{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL")
}
if !contains(url, string(RequestTypeContent)) {
t.Errorf("URL should contain content type, got %s", url)
}
}
func TestBuildMetadataURL(t *testing.T) {
c := New("test_key", "test_secret")
url, err := c.BuildMetadataURL("https://example.com", RequestOptions{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL")
}
if !contains(url, string(RequestTypeMetadata)) {
t.Errorf("URL should contain metadata type, got %s", url)
}
}
func TestScreenshotOptions(t *testing.T) {
c := New("test_key", "test_secret")
// Test all valid screenshot options according to official docs
options := RequestOptions{
"url": "https://example.com",
"httpAuth": "base64url_encoded_auth",
"vw": 1440, // Viewport Width (default: 1440)
"vh": 900, // Viewport Height (default: 900)
"scaleFactor": 1, // Screen scale factor (default: 1)
"top": 0, // Top offset for clipping (default: 0)
"left": 0, // Left offset for clipping (default: 0)
"width": 1920, // Clipping Width (default: Viewport Width)
"height": 1080, // Clipping Height (default: Viewport Height)
"waitFor": ".selector", // Wait for CSS selector
"waitForId": "element-id", // Wait for element ID
"delay": 2, // Delay in seconds (default: 0)
"full": true, // Full page capture (default: false)
"darkMode": true, // Dark mode screenshot (default: false)
"blockCookieBanners": true, // Block cookie banners (default: false)
"blockAds": true, // Block ads (default: false)
"bypassBotDetection": true, // Bypass bot detection (default: false)
"selector": ".specific-element", // Screenshot specific element
"selectorId": "specific-id", // Screenshot element by ID
"transparent": true, // Transparent background (default: false)
"userAgent": "Custom User Agent", // Custom user agent
"timestamp": "1234567890", // Force reload
"fresh": true, // Fresh screenshot (default: false)
"resizeWidth": 800, // Resize width
"resizeHeight": 600, // Resize height
"fileName": "screenshot.png", // S3 file name
"s3Acl": "public-read", // S3 ACL
"s3Redirect": true, // Redirect to S3 URL (default: false)
"skipUpload": true, // Skip S3 upload (default: false)
"type": "png", // Image type: png, jpeg, webp (default: png)
"bestFormat": true, // Best format (default: false)
}
url, err := c.BuildImageURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error building screenshot URL: %v", err)
}
if url == "" {
t.Error("Expected non-empty screenshot URL")
}
// Verify URL contains the request type
if !contains(url, string(RequestTypeImage)) {
t.Errorf("URL should contain image type, got %s", url)
}
}
func TestPDFOptions(t *testing.T) {
c := New("test_key", "test_secret")
// Test all valid PDF options according to official docs
options := RequestOptions{
"url": "https://example.com",
"httpAuth": "base64url_encoded_auth",
"userAgent": "Custom User Agent",
"width": "8.5in", // Paper width with units
"height": "11in", // Paper height with units
"marginTop": "1in", // Top margin with units
"marginRight": "1in", // Right margin with units
"marginBottom": "1in", // Bottom margin with units
"marginLeft": "1in", // Left margin with units
"scale": 1, // Scale of webpage rendering (default: 1)
"landscape": true, // Paper orientation (default: false)
"delay": 2, // Delay in seconds (default: 0)
"timestamp": "1234567890", // Force reload
"format": "A4", // Paper format (default: A4)
"fileName": "document.pdf", // S3 file name
"s3Acl": "public-read", // S3 ACL
"s3Redirect": true, // Redirect to S3 URL (default: false)
}
url, err := c.BuildPDFURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error building PDF URL: %v", err)
}
if url == "" {
t.Error("Expected non-empty PDF URL")
}
// Verify URL contains the request type
if !contains(url, string(RequestTypePDF)) {
t.Errorf("URL should contain PDF type, got %s", url)
}
}
func TestContentOptions(t *testing.T) {
c := New("test_key", "test_secret")
// Test all valid content options according to official docs
options := RequestOptions{
"url": "https://example.com",
"httpAuth": "base64url_encoded_auth",
"userAgent": "Custom User Agent",
"delay": 2, // Delay in seconds (default: 0)
"waitFor": ".selector", // Wait for CSS selector
"waitForId": "element-id", // Wait for element ID
}
url, err := c.BuildContentURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error building content URL: %v", err)
}
if url == "" {
t.Error("Expected non-empty content URL")
}
// Verify URL contains the request type
if !contains(url, string(RequestTypeContent)) {
t.Errorf("URL should contain content type, got %s", url)
}
}
func TestMetadataOptions(t *testing.T) {
c := New("test_key", "test_secret")
// Test all valid metadata options according to official docs
// Metadata API only supports basic options
options := RequestOptions{
"url": "https://example.com",
"httpAuth": "base64url_encoded_auth",
"userAgent": "Custom User Agent",
"delay": 2, // Delay in seconds (default: 0)
"waitFor": ".selector", // Wait for CSS selector
"waitForId": "element-id", // Wait for element ID
}
url, err := c.BuildMetadataURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error building metadata URL: %v", err)
}
if url == "" {
t.Error("Expected non-empty metadata URL")
}
// Verify URL contains the request type
if !contains(url, string(RequestTypeMetadata)) {
t.Errorf("URL should contain metadata type, got %s", url)
}
}
func TestInvalidOptions(t *testing.T) {
c := New("test_key", "test_secret")
// Test that invalid options are handled gracefully
invalidOptions := RequestOptions{
"invalidOption": "value",
"anotherInvalid": 123,
}
url, err := c.BuildImageURL("https://example.com", invalidOptions)
if err != nil {
t.Errorf("Unexpected error with invalid options: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL even with invalid options")
}
}
func TestOptionTypeHandling(t *testing.T) {
c := New("test_key", "test_secret")
// Test different data types for options
options := RequestOptions{
"width": 1920, // int
"height": 1080, // int
"delay": 2.5, // float64
"full": true, // bool
"userAgent": "Custom Agent", // string
"scale": 1.0, // float64
"landscape": false, // bool
}
url, err := c.BuildImageURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error with mixed option types: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL with mixed option types")
}
}
func TestEmptyAndZeroValues(t *testing.T) {
c := New("test_key", "test_secret")
// Test that empty, zero, and false values are properly handled
options := RequestOptions{
"width": 0, // Should be included
"height": 0, // Should be included
"delay": 0, // Should be included
"full": false, // Should be included
"empty": "", // Should be excluded
"nil": nil, // Should be excluded
}
url, err := c.BuildImageURL("https://example.com", options)
if err != nil {
t.Errorf("Unexpected error with empty/zero values: %v", err)
}
if url == "" {
t.Error("Expected non-empty URL with empty/zero values")
}
// Verify that empty and nil values are excluded from query string
if contains(url, "empty=") {
t.Error("URL should not contain empty value")
}
if contains(url, "nil=") {
t.Error("URL should not contain nil value")
}
}
// Helper function to check if a string contains a substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > len(substr) && (s[:len(substr)] == substr ||
s[len(s)-len(substr):] == substr ||
func() bool {
for i := 1; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}())))
}