use http::StatusCode;
use crate::http::HttpResponse;
pub fn bad_request(body: impl Into<String>) -> HttpResponse {
HttpResponse::with_status(StatusCode::BAD_REQUEST, body.into())
}
pub fn not_found(body: impl Into<String>) -> HttpResponse {
HttpResponse::with_status(StatusCode::NOT_FOUND, body.into())
}
pub fn forbidden(body: impl Into<String>) -> HttpResponse {
HttpResponse::with_status(StatusCode::FORBIDDEN, body.into())
}
pub fn server_error(body: impl Into<String>) -> HttpResponse {
HttpResponse::with_status(StatusCode::INTERNAL_SERVER_ERROR, body.into())
}
pub fn no_content() -> HttpResponse {
HttpResponse::with_status(StatusCode::NO_CONTENT, "")
}
pub fn gone() -> HttpResponse {
HttpResponse::with_status(StatusCode::GONE, "")
}
pub fn redirect(url: &str) -> HttpResponse {
let mut r = HttpResponse::with_status(StatusCode::FOUND, "");
r.set_header(http::header::LOCATION, url);
r
}
pub fn permanent_redirect(url: &str) -> HttpResponse {
let mut r = HttpResponse::with_status(StatusCode::MOVED_PERMANENTLY, "");
r.set_header(http::header::LOCATION, url);
r
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bad_request() {
assert_eq!(bad_request("oops").status_code, StatusCode::BAD_REQUEST);
}
#[test]
fn test_not_found() {
assert_eq!(not_found("gone").status_code, StatusCode::NOT_FOUND);
}
#[test]
fn test_forbidden() {
assert_eq!(forbidden("no").status_code, StatusCode::FORBIDDEN);
}
#[test]
fn test_server_error() {
assert_eq!(
server_error("crash").status_code,
StatusCode::INTERNAL_SERVER_ERROR
);
}
#[test]
fn test_no_content() {
assert_eq!(no_content().status_code, StatusCode::NO_CONTENT);
}
#[test]
fn test_gone() {
assert_eq!(gone().status_code, StatusCode::GONE);
}
#[test]
fn test_redirect() {
let r = redirect("/new");
assert_eq!(r.status_code, StatusCode::FOUND);
}
#[test]
fn test_permanent_redirect() {
let r = permanent_redirect("/new");
assert_eq!(r.status_code, StatusCode::MOVED_PERMANENTLY);
}
}