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
|
package rules
import (
"net/http"
"testing"
)
func TestHasBodyRules(t *testing.T) {
tests := []struct {
name string
rs []Rule
want bool
}{
{"empty", nil, false},
{"header rule only", []Rule{{Enabled: true, Part: "header"}}, false},
{"body rule enabled", []Rule{{Enabled: true, Part: "body"}}, true},
{"body rule disabled", []Rule{{Enabled: false, Part: "body"}}, false},
{"mixed, one enabled body rule", []Rule{
{Enabled: true, Part: "header"},
{Enabled: true, Part: "body"},
}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := HasBodyRules(tt.rs); got != tt.want {
t.Errorf("HasBodyRules(%+v) = %v, want %v", tt.rs, got, tt.want)
}
})
}
}
func TestApplyBodyLiteral(t *testing.T) {
body := []byte(`{"admin":false,"user":"bob"}`)
rs := []Rule{{Enabled: true, Part: "body", Match: `"admin":false`, Replace: `"admin":true`}}
got := ApplyBody(body, rs)
want := `{"admin":true,"user":"bob"}`
if string(got) != want {
t.Errorf("ApplyBody = %q, want %q", got, want)
}
}
func TestApplyBodyRegex(t *testing.T) {
body := []byte(`user_id=12345&session=abc`)
rs := []Rule{{Enabled: true, Part: "body", Match: `user_id=\d+`, Replace: "user_id=1", IsRegex: true}}
got := ApplyBody(body, rs)
want := `user_id=1&session=abc`
if string(got) != want {
t.Errorf("ApplyBody = %q, want %q", got, want)
}
}
func TestApplyBodySkipsDisabledAndHeaderRules(t *testing.T) {
body := []byte("hello world")
rs := []Rule{
{Enabled: false, Part: "body", Match: "hello", Replace: "bye"},
{Enabled: true, Part: "header", Match: "world", Replace: "planet"},
}
got := ApplyBody(body, rs)
if string(got) != "hello world" {
t.Errorf("ApplyBody = %q, want unchanged %q", got, "hello world")
}
}
func TestApplyBodyRespectsPositionOrder(t *testing.T) {
body := []byte("aaa")
rs := []Rule{
{Enabled: true, Part: "body", Match: "a", Replace: "b", Position: 1},
{Enabled: true, Part: "body", Match: "b", Replace: "c", Position: 0},
}
// Position 0 runs first: "aaa" -[b->c, no match]-> "aaa" -[a->b]-> "bbb"
got := ApplyBody(body, rs)
if string(got) != "bbb" {
t.Errorf("ApplyBody = %q, want %q", got, "bbb")
}
}
func TestApplyHeadersAddsRemovesHeader(t *testing.T) {
h := http.Header{"Host": []string{"example.com"}}
rs := []Rule{{Enabled: true, Part: "header", Match: "Host: example.com", Replace: "Host: example.com\r\nX-Injected: 1"}}
got := ApplyHeaders(h, rs)
if got.Get("X-Injected") != "1" {
t.Errorf("expected X-Injected header to be added, got %+v", got)
}
}
func TestApplyHeadersInvalidRegexLeavesUnchanged(t *testing.T) {
h := http.Header{"Host": []string{"example.com"}}
rs := []Rule{{Enabled: true, Part: "header", Match: "(unclosed", Replace: "x", IsRegex: true}}
got := ApplyHeaders(h, rs)
if got.Get("Host") != "example.com" {
t.Errorf("expected headers unchanged on invalid regex, got %+v", got)
}
}
|