-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_agent_policy_test.go
More file actions
214 lines (203 loc) · 6.49 KB
/
Copy pathuser_agent_policy_test.go
File metadata and controls
214 lines (203 loc) · 6.49 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package gatekeeper
import (
// Added import for log
"net/http"
"net/http/httptest" // Added import for strings
"testing"
)
func TestUserAgentPolicy(t *testing.T) {
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
tests := []struct {
name string
config UserAgentPolicyConfig
userAgentHeader string
expectedStatusCode int
expectBlocked bool
}{
// Blacklist Mode Tests
{
name: "Blacklist - Exact Match Block",
config: UserAgentPolicyConfig{
Mode: ModeBlacklist,
Exact: []string{"BadBot/1.0"},
},
userAgentHeader: "BadBot/1.0",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Blacklist - Exact Match Case Insensitive Block",
config: UserAgentPolicyConfig{
Mode: ModeBlacklist,
Exact: []string{"badbot/1.0"},
},
userAgentHeader: "BadBot/1.0",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Blacklist - Pattern Match Block",
config: UserAgentPolicyConfig{
Mode: ModeBlacklist,
Patterns: []string{`^EvilCorpBot/.*`},
},
userAgentHeader: "EvilCorpBot/2.1",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Blacklist - No Match Allow",
config: UserAgentPolicyConfig{
Mode: ModeBlacklist,
Exact: []string{"BadBot/1.0"},
},
userAgentHeader: "GoodBot/1.0",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
{
name: "Blacklist - Empty User-Agent Allow",
config: UserAgentPolicyConfig{
Mode: ModeBlacklist,
Exact: []string{"BadBot/1.0"},
},
userAgentHeader: "",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
// Whitelist Mode Tests
{
name: "Whitelist - Exact Match Allow",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Exact: []string{"GoodBot/1.0"},
},
userAgentHeader: "GoodBot/1.0",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
{
name: "Whitelist - Exact Match Case Insensitive Allow",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Exact: []string{"goodbot/1.0"},
},
userAgentHeader: "GoodBot/1.0",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
{
name: "Whitelist - Pattern Match Allow",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Patterns: []string{`^FriendlyBot/.*`},
},
userAgentHeader: "FriendlyBot/3.0",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
{
name: "Whitelist - No Exact Match Block",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Exact: []string{"GoodBot/1.0"},
},
userAgentHeader: "AnotherBot/1.0",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Whitelist - No Pattern Match Block",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Patterns: []string{`^FriendlyBot/.*`},
},
userAgentHeader: "UnknownBot/1.0",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Whitelist - Empty User-Agent Block",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Exact: []string{"GoodBot/1.0"},
},
userAgentHeader: "",
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Whitelist - Pattern Case Sensitive (User Defined)",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Patterns: []string{`^CaseSensitiveBot`}, // No (?i) flag
},
userAgentHeader: "casesensitivebot", // Should not match
expectedStatusCode: http.StatusForbidden,
expectBlocked: true,
},
{
name: "Whitelist - Pattern Case Insensitive (User Defined)",
config: UserAgentPolicyConfig{
Mode: ModeWhitelist,
Patterns: []string{`(?i)^CaseSensitiveBot`}, // With (?i) flag
},
userAgentHeader: "casesensitivebot",
expectedStatusCode: http.StatusOK,
expectBlocked: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conf := Config{ // Create a full Config object
UserAgentPolicy: &tt.config,
DefaultBlockStatusCode: http.StatusForbidden,
DefaultBlockMessage: "Blocked by Gatekeeper",
Logger: testLogger(t),
}
gk, err := New(conf) // Use New for initialization
if err != nil {
// If policy creation is expected to fail (e.g. no patterns/exacts), handle here
if !(len(tt.config.Exact) == 0 && len(tt.config.Patterns) == 0 && tt.config.Mode != "") {
// Allow error if mode is set but no rules (which New should catch)
// Or if specific tests expect an error from New()
} else if len(tt.config.Exact) == 0 && len(tt.config.Patterns) == 0 {
// This is a valid case for an error from newParsedUserAgentPolicy / New
// if the mode was specified. If no mode, it's a passthrough.
// The current tests don't explicitly test this path for successful policy creation.
} else {
t.Fatalf("New failed: %v", err)
}
}
// If UserAgentPolicyConfig is nil or has no rules, gk.parsedUserAgentPolicy might be nil.
// The UserAgentPolicy middleware method itself checks for nil gk.parsedUserAgentPolicy.
if gk.parsedUserAgentPolicy == nil && (len(tt.config.Exact) > 0 || len(tt.config.Patterns) > 0) {
t.Fatalf("gk.parsedUserAgentPolicy is nil even though config was provided with rules")
}
if gk.parsedUserAgentPolicy != nil && len(tt.config.Exact) == 0 && len(tt.config.Patterns) == 0 && tt.config.Mode != "" {
// If a mode was set, but no rules, the policy should ideally be nil or effectively a no-op
// This depends on New()'s behavior. For now, let's assume New() might create a non-nil policy
// that correctly handles no rules.
}
req := httptest.NewRequest("GET", "/", nil)
if tt.userAgentHeader != "" {
req.Header.Set("User-Agent", tt.userAgentHeader)
}
rr := httptest.NewRecorder()
handlerToTest := gk.UserAgentPolicy(dummyHandler)
handlerToTest.ServeHTTP(rr, req)
if rr.Code != tt.expectedStatusCode {
t.Errorf("Expected status code %d, got %d", tt.expectedStatusCode, rr.Code)
}
if tt.expectBlocked && rr.Body.String() == "OK" {
t.Errorf("Expected request to be blocked, but it was allowed.")
}
if !tt.expectBlocked && rr.Body.String() != "OK" {
t.Errorf("Expected request to be allowed, but it was blocked. Body: %s", rr.Body.String())
}
})
}
}