Skip to content

Commit

Permalink
e2e: add proxy protocol tests
Browse files Browse the repository at this point in the history
Enable proxy protocol by default in tests.
Add tests to check if XFF header is properly updated.
  • Loading branch information
mmatczuk committed Sep 18, 2024
1 parent d41d18a commit 62b7f95
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion e2e/forwarder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func ProxyService() *Service {
Name: ProxyServiceName,
Image: Image,
Environment: map[string]string{
"FORWARDER_API_ADDRESS": ":10000",
"FORWARDER_API_ADDRESS": ":10000",
"FORWARDER_PROXY_PROTOCOL_ENABLED": "true",
},
}
}
Expand Down
45 changes: 45 additions & 0 deletions e2e/tests/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/pires/go-proxyproto"
"github.com/saucelabs/forwarder/e2e/forwarder"
"github.com/saucelabs/forwarder/utils/httpexpect"
)
Expand Down Expand Up @@ -335,3 +336,47 @@ func TestProxyReuseConnection(t *testing.T) {
}
}
}

func TestProxyProxyProtocol(t *testing.T) {
if os.Getenv("HTTPBIN_PROTOCOL") != "http" {
t.Skip("HTTPBIN_PROTOCOL not set to http")
}

h := proxyproto.Header{
Version: 1,
Command: proxyproto.PROXY,
TransportProtocol: proxyproto.TCPv4,
SourceAddr: &net.TCPAddr{
IP: net.ParseIP("1.1.1.1"),
Port: 1000,
},
DestinationAddr: &net.TCPAddr{
IP: net.ParseIP("2.2.2.2"),
Port: 2000,
},
}

xff := newClient(t, httpbin, func(tr *http.Transport) {
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
if _, err := h.WriteTo(conn); err != nil {
return nil, err
}
return conn, nil
}
}).GET("/headers/").Header["X-Forwarded-For"]

if len(xff) == 0 {
t.Fatal("Expected X-Forwarded-For header, got none")
}

want := h.SourceAddr.(*net.TCPAddr).IP.String() //nolint: forcetypeassert // we know it's a TCPAddr

xff0, _, _ := strings.Cut(xff[0], ",")
if xff0 != want {
t.Fatalf("Expected %s, got %s", want, xff[0])
}
}

0 comments on commit 62b7f95

Please sign in to comment.