-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing_test.go
67 lines (60 loc) · 1.3 KB
/
parsing_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
package kraaler_test
import (
"net/url"
"testing"
"github.com/aau-network-security/kraaler"
)
func TestRetrieveLinks(t *testing.T) {
domain, _ := url.Parse("https://test.com")
tt := []struct {
name string
src string
urls []string
}{
{
name: "simple valid",
src: `<html><a href="https://google.com">t</a></html>`,
urls: []string{
"https://google.com",
},
},
{
name: "relative valid",
src: `<html><a href="/search">t</a></html>`,
urls: []string{
domain.String() + "/search",
},
},
{
name: "overlap valid",
src: `<html><a href="https://google.com">https://google.com</a></html>`,
urls: []string{
"https://google.com",
},
},
{
name: "empty",
src: "<html></html>",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
found, err := kraaler.RetrieveLinks(domain, []byte(tc.src))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if n := len(found); n != len(tc.urls) {
t.Fatalf("expected to find %d url(s), but found %d", len(tc.urls), n)
}
expectedUrls := map[string]bool{}
for _, link := range tc.urls {
expectedUrls[link] = true
}
for _, foundUrl := range found {
if ok := expectedUrls[foundUrl.String()]; !ok {
t.Fatalf("unexpected url: %s", foundUrl)
}
}
})
}
}