use std::convert::Infallible;
use actix_utils::future::{ok, Ready};
use actix_web::{dev::Payload, FromRequest, HttpRequest};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Host(String);
impl_more::impl_as_ref!(Host => String);
impl_more::impl_into!(Host => String);
impl_more::forward_display!(Host);
impl Host {
pub fn into_inner(self) -> String {
self.0
}
}
impl FromRequest for Host {
type Error = Infallible;
type Future = Ready<Result<Self, Self::Error>>;
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ok(Host(req.connection_info().host().to_owned()))
}
}
#[cfg(test)]
mod tests {
use actix_web::{
http::StatusCode,
test::{self, TestRequest},
web, App, HttpResponse,
};
use super::*;
#[actix_web::test]
async fn extracts_host() {
let app =
test::init_service(App::new().default_service(web::to(|host: Host| async move {
HttpResponse::Ok().body(host.to_string())
})))
.await;
let req = TestRequest::default()
.insert_header(("host", "in-header.com"))
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(test::read_body(res).await, b"in-header.com".as_ref());
let req = TestRequest::default().uri("http://in-url.com").to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(test::read_body(res).await, b"in-url.com".as_ref());
let req = TestRequest::default().to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(test::read_body(res).await, b"localhost:8080".as_ref());
}
}