- Updated the Mihomo proxy implementation to ensure the client Host header is removed from outgoing requests, preventing strict upstream servers from returning errors. - Revised the test for Mihomo forwarding to verify that the Host header is correctly set and not forwarded to the upstream server, improving test reliability and coverage.
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
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)
|
|
}
|