From 341baae22cc08c02ff19d6a225006027da3856c8 Mon Sep 17 00:00:00 2001 From: Giovanni Tirloni Date: Sat, 24 May 2025 06:20:53 -0300 Subject: [PATCH] fix(search): follow http redirects Fixes #15592 --- src/cargo/ops/registry/mod.rs | 3 ++- tests/testsuite/search.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/registry/mod.rs b/src/cargo/ops/registry/mod.rs index 0189b2b2d2c..e7e555f5e46 100644 --- a/src/cargo/ops/registry/mod.rs +++ b/src/cargo/ops/registry/mod.rs @@ -169,7 +169,8 @@ fn registry<'gctx>( } else { None }; - let handle = http_handle(gctx)?; + let mut handle = http_handle(gctx)?; + handle.follow_location(true)?; Ok(( Registry::new_handle(api_host, token, handle, cfg.auth_required), src, diff --git a/tests/testsuite/search.rs b/tests/testsuite/search.rs index 87203606714..327596a899d 100644 --- a/tests/testsuite/search.rs +++ b/tests/testsuite/search.rs @@ -224,3 +224,34 @@ fn auth_required() { .with_stdout_data(SEARCH_RESULTS) .run(); } + +#[cargo_test] +fn follows_redirect() { + let _registry = RegistryBuilder::new() + .http_api() + .add_responder("/api/v1/crates", |req, _server| { + let query = req.url.query().unwrap_or(""); + let redirect_url = format!("/api/v1/crates/redirected?{}", query); + Response { + code: 302, + headers: vec![format!("Location: {}", redirect_url)], + body: vec![], + } + }) + .add_responder("/api/v1/crates/redirected", |_, _| Response { + code: 200, + headers: vec![], + body: SEARCH_API_RESPONSE.to_vec(), + }) + .build(); + + cargo_process("search postgres") + .replace_crates_io(&_registry.index_url()) + .with_stdout_data(SEARCH_RESULTS) + .with_stderr_data(str![[r#" +[UPDATING] crates.io index +[NOTE] to learn more about a package, run `cargo info ` + +"#]]) + .run(); +}