Redirect
We can redirect a request to another page on our side or to a page on another site.
For this we use the Redirect struct that has methods for permanent redirection (308 Permanent Redirect) and temporary redirection (307 Temporary Redirect).
[package]
name = "redirect"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
askama = "0.15.6"
axum = "0.8.8"
serde = { version = "1.0.228", features = ["derive"] }
tokio = { version = "1.50.0", features = ["full"] }
[dev-dependencies]
headers = "0.4.1"
http-body-util = "0.1.3"
tower = { version = "0.5.3", features = ["util"] }
use axum::{
Router,
response::{Html, Redirect},
routing::get,
};
async fn main_page() -> Html<&'static str> {
Html(
r#"
<h1>Redirect</h1>
<a href="/internal-redirect">Internal Redirect</a><br>
<a href="/external-redirect">External Redirect</a><br>
<a href="/target-page">Target page</a>
"#,
)
}
async fn internal_redirect() -> Redirect {
Redirect::temporary("/target-page")
}
async fn external_redirect() -> Redirect {
Redirect::permanent("https://rust.code-maven.com/")
}
async fn target_page() -> Html<&'static str> {
Html("Arrived")
}
fn create_router() -> Router {
Router::new()
.route("/", get(main_page))
.route("/internal-redirect", get(internal_redirect))
.route("/external-redirect", get(external_redirect))
.route("/target-page", get(target_page))
}
#[tokio::main]
async fn main() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, create_router()).await.unwrap();
}
#[cfg(test)]
mod tests;
#![allow(unused)]
fn main() {
use super::*;
use axum::{
body::Body,
http::{Request, StatusCode},
};
use http_body_util::BodyExt;
use tower::ServiceExt;
#[tokio::test]
async fn test_main() {
let response = create_router()
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let content_type = response.headers().get("content-type").unwrap();
assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert!(html.contains("<h1>Redirect</h1>"));
}
#[tokio::test]
async fn test_target_page() {
let response = create_router()
.oneshot(
Request::builder()
.uri("/target-page")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let content_type = response.headers().get("content-type").unwrap();
assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(html, "Arrived");
}
#[tokio::test]
async fn test_internal_redirect() {
let response = create_router()
.oneshot(
Request::builder()
.uri("/internal-redirect")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
assert!(response.headers().get("content-type").is_none());
let location = response.headers().get("Location").unwrap();
assert_eq!(location, "/target-page");
}
#[tokio::test]
async fn test_external_redirect() {
let response = create_router()
.oneshot(
Request::builder()
.uri("/external-redirect")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
assert!(response.headers().get("content-type").is_none());
let location = response.headers().get("Location").unwrap();
assert_eq!(location, "https://rust.code-maven.com/");
}
}