-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestfilter_test.go
More file actions
180 lines (165 loc) · 4.83 KB
/
requestfilter_test.go
File metadata and controls
180 lines (165 loc) · 4.83 KB
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
package requestfilter_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/aveq-research/requestfilter"
)
//nolint:gocognit
func TestRequestFilter(t *testing.T) {
tests := []struct {
name string
config *requestfilter.Config
method string
url string
body string
expectedStatus int
expectedBody string
expectedResponse string
}{
{
name: "No filter",
config: &requestfilter.Config{
FilterRegexes: []string{},
},
method: http.MethodGet,
url: "http://example.com",
expectedStatus: http.StatusOK,
expectedResponse: "",
},
{
name: "URL path filter - blocked",
config: &requestfilter.Config{
FilterRegexes: []string{"sensitive"},
},
method: http.MethodGet,
url: "http://example.com/sensitive-data",
expectedStatus: http.StatusForbidden,
expectedBody: "Request blocked\n",
},
{
name: "Body filter - POST blocked",
config: &requestfilter.Config{
FilterRegexes: []string{"confidential"},
},
method: http.MethodPost,
url: "http://example.com",
body: "This contains confidential information",
expectedStatus: http.StatusForbidden,
expectedBody: "Request blocked\n",
},
{
name: "Body filter - PATCH blocked",
config: &requestfilter.Config{
FilterRegexes: []string{"classified"},
},
method: http.MethodPatch,
url: "http://example.com",
body: "This is classified information",
expectedStatus: http.StatusForbidden,
expectedBody: "Request blocked\n",
},
{
name: "Custom error message",
config: &requestfilter.Config{
FilterRegexes: []string{"sensitive"},
HTTPErrorMessage: "Access Denied",
},
method: http.MethodGet,
url: "http://example.com/sensitive-data",
expectedStatus: http.StatusForbidden,
expectedBody: "Access Denied\n",
},
{
name: "PathOnly - body not checked",
config: &requestfilter.Config{
FilterRegexes: []string{"confidential"},
PathOnly: true,
},
method: http.MethodPost,
url: "http://example.com",
body: "This contains confidential information",
expectedStatus: http.StatusOK,
expectedResponse: "This contains confidential information",
},
{
name: "BodyOnly - path not checked",
config: &requestfilter.Config{
FilterRegexes: []string{"sensitive"},
BodyOnly: true,
},
method: http.MethodGet,
url: "http://example.com/sensitive-data",
expectedStatus: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.Body != nil {
body, _ := io.ReadAll(req.Body)
_, err := rw.Write(body)
if err != nil {
t.Fatal(err)
}
}
})
handler, err := requestfilter.New(ctx, next, tt.config, "request-filter")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
var req *http.Request
if tt.body != "" {
req, err = http.NewRequestWithContext(ctx, tt.method, tt.url, strings.NewReader(tt.body))
} else {
req, err = http.NewRequestWithContext(ctx, tt.method, tt.url, nil)
}
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
if recorder.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, recorder.Code)
}
if tt.expectedBody != "" && recorder.Body.String() != tt.expectedBody {
t.Errorf("Expected body '%s', got '%s'", tt.expectedBody, recorder.Body.String())
}
if tt.expectedResponse != "" && recorder.Body.String() != tt.expectedResponse {
t.Errorf("Expected response '%s', got '%s'", tt.expectedResponse, recorder.Body.String())
}
})
}
}
func TestCreateConfig(t *testing.T) {
cfg := requestfilter.CreateConfig()
if cfg == nil {
t.Error("CreateConfig returned nil")
}
if cfg != nil && len(cfg.FilterRegexes) != 0 {
t.Errorf("Expected empty FilterRegexes, got %v", cfg.FilterRegexes)
}
if cfg.HTTPErrorMessage != "" {
t.Errorf("Expected empty HttpErrorMessage, got %v", cfg.HTTPErrorMessage)
}
if cfg.PathOnly != false {
t.Errorf("Expected PathOnly to be false, got %v", cfg.PathOnly)
}
if cfg.BodyOnly != false {
t.Errorf("Expected BodyOnly to be false, got %v", cfg.BodyOnly)
}
}
func TestNewWithInvalidRegex(t *testing.T) {
cfg := requestfilter.CreateConfig()
cfg.FilterRegexes = []string{"["} // Invalid regex
ctx := context.Background()
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
_, err := requestfilter.New(ctx, next, cfg, "request-filter")
if err == nil {
t.Error("Expected error for invalid regex, got nil")
}
}