-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage_proxy_filter_test.go
75 lines (67 loc) · 2.53 KB
/
image_proxy_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
package pipeline
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var (
imageProxyFilter = ImageProxyFilter{
IgnoreHosts: []string{"*.ruby-china.com", "ruby-china.org", "localhost"},
Formatter: func(src string) string {
return fmt.Sprintf("https://imageproxy.ruby-china.com/%s", src)
},
}
)
func Test_ImageProxyFilter_isIgnoreHost(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 {
assert.Equal(t, true, imageProxyFilter.IsIgnoreHost(host))
}
hostsWillNotIgnore := []string{
"https://baidu.com/foo.jpg",
"https://aaa.com/foo.jpg",
}
for _, host := range hostsWillNotIgnore {
assert.Equal(t, false, imageProxyFilter.IsIgnoreHost(host))
}
}
func Test_ImageProxyFilter(t *testing.T) {
pipe := NewPipeline([]Filter{
imageProxyFilter,
})
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>
`
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>
`
out, err := pipe.Call(html)
assert.NoError(t, err)
assertHTMLEqual(t, expected, out)
}