-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_crawl_test.go
More file actions
666 lines (593 loc) · 17.9 KB
/
browser_crawl_test.go
File metadata and controls
666 lines (593 loc) · 17.9 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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package inspect
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/GrayCodeAI/inspect/internal/crawler"
)
func TestBrowserOpts_Defaults(t *testing.T) {
opts := BrowserOpts{}
if opts.Viewport.Width != 0 {
t.Error("expected zero default width")
}
if opts.InjectAxe {
t.Error("expected InjectAxe false by default")
}
if opts.Screenshot {
t.Error("expected Screenshot false by default")
}
}
func TestBrowserOpts_ConfigureAll(t *testing.T) {
opts := BrowserOpts{
Viewport: Viewport{
Width: 1920,
Height: 1080,
Mobile: false,
},
WaitFor: "#content",
Timeout: 15 * time.Second,
InjectAxe: true,
Screenshot: true,
UserAgent: "test-bot/1.0",
}
if opts.Viewport.Width != 1920 || opts.Viewport.Height != 1080 {
t.Error("unexpected viewport dimensions")
}
if opts.WaitFor != "#content" {
t.Errorf("expected WaitFor '#content', got %q", opts.WaitFor)
}
if opts.Timeout != 15*time.Second {
t.Errorf("expected 15s timeout, got %v", opts.Timeout)
}
if !opts.InjectAxe {
t.Error("expected InjectAxe true")
}
if !opts.Screenshot {
t.Error("expected Screenshot true")
}
if opts.UserAgent != "test-bot/1.0" {
t.Errorf("expected UserAgent 'test-bot/1.0', got %q", opts.UserAgent)
}
}
func TestViewport_Mobile(t *testing.T) {
v := Viewport{Width: 375, Height: 812, Mobile: true}
if !v.Mobile {
t.Error("expected Mobile true")
}
if v.Width != 375 || v.Height != 812 {
t.Error("unexpected mobile viewport dimensions")
}
}
func TestPageData_Fields(t *testing.T) {
data := &PageData{
FinalURL: "https://example.com/page",
Title: "Test Page",
RenderedHTML: "<html><body>rendered</body></html>",
AccessTree: []AXNode{
{Role: "heading", Name: "Main Title", Properties: map[string]string{"level": "1"}},
{Role: "link", Name: "About", Properties: map[string]string{}},
},
AxeViolations: []AxeViolation{
{
ID: "color-contrast",
Impact: "serious",
Description: "Elements must have sufficient color contrast",
Help: "Ensure contrast ratio meets WCAG AA",
HelpURL: "https://dequeuniversity.com/rules/axe/4.7/color-contrast",
Nodes: []AxeNode{
{
HTML: "<span style='color:#aaa'>low contrast</span>",
Target: []string{".low-contrast"},
FailureSummary: "Element has insufficient color contrast",
},
},
},
},
ConsoleErrors: []string{"TypeError: undefined is not a function"},
NetworkLog: []NetworkEntry{
{URL: "https://example.com/app.js", Method: "GET", Status: 200, MimeType: "application/javascript", Size: 1024, Duration: 50 * time.Millisecond},
{URL: "https://example.com/missing.css", Method: "GET", Status: 404, Failed: true, FailReason: "Not Found"},
},
Screenshot: []byte{0x89, 0x50, 0x4E, 0x47}, // PNG magic bytes
LoadTime: 1500 * time.Millisecond,
}
if data.FinalURL != "https://example.com/page" {
t.Errorf("unexpected FinalURL: %s", data.FinalURL)
}
if data.Title != "Test Page" {
t.Errorf("unexpected Title: %s", data.Title)
}
if len(data.AccessTree) != 2 {
t.Errorf("expected 2 access tree nodes, got %d", len(data.AccessTree))
}
if data.AccessTree[0].Role != "heading" {
t.Errorf("expected heading role, got %q", data.AccessTree[0].Role)
}
if len(data.AxeViolations) != 1 {
t.Errorf("expected 1 axe violation, got %d", len(data.AxeViolations))
}
if data.AxeViolations[0].ID != "color-contrast" {
t.Errorf("expected color-contrast violation, got %q", data.AxeViolations[0].ID)
}
if len(data.ConsoleErrors) != 1 {
t.Errorf("expected 1 console error, got %d", len(data.ConsoleErrors))
}
if len(data.NetworkLog) != 2 {
t.Errorf("expected 2 network entries, got %d", len(data.NetworkLog))
}
if !data.NetworkLog[1].Failed {
t.Error("expected second network entry to be failed")
}
if data.LoadTime != 1500*time.Millisecond {
t.Errorf("unexpected load time: %v", data.LoadTime)
}
}
func TestAXNode_Nested(t *testing.T) {
node := AXNode{
Role: "navigation",
Name: "Main Navigation",
Children: []AXNode{
{Role: "link", Name: "Home"},
{Role: "link", Name: "About"},
{
Role: "menu",
Name: "More",
Children: []AXNode{
{Role: "menuitem", Name: "Settings"},
},
},
},
}
if len(node.Children) != 3 {
t.Fatalf("expected 3 children, got %d", len(node.Children))
}
if node.Children[2].Children[0].Role != "menuitem" {
t.Errorf("expected nested menuitem, got %q", node.Children[2].Children[0].Role)
}
}
func TestAxeViolation_MultipleNodes(t *testing.T) {
v := AxeViolation{
ID: "image-alt",
Impact: "critical",
Nodes: []AxeNode{
{HTML: "<img src='a.jpg'>", Target: []string{"#img1"}, FailureSummary: "Missing alt"},
{HTML: "<img src='b.jpg'>", Target: []string{"#img2"}, FailureSummary: "Missing alt"},
{HTML: "<img src='c.jpg'>", Target: []string{"#img3"}, FailureSummary: "Missing alt"},
},
}
if len(v.Nodes) != 3 {
t.Errorf("expected 3 nodes, got %d", len(v.Nodes))
}
}
func TestNetworkEntry_FailedWithReason(t *testing.T) {
entry := NetworkEntry{
URL: "https://example.com/resource",
Method: "GET",
Status: 0,
Failed: true,
FailReason: "net::ERR_CONNECTION_REFUSED",
Duration: 5 * time.Second,
}
if !entry.Failed {
t.Error("expected Failed true")
}
if entry.FailReason != "net::ERR_CONNECTION_REFUSED" {
t.Errorf("unexpected fail reason: %s", entry.FailReason)
}
if entry.Status != 0 {
t.Errorf("expected status 0 for failed request, got %d", entry.Status)
}
}
// TestBrowserCrawler_LinkDiscovery tests link extraction from HTML that a
// browser crawler would render, using the real crawler infrastructure.
func TestBrowserCrawler_LinkDiscovery(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Home</title></head>
<body>
<nav>
<a href="/about">About</a>
<a href="/products">Products</a>
<a href="/contact">Contact</a>
<a href="https://external.example.com">External</a>
</nav>
<main>
<h1>Welcome</h1>
<img src="/hero.jpg" alt="Hero image">
<script src="/app.js"></script>
</main>
</body>
</html>`)
})
mux.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>About</title></head>
<body>
<h1>About Us</h1>
<a href="/">Home</a>
<a href="/team">Our Team</a>
</body>
</html>`)
})
mux.HandleFunc("/products", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Products</title></head>
<body>
<h1>Products</h1>
<a href="/">Home</a>
<a href="/products/widget">Widget</a>
</body>
</html>`)
})
mux.HandleFunc("/contact", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Contact</title></head>
<body>
<h1>Contact</h1>
<form action="/submit" method="POST">
<input name="email" type="email" required>
<input name="_csrf" type="hidden" value="token123">
<button type="submit">Send</button>
</form>
</body>
</html>`)
})
mux.HandleFunc("/team", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Team</title></head>
<body><h1>Our Team</h1></body>
</html>`)
})
mux.HandleFunc("/products/widget", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Widget</title></head>
<body><h1>Widget</h1><a href="/products">Back to Products</a></body>
</html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 2,
Concurrency: 2,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
if len(pages) < 3 {
t.Errorf("expected at least 3 pages, got %d", len(pages))
}
// Verify link extraction worked on crawled pages
totalLinks := 0
for _, p := range pages {
totalLinks += len(p.Links)
}
if totalLinks < 5 {
t.Errorf("expected at least 5 total links across all pages, got %d", totalLinks)
}
}
// TestBrowserCrawler_FormDetection tests that forms are detected from
// HTML responses during crawling.
func TestBrowserCrawler_FormDetection(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head><title>Login</title></head>
<body>
<form action="/login" method="POST" id="login-form">
<input name="username" type="text" required>
<input name="password" type="password" required>
<input name="_csrf" type="hidden" value="abc123">
<button type="submit">Login</button>
</form>
<form action="/search" method="GET">
<input name="q" type="text">
<button type="submit">Search</button>
</form>
</body>
</html>`)
})
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<html><body>Logged in</body></html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 1,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
if len(pages) == 0 {
t.Fatal("expected at least 1 page")
}
// Find the home page and check forms
homePage := pages[0]
if len(homePage.Forms) != 2 {
t.Fatalf("expected 2 forms on home page, got %d", len(homePage.Forms))
}
// Check CSRF detection
foundCSRF := false
for _, f := range homePage.Forms {
if f.HasCSRF {
foundCSRF = true
}
}
if !foundCSRF {
t.Error("expected at least one form with CSRF token")
}
// Check form methods
methods := map[string]bool{}
for _, f := range homePage.Forms {
methods[f.Method] = true
}
if !methods["POST"] || !methods["GET"] {
t.Error("expected both POST and GET forms")
}
}
// TestBrowserCrawler_RedirectHandling tests that the crawler properly
// follows redirects and records final status codes.
func TestBrowserCrawler_RedirectHandling(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Home</title></head>
<body><a href="/old-page">Old Page</a></body></html>`)
})
mux.HandleFunc("/old-page", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/new-page", http.StatusMovedPermanently)
})
mux.HandleFunc("/new-page", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>New Page</title></head>
<body><h1>Moved here</h1></body></html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 2,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
FollowRedirects: 5,
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
// Should have encountered the redirect page
foundOldPage := false
for _, p := range pages {
if p.URL == srv.URL+"/old-page" {
foundOldPage = true
// The crawler follows redirects, so we should see the final URL
// or the redirect status code
}
if p.URL == srv.URL+"/new-page" {
foundOldPage = true
}
}
if !foundOldPage {
// At minimum, the crawler should have discovered the old-page link
t.Error("expected to encounter /old-page or /new-page in crawl results")
}
}
// TestBrowserCrawler_NonHTMLIgnored tests that non-HTML resources are
// recorded but not followed for links.
func TestBrowserCrawler_NonHTMLIgnored(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Home</title></head>
<body><a href="/image.png">Image</a></body></html>`)
})
mux.HandleFunc("/image.png", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write([]byte{0x89, 0x50, 0x4E, 0x47})
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 2,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
// Find the image page
for _, p := range pages {
if p.URL == srv.URL+"/image.png" {
if len(p.Links) != 0 {
t.Error("non-HTML page should have no extracted links")
}
if p.StatusCode != 200 {
t.Errorf("expected 200 for image, got %d", p.StatusCode)
}
}
}
}
// TestBrowserCrawler_ExcludePatterns tests URL exclusion during crawling.
func TestBrowserCrawler_ExcludePatterns(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Home</title></head>
<body>
<a href="/admin">Admin</a>
<a href="/public">Public</a>
</body></html>`)
})
mux.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Admin</title></head>
<body><h1>Admin Panel</h1></body></html>`)
})
mux.HandleFunc("/public", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Public</title></head>
<body><h1>Public Page</h1></body></html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 2,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
Exclude: []string{"/admin"},
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
for _, p := range pages {
if p.URL == srv.URL+"/admin" {
t.Error("should not have crawled excluded /admin page")
}
}
}
// TestBrowserCrawler_EmptyPage tests crawling a page with no links or forms.
func TestBrowserCrawler_EmptyPage(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html><html><head><title>Empty</title></head>
<body><h1>Nothing here</h1></body></html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 1,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
if len(pages) != 1 {
t.Fatalf("expected exactly 1 page, got %d", len(pages))
}
if len(pages[0].Links) != 0 {
t.Errorf("expected 0 links on empty page, got %d", len(pages[0].Links))
}
if len(pages[0].Forms) != 0 {
t.Errorf("expected 0 forms on empty page, got %d", len(pages[0].Forms))
}
}
// TestBrowserCrawler_MixedContent tests pages with various HTML elements
// including images, iframes, and scripts.
func TestBrowserCrawler_MixedContent(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head>
<title>Mixed Content</title>
<link rel="stylesheet" href="/style.css">
<script src="/app.js"></script>
</head>
<body>
<h1>Page with Mixed Content</h1>
<a href="/page2">Link</a>
<img src="/photo.jpg" alt="Photo">
<iframe src="/embed"></iframe>
<video>
<track src="/subs.vtt" kind="subtitles">
</video>
</body>
</html>`)
})
mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<html><head><title>Page 2</title></head><body>Page 2</body></html>`)
})
mux.HandleFunc("/embed", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `<html><body>Embedded content</body></html>`)
})
srv := httptest.NewServer(mux)
defer srv.Close()
c := crawler.New(crawler.Config{
MaxDepth: 2,
Concurrency: 1,
Timeout: 10 * time.Second,
PageTimeout: 5 * time.Second,
RateLimit: 100,
UserAgent: "inspect-test",
AllowPrivateIPs: true,
})
pages, err := c.Crawl(context.Background(), srv.URL)
if err != nil {
t.Fatalf("Crawl failed: %v", err)
}
if len(pages) < 2 {
t.Errorf("expected at least 2 pages, got %d", len(pages))
}
// Home page should have many links (a, img, iframe, track, script)
homePage := pages[0]
hasImg := false
hasScript := false
for _, l := range homePage.Links {
if l.Tag == "img" {
hasImg = true
}
if l.Tag == "script" {
hasScript = true
}
}
if !hasImg {
t.Error("expected img link on home page")
}
if !hasScript {
t.Error("expected script link on home page")
}
}