Files
codex/codex-rs/http-client/src/client_builder_tests.rs
Michael Bolin adb143a291 Add a policy-aware HTTP client builder (#34630)
## What changed

- Add `HttpClientBuilder` for configuring default headers, redirects, the
  Cloudflare cookie store, and request diagnostics without exposing the
  underlying transport.
- Provide factory-backed construction for fixed destinations that respects
  outbound proxy policy, alongside explicit direct and legacy
  transport-default construction paths.
- Preserve the request-logging setting in `ReqwestTransport`, so disabling
  diagnostics also suppresses transport-level URL and request-body traces.
- Add `HEAD`, `DELETE`, and query-parameter helpers to the shared client
  wrappers.

## Testing

- Verify builder configuration survives policy-aware construction and custom
  CA fallback.
- Verify disabled request logging omits request URLs and bodies from transport
  traces.

GitOrigin-RevId: 529c33abefeb88f38ff9a0ead374d29fcc872e6a
2026-07-21 22:40:12 +00:00

53 lines
2.1 KiB
Rust

use super::*;
use http::HeaderValue;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
#[tokio::test]
async fn custom_ca_fallback_preserves_builder_configuration() {
let listener =
std::net::TcpListener::bind(("127.0.0.1", 0)).expect("HTTP listener should bind");
let address = listener
.local_addr()
.expect("HTTP listener should have an address");
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().expect("HTTP listener should accept");
let mut request = Vec::new();
let mut chunk = [0_u8; 1024];
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
let bytes_read = stream.read(&mut chunk).expect("HTTP request should read");
assert!(bytes_read > 0, "HTTP request should include headers");
request.extend_from_slice(&chunk[..bytes_read]);
}
stream
.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
.expect("HTTP listener should write response");
String::from_utf8(request).expect("HTTP request should be UTF-8")
});
let mut headers = HeaderMap::new();
headers.insert("x-builder-test", HeaderValue::from_static("preserved"));
let client = HttpClientBuilder::new()
.default_headers(headers)
.build_with_custom_ca_fallback_using(ProxyRouting::Direct, |_| {
Err(BuildCustomCaTransportError::InvalidCaFile {
source_env: "TEST_CA_ENV",
path: PathBuf::from("invalid-test-ca.pem"),
detail: "synthetic invalid CA".to_string(),
})
});
let response = client
.get(format!("http://{address}/fallback"))
.send()
.await
.expect("fallback client should send request");
assert!(response.status().is_success());
let request = server.join().expect("HTTP listener should finish");
assert!(
request
.lines()
.any(|line| line.eq_ignore_ascii_case("x-builder-test: preserved"))
);
}