-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetLinks_test.go
188 lines (177 loc) · 5.68 KB
/
GetLinks_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
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
package katsuragi
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestGetLinks(t *testing.T) {
tests := []struct {
name string
category string
url string
responseBody func(serverURL string) string // Function to generate response body dynamically
expectedErr string
expectedLinks []string
}{
{
name: "all",
category: "all",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href="%s/internal1">Internal 1</a>
<a href="%s/internal2">Internal 2</a>
<a href="http://external.com">External</a>
</body></html>`, serverURL, serverURL)
},
expectedErr: "",
expectedLinks: []string{"<serverURL>/internal1", "<serverURL>/internal2", "http://external.com"},
},
{
name: "internal",
category: "internal",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href="%s/internal1">Internal 1</a>
<a href="%s/internal2">Internal 2</a>
</body></html>`, serverURL, serverURL)
},
expectedErr: "",
expectedLinks: []string{"<serverURL>/internal1", "<serverURL>/internal2"},
},
{
name: "external",
category: "external",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href="%s/internal1">Internal 1</a>
<a href="http://external.com">External</a>
</body></html>`, serverURL)
},
expectedErr: "",
expectedLinks: []string{"http://external.com"},
},
{
name: "no category",
category: "",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href="%s/internal1">Internal 1</a>
<a href="%s/internal2">Internal 2</a>
<a href="http://external.com">External</a>
</body></html>`, serverURL, serverURL)
},
expectedErr: "",
expectedLinks: []string{"<serverURL>/internal1", "<serverURL>/internal2", "http://external.com"},
},
// bad url
{
name: "bad url",
category: "all",
url: "http:/",
responseBody: func(serverURL string) string {
return ""
},
expectedErr: "Get \"http:/\": http: no Host in request URL",
expectedLinks: []string{},
},
// broken link in html
{
name: "good and invalid links in html",
category: "all",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href=":/|%s/internal1">Internal 1</a>
<a href=":htpexternal.com">External</a>
<a href="http://external2.com">External</a>
<a href="/test">External</a>
</body></html>`, serverURL)
},
expectedErr: "",
expectedLinks: []string{"http://external2.com", "<serverURL>/test"},
},
// no links in html
{
name: "no links in html",
category: "all",
responseBody: func(serverURL string) string {
return "<html><body></body></html>"
},
expectedErr: "GetTitle failed to find any links in HTML",
expectedLinks: []string{},
},
// unparsable links
{
name: "unparsable links",
category: "all",
responseBody: func(serverURL string) string {
return `<html><body>
<a href="http://[::1]:9999">Internal 1</a>
</body></html>`
},
expectedErr: "GetTitle failed to find any links in HTML",
expectedLinks: []string{},
},
// multiple level subdomains
{
name: "multiple level subdomains",
category: "all",
responseBody: func(serverURL string) string {
return fmt.Sprintf(`<html><body>
<a href="http://sub1.%s/internal1">Internal 1</a>
<a href="http://sub2.%s/internal2">Internal 2</a>
<a href="http://external.com">External</a>
<a href="http://sub3.sub2.%s/internal3">Internal 3</a>
</body></html>`, serverURL, serverURL, serverURL)
},
expectedErr: "",
expectedLinks: []string{"http://sub1.<serverURL>/internal1", "http://sub2.<serverURL>/internal2", "http://external.com", "http://sub3.sub2.<serverURL>/internal3"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var server *httptest.Server
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, tt.responseBody(server.URL))
}))
defer server.Close()
// Replace "<serverURL>" in expectedLinks with the actual server.URL before assertions
for i, link := range tt.expectedLinks {
tt.expectedLinks[i] = strings.Replace(link, "<serverURL>", server.URL, -1)
}
fetcher := NewFetcher(&FetcherProps{Timeout: 3000, CacheCap: 10})
var links []string
var err error
if tt.url != "" {
links, err = fetcher.GetLinks(GetLinksProps{Url: tt.url, Category: tt.category})
} else {
links, err = fetcher.GetLinks(GetLinksProps{Url: server.URL, Category: tt.category})
}
// Test assertions follow
if err != nil && tt.expectedErr == "" {
t.Errorf("Expected no error, got %v", err)
}
if err == nil && tt.expectedErr != "" {
t.Errorf("Expected error %v, got none", tt.expectedErr)
}
// compare errors
if err != nil && tt.expectedErr != "" {
if err.Error() != tt.expectedErr {
t.Errorf("Expected error %q, got %q", tt.expectedErr, err.Error())
}
}
if len(links) != len(tt.expectedLinks) {
t.Errorf("Expected %d links, got %d. Links: %s", len(tt.expectedLinks), len(links), links)
}
// compare expected links with actual links
if len(links) > 0 {
for i, link := range links {
if link != tt.expectedLinks[i] {
t.Errorf("Expected link %s, got %s", tt.expectedLinks[i], link)
}
}
}
})
}
}