Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

test(cli): cover hook client redirect refusal across events, batches, and handoff

Vinícius DS Andrade3075b23744cb
diff --git a/crates/ai-memory-cli/src/commands/hook_capture.rs b/crates/ai-memory-cli/src/commands/hook_capture.rs
index 0a6ee21b..56d21b86 100644
--- a/crates/ai-memory-cli/src/commands/hook_capture.rs
+++ b/crates/ai-memory-cli/src/commands/hook_capture.rs
@@ -1282,6 +1282,89 @@ mod tests {
assert!(got.is_none(), "non-2xx body must not become context");
}
+ async fn redirect_server(status: &'static str, target_url: &str) -> String {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let target = target_url.to_owned();
+ tokio::spawn(async move {
+ let (mut stream, _) = listener.accept().await.unwrap();
+ let mut buf = [0_u8; 1024];
+ let _ = stream.read(&mut buf).await;
+ let response = format!(
+ "HTTP/1.1 {status}\r\nLocation: {target}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
+ );
+ let _ = stream.write_all(response.as_bytes()).await;
+ });
+ format!("http://{addr}/redirect")
+ }
+
+ #[tokio::test]
+ async fn build_client_refuses_redirects() {
+ let target = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let target_url = format!("http://{}/target", target.local_addr().unwrap());
+ let source_url = redirect_server("307 Temporary Redirect", &target_url).await;
+
+ let response = build_client().get(&source_url).send().await.unwrap();
+ assert_eq!(response.status(), reqwest::StatusCode::TEMPORARY_REDIRECT);
+ assert!(
+ tokio::time::timeout(Duration::from_millis(100), target.accept())
+ .await
+ .is_err(),
+ "redirect target must never be contacted"
+ );
+ }
+
+ #[tokio::test]
+ async fn post_hook_does_not_follow_redirect() {
+ let target = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let target_url = format!("http://{}/target", target.local_addr().unwrap());
+ let source_url = redirect_server("307 Temporary Redirect", &target_url).await;
+
+ let outcome =
+ post_hook(&build_client(), &source_url, "{}", None, Duration::from_secs(1)).await;
+ assert_eq!(outcome, PostOutcome::Failed);
+ assert!(
+ tokio::time::timeout(Duration::from_millis(100), target.accept())
+ .await
+ .is_err(),
+ "redirect target must never be contacted"
+ );
+ }
+
+ #[tokio::test]
+ async fn post_batch_does_not_follow_redirect() {
+ let target = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let target_url = format!("http://{}/target", target.local_addr().unwrap());
+ let source_url = redirect_server("307 Temporary Redirect", &target_url).await;
+
+ let outcome =
+ post_batch(&build_client(), &source_url, "[]", None, Duration::from_secs(1)).await;
+ assert_eq!(outcome, BatchOutcome::Failed);
+ assert!(
+ tokio::time::timeout(Duration::from_millis(100), target.accept())
+ .await
+ .is_err(),
+ "redirect target must never be contacted"
+ );
+ }
+
+ #[tokio::test]
+ async fn get_handoff_does_not_follow_redirect() {
+ let target = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let target_url = format!("http://{}/login", target.local_addr().unwrap());
+ let source_url = redirect_server("302 Found", &target_url).await;
+
+ let got =
+ get_handoff(&build_client(), &source_url, None, Duration::from_secs(1)).await;
+ assert!(got.is_none(), "redirect must not inject target content");
+ assert!(
+ tokio::time::timeout(Duration::from_millis(100), target.accept())
+ .await
+ .is_err(),
+ "redirect target must never be contacted"
+ );
+ }
+
/// Like `serve_once`, but answers 200 and hands back the raw request.
async fn capture_once() -> (String, tokio::task::JoinHandle<String>) {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();