1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00

update tests for defaultheaders middleware

This commit is contained in:
Nikolay Kim 2019-03-02 21:35:31 -08:00
parent 8103d33270
commit 352e7b7a75
2 changed files with 56 additions and 29 deletions

View File

@ -13,17 +13,15 @@ use crate::service::{ServiceRequest, ServiceResponse};
/// ///
/// This middleware does not set header if response headers already contains it. /// This middleware does not set header if response headers already contains it.
/// ///
/// ```rust,ignore /// ```rust
/// # extern crate actix_web; /// use actix_web::{web, http, middleware, App, HttpResponse};
/// use actix_web::{http, middleware, App, HttpResponse};
/// ///
/// fn main() { /// fn main() {
/// let app = App::new() /// let app = App::new()
/// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2")) /// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
/// .resource("/test", |r| { /// .resource("/test", |r| {
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// r.route(web::get().to(|| HttpResponse::Ok()))
/// r.method(http::Method::HEAD) /// .route(web::method(http::Method::HEAD).to(|| HttpResponse::MethodNotAllowed()))
/// .f(|_| HttpResponse::MethodNotAllowed());
/// }); /// });
/// } /// }
/// ``` /// ```
@ -134,30 +132,48 @@ where
} }
} }
// #[cfg(test)] #[cfg(test)]
// mod tests { mod tests {
// use super::*; use actix_http::http::header::CONTENT_TYPE;
// use actix_http::http::header::CONTENT_TYPE; use actix_service::FnService;
// use actix_http::test::TestRequest;
// #[test] use super::*;
// fn test_default_headers() { use crate::test::TestServiceRequest;
// let mw = DefaultHeaders::new().header(CONTENT_TYPE, "0001"); use crate::{HttpResponse, ServiceRequest};
// let req = TestRequest::default().finish(); #[test]
fn test_default_headers() {
let mut rt = actix_rt::Runtime::new().unwrap();
let mut mw = DefaultHeaders::new().header(CONTENT_TYPE, "0001");
let mut srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().finish())
});
// let resp = Response::Ok().finish(); let req = TestServiceRequest::default().finish();
// let resp = match mw.response(&req, resp) { let resp = rt.block_on(mw.call(req, &mut srv)).unwrap();
// Ok(Response::Done(resp)) => resp, assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
// _ => panic!(),
// };
// assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
// let resp = Response::Ok().header(CONTENT_TYPE, "0002").finish(); let req = TestServiceRequest::default().finish();
// let resp = match mw.response(&req, resp) { let mut srv = FnService::new(|req: ServiceRequest<_>| {
// Ok(Response::Done(resp)) => resp, req.into_response(HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish())
// _ => panic!(), });
// }; let resp = rt.block_on(mw.call(req, &mut srv)).unwrap();
// assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002"); assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002");
// } }
// }
#[test]
fn test_content_type() {
let mut rt = actix_rt::Runtime::new().unwrap();
let mut mw = DefaultHeaders::new().content_type();
let mut srv = FnService::new(|req: ServiceRequest<_>| {
req.into_response(HttpResponse::Ok().finish())
});
let req = TestServiceRequest::default().finish();
let resp = rt.block_on(mw.call(req, &mut srv)).unwrap();
assert_eq!(
resp.headers().get(CONTENT_TYPE).unwrap(),
"application/octet-stream"
);
}
}

View File

@ -8,6 +8,7 @@ use actix_http::{
ResponseHead, ResponseHead,
}; };
use actix_router::{Path, Url}; use actix_router::{Path, Url};
use futures::future::{ok, FutureResult, IntoFuture};
use crate::request::HttpRequest; use crate::request::HttpRequest;
@ -288,3 +289,13 @@ impl<B: MessageBody> Into<Response<B>> for ServiceResponse<B> {
self.response self.response
} }
} }
impl<B: MessageBody> IntoFuture for ServiceResponse<B> {
type Item = ServiceResponse<B>;
type Error = Error;
type Future = FutureResult<ServiceResponse<B>, Error>;
fn into_future(self) -> Self::Future {
ok(self)
}
}