Remove User-Agent header removal from ReverseProxy director func

This commit is contained in:
Romain 2023-02-28 17:06:05 +01:00 committed by GitHub
parent 667b2a4078
commit 21c455cf20
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 8 deletions

View file

@ -55,10 +55,6 @@ func buildProxy(passHostHeader *bool, responseForwarding *dynamic.ResponseForwar
outReq.ProtoMajor = 1 outReq.ProtoMajor = 1
outReq.ProtoMinor = 1 outReq.ProtoMinor = 1
if _, ok := outReq.Header["User-Agent"]; !ok {
outReq.Header.Set("User-Agent", "")
}
// Do not pass client Host header unless optsetter PassHostHeader is set. // Do not pass client Host header unless optsetter PassHostHeader is set.
if passHostHeader != nil && !*passHostHeader { if passHostHeader != nil && !*passHostHeader {
outReq.Host = outReq.URL.Host outReq.Host = outReq.URL.Host

View file

@ -89,24 +89,34 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-From", "first") w.Header().Set("X-From", "first")
})) }))
defer server1.Close() t.Cleanup(server1.Close)
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-From", "second") w.Header().Set("X-From", "second")
})) }))
defer server2.Close() t.Cleanup(server2.Close)
serverPassHost := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { serverPassHost := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-From", "passhost") w.Header().Set("X-From", "passhost")
assert.Equal(t, "callme", r.Host) assert.Equal(t, "callme", r.Host)
})) }))
defer serverPassHost.Close() t.Cleanup(serverPassHost.Close)
serverPassHostFalse := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { serverPassHostFalse := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-From", "passhostfalse") w.Header().Set("X-From", "passhostfalse")
assert.NotEqual(t, "callme", r.Host) assert.NotEqual(t, "callme", r.Host)
})) }))
defer serverPassHostFalse.Close() t.Cleanup(serverPassHostFalse.Close)
hasNoUserAgent := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Empty(t, r.Header.Get("User-Agent"))
}))
t.Cleanup(hasNoUserAgent.Close)
hasUserAgent := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "foobar", r.Header.Get("User-Agent"))
}))
t.Cleanup(hasUserAgent.Close)
type ExpectedResult struct { type ExpectedResult struct {
StatusCode int StatusCode int
@ -122,6 +132,7 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
service *dynamic.ServersLoadBalancer service *dynamic.ServersLoadBalancer
responseModifier func(*http.Response) error responseModifier func(*http.Response) error
cookieRawValue string cookieRawValue string
userAgent string
expected []ExpectedResult expected []ExpectedResult
}{ }{
@ -286,6 +297,39 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
}, },
}, },
}, },
{
desc: "No user-agent",
serviceName: "test",
service: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: hasNoUserAgent.URL,
},
},
},
expected: []ExpectedResult{
{
StatusCode: http.StatusOK,
},
},
},
{
desc: "Custom user-agent",
serviceName: "test",
userAgent: "foobar",
service: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: hasUserAgent.URL,
},
},
},
expected: []ExpectedResult{
{
StatusCode: http.StatusOK,
},
},
},
} }
for _, test := range testCases { for _, test := range testCases {
@ -297,6 +341,12 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
assert.NotNil(t, handler) assert.NotNil(t, handler)
req := testhelpers.MustNewRequest(http.MethodGet, "http://callme", nil) req := testhelpers.MustNewRequest(http.MethodGet, "http://callme", nil)
assert.Equal(t, "", req.Header.Get("User-Agent"))
if test.userAgent != "" {
req.Header.Set("User-Agent", test.userAgent)
}
if test.cookieRawValue != "" { if test.cookieRawValue != "" {
req.Header.Set("Cookie", test.cookieRawValue) req.Header.Set("Cookie", test.cookieRawValue)
} }