diff --git a/src/redirect.rs b/src/redirect.rs index 967360a6a..ee2c3ad79 100644 --- a/src/redirect.rs +++ b/src/redirect.rs @@ -28,6 +28,22 @@ pub fn redirect(uri: impl AsLocation) -> impl Reply { ) } +/// A simple `302` found redirect to a different location +/// +/// # Example +/// +/// ``` +/// use warp::{http::Uri, Filter}; +/// +/// let route = warp::path("v1") +/// .map(|| { +/// warp::redirect::found(Uri::from_static("/v2")) +/// }); +/// ``` +pub fn found(uri: impl AsLocation) -> impl Reply { + reply::with_header(StatusCode::FOUND, header::LOCATION, uri.header_value()) +} + /// A simple `303` redirect to a different location. /// /// The HTTP method of the request to the new location will always be `GET`. diff --git a/tests/redirect.rs b/tests/redirect.rs index 911a16072..be7fd33e4 100644 --- a/tests/redirect.rs +++ b/tests/redirect.rs @@ -12,6 +12,17 @@ async fn redirect_uri() { assert_eq!(resp.headers()["location"], "/over-there"); } +#[tokio::test] +async fn redirect_found_uri() { + let over_there = warp::any().map(|| warp::redirect::found(Uri::from_static("/over-there"))); + + let req = warp::test::request(); + let resp = req.reply(&over_there).await; + + assert_eq!(resp.status(), 302); + assert_eq!(resp.headers()["location"], "/over-there"); +} + #[tokio::test] async fn redirect_see_other_uri() { let over_there = warp::any().map(|| warp::redirect::see_other(Uri::from_static("/over-there")));