Commit 12438f6c authored by comp500's avatar comp500 Committed by Łukasz Nowak

Add more tests and comments

parent e28adf23
......@@ -394,9 +394,11 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer,
for ruleField, ruleValues := range replacements {
for _, ruleValue := range ruleValues {
// Replace variables in replacement string
replacement := repl.Replace(ruleValue.to)
original := headers.Get(ruleField)
if len(replacement) > 0 && len(original) > 0 {
// Replace matches in original string with replacement string
replaced := ruleValue.regexp.ReplaceAllString(original, replacement)
headers.Set(ruleField, replaced)
}
......
......@@ -78,11 +78,14 @@ type srvResolver interface {
LookupSRV(context.Context, string, string, string) (string, []*net.SRV, error)
}
// headerReplacement stores a compiled regex matcher and a string replacer, for replacement rules
type headerReplacement struct {
regexp *regexp.Regexp
to string
}
// headerReplacements stores a mapping of canonical MIME header to headerReplacement
// Implements a subset of http.Header functions, to allow convenient addition and deletion of rules
type headerReplacements map[string][]headerReplacement
func (h headerReplacements) Add(key string, value headerReplacement) {
......
......@@ -325,6 +325,61 @@ func TestParseBlockTransparent(t *testing.T) {
}
}
func TestParseBlockRegex(t *testing.T) {
// tests for regex replacement of headers
r, _ := http.NewRequest("GET", "/", nil)
tests := []struct {
config string
}{
// Test #1: transparent preset with replacement of Host
{"proxy / localhost:8080 {\n transparent \nheader_upstream Host (.*) NewHost \n}"},
// Test #2: transparent preset with replacement of another param
{"proxy / localhost:8080 {\n transparent \nheader_upstream X-Test Tester \nheader_upstream X-Test Test Host \n}"},
// Test #3: transparent preset with multiple params
{"proxy / localhost:8080 {\n transparent \nheader_upstream X-Test Tester \nheader_upstream X-Test Test Host \nheader_upstream X-Test er ing \n}"},
}
for i, test := range tests {
upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config)), "")
if err != nil {
t.Errorf("Expected no error. Got: %s", err.Error())
}
for _, upstream := range upstreams {
headers := upstream.Select(r).UpstreamHeaderReplacements
switch i {
case 0:
if host, ok := headers["Host"]; !ok || host[0].to != "NewHost" {
t.Errorf("Test %d: Incorrect Host replacement: %v", i+1, host[0])
}
case 1:
if v, ok := headers["X-Test"]; !ok {
t.Errorf("Test %d: Incorrect X-Test replacement", i+1)
} else {
if v[0].to != "Host" {
t.Errorf("Test %d: Incorrect X-Test replacement: %v", i+1, v[0])
}
}
case 2:
if v, ok := headers["X-Test"]; !ok {
t.Errorf("Test %d: Incorrect X-Test replacement", i+1)
} else {
if v[0].to != "Host" {
t.Errorf("Test %d: Incorrect X-Test replacement: %v", i+1, v[0])
}
if v[1].to != "ing" {
t.Errorf("Test %d: Incorrect X-Test replacement: %v", i+1, v[1])
}
}
default:
t.Error("Testing error")
}
}
}
}
func TestHealthSetUp(t *testing.T) {
// tests for insecure skip verify
tests := []struct {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment