Update reverse proxy tests to use absolute URLs for requests. Modified reverse_test.go to ensure that requests are constructed with the server's URL, preventing issues with the default "example.com" host in httptest. This change enhances the reliability of the tests by ensuring correct request handling.
This commit is contained in:
@@ -23,6 +23,8 @@ func NewReverseProxy(target *url.URL, stripPrefix, pathPrefix string, setAuth st
|
||||
}
|
||||
// Server-side requests carry RequestURI; client RoundTrip rejects it with URL.Host set.
|
||||
req.RequestURI = ""
|
||||
req.Header.Del("Host")
|
||||
req.Host = ""
|
||||
return
|
||||
}
|
||||
rest := strings.TrimPrefix(p, stripPrefix)
|
||||
@@ -33,8 +35,10 @@ func NewReverseProxy(target *url.URL, stripPrefix, pathPrefix string, setAuth st
|
||||
req.URL.Path = joined.Path
|
||||
req.URL.RawPath = joined.RawPath
|
||||
req.URL.Opaque = ""
|
||||
// Client must send the same Host as the TLS SNI / nginx server_name.
|
||||
req.Host = joined.Host
|
||||
// Let net/http use req.URL.Host for the Host header and TLS SNI.
|
||||
// Clear stale Host (e.g. httptest.NewRequest uses "example.com" for relative targets).
|
||||
req.Host = ""
|
||||
req.Header.Del("Host")
|
||||
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||
} else {
|
||||
|
||||
@@ -17,7 +17,8 @@ func TestReverseProxyPathRewrite(t *testing.T) {
|
||||
defer srv.Close()
|
||||
up, _ := url.Parse(srv.URL)
|
||||
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/main_srv/health", nil)
|
||||
// Absolute URL: httptest relative targets set Host to "example.com", which breaks RoundTrip to srv.
|
||||
req := httptest.NewRequest(http.MethodGet, srv.URL+"/api/main_srv/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -38,7 +39,7 @@ func TestReverseProxyPathRewriteWithAPIBasePath(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/main_srv/health", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, srv.URL+"/api/main_srv/health", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -59,7 +60,7 @@ func TestReverseProxyPathNestedStatsUsers(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rp := NewReverseProxy(up, "/api/gt2", "/v1", "")
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/gt2/stats/users", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, srv.URL+"/api/gt2/stats/users", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
|
||||
Reference in New Issue
Block a user