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:
@@ -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