From 81273f71ef521fe9ec131d13ab4a57e19bb13c99 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 5 Mar 2019 19:03:59 -0800 Subject: [PATCH] update tests --- src/app.rs | 68 +++++++++++++++++++++++++--------------------------- src/scope.rs | 33 ++++++++++--------------- src/test.rs | 10 ++++---- 3 files changed, 51 insertions(+), 60 deletions(-) diff --git a/src/app.rs b/src/app.rs index b21645016..e38180c4f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -902,16 +902,14 @@ mod tests { use actix_http::http::{Method, StatusCode}; use super::*; - use crate::test::{block_on, TestRequest}; + use crate::test::{self, block_on, TestRequest}; use crate::{web, HttpResponse, State}; #[test] fn test_default_resource() { - let app = App::new() - .resource("/test", |r| r.to(|| HttpResponse::Ok())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); - + let mut srv = test::init_service( + App::new().resource("/test", |r| r.to(|| HttpResponse::Ok())), + ); let req = TestRequest::with_uri("/test").to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); @@ -920,15 +918,15 @@ mod tests { let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::NOT_FOUND); - let app = App::new() - .resource("/test", |r| r.to(|| HttpResponse::Ok())) - .resource("/test2", |r| { - r.default_resource(|r| r.to(|| HttpResponse::Created())) - .route(web::get().to(|| HttpResponse::Ok())) - }) - .default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service( + App::new() + .resource("/test", |r| r.to(|| HttpResponse::Ok())) + .resource("/test2", |r| { + r.default_resource(|r| r.to(|| HttpResponse::Created())) + .route(web::get().to(|| HttpResponse::Ok())) + }) + .default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())), + ); let req = TestRequest::with_uri("/blah").to_request(); let resp = block_on(srv.call(req)).unwrap(); @@ -947,21 +945,21 @@ mod tests { #[test] fn test_state() { - let app = App::new() - .state(10usize) - .resource("/", |r| r.to(|_: State| HttpResponse::Ok())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service( + App::new() + .state(10usize) + .resource("/", |r| r.to(|_: State| HttpResponse::Ok())), + ); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - let app = App::new() - .state(10u32) - .resource("/", |r| r.to(|_: State| HttpResponse::Ok())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service( + App::new() + .state(10u32) + .resource("/", |r| r.to(|_: State| HttpResponse::Ok())), + ); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); @@ -970,21 +968,21 @@ mod tests { #[test] fn test_state_factory() { - let app = App::new() - .state_factory(|| Ok::<_, ()>(10usize)) - .resource("/", |r| r.to(|_: State| HttpResponse::Ok())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service( + App::new() + .state_factory(|| Ok::<_, ()>(10usize)) + .resource("/", |r| r.to(|_: State| HttpResponse::Ok())), + ); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); assert_eq!(resp.status(), StatusCode::OK); - let app = App::new() - .state_factory(|| Ok::<_, ()>(10u32)) - .resource("/", |r| r.to(|_: State| HttpResponse::Ok())) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service( + App::new() + .state_factory(|| Ok::<_, ()>(10u32)) + .resource("/", |r| r.to(|_: State| HttpResponse::Ok())), + ); let req = TestRequest::default().to_request(); let resp = block_on(srv.call(req)).unwrap(); diff --git a/src/scope.rs b/src/scope.rs index fa7392b41..dc88388f8 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -509,17 +509,14 @@ mod tests { use actix_service::{IntoNewService, NewService, Service}; use bytes::Bytes; - use crate::test::{block_on, TestRequest}; + use crate::test::{self, block_on, TestRequest}; use crate::{guard, web, App, HttpRequest, HttpResponse}; #[test] fn test_scope() { - let app = App::new() - .scope("/app", |scope| { - scope.resource("/path1", |r| r.to(|| HttpResponse::Ok())) - }) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service(App::new().scope("/app", |scope| { + scope.resource("/path1", |r| r.to(|| HttpResponse::Ok())) + })); let req = TestRequest::with_uri("/app/path1").to_request(); let resp = block_on(srv.call(req)).unwrap(); @@ -528,14 +525,11 @@ mod tests { #[test] fn test_scope_root() { - let app = App::new() - .scope("/app", |scope| { - scope - .resource("", |r| r.to(|| HttpResponse::Ok())) - .resource("/", |r| r.to(|| HttpResponse::Created())) - }) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service(App::new().scope("/app", |scope| { + scope + .resource("", |r| r.to(|| HttpResponse::Ok())) + .resource("/", |r| r.to(|| HttpResponse::Created())) + })); let req = TestRequest::with_uri("/app").to_request(); let resp = block_on(srv.call(req)).unwrap(); @@ -548,12 +542,9 @@ mod tests { #[test] fn test_scope_root2() { - let app = App::new() - .scope("/app/", |scope| { - scope.resource("", |r| r.to(|| HttpResponse::Ok())) - }) - .into_new_service(); - let mut srv = block_on(app.new_service(&())).unwrap(); + let mut srv = test::init_service(App::new().scope("/app/", |scope| { + scope.resource("", |r| r.to(|| HttpResponse::Ok())) + })); let req = TestRequest::with_uri("/app").to_request(); let resp = block_on(srv.call(req)).unwrap(); diff --git a/src/test.rs b/src/test.rs index 8495ea89c..22bfe0c39 100644 --- a/src/test.rs +++ b/src/test.rs @@ -41,7 +41,7 @@ where /// This method accepts application builder instance, and constructs /// service. /// -/// ```rust +/// ```rust,ignore /// use actix_web::{test, App, HttpResponse, http::StatusCode}; /// use actix_service::Service; /// @@ -80,7 +80,9 @@ where /// * `TestRequest::to_http_request` creates `HttpRequest` instance, which is used for testing handlers. /// /// ```rust,ignore -/// use actix_web::test; +/// # use futures::IntoFuture; +/// use actix_web::{test, HttpRequest, HttpResponse, HttpMessage}; +/// use actix_web::http::{header, StatusCode}; /// /// fn index(req: HttpRequest) -> HttpResponse { /// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) { @@ -94,11 +96,11 @@ where /// let req = test::TestRequest::with_header("content-type", "text/plain") /// .to_http_request(); /// -/// let resp = test::block_on(index(req)); +/// let resp = test::block_on(index(req).into_future()).unwrap(); /// assert_eq!(resp.status(), StatusCode::OK); /// /// let req = test::TestRequest::default().to_http_request(); -/// let resp = test::block_on(index(req)); +/// let resp = test::block_on(index(req).into_future()).unwrap(); /// assert_eq!(resp.status(), StatusCode::BAD_REQUEST); /// } /// ```