This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
forked from adhocteam/linkcheck
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkcheck_test.go
60 lines (53 loc) · 1.8 KB
/
linkcheck_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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRun(t *testing.T) {
// Silence during test
log.SetOutput(ioutil.Discard)
// Special for excluded path test
excludePaths = []string{"https://example.com/excluded-path"}
// Test server for our known sites
ts := httptest.NewServer(http.FileServer(http.Dir("test-fixtures/sample-site")))
defer ts.Close()
var testcases = []struct {
name string
base string
crawlers int
exitCode int
contains string
}{
{"basic failure", ts.URL + "/404", 1, 1, "/404: 404 Not Found"},
{"basic success", ts.URL + "/basic-a.html", 1, 0, ""},
{"more crawlers failure", ts.URL + "/404", 5, 1, "/404: 404 Not Found"},
{"more crawlers success", ts.URL + "/basic-a.html", 5, 0, ""},
{"circular success", ts.URL + "/circular-a.html", 1, 0, ""},
{"good external link", ts.URL + "/external-good.html", 1, 0, ""},
{"bad external link", ts.URL + "/external-bad.html", 1, 1,
"failed to fetch: https://example.com/404"},
{"good ID link", ts.URL + "/id-good-a.html", 1, 0, ""},
{"bad ID link", ts.URL + "/id-bad-a.html", 1, 1, "missing fragment"},
{"excluded path", ts.URL + "/excluded.html", 1, 0, ""},
}
for _, test := range testcases {
test := test
t.Run(test.name, func(t *testing.T) {
var buf bytes.Buffer
if exitCode := run(test.base, test.crawlers, &buf); exitCode != test.exitCode {
t.Errorf("Unexpected exit code. Got %d; expected %d.", exitCode, test.exitCode)
}
if output := buf.String(); test.contains == "" && output != "" {
t.Errorf("Unexpected output. Got %q; expected no output.", output)
} else if !strings.Contains(output, test.contains) {
t.Errorf("Output missing expected sequence. Got %q; expected to contain %q.",
output, test.contains)
}
})
}
}