-
Notifications
You must be signed in to change notification settings - Fork 3
/
image_url_filter_test.go
99 lines (87 loc) · 3.17 KB
/
image_url_filter_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
package pipeline
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var (
ignoreHosts = []string{"*.ruby-china.com", "ruby-china.org", "localhost"}
)
func Test_ImageURLFilter_isHost(t *testing.T) {
hostsWillIgnore := []string{
"https://ruby-china.com/foo.jpg",
"https://www.ruby-china.com/foo.jpg",
"https://www.Ruby-china.com/foo.jpg",
"https://Ruby-china.org/foo.jpg",
"https://www.Ruby-china.org/foo.jpg",
"https://localhost/foo.jpg",
"https://localhost:3000/foo.jpg",
}
for _, host := range hostsWillIgnore {
if !isHost(ignoreHosts, host) {
t.Errorf("%s not match", host)
}
}
hostsWillNotIgnore := []string{
"https://baidu.com/foo.jpg",
"https://aaa.com/foo.jpg",
}
for _, host := range hostsWillNotIgnore {
if isHost(ignoreHosts, host) {
t.Errorf("%s matched, but not expected", host)
}
}
}
func Test_ImageURLFilter(t *testing.T) {
pipe := NewPipeline([]Filter{
ImageURLFilter{
IgnoreHosts: ignoreHosts,
Format: func(src string) string {
return fmt.Sprintf("https://imageproxy.ruby-china.com/%s", src)
},
},
})
html := `
<p>Hello <img src="https://ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://www.ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://fooo.ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://www.ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://l.ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://localhost/test/image.jpg"/></p>
<p>Hello <img src="https://localhost:3000/test/image.jpg"/></p>
<p>Hello <img src="https://file.github.com/test/image.jpg"/></p>
<p>Hello <img src="https://f.google.com/test/image.jpg"/></p>
<p>Hello <img data-src="https://f.google.com/test/image.jpg"/></p>
<p>Hello <img src="/foo/bar.jpg" data-src="https://f.google.com/test/image.jpg"/></p>
`
expected := `
<p>Hello <img src="https://ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://www.ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://fooo.ruby-china.com/test/image.jpg"/></p>
<p>Hello <img src="https://ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://www.ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://l.ruby-china.org/test/image.jpg"/></p>
<p>Hello <img src="https://localhost/test/image.jpg"/></p>
<p>Hello <img src="https://localhost:3000/test/image.jpg"/></p>
<p>Hello <img src="https://imageproxy.ruby-china.com/https://file.github.com/test/image.jpg"/></p>
<p>Hello <img src="https://imageproxy.ruby-china.com/https://f.google.com/test/image.jpg"/></p>
<p>Hello <img data-src="https://f.google.com/test/image.jpg"/></p>
<p>Hello <img src="/foo/bar.jpg" data-src="https://f.google.com/test/image.jpg"/></p>
`
out, err := pipe.Call(html)
assert.NoError(t, err)
assertHTMLEqual(t, expected, out)
// Match and ignore
pipe = NewPipeline([]Filter{
ImageURLFilter{
MatchHosts: []string{"github.com", "google.com"},
Format: func(src string) string {
return fmt.Sprintf("https://imageproxy.ruby-china.com/%s", src)
},
},
})
out, err = pipe.Call(html)
assert.NoError(t, err)
assertHTMLEqual(t, expected, out)
}