starnet/proxy_test.go

51 lines
1.5 KiB
Go
Raw Permalink Normal View History

2026-03-08 20:19:40 +08:00
package starnet
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRequestProxy(t *testing.T) {
// Create a proxy server
proxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Proxy received the request
w.Header().Set("X-Proxied", "true")
w.WriteHeader(http.StatusOK)
w.Write([]byte("proxied"))
}))
defer proxyServer.Close()
// Note: This is a simplified test. Real proxy testing requires more setup
req := NewSimpleRequest("http://example.com", "GET").
SetProxy(proxyServer.URL)
// Just verify the proxy is set in config
if req.config.Network.Proxy != proxyServer.URL {
t.Errorf("Proxy = %v; want %v", req.config.Network.Proxy, proxyServer.URL)
}
}
func TestClientLevelProxy(t *testing.T) {
proxyURL := "http://proxy.example.com:8080"
client := NewClientNoErr(WithProxy(proxyURL))
req, _ := client.NewRequest("http://example.com", "GET")
if req.config.Network.Proxy != proxyURL {
t.Errorf("Proxy = %v; want %v", req.config.Network.Proxy, proxyURL)
}
}
func TestRequestLevelProxyOverride(t *testing.T) {
clientProxy := "http://client-proxy.com:8080"
requestProxy := "http://request-proxy.com:8080"
client := NewClientNoErr(WithProxy(clientProxy))
req, _ := client.NewRequest("http://example.com", "GET", WithProxy(requestProxy))
// Request level should override client level
if req.config.Network.Proxy != requestProxy {
t.Errorf("Proxy = %v; want %v", req.config.Network.Proxy, requestProxy)
}
}