-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
119 lines (95 loc) · 2.82 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var serverURL string
func TestMain(m *testing.M) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
var URI string
if req.Method != "GET" {
panic(fmt.Errorf("unexpected request type %s", req.Method))
}
switch req.URL.String() {
case "/about":
URI = "stubs/site/about.html"
case "/contact":
URI = "stubs/site/contact.html"
case "/":
URI = "stubs/site/index.html"
default:
panic(fmt.Errorf("request domain %s not set up in test server", req.URL.String()))
}
body, err := ioutil.ReadFile(URI)
if err != nil {
panic(err)
}
res.Header().Set("Content-Type", "text/html")
_, err = res.Write(body)
if err != nil {
panic(err)
}
}))
serverURL = ts.URL
defer ts.Close()
os.Exit(m.Run())
}
func FindCrawlResultByRequestURL(results []CrawlResult, requestURL string) (CrawlResult, bool) {
for _, result := range results {
if result.requestURL == requestURL {
return result, true
}
}
return CrawlResult{}, false
}
func AssertStringSetsEqual(actual StringSet, expected StringSet, t *testing.T) {
if len(actual) != len(expected) {
t.Errorf("actual set size %d, expected %d", len(actual), len(expected))
}
for v := range expected {
_, ok := actual[v]
if !ok {
t.Errorf("actual set missing expected element %s", v)
}
}
}
func AssertEqualCrawlResults(actual []CrawlResult, expected []CrawlResult, t *testing.T) {
if len(actual) != len(expected) {
t.Errorf("got %d crawl results, expected %d", len(actual), len(expected))
}
for _, e := range expected {
a, ok := FindCrawlResultByRequestURL(actual, e.requestURL)
if !ok {
t.Errorf("expected crawl result with requestURL: %s", e.requestURL)
continue
}
AssertStringSetsEqual(a.externalLinks, e.externalLinks, t)
AssertStringSetsEqual(a.internalLinks, e.internalLinks, t)
}
}
func TestGivenValidDomain_WhenCrawl_ReturnsExpectedLinks(t *testing.T) {
fmt.Println("server URL: " + serverURL)
actualCrawlResults := Crawl(serverURL)
expectedResults := []CrawlResult{
{
requestURL: serverURL,
internalLinks: NewStringSet([]string{serverURL + "/about"}),
externalLinks: NewStringSet([]string{"https://monzo.com/contact", "https://monzo.com/about", "https://monzo.com/contact?name=Aran"}),
},
{
requestURL: serverURL + "/about",
internalLinks: NewStringSet([]string{serverURL, serverURL + "/contact"}),
externalLinks: NewStringSet([]string{"https://aran.site/hello/world", "https://aran.site/hello/world?name=aran"}),
},
{
requestURL: serverURL + "/contact",
internalLinks: NewStringSet([]string{serverURL}),
externalLinks: NewStringSet([]string{"https://youtube.com/hello/world"}),
},
}
AssertEqualCrawlResults(actualCrawlResults, expectedResults, t)
}