litellm/litellm-rust/crates/router/tests/hooks.rs
Yujong Lee 302ab0b1c2 refactor(rust): add gateway and router hook seams
The Rust gateway now calls hooks at all three request layers, each from its
own layer code:

- gateway: GatewayHooks::pre_call in the shared request::route step every
  handler already goes through, post_call as a layer on the inference router
- router: RouterHooks::filter_deployments inside Router::select
- inference: the existing Interceptors, composed with the cache-key capture
  through a route-neutral Chain

Hooks are stored type-erased (Arc<dyn ...>, DynInterceptors in litellm-host),
so Gateway, Router and the route handlers keep their plain signatures.
Defaults are no-ops; only tests install recorders.
2026-10-06 16:21:44 -07:00

61 lines
1.4 KiB
Rust

use std::sync::Arc;
use futures_util::future::BoxFuture;
use litellm_router::{Deployment, Router, RouterHooks};
use rstest::{fixture, rstest};
#[fixture]
fn router() -> Router {
Router::from_iter([(
"public-model".into(),
Deployment {
model: "provider/model".into(),
..Default::default()
},
)])
}
struct Reject;
impl RouterHooks for Reject {
fn filter_deployments<'h, 'd: 'h>(
&'h self,
model: &'h str,
deployments: Vec<&'d Deployment>,
) -> BoxFuture<'h, Vec<&'d Deployment>> {
assert_eq!(model, "public-model");
assert_eq!(deployments.len(), 1);
assert_eq!(deployments[0].model, "provider/model");
Box::pin(async { Vec::new() })
}
}
#[rstest]
#[case::known("public-model", Some("provider/model"))]
#[case::unknown("missing", None)]
#[tokio::test]
async fn default_hooks_select_by_model_name(
router: Router,
#[case] model: &str,
#[case] expected: Option<&str>,
) {
assert_eq!(
router
.select(model)
.await
.map(|deployment| deployment.model.as_str()),
expected,
);
}
#[rstest]
#[tokio::test]
async fn selection_uses_filtered_candidates(router: Router) {
assert!(
router
.with_hooks(Arc::new(Reject))
.select("public-model")
.await
.is_none()
);
}