feat #14740: http transport seam

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 16:05:51 +01:00
co-authored by Claude Sonnet 5
parent 3dfa693c4e
commit 3facefeeed
5 changed files with 714 additions and 9 deletions
+6
View File
@@ -7,6 +7,9 @@ edition = "2021"
name = "recorder_core"
path = "src/lib.rs"
[features]
test-util = []
[dependencies]
rusqlite = { version = "0.31", features = ["bundled"] }
serde = { version = "1.0", features = ["derive"] }
@@ -17,6 +20,9 @@ rubato = "0.15"
flacenc = "0.5"
hound = "3.5"
keyring = { version = "3", features = ["windows-native", "sync-secret-service", "crypto-rust"] }
async-trait = "0.1"
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "multipart", "json", "stream"] }
tokio = { version = "1", features = ["rt", "fs", "time", "macros"] }
[dev-dependencies]
tempfile = "3"
+282
View File
@@ -0,0 +1,282 @@
//! The HTTP transport seam: every outgoing request the DeepScript and Claude
//! clients (Tasks 12/13) make goes through [`HttpTransport`], so unit tests
//! can swap in [`crate::testing::FakeTransport`] and run with the network
//! unplugged. [`ReqwestTransport`] is the real implementation, exercised only
//! by the live integration suite (`crates/core/tests/live_api.rs`).
use std::path::PathBuf;
use std::time::Duration;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use thiserror::Error;
/// HTTP method a [`HttpRequest`] is sent with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
Get,
Post,
Delete,
}
/// One field of a `multipart/form-data` body.
#[derive(Debug, Clone, PartialEq)]
pub enum Part {
Text {
name: String,
value: String,
},
/// Streamed from disk by [`ReqwestTransport`]; never read fully into
/// memory (recordings can be tens of megabytes).
File {
name: String,
file_name: String,
path: PathBuf,
mime: String,
},
}
/// Body of a [`HttpRequest`].
#[derive(Debug, Clone, PartialEq)]
pub enum HttpBody {
Empty,
Json(serde_json::Value),
Multipart(Vec<Part>),
}
/// A request to send through a [`HttpTransport`].
#[derive(Debug, Clone, PartialEq)]
pub struct HttpRequest {
pub method: Method,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: HttpBody,
}
impl HttpRequest {
/// The value of the first header matching `name`, case-insensitively.
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
}
/// A response as returned by a [`HttpTransport`]. Non-2xx statuses are not an
/// error at this layer: callers inspect `status` themselves.
#[derive(Debug, Clone, PartialEq)]
pub struct HttpResponse {
pub status: u16,
pub body: Vec<u8>,
}
impl HttpResponse {
/// Decodes the body as JSON.
pub fn json<T: DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
serde_json::from_slice(&self.body)
}
}
/// Transport-level failure: a request that could not be sent or whose
/// response could not be read at all. A non-2xx status is *not* one of
/// these; it comes back as an ordinary [`HttpResponse`].
#[derive(Debug, Error)]
pub enum TransportError {
#[error("E/A-Fehler: {0}")]
Io(String),
#[error("Netzwerkfehler: {0}")]
Network(String),
#[error("Zeitüberschreitung")]
Timeout,
}
/// Seam between the API clients and the network. Implemented by
/// [`ReqwestTransport`] for real traffic and by
/// [`crate::testing::FakeTransport`] for unit tests.
#[async_trait]
pub trait HttpTransport: Send + Sync {
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TransportError>;
}
/// Live `HttpTransport` backed by `reqwest` (rustls). Shares one
/// `reqwest::Client` (connection pooling) with a 10 minute request timeout
/// and a 30 second connect timeout, matching the Claude/DeepScript clients'
/// worst-case wait.
pub struct ReqwestTransport {
client: reqwest::Client,
}
impl ReqwestTransport {
pub fn new() -> Self {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10 * 60))
.connect_timeout(Duration::from_secs(30))
.build()
.expect("failed to build reqwest client");
ReqwestTransport { client }
}
}
impl Default for ReqwestTransport {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl HttpTransport for ReqwestTransport {
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TransportError> {
let mut builder = match req.method {
Method::Get => self.client.get(&req.url),
Method::Post => self.client.post(&req.url),
Method::Delete => self.client.delete(&req.url),
};
for (name, value) in &req.headers {
builder = builder.header(name, value);
}
builder = match &req.body {
HttpBody::Empty => builder,
HttpBody::Json(value) => builder.json(value),
HttpBody::Multipart(parts) => {
let form = build_multipart(parts).await?;
builder.multipart(form)
}
};
let response = builder.send().await.map_err(map_reqwest_error)?;
let status = response.status().as_u16();
let body = response.bytes().await.map_err(map_reqwest_error)?.to_vec();
Ok(HttpResponse { status, body })
}
}
/// Builds a `reqwest` multipart form, streaming any [`Part::File`] from disk
/// rather than reading it into memory.
async fn build_multipart(parts: &[Part]) -> Result<reqwest::multipart::Form, TransportError> {
let mut form = reqwest::multipart::Form::new();
for part in parts {
form = match part {
Part::Text { name, value } => form.text(name.clone(), value.clone()),
Part::File {
name,
file_name,
path,
mime,
} => {
let file = tokio::fs::File::open(path)
.await
.map_err(|e| TransportError::Io(format!("{}: {e}", path.display())))?;
let len = file.metadata().await.ok().map(|m| m.len());
let body = reqwest::Body::from(file);
let mut field = match len {
Some(len) => reqwest::multipart::Part::stream_with_length(body, len),
None => reqwest::multipart::Part::stream(body),
};
field = field.file_name(file_name.clone());
field = field
.mime_str(mime)
.map_err(|e| TransportError::Io(format!("ungültiger MIME-Typ {mime}: {e}")))?;
form.part(name.clone(), field)
}
};
}
Ok(form)
}
fn map_reqwest_error(e: reqwest::Error) -> TransportError {
if e.is_timeout() {
TransportError::Timeout
} else {
TransportError::Network(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::FakeTransport;
fn get(url: &str) -> HttpRequest {
HttpRequest {
method: Method::Get,
url: url.to_string(),
headers: vec![],
body: HttpBody::Empty,
}
}
#[tokio::test]
async fn fake_transport_returns_scripted_responses_in_order() {
let transport = FakeTransport::new();
transport.expect(Method::Get, "https://api.example.com/a", 200, "first");
transport.expect(Method::Post, "https://api.example.com/b?x=1", 201, "second");
let first = transport
.send(get("https://api.example.com/a"))
.await
.unwrap();
assert_eq!(first.status, 200);
assert_eq!(first.body, b"first");
let second = transport
.send(HttpRequest {
method: Method::Post,
url: "https://api.example.com/b?x=1".to_string(),
headers: vec![("x-test".to_string(), "1".to_string())],
body: HttpBody::Empty,
})
.await
.unwrap();
assert_eq!(second.status, 201);
assert_eq!(second.body, b"second");
let requests = transport.requests();
assert_eq!(requests.len(), 2);
assert_eq!(requests[0].url, "https://api.example.com/a");
assert_eq!(requests[1].url, "https://api.example.com/b?x=1");
assert_eq!(requests[1].header("x-test"), Some("1"));
transport.assert_done();
}
#[tokio::test]
#[should_panic(expected = "FakeTransport")]
async fn fake_transport_panics_on_unexpected_url() {
let transport = FakeTransport::new();
transport.expect(Method::Get, "https://api.example.com/a", 200, "body");
let _ = transport.send(get("https://api.example.com/wrong")).await;
}
#[test]
#[should_panic(expected = "FakeTransport")]
fn fake_transport_assert_done_detects_missing_calls() {
let transport = FakeTransport::new();
transport.expect(Method::Get, "https://api.example.com/a", 200, "body");
transport.assert_done();
}
#[test]
fn http_request_header_lookup_is_case_insensitive() {
let req = HttpRequest {
method: Method::Get,
url: "https://api.example.com".to_string(),
headers: vec![("Authorization".to_string(), "Bearer xyz".to_string())],
body: HttpBody::Empty,
};
assert_eq!(req.header("authorization"), Some("Bearer xyz"));
assert_eq!(req.header("missing"), None);
}
#[test]
fn http_response_json_decodes_body() {
let resp = HttpResponse {
status: 200,
body: br#"{"ok":true}"#.to_vec(),
};
let value: serde_json::Value = resp.json().unwrap();
assert_eq!(value["ok"], serde_json::json!(true));
}
}
+4
View File
@@ -7,6 +7,7 @@
pub mod audio;
pub mod capture;
pub mod detect;
pub mod http;
pub mod merge;
pub mod model;
pub mod recorder;
@@ -15,6 +16,9 @@ pub mod secrets;
pub mod settings;
pub mod store;
#[cfg(any(test, feature = "test-util"))]
pub mod testing;
#[cfg(test)]
mod tests {
#[test]
+100
View File
@@ -0,0 +1,100 @@
//! Test doubles shared across `recorder-core`'s unit tests, and usable from
//! other crates/integration tests via the `test-util` feature.
use std::sync::Mutex;
use async_trait::async_trait;
use crate::http::{HttpRequest, HttpResponse, HttpTransport, Method, TransportError};
struct Expectation {
method: Method,
url: String,
status: u16,
body: Vec<u8>,
}
/// Scripted [`HttpTransport`]: queue expected requests in order with
/// [`FakeTransport::expect`], run the code under test against it, then call
/// [`FakeTransport::assert_done`] to confirm every expectation was consumed.
///
/// Matching is exact (method and full URL, including query string) and
/// strictly FIFO. Uses interior mutability so `send` works through
/// `&self`, as required behind `Arc<dyn HttpTransport>`.
pub struct FakeTransport {
expectations: Mutex<Vec<Expectation>>,
requests: Mutex<Vec<HttpRequest>>,
}
impl FakeTransport {
pub fn new() -> Self {
FakeTransport {
expectations: Mutex::new(Vec::new()),
requests: Mutex::new(Vec::new()),
}
}
/// Queues the next expected request and the response to return for it.
pub fn expect(&self, method: Method, url: &str, status: u16, body: impl Into<Vec<u8>>) {
self.expectations.lock().unwrap().push(Expectation {
method,
url: url.to_string(),
status,
body: body.into(),
});
}
/// Every request sent through this transport so far, in order.
pub fn requests(&self) -> Vec<HttpRequest> {
self.requests.lock().unwrap().clone()
}
/// Panics listing any scripted expectation that was never sent.
pub fn assert_done(&self) {
let remaining = self.expectations.lock().unwrap();
if !remaining.is_empty() {
let pending: Vec<String> = remaining
.iter()
.map(|e| format!("{:?} {}", e.method, e.url))
.collect();
panic!(
"FakeTransport: {} expectation(s) never called: {}",
pending.len(),
pending.join(", ")
);
}
}
}
impl Default for FakeTransport {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl HttpTransport for FakeTransport {
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TransportError> {
let expectation = {
let mut expectations = self.expectations.lock().unwrap();
if expectations.is_empty() {
panic!(
"FakeTransport: unexpected {:?} {} (no expectations remain)",
req.method, req.url
);
}
expectations.remove(0)
};
if expectation.method != req.method || expectation.url != req.url {
panic!(
"FakeTransport: expected {:?} {}, got {:?} {}",
expectation.method, expectation.url, req.method, req.url
);
}
self.requests.lock().unwrap().push(req);
Ok(HttpResponse {
status: expectation.status,
body: expectation.body,
})
}
}