package main import ( "net" "net/http" "net/http/httptest" "net/url" "testing" "github.com/gin-gonic/gin" ) func TestProxyAPIRequestUsesConfiguredPort(t *testing.T) { backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"path":"` + r.URL.RequestURI() + `"}`)) })) defer backend.Close() backendURL, err := url.Parse(backend.URL) if err != nil { t.Fatal(err) } _, port, err := net.SplitHostPort(backendURL.Host) if err != nil { t.Fatal(err) } gin.SetMode(gin.TestMode) router := gin.New() router.Any("/api/*path", proxyAPIRequest(port)) recorder := httptest.NewRecorder() router.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/config?custom=1", nil)) if recorder.Code != http.StatusOK || recorder.Body.String() != `{"path":"/api/config?custom=1"}` { t.Fatalf("unexpected proxy response: status=%d body=%s", recorder.Code, recorder.Body.String()) } }