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
/
Copy pathfilter_test.go
131 lines (118 loc) · 3.73 KB
/
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
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
package filter
import (
"errors"
"fmt"
"github.com/caddyserver/caddy/caddyhttp/fastcgi"
. "gopkg.in/check.v1"
"net/http"
"regexp"
)
type filterTest struct {
request *http.Request
writer *mockResponseWriter
nextHandler *mockHandler
handler *filterHandler
}
func init() {
Suite(&filterTest{})
}
func (s *filterTest) SetUpTest(c *C) {
s.request = &http.Request{
URL: testUrl1,
}
s.writer = newMockResponseWriter()
s.nextHandler = newMockHandler("Hello world!", 200)
s.handler = &filterHandler{
next: s.nextHandler,
rules: []*rule{
{
path: regexp.MustCompile(".*\\.html"),
searchPattern: regexp.MustCompile("w(.)rld"),
replacement: []byte("2nd is '{1}'"),
},
},
maximumBufferSize: defaultMaxBufferSize,
}
}
func (s *filterTest) Test_withFiltering(c *C) {
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, IsNil)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "Hello 2nd is 'o'!")
}
func (s *filterTest) Test_withBufferOverflow(c *C) {
s.handler.maximumBufferSize = 5
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, IsNil)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "Hello world!")
}
func (s *filterTest) Test_withoutFiltering(c *C) {
s.request.URL = testUrl2
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, IsNil)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "Hello world!")
}
func (s *filterTest) Test_withErrorInNext(c *C) {
s.nextHandler.error = errors.New("Oops")
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, DeepEquals, s.nextHandler.error)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "")
}
// This handles the bug https://github.com/echocat/caddy-filter/issues/4
// See filter.go for more details.
func (s *filterTest) Test_withLogErrorInNext(c *C) {
s.nextHandler.error = fastcgi.LogError("Oops")
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, DeepEquals, s.nextHandler.error)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "Hello 2nd is 'o'!")
}
func (s *filterTest) Test_withErrorInWriter(c *C) {
s.writer.error = errors.New("Oops")
status, err := s.handler.ServeHTTP(s.writer, s.request)
c.Assert(err, DeepEquals, s.writer.error)
c.Assert(status, Equals, 200)
c.Assert(s.writer.buffer.String(), Equals, "")
}
///////////////////////////////////////////////////////////////////////////////////////////
// MOCKS
///////////////////////////////////////////////////////////////////////////////////////////
func newMockHandler(response string, status int) *mockHandler {
result := new(mockHandler)
result.response = response
result.status = status
return result
}
type mockHandler struct {
response string
status int
error error
}
func (instance mockHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) (int, error) {
writer.WriteHeader(instance.status)
toReturn := []byte(instance.response)
if len(toReturn) < 2 {
return 0, fmt.Errorf("Response is too short: %v", toReturn)
}
middle := len(toReturn) / 2
part1 := toReturn[:middle]
part2 := toReturn[middle:]
written, err := writer.Write(part1)
if err != nil {
return 0, err
}
if len(part1) != written {
return 0, fmt.Errorf("Part 1 (%v) of response (%v) length was not written. Expected bytes written %v but got %v.", part1, toReturn, len(part1), written)
}
written, err = writer.Write(part2)
if err != nil {
return 0, err
}
if len(part2) != written {
return 0, fmt.Errorf("Part 2 (%v) of response (%v) length was not written. Expected bytes written %v but got %v.", part2, toReturn, len(part2), written)
}
return instance.status, instance.error
}