-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawler_test.go
58 lines (49 loc) · 1.43 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
package crawler
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/yashmurty/go-web-crawler/mock"
)
func TestCrawl(t *testing.T) {
mockedStore := &mock.Store{}
mockedFetcher := &mock.Fetcher{}
done := make(chan bool)
var parentURLs []string
var parentURL string
go Crawl("https://golang.org/", parentURL, parentURLs, 4, mockedFetcher, mockedStore, done)
<-done
if len(mockedStore.MockedStore) != 5 {
t.Fatalf("Unique urls stored %d does not match the expected value of %d",
len(mockedStore.MockedStore), 5)
}
if mockedFetcher.FetchFnCalled != 5 {
t.Fatalf("Unique urls fetched %d does not match the expected value of %d",
mockedFetcher.FetchFnCalled, 5)
}
}
func TestExtractHost(t *testing.T) {
t.Run("should succeed to extract the url", func(t *testing.T) {
scheme := "https"
host := "www.google.com"
url := scheme + "://" + host
extractedScheme, extractedHost := ExtractHost(url)
if extractedScheme != scheme {
t.Fatalf("Scheme %s does not match extracted value %s",
scheme, extractedScheme)
}
if extractedHost != host {
t.Fatalf("Host %s does not match extracted value %s",
host, extractedHost)
}
})
t.Run("should fail due to incorrect scheme", func(t *testing.T) {
require.Panics(t, func() {
ExtractHost("asd://www.google.com")
})
})
t.Run("should fail due to incorrect host", func(t *testing.T) {
require.Panics(t, func() {
ExtractHost("http://@")
})
})
}