This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
crawler_test.go
66 lines (55 loc) · 2.11 KB
/
crawler_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
package main
import (
"strconv"
"testing"
)
func TestExtractUrlsWithNoUrls(t *testing.T) {
var content = "<html><title>My WebSite</title><body>'tevrevtertretretretretretrezecnhfze</body></html>"
urls := extractUrls("", []byte(content))
if len(urls) != 0 {
t.Errorf("Urls found but no one exist")
}
}
func TestExtractUrlsWithNonOnionUrls(t *testing.T) {
var content = "<html><title>My WebSite</title><body>'tevhttp://IamTheBest.de/tretretrezecnhfze<div>https://creekorful.me</div></body></html>"
urls := extractUrls("", []byte(content))
if len(urls) != 0 {
t.Errorf("Urls found but shouldn't because non .onion URLs")
}
}
func TestExtractUrlsWithOnionUrls(t *testing.T) {
var content = "<html><title>My WebSite</title><body>'tevhttp://IamTheBest.onion/tretretrezecnhfze<div>https://creekorful.me</div>https://efezfezf.onion</body></html>"
urls := extractUrls("", []byte(content))
if len(urls) != 2 {
t.Errorf("More or less than 2 urls found")
return
}
if url := urls[0]; url != "http://IamTheBest.onion/tretretrezecnhfze" {
t.Errorf("Urls does match http://IamTheBest.onion/tretretrezecnhfze (value: " + url + ")")
return
}
if url := urls[1]; url != "https://efezfezf.onion" {
t.Errorf("Urls does match https://efezfezf.onion (value: " + url + ")")
return
}
}
func TestExtractUrlsWithRelativeUrls(t *testing.T) {
var content = "<a href=\"https://google.com\"></a><a href=\"http://google.onion\"></a><a href=\"the-best/page.img\"></a><a href=\"/the-best/page.iso\"></a>"
urls := extractUrls("http://mysite.onion", []byte(content))
if len(urls) != 3 {
t.Errorf("More or less than 3 urls found (" + strconv.Itoa(len(urls)) + ")")
return
}
if url := urls[0]; url != "http://google.onion" {
t.Errorf("Urls does match http://google.onion (value: " + url + ")")
return
}
if url := urls[1]; url != "http://mysite.onion/the-best/page.img" {
t.Errorf("Urls does match http://mysite.onion/the-best/page.img (value: " + url + ")")
return
}
if url := urls[2]; url != "http://mysite.onion/the-best/page.iso" {
t.Errorf("Urls does match http://mysite.onion/the-best/page.iso (value: " + url + ")")
return
}
}