51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
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)
|
|
}
|
|
}
|