From d9af8f66ba7ab645f160ee195136fcd1147cea99 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 25 Sep 2019 10:28:41 +0600 Subject: [PATCH] Use actix-testing for testing utils --- CHANGES.md | 14 +++--- Cargo.toml | 3 +- actix-http/src/client/connector.rs | 2 +- src/resource.rs | 18 ++++--- src/scope.rs | 22 +++++---- src/test.rs | 77 +----------------------------- 6 files changed, 36 insertions(+), 100 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ead84ac8..c27c8865 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,23 +1,23 @@ # Changes -## not released yet + +## [1.0.8] - 2019-09-xx ### Added * Add `Scope::register_data` and `Resource::register_data` methods, parallel to `App::register_data`. -## [1.0.8] - 2019-09-xx - -### Added - * Add `middleware::Condition` that conditionally enables another middleware -### Fixed - * Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload` +### Changed + * Make UrlEncodedError::Overflow more informativve +* Use actix-testing for testing utils + + ## [1.0.7] - 2019-08-29 ### Fixed diff --git a/Cargo.toml b/Cargo.toml index c2d3b0d2..b0d34e1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web" -version = "1.0.7" +version = "1.0.8" authors = ["Nikolay Kim "] description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust." readme = "README.md" @@ -81,6 +81,7 @@ actix-web-codegen = "0.1.2" actix-http = "0.2.9" actix-server = "0.6.0" actix-server-config = "0.1.2" +actix-testing = "0.1.0" actix-threadpool = "0.1.1" awc = { version = "0.2.4", optional = true } diff --git a/actix-http/src/client/connector.rs b/actix-http/src/client/connector.rs index d92441f2..98e8618c 100644 --- a/actix-http/src/client/connector.rs +++ b/actix-http/src/client/connector.rs @@ -212,7 +212,7 @@ where pub fn finish( self, ) -> impl Service - + Clone { + + Clone { #[cfg(not(any(feature = "ssl", feature = "rust-tls")))] { let connector = TimeoutService::new( diff --git a/src/resource.rs b/src/resource.rs index b711fc32..3ee0167a 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -786,13 +786,17 @@ mod tests { .data(10usize) .register_data(web::Data::new('*')) .guard(guard::Get()) - .to(|data1: web::Data, data2: web::Data, data3: web::Data| { - assert_eq!(*data1, 10); - assert_eq!(*data2, '*'); - assert_eq!(*data3, 1.0); - HttpResponse::Ok() - }), - ) + .to( + |data1: web::Data, + data2: web::Data, + data3: web::Data| { + assert_eq!(*data1, 10); + assert_eq!(*data2, '*'); + assert_eq!(*data3, 1.0); + HttpResponse::Ok() + }, + ), + ), ); let req = TestRequest::get().uri("/test").to_request(); diff --git a/src/scope.rs b/src/scope.rs index 06ebbd94..c152bc33 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1093,16 +1093,20 @@ mod tests { #[test] fn test_override_register_data() { - let mut srv = init_service(App::new().register_data(web::Data::new(1usize)).service( - web::scope("app").register_data(web::Data::new(10usize)).route( - "/t", - web::get().to(|data: web::Data| { - assert_eq!(*data, 10); - let _ = data.clone(); - HttpResponse::Ok() - }), + let mut srv = init_service( + App::new().register_data(web::Data::new(1usize)).service( + web::scope("app") + .register_data(web::Data::new(10usize)) + .route( + "/t", + web::get().to(|data: web::Data| { + assert_eq!(*data, 10); + let _ = data.clone(); + HttpResponse::Ok() + }), + ), ), - )); + ); let req = TestRequest::with_uri("/app/t").to_request(); let resp = call_service(&mut srv, req); diff --git a/src/test.rs b/src/test.rs index 903679ca..6563253c 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,5 +1,4 @@ //! Various helpers for Actix applications to use during testing. -use std::cell::RefCell; use std::rc::Rc; use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue}; @@ -7,17 +6,17 @@ use actix_http::http::{HttpTryFrom, Method, StatusCode, Uri, Version}; use actix_http::test::TestRequest as HttpTestRequest; use actix_http::{cookie::Cookie, Extensions, Request}; use actix_router::{Path, ResourceDef, Url}; -use actix_rt::{System, SystemRunner}; use actix_server_config::ServerConfig; use actix_service::{IntoNewService, IntoService, NewService, Service}; use bytes::{Bytes, BytesMut}; -use futures::future::{lazy, ok, Future, IntoFuture}; +use futures::future::{ok, Future}; use futures::Stream; use serde::de::DeserializeOwned; use serde::Serialize; use serde_json; pub use actix_http::test::TestBuffer; +pub use actix_testing::{block_fn, block_on, run_on}; use crate::config::{AppConfig, AppConfigInner}; use crate::data::Data; @@ -27,78 +26,6 @@ use crate::rmap::ResourceMap; use crate::service::{ServiceRequest, ServiceResponse}; use crate::{Error, HttpRequest, HttpResponse}; -thread_local! { - static RT: RefCell = { - RefCell::new(Inner(Some(System::builder().build()))) - }; -} - -struct Inner(Option); - -impl Inner { - fn get_mut(&mut self) -> &mut SystemRunner { - self.0.as_mut().unwrap() - } -} - -impl Drop for Inner { - fn drop(&mut self) { - std::mem::forget(self.0.take().unwrap()) - } -} - -/// Runs the provided future, blocking the current thread until the future -/// completes. -/// -/// This function can be used to synchronously block the current thread -/// until the provided `future` has resolved either successfully or with an -/// error. The result of the future is then returned from this function -/// call. -/// -/// Note that this function is intended to be used only for testing purpose. -/// This function panics on nested call. -pub fn block_on(f: F) -> Result -where - F: IntoFuture, -{ - RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f.into_future())) -} - -/// Runs the provided function, blocking the current thread until the result -/// future completes. -/// -/// This function can be used to synchronously block the current thread -/// until the provided `future` has resolved either successfully or with an -/// error. The result of the future is then returned from this function -/// call. -/// -/// Note that this function is intended to be used only for testing purpose. -/// This function panics on nested call. -pub fn block_fn(f: F) -> Result -where - F: FnOnce() -> R, - R: IntoFuture, -{ - RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(f))) -} - -#[doc(hidden)] -/// Runs the provided function, with runtime enabled. -/// -/// Note that this function is intended to be used only for testing purpose. -/// This function panics on nested call. -pub fn run_on(f: F) -> R -where - F: FnOnce() -> R, -{ - RT.with(move |rt| { - rt.borrow_mut() - .get_mut() - .block_on(lazy(|| Ok::<_, ()>(f()))) - }) - .unwrap() -} - /// Create service that always responds with `HttpResponse::Ok()` pub fn ok_service( ) -> impl Service, Error = Error>