diff --git a/Cargo.toml b/Cargo.toml index 6920bc09..185f3fc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,6 +105,7 @@ actix-http-test = { git = "https://github.com/actix/actix-http.git", features=[" rand = "0.6" env_logger = "0.6" serde_derive = "1.0" +tokio-timer = "0.2.8" [profile.release] lto = true diff --git a/src/resource.rs b/src/resource.rs index 632e9c33..55237157 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -542,8 +542,11 @@ impl NewService for ResourceEndpoint

{ #[cfg(test)] mod tests { + use std::time::Duration; + use actix_service::Service; use futures::{Future, IntoFuture}; + use tokio_timer::sleep; use crate::http::{header, HeaderValue, Method, StatusCode}; use crate::service::{ServiceRequest, ServiceResponse}; @@ -573,6 +576,7 @@ mod tests { let mut srv = init_service( App::new().service( web::resource("/test") + .name("test") .wrap(md) .route(web::get().to(|| HttpResponse::Ok())), ), @@ -612,6 +616,17 @@ mod tests { ); } + #[test] + fn test_to_async() { + let mut srv = + init_service(App::new().service(web::resource("/test").to_async(|| { + sleep(Duration::from_millis(100)).then(|_| HttpResponse::Ok()) + }))); + let req = TestRequest::with_uri("/test").to_request(); + let resp = call_success(&mut srv, req); + assert_eq!(resp.status(), StatusCode::OK); + } + #[test] fn test_default_resource() { let mut srv = init_service( diff --git a/src/route.rs b/src/route.rs index 1f1aed47..7f1cee3d 100644 --- a/src/route.rs +++ b/src/route.rs @@ -410,21 +410,36 @@ where #[cfg(test)] mod tests { + use std::time::Duration; + + use futures::Future; + use tokio_timer::sleep; + use crate::http::{Method, StatusCode}; use crate::test::{call_success, init_service, TestRequest}; - use crate::{web, App, Error, HttpResponse}; + use crate::{error, web, App, HttpResponse}; #[test] fn test_route() { - let mut srv = init_service( - App::new().service( - web::resource("/test") - .route(web::get().to(|| HttpResponse::Ok())) - .route( - web::post().to_async(|| Ok::<_, Error>(HttpResponse::Created())), - ), - ), - ); + let mut srv = + init_service( + App::new().service( + web::resource("/test") + .route(web::get().to(|| HttpResponse::Ok())) + .route(web::put().to(|| { + Err::(error::ErrorBadRequest("err")) + })) + .route(web::post().to_async(|| { + sleep(Duration::from_millis(100)) + .then(|_| HttpResponse::Created()) + })) + .route(web::delete().to_async(|| { + sleep(Duration::from_millis(100)).then(|_| { + Err::(error::ErrorBadRequest("err")) + }) + })), + ), + ); let req = TestRequest::with_uri("/test") .method(Method::GET) @@ -438,6 +453,18 @@ mod tests { let resp = call_success(&mut srv, req); assert_eq!(resp.status(), StatusCode::CREATED); + let req = TestRequest::with_uri("/test") + .method(Method::PUT) + .to_request(); + let resp = call_success(&mut srv, req); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + + let req = TestRequest::with_uri("/test") + .method(Method::DELETE) + .to_request(); + let resp = call_success(&mut srv, req); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + let req = TestRequest::with_uri("/test") .method(Method::HEAD) .to_request();