diff --git a/caddyhttp/proxy/upstream.go b/caddyhttp/proxy/upstream.go index 4dc78e82..00d23e31 100644 --- a/caddyhttp/proxy/upstream.go +++ b/caddyhttp/proxy/upstream.go @@ -260,6 +260,10 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error { return c.ArgErr() } u.downstreamHeaders.Add(header, value) + case "transparent": + u.upstreamHeaders.Add("Host", "{host}") + u.upstreamHeaders.Add("X-Real-IP", "{remote}") + u.upstreamHeaders.Add("X-Forwarded-Proto", "{scheme}") case "websocket": u.upstreamHeaders.Add("Connection", "{>Connection}") u.upstreamHeaders.Add("Upgrade", "{>Upgrade}") diff --git a/caddyhttp/proxy/upstream_test.go b/caddyhttp/proxy/upstream_test.go index 9d38b785..0378475f 100644 --- a/caddyhttp/proxy/upstream_test.go +++ b/caddyhttp/proxy/upstream_test.go @@ -1,8 +1,11 @@ package proxy import ( + "strings" "testing" "time" + + "github.com/mholt/caddy/caddyfile" ) func TestNewHost(t *testing.T) { @@ -133,3 +136,40 @@ func TestAllowedPaths(t *testing.T) { } } } + +func TestParseBlock(t *testing.T) { + tests := []struct { + config string + }{ + // Test #1: transparent preset + {"proxy / localhost:8080 {\n transparent \n}"}, + + // Test #2: transparent preset with another param + {"proxy / localhost:8080 {\n transparent \nproxy_header X-Test Tester \n}"}, + + // Test #3: transparent preset on multiple sites + {"proxy / localhost:8080 {\n transparent \n} \nproxy /api localhost:8081 { \ntransparent \n}"}, + } + + for i, test := range tests { + upstreams, err := NewStaticUpstreams(caddyfile.NewDispenser("Testfile", strings.NewReader(test.config))) + if err != nil { + t.Error("Expected no error. Got:", err.Error()) + } + for _, upstream := range upstreams { + headers := upstream.Select().UpstreamHeaders + + if _, ok := headers["Host"]; !ok { + t.Errorf("Test %d: Could not find the Host header", i+1) + } + + if _, ok := headers["X-Real-Ip"]; !ok { + t.Errorf("Test %d: Could not find the X-Real-Ip header", i+1) + } + + if _, ok := headers["X-Forwarded-Proto"]; !ok { + t.Errorf("Test %d: Could not find the X-Forwarded-Proto header", i+1) + } + } + } +}