package proxy import ( "context" "net/http" "net/http/httptest" "net/url" "testing" ) func TestMihomoForwardRewritesPathAndAuth(t *testing.T) { target, err := url.Parse("http://127.0.0.1:9090") if err != nil { t.Fatal(err) } cap := &captureTransport{} h := NewMihomoForward(target, "/api/mtg/mihomo", "Bearer testsecret", cap, nil) req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://gw/api/mtg/mihomo/proxies", nil) if err != nil { t.Fatal(err) } req.Header.Set("Authorization", "Bearer client-should-not-forward") req.Header.Set("Host", "public-gateway.example:8888") h.ServeHTTP(httptest.NewRecorder(), req) if cap.got == nil { t.Fatal("no outgoing request captured") } if got, want := cap.got.Host, "127.0.0.1:9090"; got != want { t.Fatalf("Host: got %q want %q (upstream must not see client Host)", got, want) } if got := cap.got.Header.Get("Authorization"); got != "Bearer testsecret" { t.Fatalf("Authorization: got %q want Bearer testsecret", got) } want, err := url.Parse("http://127.0.0.1:9090/proxies") if err != nil { t.Fatal(err) } assertSameURL(t, cap.got.URL, want) }