newProxyClient creates an HTTP(S) client configured to use the proxy. It adds a Proxy-Authorization header with the provided token for authentication. The certPool and insecureSkipVerify parameters control TLS verification: - If the proxy listener is TLS, include the listener certificate. - For MITM
(t *testing.T, srv *aibridgeproxyd.Server, proxyAuth string, certPool *x509.CertPool, insecureSkipVerify bool)
| 389 | // - For tunneled requests, include the target server's certificate. |
| 390 | // - Set insecureSkipVerify when the target cert SANs do not match the hostname. |
| 391 | func newProxyClient(t *testing.T, srv *aibridgeproxyd.Server, proxyAuth string, certPool *x509.CertPool, insecureSkipVerify bool) *http.Client { |
| 392 | t.Helper() |
| 393 | |
| 394 | // Create an HTTP(S) client configured to use the proxy. |
| 395 | scheme := "http" |
| 396 | if srv.IsTLSListener() { |
| 397 | scheme = "https" |
| 398 | } |
| 399 | proxyURL, err := url.Parse(scheme + "://" + srv.Addr()) |
| 400 | require.NoError(t, err) |
| 401 | |
| 402 | transport := &http.Transport{ |
| 403 | Proxy: http.ProxyURL(proxyURL), |
| 404 | TLSClientConfig: &tls.Config{ |
| 405 | MinVersion: tls.VersionTLS12, |
| 406 | RootCAs: certPool, |
| 407 | InsecureSkipVerify: insecureSkipVerify, //nolint:gosec |
| 408 | }, |
| 409 | } |
| 410 | |
| 411 | // Only set the header if proxyAuth is provided. This allows tests to |
| 412 | // verify behavior when the Proxy-Authorization header is missing. |
| 413 | if proxyAuth != "" { |
| 414 | transport.ProxyConnectHeader = http.Header{ |
| 415 | "Proxy-Authorization": []string{proxyAuth}, |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return &http.Client{Transport: transport} |
| 420 | } |
| 421 | |
| 422 | // newTargetServer creates a mock HTTPS server that will be the target of proxied requests. |
| 423 | // It returns the server and its parsed URL. The server is automatically closed when the test ends. |
no test coverage detected