Commit 7497cb26 authored by comp500's avatar comp500 Committed by Łukasz Nowak

Add tests, fix a few bugs

parent b2e0264f
......@@ -232,7 +232,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// headers coming back downstream
var downHeaderUpdateFn respUpdateFn
if host.DownstreamHeaders != nil {
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer, host.UpstreamHeaderReplacements)
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer, host.DownstreamHeaderReplacements)
}
// Before we retry the request we have to make sure
......
......@@ -31,6 +31,7 @@ import (
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
......@@ -724,6 +725,14 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
"Clear-Me": {""},
"Host": {"{>Host}"},
}
regex1, _ := regexp.Compile("was originally")
regex2, _ := regexp.Compile("this")
regex3, _ := regexp.Compile("bad")
upstream.host.UpstreamHeaderReplacements = headerReplacements{
"Regex-Me": {headerReplacement{regex1, "am now"}, headerReplacement{regex2, "that"}},
"Regexreplace-Me": {headerReplacement{regex3, "{hostname}"}},
}
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
......@@ -740,6 +749,8 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
r.Header.Add("Remove-Me", "Remove-Value")
r.Header.Add("Replace-Me", "Replace-Value")
r.Header.Add("Host", expectHost)
r.Header.Add("Regex-Me", "I was originally this")
r.Header.Add("Regexreplace-Me", "The host is bad")
p.ServeHTTP(w, r)
......@@ -752,6 +763,8 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
"Remove-Me": nil,
"Replace-Me": {replacer.Replace("{hostname}")},
"Clear-Me": nil,
"Regex-Me": {"I am now that"},
"Regexreplace-Me": {"The host is " + replacer.Replace("{hostname}")},
} {
if got := actualHeaders[headerKey]; !reflect.DeepEqual(got, expect) {
t.Errorf("Upstream request does not contain expected %v header: expect %v, but got %v",
......@@ -775,6 +788,8 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
w.Header().Add("Replace-Me", "Replace-Value")
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Overwrite-Me", "Overwrite-Value")
w.Header().Add("Regex-Me", "I was originally this")
w.Header().Add("Regexreplace-Me", "The host is bad")
w.Write([]byte("Hello, client"))
}))
defer backend.Close()
......@@ -786,6 +801,13 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
"-Remove-Me": {""},
"Replace-Me": {"{hostname}"},
}
regex1, _ := regexp.Compile("was originally")
regex2, _ := regexp.Compile("this")
regex3, _ := regexp.Compile("bad")
upstream.host.DownstreamHeaderReplacements = headerReplacements{
"Regex-Me": {headerReplacement{regex1, "am now"}, headerReplacement{regex2, "that"}},
"Regexreplace-Me": {headerReplacement{regex3, "{hostname}"}},
}
// set up proxy
p := &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
......@@ -812,6 +834,8 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
"Replace-Me": {replacer.Replace("{hostname}")},
"Content-Type": {"text/css"},
"Overwrite-Me": {"Overwrite-Value"},
"Regex-Me": {"I am now that"},
"Regexreplace-Me": {"The host is " + replacer.Replace("{hostname}")},
} {
if got := actualHeaders[headerKey]; !reflect.DeepEqual(got, expect) {
t.Errorf("Downstream response does not contain expected %s header: expect %v, but got %v",
......
......@@ -461,7 +461,7 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
}
u.upstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} else {
if !c.Args(&header, &value) {
if len(value) == 0 {
// When removing a header, the value can be optional.
if !strings.HasPrefix(header, "-") {
return c.ArgErr()
......@@ -480,9 +480,9 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
if err != nil {
return c.ArgErr()
}
u.upstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
u.downstreamHeaderReplacements.Add(header, headerReplacement{r, replaced})
} else {
if !c.Args(&header, &value) {
if len(value) == 0 {
// When removing a header, the value can be optional.
if !strings.HasPrefix(header, "-") {
return c.ArgErr()
......
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