|
1 | 1 | package reverseproxy |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "reflect" |
7 | 8 | "testing" |
8 | 9 |
|
| 10 | + "github.com/caddyserver/caddy/v2" |
9 | 11 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
10 | 12 | ) |
11 | 13 |
|
@@ -115,3 +117,81 @@ func TestHTTPTransport_RequestHeaderOps_TLS(t *testing.T) { |
115 | 117 | t.Fatalf("unexpected Host value; want placeholder, got: %s", got) |
116 | 118 | } |
117 | 119 | } |
| 120 | + |
| 121 | +// TestHTTPTransport_DialTLSContext_ProxyProtocol verifies that when TLS and |
| 122 | +// ProxyProtocol are both enabled, DialTLSContext is set. This is critical because |
| 123 | +// ProxyProtocol modifies req.URL.Host to include client info with "->" separator |
| 124 | +// (e.g., "[2001:db8::1]:12345->127.0.0.1:443"), which breaks Go's address parsing. |
| 125 | +// Without a custom DialTLSContext, Go's HTTP library would fail with |
| 126 | +// "too many colons in address" when trying to parse the mangled host. |
| 127 | +func TestHTTPTransport_DialTLSContext_ProxyProtocol(t *testing.T) { |
| 128 | + ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 129 | + defer cancel() |
| 130 | + |
| 131 | + tests := []struct { |
| 132 | + name string |
| 133 | + tls *TLSConfig |
| 134 | + proxyProtocol string |
| 135 | + serverNameHasPlaceholder bool |
| 136 | + expectDialTLSContext bool |
| 137 | + }{ |
| 138 | + { |
| 139 | + name: "no TLS, no proxy protocol", |
| 140 | + tls: nil, |
| 141 | + proxyProtocol: "", |
| 142 | + expectDialTLSContext: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "TLS without proxy protocol", |
| 146 | + tls: &TLSConfig{}, |
| 147 | + proxyProtocol: "", |
| 148 | + expectDialTLSContext: false, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "TLS with proxy protocol v1", |
| 152 | + tls: &TLSConfig{}, |
| 153 | + proxyProtocol: "v1", |
| 154 | + expectDialTLSContext: true, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "TLS with proxy protocol v2", |
| 158 | + tls: &TLSConfig{}, |
| 159 | + proxyProtocol: "v2", |
| 160 | + expectDialTLSContext: true, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "TLS with placeholder ServerName", |
| 164 | + tls: &TLSConfig{ServerName: "{http.request.host}"}, |
| 165 | + proxyProtocol: "", |
| 166 | + serverNameHasPlaceholder: true, |
| 167 | + expectDialTLSContext: true, |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "TLS with placeholder ServerName and proxy protocol", |
| 171 | + tls: &TLSConfig{ServerName: "{http.request.host}"}, |
| 172 | + proxyProtocol: "v2", |
| 173 | + serverNameHasPlaceholder: true, |
| 174 | + expectDialTLSContext: true, |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + for _, tt := range tests { |
| 179 | + t.Run(tt.name, func(t *testing.T) { |
| 180 | + ht := &HTTPTransport{ |
| 181 | + TLS: tt.tls, |
| 182 | + ProxyProtocol: tt.proxyProtocol, |
| 183 | + } |
| 184 | + |
| 185 | + rt, err := ht.NewTransport(ctx) |
| 186 | + if err != nil { |
| 187 | + t.Fatalf("NewTransport() error = %v", err) |
| 188 | + } |
| 189 | + |
| 190 | + hasDialTLSContext := rt.DialTLSContext != nil |
| 191 | + if hasDialTLSContext != tt.expectDialTLSContext { |
| 192 | + t.Errorf("DialTLSContext set = %v, want %v", hasDialTLSContext, tt.expectDialTLSContext) |
| 193 | + } |
| 194 | + }) |
| 195 | + } |
| 196 | +} |
| 197 | + |
0 commit comments