forked from echocat/caddy-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_test.go
203 lines (175 loc) · 8.61 KB
/
init_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package filter
import (
"errors"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
. "gopkg.in/check.v1"
"regexp"
"regexp/syntax"
)
type initTest struct{}
func init() {
Suite(&initTest{})
}
func (s *initTest) Test_setup(c *C) {
controller := s.newControllerFor("filter rule {\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\nreplacement myReplacement\n}\n")
err := setup(controller)
c.Assert(err, IsNil)
config := httpserver.GetConfig(controller)
middlewares := config.Middleware()
c.Assert(len(middlewares), Equals, 1)
handler, ok := middlewares[0](newMockHandler("moo", 200)).(*filterHandler)
c.Assert(ok, Equals, true)
c.Assert(len(handler.rules), Equals, 1)
r := handler.rules[0]
c.Assert(r.path.String(), Equals, "myPath")
c.Assert(r.contentType.String(), Equals, "myContentType")
c.Assert(r.pathAndContentTypeCombination, Equals, pathAndContentTypeAndCombination)
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern")
c.Assert(string(r.replacement), Equals, "myReplacement")
}
func (s *initTest) Test_parseConfiguration_withPathAndContentTypeCombination(c *C) {
controller := s.newControllerFor("filter rule {\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\nreplacement myReplacement\n}\n")
err := setup(controller)
c.Assert(err, IsNil)
handler, ok := httpserver.GetConfig(controller).Middleware()[0](newMockHandler("moo", 200)).(*filterHandler)
c.Assert(ok, Equals, true)
c.Assert(len(handler.rules), Equals, 1)
c.Assert(handler.rules[0].pathAndContentTypeCombination, Equals, pathAndContentTypeAndCombination)
controller = s.newControllerFor("filter rule {\npath_content_type_combination or\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\n}\n")
err = setup(controller)
c.Assert(err, IsNil)
handler, ok = httpserver.GetConfig(controller).Middleware()[0](newMockHandler("moo", 200)).(*filterHandler)
c.Assert(ok, Equals, true)
c.Assert(len(handler.rules), Equals, 1)
c.Assert(handler.rules[0].pathAndContentTypeCombination, Equals, pathAndContentTypeOrCombination)
controller = s.newControllerFor("filter rule {\npath_content_type_combination and\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\n}\n")
err = setup(controller)
c.Assert(err, IsNil)
handler, ok = httpserver.GetConfig(controller).Middleware()[0](newMockHandler("moo", 200)).(*filterHandler)
c.Assert(ok, Equals, true)
c.Assert(len(handler.rules), Equals, 1)
c.Assert(handler.rules[0].pathAndContentTypeCombination, Equals, pathAndContentTypeAndCombination)
controller = s.newControllerFor("filter rule {\npath_content_type_combination foo\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\n}\n")
err = setup(controller)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Testfile:2 - Error during parsing: Illegal value for 'path_content_type_combination': foo")
}
func (s *initTest) Test_parseConfiguration_directNamed(c *C) {
handler, err := parseConfiguration(s.newControllerFor("filter rule {\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\nreplacement myReplacement\n}\n"))
c.Assert(err, IsNil)
c.Assert(len(handler.rules), Equals, 1)
r := handler.rules[0]
c.Assert(r.path.String(), Equals, "myPath")
c.Assert(r.contentType.String(), Equals, "myContentType")
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern")
c.Assert(string(r.replacement), Equals, "myReplacement")
handler, err = parseConfiguration(s.newControllerFor(
"filter rule {\npath myPath\nsearch_pattern mySearchPattern\n}\n" +
"filter rule {\npath myPath2\nsearch_pattern mySearchPattern2\n}\n" +
"filter max_buffer_size 666\n"),
)
c.Assert(err, IsNil)
c.Assert(len(handler.rules), Equals, 2)
r = handler.rules[0]
c.Assert(r.path.String(), Equals, "myPath")
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern")
r = handler.rules[1]
c.Assert(r.path.String(), Equals, "myPath2")
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern2")
c.Assert(handler.maximumBufferSize, Equals, 666)
}
func (s *initTest) Test_parseConfiguration_withReplacementFromFile(c *C) {
handler, err := parseConfiguration(s.newControllerFor("filter rule {\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\nreplacement @resources/test/testReplacement\n}\n"))
c.Assert(err, IsNil)
c.Assert(len(handler.rules), Equals, 1)
r := handler.rules[0]
c.Assert(r.path.String(), Equals, "myPath")
c.Assert(r.contentType.String(), Equals, "myContentType")
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern")
c.Assert(string(r.replacement), Equals, "Replacement from file.\n")
}
func (s *initTest) Test_evalSimpleOption(c *C) {
err := evalSimpleOption(s.newControllerFor("\"my value\""), func(value string) error {
c.Assert(value, Equals, "my value")
return nil
})
c.Assert(err, IsNil)
err = evalSimpleOption(s.newControllerFor(""), func(value string) error {
c.Error("This method should not be called.")
return nil
})
c.Assert(err, DeepEquals, errors.New("Testfile:1 - Error during parsing: Wrong argument count or unexpected line ending after 'start'"))
}
func (s *initTest) Test_evalRegexpOption(c *C) {
err := evalRegexpOption(s.newControllerFor("f.*bar"), func(value *regexp.Regexp) error {
c.Assert(value.MatchString("foobar"), Equals, true)
return nil
})
c.Assert(err, IsNil)
err = evalRegexpOption(s.newControllerFor("<???"), func(value *regexp.Regexp) error {
c.Error("This method should not be called.")
return nil
})
c.Assert(err, DeepEquals, &syntax.Error{Code: "invalid nested repetition operator", Expr: "???"})
}
func (s *initTest) Test_evalPath(c *C) {
r := new(rule)
err := evalPath(s.newControllerFor("f.*bar"), r)
c.Assert(err, IsNil)
c.Assert(r.path.String(), Equals, "f.*bar")
}
func (s *initTest) Test_evalContentType(c *C) {
r := new(rule)
err := evalContentType(s.newControllerFor("f.*bar"), r)
c.Assert(err, IsNil)
c.Assert(r.contentType.String(), Equals, "f.*bar")
}
func (s *initTest) Test_searchPattern(c *C) {
r := new(rule)
err := evalSearchPattern(s.newControllerFor("f.*bar"), r)
c.Assert(err, IsNil)
c.Assert(r.searchPattern.String(), Equals, "f.*bar")
}
func (s *initTest) Test_evalReplacement(c *C) {
r := new(rule)
err := evalReplacement(s.newControllerFor("foobar"), r)
c.Assert(err, IsNil)
c.Assert(string(r.replacement), Equals, "foobar")
}
func (s *initTest) Test_evalRule(c *C) {
handler := new(filterHandler)
err := evalRule(s.newControllerFor("{\npath myPath\ncontent_type myContentType\nsearch_pattern mySearchPattern\nreplacement myReplacement\n}\n"), []string{}, handler)
c.Assert(err, IsNil)
c.Assert(len(handler.rules), Equals, 1)
r := handler.rules[0]
c.Assert(r.path.String(), Equals, "myPath")
c.Assert(r.contentType.String(), Equals, "myContentType")
c.Assert(r.searchPattern.String(), Equals, "mySearchPattern")
c.Assert(string(r.replacement), Equals, "myReplacement")
err = evalRule(s.newControllerFor("{\nfoo bar\n}\n"), []string{}, handler)
c.Assert(err, DeepEquals, errors.New("Testfile:2 - Error during parsing: Unknown option: foo"))
err = evalRule(s.newControllerFor("{\n}\n"), []string{}, handler)
c.Assert(err, DeepEquals, errors.New("Testfile:2 - Error during parsing: Neither 'path' nor 'content_type' definition was provided for filter rule block."))
err = evalRule(s.newControllerFor("{\npath myPath\n}\n"), []string{}, handler)
c.Assert(err, DeepEquals, errors.New("Testfile:3 - Error during parsing: No 'search_pattern' definition was provided for filter rule block."))
err = evalRule(s.newControllerFor(""), []string{"foo"}, handler)
c.Assert(err, DeepEquals, errors.New("Testfile:1 - Error during parsing: No more arguments for filter block 'rule' supported."))
}
func (s *initTest) Test_evalMaximumBufferSize(c *C) {
handler := new(filterHandler)
err := evalMaximumBufferSize(s.newControllerFor(""), []string{"123"}, handler)
c.Assert(err, IsNil)
c.Assert(handler.maximumBufferSize, Equals, 123)
err = evalMaximumBufferSize(s.newControllerFor(""), []string{}, handler)
c.Assert(err, DeepEquals, errors.New("Testfile:1 - Error during parsing: There are exact one argument for filter directive 'max_buffer_size' expected."))
err = evalMaximumBufferSize(s.newControllerFor(""), []string{"abc"}, handler)
c.Assert(err, ErrorMatches, "Testfile:1 - Error during parsing: There is no valid value for filter directive 'max_buffer_size' provided. Got: strconv.(ParseInt|Atoi): parsing \"abc\": invalid syntax")
}
func (s *initTest) newControllerFor(plainTokens string) *caddy.Controller {
controller := caddy.NewTestController("http", "start "+plainTokens)
if !controller.Next() {
panic("There must be an entry.")
}
return controller
}