axum/response/
redirect.rs1use axum_core::response::{IntoResponse, IntoResponseParts, Response, ResponseParts};
2use http::{header::LOCATION, HeaderValue, StatusCode};
3
4#[must_use = "needs to be returned from a handler or otherwise turned into a Response to be useful"]
21#[derive(Debug, Clone)]
22pub struct Redirect {
23 status_code: StatusCode,
24 location: String,
25}
26
27impl Redirect {
28 pub fn to(uri: impl Into<String>) -> Self {
38 Self::with_status_code(StatusCode::SEE_OTHER, uri.into())
39 }
40
41 pub fn temporary(uri: impl Into<String>) -> Self {
48 Self::with_status_code(StatusCode::TEMPORARY_REDIRECT, uri.into())
49 }
50
51 pub fn permanent(uri: impl Into<String>) -> Self {
55 Self::with_status_code(StatusCode::PERMANENT_REDIRECT, uri.into())
56 }
57
58 #[must_use]
60 pub fn status_code(&self) -> StatusCode {
61 self.status_code
62 }
63
64 #[must_use]
66 pub fn location(&self) -> &str {
67 &self.location
68 }
69
70 fn with_status_code(status_code: StatusCode, uri: String) -> Self {
75 assert!(
76 status_code.is_redirection(),
77 "not a redirection status code"
78 );
79
80 Self {
81 status_code,
82 location: uri,
83 }
84 }
85}
86
87impl IntoResponse for Redirect {
88 fn into_response(self) -> Response {
89 (self, ()).into_response()
90 }
91}
92
93impl IntoResponseParts for Redirect {
94 type Error = (StatusCode, String);
95
96 fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
133 match HeaderValue::try_from(self.location) {
134 Ok(location) => {
135 *res.status_mut() = self.status_code;
136 res.headers_mut().insert(LOCATION, location);
137 Ok(res)
138 }
139 Err(err) => Err((
140 StatusCode::INTERNAL_SERVER_ERROR,
141 format!("invalid redirect location: {err}"),
142 )),
143 }
144 }
145}
146
147#[cfg(test)]
148mod tests {
149 use super::Redirect;
150 use axum_core::response::IntoResponse;
151 use http::{header::LOCATION, StatusCode};
152
153 const EXAMPLE_URL: &str = "https://example.com";
154
155 #[test]
158 fn correct_status() {
159 assert_eq!(
160 StatusCode::SEE_OTHER,
161 Redirect::to(EXAMPLE_URL).status_code()
162 );
163
164 assert_eq!(
165 StatusCode::TEMPORARY_REDIRECT,
166 Redirect::temporary(EXAMPLE_URL).status_code()
167 );
168
169 assert_eq!(
170 StatusCode::PERMANENT_REDIRECT,
171 Redirect::permanent(EXAMPLE_URL).status_code()
172 );
173 }
174
175 #[test]
176 fn correct_location() {
177 assert_eq!(EXAMPLE_URL, Redirect::permanent(EXAMPLE_URL).location());
178
179 assert_eq!("/redirect", Redirect::permanent("/redirect").location())
180 }
181
182 #[test]
183 fn test_internal_error() {
184 let response = Redirect::permanent("Axum is awesome, \n but newlines aren't allowed :(")
185 .into_response();
186
187 assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
188 }
189
190 #[test]
191 fn into_response_parts_sets_status_and_location() {
192 let response = (Redirect::to(EXAMPLE_URL), "body").into_response();
193
194 assert_eq!(response.status(), StatusCode::SEE_OTHER);
195 assert_eq!(response.headers().get(LOCATION).unwrap(), EXAMPLE_URL);
196 }
197
198 #[test]
199 fn into_response_parts_with_permanent_redirect() {
200 let response = (Redirect::permanent(EXAMPLE_URL), "body").into_response();
201
202 assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
203 assert_eq!(response.headers().get(LOCATION).unwrap(), EXAMPLE_URL);
204 }
205
206 #[test]
207 fn into_response_parts_with_temporary_redirect() {
208 let response = (Redirect::temporary(EXAMPLE_URL), "body").into_response();
209
210 assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
211 assert_eq!(response.headers().get(LOCATION).unwrap(), EXAMPLE_URL);
212 }
213
214 #[test]
215 fn into_response_parts_explicit_status_overrides() {
216 let response = (
218 StatusCode::TEMPORARY_REDIRECT,
219 Redirect::to(EXAMPLE_URL),
220 "body",
221 )
222 .into_response();
223
224 assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
225 assert_eq!(response.headers().get(LOCATION).unwrap(), EXAMPLE_URL);
226 }
227
228 #[test]
229 fn into_response_parts_invalid_location() {
230 let response = (Redirect::permanent("invalid\nlocation"), "body").into_response();
231
232 assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
233 }
234}