This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
rule_test.go
115 lines (92 loc) · 3.03 KB
/
rule_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
package filter
import (
. "gopkg.in/check.v1"
"net/http"
"net/url"
"regexp"
)
var (
testUrl1, _ = url.ParseRequestURI("http://foo.bar/my/path.html")
testUrl2, _ = url.ParseRequestURI("http://foo.bar/my/path.txt")
)
type ruleTest struct{}
func init() {
Suite(&ruleTest{})
}
func (s *ruleTest) Test_matches_path(c *C) {
req := &http.Request{}
r := &rule{
path: regexp.MustCompile(".*\\.html"),
}
req.URL = testUrl1
c.Assert(r.matches(req, nil), Equals, true)
req.URL = testUrl2
c.Assert(r.matches(req, nil), Equals, false)
}
func (s *ruleTest) Test_matches_contentType(c *C) {
header := http.Header{}
r := &rule{
contentType: regexp.MustCompile("text/html.*"),
}
header.Set("Content-Type", "text/html")
c.Assert(r.matches(nil, &header), Equals, true)
header.Set("Content-Type", "text/plain")
c.Assert(r.matches(nil, &header), Equals, false)
header.Del("Content-Type")
c.Assert(r.matches(nil, &header), Equals, false)
}
func (s *ruleTest) Test_matches_and_combined(c *C) {
req := &http.Request{}
header := http.Header{}
r := &rule{
path: regexp.MustCompile(".*\\.html"),
contentType: regexp.MustCompile("text/html.*"),
pathAndContentTypeCombination: pathAndContentTypeAndCombination,
}
req.URL = testUrl1
header.Set("Content-Type", "text/html")
c.Assert(r.matches(req, &header), Equals, true)
c.Assert(r.matches(nil, &header), Equals, false)
c.Assert(r.matches(req, nil), Equals, false)
req.URL = testUrl2
c.Assert(r.matches(req, &header), Equals, false)
req.URL = testUrl1
header.Set("Content-Type", "text/plain")
c.Assert(r.matches(req, &header), Equals, false)
}
func (s *ruleTest) Test_matches_or_combined(c *C) {
req := &http.Request{}
header := http.Header{}
r := &rule{
path: regexp.MustCompile(".*\\.html"),
contentType: regexp.MustCompile("text/html.*"),
pathAndContentTypeCombination: pathAndContentTypeOrCombination,
}
req.URL = testUrl1
header.Set("Content-Type", "text/html")
c.Assert(r.matches(req, &header), Equals, true)
c.Assert(r.matches(nil, &header), Equals, true)
c.Assert(r.matches(req, nil), Equals, true)
c.Assert(r.matches(nil, nil), Equals, false)
req.URL = testUrl2
c.Assert(r.matches(req, &header), Equals, true)
req.URL = testUrl1
header.Set("Content-Type", "text/plain")
c.Assert(r.matches(req, &header), Equals, true)
req.URL = testUrl2
c.Assert(r.matches(req, &header), Equals, false)
}
func (s *ruleTest) Test_execute(c *C) {
req := &http.Request{}
header := http.Header{}
header.Set("Server", "Caddy")
r := &rule{
searchPattern: regexp.MustCompile("My name is (.*?)\\."),
replacement: []byte("Hi {1}! The name of this server is {response_header_Server}."),
}
result := r.execute(req, &header, []byte("Hello I'am a test.\nMy name is Test_execute."))
c.Assert(string(result), Equals, "Hello I'am a test.\nHi Test_execute! The name of this server is Caddy.")
r.searchPattern = nil
result = r.execute(req, &header, []byte("foobar"))
c.Assert(string(result), Equals, "foobar")
}