mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Use actix-testing for testing utils
This commit is contained in:
parent
aa39b8ca6f
commit
d9af8f66ba
14
CHANGES.md
14
CHANGES.md
@ -1,23 +1,23 @@
|
|||||||
# Changes
|
# Changes
|
||||||
## not released yet
|
|
||||||
|
## [1.0.8] - 2019-09-xx
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
* Add `Scope::register_data` and `Resource::register_data` methods, parallel to
|
* Add `Scope::register_data` and `Resource::register_data` methods, parallel to
|
||||||
`App::register_data`.
|
`App::register_data`.
|
||||||
|
|
||||||
## [1.0.8] - 2019-09-xx
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
* Add `middleware::Condition` that conditionally enables another middleware
|
* Add `middleware::Condition` that conditionally enables another middleware
|
||||||
|
|
||||||
### Fixed
|
|
||||||
|
|
||||||
* Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload`
|
* Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload`
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
* Make UrlEncodedError::Overflow more informativve
|
* Make UrlEncodedError::Overflow more informativve
|
||||||
|
|
||||||
|
* Use actix-testing for testing utils
|
||||||
|
|
||||||
|
|
||||||
## [1.0.7] - 2019-08-29
|
## [1.0.7] - 2019-08-29
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web"
|
name = "actix-web"
|
||||||
version = "1.0.7"
|
version = "1.0.8"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
|
description = "Actix web is a simple, pragmatic and extremely fast web framework for Rust."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -81,6 +81,7 @@ actix-web-codegen = "0.1.2"
|
|||||||
actix-http = "0.2.9"
|
actix-http = "0.2.9"
|
||||||
actix-server = "0.6.0"
|
actix-server = "0.6.0"
|
||||||
actix-server-config = "0.1.2"
|
actix-server-config = "0.1.2"
|
||||||
|
actix-testing = "0.1.0"
|
||||||
actix-threadpool = "0.1.1"
|
actix-threadpool = "0.1.1"
|
||||||
awc = { version = "0.2.4", optional = true }
|
awc = { version = "0.2.4", optional = true }
|
||||||
|
|
||||||
|
@ -786,13 +786,17 @@ mod tests {
|
|||||||
.data(10usize)
|
.data(10usize)
|
||||||
.register_data(web::Data::new('*'))
|
.register_data(web::Data::new('*'))
|
||||||
.guard(guard::Get())
|
.guard(guard::Get())
|
||||||
.to(|data1: web::Data<usize>, data2: web::Data<char>, data3: web::Data<f64>| {
|
.to(
|
||||||
|
|data1: web::Data<usize>,
|
||||||
|
data2: web::Data<char>,
|
||||||
|
data3: web::Data<f64>| {
|
||||||
assert_eq!(*data1, 10);
|
assert_eq!(*data1, 10);
|
||||||
assert_eq!(*data2, '*');
|
assert_eq!(*data2, '*');
|
||||||
assert_eq!(*data3, 1.0);
|
assert_eq!(*data3, 1.0);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}),
|
},
|
||||||
)
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let req = TestRequest::get().uri("/test").to_request();
|
let req = TestRequest::get().uri("/test").to_request();
|
||||||
|
10
src/scope.rs
10
src/scope.rs
@ -1093,8 +1093,11 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_override_register_data() {
|
fn test_override_register_data() {
|
||||||
let mut srv = init_service(App::new().register_data(web::Data::new(1usize)).service(
|
let mut srv = init_service(
|
||||||
web::scope("app").register_data(web::Data::new(10usize)).route(
|
App::new().register_data(web::Data::new(1usize)).service(
|
||||||
|
web::scope("app")
|
||||||
|
.register_data(web::Data::new(10usize))
|
||||||
|
.route(
|
||||||
"/t",
|
"/t",
|
||||||
web::get().to(|data: web::Data<usize>| {
|
web::get().to(|data: web::Data<usize>| {
|
||||||
assert_eq!(*data, 10);
|
assert_eq!(*data, 10);
|
||||||
@ -1102,7 +1105,8 @@ mod tests {
|
|||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
));
|
),
|
||||||
|
);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/app/t").to_request();
|
let req = TestRequest::with_uri("/app/t").to_request();
|
||||||
let resp = call_service(&mut srv, req);
|
let resp = call_service(&mut srv, req);
|
||||||
|
77
src/test.rs
77
src/test.rs
@ -1,5 +1,4 @@
|
|||||||
//! Various helpers for Actix applications to use during testing.
|
//! Various helpers for Actix applications to use during testing.
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue};
|
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::test::TestRequest as HttpTestRequest;
|
||||||
use actix_http::{cookie::Cookie, Extensions, Request};
|
use actix_http::{cookie::Cookie, Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Url};
|
use actix_router::{Path, ResourceDef, Url};
|
||||||
use actix_rt::{System, SystemRunner};
|
|
||||||
use actix_server_config::ServerConfig;
|
use actix_server_config::ServerConfig;
|
||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::future::{lazy, ok, Future, IntoFuture};
|
use futures::future::{ok, Future};
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
pub use actix_http::test::TestBuffer;
|
pub use actix_http::test::TestBuffer;
|
||||||
|
pub use actix_testing::{block_fn, block_on, run_on};
|
||||||
|
|
||||||
use crate::config::{AppConfig, AppConfigInner};
|
use crate::config::{AppConfig, AppConfigInner};
|
||||||
use crate::data::Data;
|
use crate::data::Data;
|
||||||
@ -27,78 +26,6 @@ use crate::rmap::ResourceMap;
|
|||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
use crate::{Error, HttpRequest, HttpResponse};
|
use crate::{Error, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static RT: RefCell<Inner> = {
|
|
||||||
RefCell::new(Inner(Some(System::builder().build())))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Inner(Option<SystemRunner>);
|
|
||||||
|
|
||||||
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: F) -> Result<F::Item, F::Error>
|
|
||||||
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, R>(f: F) -> Result<R::Item, R::Error>
|
|
||||||
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, R>(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()`
|
/// Create service that always responds with `HttpResponse::Ok()`
|
||||||
pub fn ok_service(
|
pub fn ok_service(
|
||||||
) -> impl Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>
|
) -> impl Service<Request = ServiceRequest, Response = ServiceResponse<Body>, Error = Error>
|
||||||
|
Loading…
Reference in New Issue
Block a user