Refactor reverse proxy tests to utilize context-aware requests. Updated reverse_test.go to create HTTP requests using context, improving test reliability and aligning with best practices for request handling. This change ensures that tests are more robust and less prone to issues related to request context.
This commit is contained in:
@@ -24,7 +24,7 @@ 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 = ""
|
||||
req.Host = req.URL.Host
|
||||
return
|
||||
}
|
||||
rest := strings.TrimPrefix(p, stripPrefix)
|
||||
@@ -35,10 +35,9 @@ func NewReverseProxy(target *url.URL, stripPrefix, pathPrefix string, setAuth st
|
||||
req.URL.Path = joined.Path
|
||||
req.URL.RawPath = joined.RawPath
|
||||
req.URL.Opaque = ""
|
||||
// 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 = ""
|
||||
// Match Host header to authority; clear stale map entry (e.g. from httptest.NewRequest).
|
||||
req.Header.Del("Host")
|
||||
req.Host = req.URL.Host
|
||||
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -17,8 +18,11 @@ func TestReverseProxyPathRewrite(t *testing.T) {
|
||||
defer srv.Close()
|
||||
up, _ := url.Parse(srv.URL)
|
||||
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
|
||||
// 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)
|
||||
// Client-style request (no RequestURI / server quirks from httptest.NewRequest).
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/api/main_srv/health", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -39,7 +43,10 @@ func TestReverseProxyPathRewriteWithAPIBasePath(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rp := NewReverseProxy(up, "/api/main_srv", "/v1", "")
|
||||
req := httptest.NewRequest(http.MethodGet, srv.URL+"/api/main_srv/health", nil)
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/api/main_srv/health", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
@@ -60,7 +67,10 @@ func TestReverseProxyPathNestedStatsUsers(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rp := NewReverseProxy(up, "/api/gt2", "/v1", "")
|
||||
req := httptest.NewRequest(http.MethodGet, srv.URL+"/api/gt2/stats/users", nil)
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, srv.URL+"/api/gt2/stats/users", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
rp.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
|
||||
Reference in New Issue
Block a user