forked from raviqqe/liche
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl_checker_test.go
106 lines (83 loc) · 2.7 KB
/
url_checker_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
package main
import (
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestURLCheckerCheck(t *testing.T) {
c := newURLChecker(0, "", nil, newSemaphore(1024))
for _, u := range []string{"https://google.com", "README.md"} {
assert.Equal(t, nil, c.Check(u, "README.md"))
}
assert.NotEqual(t, nil, c.Check("http://www.google.com/README-I-DONT-EXIST.md", "README.md"))
for _, u := range []string{"https://hey-hey-hi-google.com", "READYOU.md", "://"} {
assert.NotEqual(t, nil, c.Check(u, "README.md"))
}
}
func TestURLCheckerCheckWithExclude(t *testing.T) {
c := newURLChecker(0, "", regexp.MustCompile(`^http:\/\/localhost:[13]$`), newSemaphore(1024))
for _, u := range []string{"http://localhost:1", "http://localhost:3", "README.md"} {
assert.Equal(t, nil, c.Check(u, "README.md"))
}
for _, u := range []string{"http://localhost:2", "READYOU.md"} {
assert.NotEqual(t, nil, c.Check(u, "README.md"))
}
}
func TestURLCheckerCheckWithTimeout(t *testing.T) {
c := newURLChecker(30*time.Second, "", nil, newSemaphore(1024))
for _, u := range []string{"https://google.com", "README.md"} {
assert.Equal(t, nil, c.Check(u, "README.md"))
}
for _, u := range []string{"https://hey-hey-hi-google.com", "READYOU.md", "://"} {
assert.NotEqual(t, nil, c.Check(u, "README.md"))
}
}
func TestURLCheckerCheckMany(t *testing.T) {
c := newURLChecker(0, "", nil, newSemaphore(1024))
for _, us := range [][]string{{}, {"https://google.com", "README.md"}} {
rc := make(chan urlResult, 1024)
c.CheckMany(us, "README.md", rc)
for r := range rc {
assert.NotEqual(t, "", r.url)
assert.Equal(t, nil, r.err)
}
}
}
func TestURLCheckerResolveURL(t *testing.T) {
f := newURLChecker(0, "", nil, newSemaphore(1024))
for _, c := range []struct {
source, target string
local bool
}{
{"foo", "foo", true},
{"https://google.com", "https://google.com", false},
} {
u, local, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
assert.Equal(t, c.local, local)
}
}
func TestURLCheckerResolveURLWithAbsolutePath(t *testing.T) {
f := newURLChecker(0, "", nil, newSemaphore(1024))
u, _, err := f.resolveURL("/foo", "foo.md")
assert.NotEqual(t, nil, err)
assert.Equal(t, "", u)
}
func TestURLCheckerResolveURLWithDocumentRoot(t *testing.T) {
f := newURLChecker(0, "foo", nil, newSemaphore(1024))
for _, c := range []struct {
source, target string
local bool
}{
{"foo", "foo", true},
{"https://google.com", "https://google.com", false},
{"/foo", "foo/foo", true},
} {
u, local, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
assert.Equal(t, c.local, local)
}
}