Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 302 redirect helper #890

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
11 changes: 11 additions & 0 deletions tests/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down