mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-24 20:57:39 +00:00
feat(proxy): centralize proxy handling with `proxyutil` package and enhance test coverage - Added `proxyutil` package to simplify proxy handling across the codebase. - Refactored various components (`executor`, `cliproxy`, `auth`, etc.) to use `proxyutil` for consistent and reusable proxy logic. - Introduced support for "direct" proxy mode to explicitly bypass all proxies. - Updated tests to validate proxy behavior (e.g., `direct`, HTTP/HTTPS, and SOCKS5). - Enhanced YAML configuration documentation for proxy options.
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
package proxyutil
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want Mode
|
|
wantErr bool
|
|
}{
|
|
{name: "inherit", input: "", want: ModeInherit},
|
|
{name: "direct", input: "direct", want: ModeDirect},
|
|
{name: "none", input: "none", want: ModeDirect},
|
|
{name: "http", input: "http://proxy.example.com:8080", want: ModeProxy},
|
|
{name: "https", input: "https://proxy.example.com:8443", want: ModeProxy},
|
|
{name: "socks5", input: "socks5://proxy.example.com:1080", want: ModeProxy},
|
|
{name: "invalid", input: "bad-value", want: ModeInvalid, wantErr: true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
setting, errParse := Parse(tt.input)
|
|
if tt.wantErr && errParse == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !tt.wantErr && errParse != nil {
|
|
t.Fatalf("unexpected error: %v", errParse)
|
|
}
|
|
if setting.Mode != tt.want {
|
|
t.Fatalf("mode = %d, want %d", setting.Mode, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuildHTTPTransportDirectBypassesProxy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
transport, mode, errBuild := BuildHTTPTransport("direct")
|
|
if errBuild != nil {
|
|
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
|
|
}
|
|
if mode != ModeDirect {
|
|
t.Fatalf("mode = %d, want %d", mode, ModeDirect)
|
|
}
|
|
if transport == nil {
|
|
t.Fatal("expected transport, got nil")
|
|
}
|
|
if transport.Proxy != nil {
|
|
t.Fatal("expected direct transport to disable proxy function")
|
|
}
|
|
}
|
|
|
|
func TestBuildHTTPTransportHTTPProxy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
transport, mode, errBuild := BuildHTTPTransport("http://proxy.example.com:8080")
|
|
if errBuild != nil {
|
|
t.Fatalf("BuildHTTPTransport returned error: %v", errBuild)
|
|
}
|
|
if mode != ModeProxy {
|
|
t.Fatalf("mode = %d, want %d", mode, ModeProxy)
|
|
}
|
|
if transport == nil {
|
|
t.Fatal("expected transport, got nil")
|
|
}
|
|
|
|
req, errRequest := http.NewRequest(http.MethodGet, "https://example.com", nil)
|
|
if errRequest != nil {
|
|
t.Fatalf("http.NewRequest returned error: %v", errRequest)
|
|
}
|
|
|
|
proxyURL, errProxy := transport.Proxy(req)
|
|
if errProxy != nil {
|
|
t.Fatalf("transport.Proxy returned error: %v", errProxy)
|
|
}
|
|
if proxyURL == nil || proxyURL.String() != "http://proxy.example.com:8080" {
|
|
t.Fatalf("proxy URL = %v, want http://proxy.example.com:8080", proxyURL)
|
|
}
|
|
}
|