2017-12-27 01:35:00 +01:00
|
|
|
//! Various helpers for Actix applications to use during testing.
|
2019-03-04 22:25:35 +01:00
|
|
|
use std::cell::RefCell;
|
2019-03-03 01:24:14 +01:00
|
|
|
use std::rc::Rc;
|
2017-12-27 01:35:00 +01:00
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_http::http::header::{Header, HeaderName, IntoHeaderValue};
|
2019-03-03 01:24:14 +01:00
|
|
|
use actix_http::http::{HttpTryFrom, Method, Version};
|
2019-03-03 07:03:45 +01:00
|
|
|
use actix_http::test::TestRequest as HttpTestRequest;
|
2019-03-04 06:02:01 +01:00
|
|
|
use actix_http::{Extensions, PayloadStream, Request};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_router::{Path, Url};
|
2019-03-04 22:25:35 +01:00
|
|
|
use actix_rt::Runtime;
|
2019-03-06 03:47:18 +01:00
|
|
|
use actix_service::{IntoNewService, NewService, Service};
|
2019-03-03 01:24:14 +01:00
|
|
|
use bytes::Bytes;
|
2019-03-04 22:25:35 +01:00
|
|
|
use futures::Future;
|
2017-12-27 01:35:00 +01:00
|
|
|
|
2019-03-03 01:24:14 +01:00
|
|
|
use crate::request::HttpRequest;
|
2019-03-06 03:47:18 +01:00
|
|
|
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
2018-03-20 19:23:35 +01:00
|
|
|
|
2019-03-04 22:25:35 +01:00
|
|
|
thread_local! {
|
|
|
|
static RT: RefCell<Runtime> = {
|
|
|
|
RefCell::new(Runtime::new().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: Future,
|
|
|
|
{
|
|
|
|
RT.with(move |rt| rt.borrow_mut().block_on(f))
|
|
|
|
}
|
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
/// This method accepts application builder instance, and constructs
|
|
|
|
/// service.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_http::http::{test, App, HttpResponse};
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = 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);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn init_service<R, S, B, E>(
|
|
|
|
app: R,
|
|
|
|
) -> impl Service<Request, Response = ServiceResponse<B>, Error = E>
|
|
|
|
where
|
|
|
|
R: IntoNewService<S, Request, ()>,
|
|
|
|
S: NewService<Request, Response = ServiceResponse<B>, Error = E>,
|
|
|
|
S::InitError: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
block_on(app.into_new_service().new_service(&())).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:25:35 +01:00
|
|
|
/// Test `Request` builder.
|
|
|
|
///
|
|
|
|
/// For unit testing, actix provides a request builder type and a simple handler runner. TestRequest implements a builder-like pattern.
|
|
|
|
/// You can generate various types of request via TestRequest's methods:
|
|
|
|
/// * `TestRequest::to_request` creates `actix_http::Request` instance.
|
|
|
|
/// * `TestRequest::to_service` creates `ServiceRequest` instance, which is used for testing middlewares and chain adapters.
|
|
|
|
/// * `TestRequest::to_from` creates `ServiceFromRequest` instance, which is used for testing extractors.
|
|
|
|
/// * `TestRequest::to_http_request` creates `HttpRequest` instance, which is used for testing handlers.
|
2018-03-29 06:49:50 +02:00
|
|
|
///
|
2019-03-02 07:51:32 +01:00
|
|
|
/// ```rust,ignore
|
2019-03-04 22:25:35 +01:00
|
|
|
/// use actix_web::test;
|
2017-12-27 04:48:02 +01:00
|
|
|
///
|
2019-03-03 22:53:31 +01:00
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
2017-12-27 04:48:02 +01:00
|
|
|
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// HttpResponse::Ok().into()
|
2017-12-27 04:48:02 +01:00
|
|
|
/// } else {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// HttpResponse::BadRequest().into()
|
2017-12-27 04:48:02 +01:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-04 22:25:35 +01:00
|
|
|
/// let req = test::TestRequest::with_header("content-type", "text/plain")
|
|
|
|
/// .to_http_request();
|
|
|
|
///
|
|
|
|
/// let resp = test::block_on(index(req));
|
2017-12-27 04:48:02 +01:00
|
|
|
/// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
///
|
2019-03-04 22:25:35 +01:00
|
|
|
/// let req = test::TestRequest::default().to_http_request();
|
|
|
|
/// let resp = test::block_on(index(req));
|
2017-12-27 04:48:02 +01:00
|
|
|
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-03 07:03:45 +01:00
|
|
|
pub struct TestRequest {
|
|
|
|
req: HttpTestRequest,
|
2019-03-03 01:24:14 +01:00
|
|
|
extensions: Extensions,
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 07:03:45 +01:00
|
|
|
impl Default for TestRequest {
|
|
|
|
fn default() -> TestRequest {
|
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default(),
|
2019-03-03 01:24:14 +01:00
|
|
|
extensions: Extensions::new(),
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 07:03:45 +01:00
|
|
|
impl TestRequest {
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Create TestRequest and set request uri
|
2019-03-03 07:03:45 +01:00
|
|
|
pub fn with_uri(path: &str) -> TestRequest {
|
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default().uri(path).take(),
|
2019-03-03 01:24:14 +01:00
|
|
|
extensions: Extensions::new(),
|
|
|
|
}
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
/// Create TestRequest and set header
|
2019-03-03 07:03:45 +01:00
|
|
|
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
|
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default().set(hdr).take(),
|
2019-03-03 01:24:14 +01:00
|
|
|
extensions: Extensions::new(),
|
|
|
|
}
|
2018-03-06 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Create TestRequest and set header
|
2019-03-03 07:03:45 +01:00
|
|
|
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
2017-12-27 04:48:02 +01:00
|
|
|
{
|
2019-03-03 07:03:45 +01:00
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default().header(key, value).take(),
|
2019-03-03 01:24:14 +01:00
|
|
|
extensions: Extensions::new(),
|
2017-12-27 04:48:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 03:47:18 +01:00
|
|
|
/// Create TestRequest and set method to `Method::GET`
|
|
|
|
pub fn get() -> TestRequest {
|
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default().method(Method::GET).take(),
|
|
|
|
extensions: Extensions::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create TestRequest and set method to `Method::POST`
|
|
|
|
pub fn post() -> TestRequest {
|
|
|
|
TestRequest {
|
|
|
|
req: HttpTestRequest::default().method(Method::POST).take(),
|
|
|
|
extensions: Extensions::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Set HTTP version of this request
|
|
|
|
pub fn version(mut self, ver: Version) -> Self {
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.version(ver);
|
2017-12-27 04:48:02 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set HTTP method of this request
|
|
|
|
pub fn method(mut self, meth: Method) -> Self {
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.method(meth);
|
2017-12-27 04:48:02 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set HTTP Uri of this request
|
|
|
|
pub fn uri(mut self, path: &str) -> Self {
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.uri(path);
|
2017-12-27 04:48:02 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-06 04:28:42 +01:00
|
|
|
/// Set a header
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.set(hdr);
|
|
|
|
self
|
2018-03-06 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Set a header
|
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
2017-12-27 04:48:02 +01:00
|
|
|
{
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.header(key, value);
|
2017-12-27 04:48:02 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-02-20 05:01:38 +01:00
|
|
|
/// Set request payload
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
2019-03-03 01:24:14 +01:00
|
|
|
self.req.set_payload(data);
|
2018-02-20 05:01:38 +01:00
|
|
|
self
|
|
|
|
}
|
2018-03-02 04:12:59 +01:00
|
|
|
|
2019-03-04 00:32:47 +01:00
|
|
|
/// Set request config
|
|
|
|
pub fn config<T: 'static>(mut self, data: T) -> Self {
|
|
|
|
self.extensions.insert(data);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-03 01:24:14 +01:00
|
|
|
/// Complete request creation and generate `ServiceRequest` instance
|
2019-03-04 22:25:35 +01:00
|
|
|
pub fn to_service(mut self) -> ServiceRequest<PayloadStream> {
|
2019-03-03 01:24:14 +01:00
|
|
|
let req = self.req.finish();
|
|
|
|
|
|
|
|
ServiceRequest::new(
|
|
|
|
Path::new(Url::new(req.uri().clone())),
|
|
|
|
req,
|
|
|
|
Rc::new(self.extensions),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-04 06:02:01 +01:00
|
|
|
/// Complete request creation and generate `Request` instance
|
|
|
|
pub fn to_request(mut self) -> Request<PayloadStream> {
|
|
|
|
self.req.finish()
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
/// Complete request creation and generate `HttpRequest` instance
|
2019-03-04 06:02:01 +01:00
|
|
|
pub fn to_http_request(mut self) -> HttpRequest {
|
2019-03-03 01:24:14 +01:00
|
|
|
let req = self.req.finish();
|
|
|
|
|
|
|
|
ServiceRequest::new(
|
|
|
|
Path::new(Url::new(req.uri().clone())),
|
|
|
|
req,
|
|
|
|
Rc::new(self.extensions),
|
|
|
|
)
|
|
|
|
.into_request()
|
|
|
|
}
|
2019-03-03 09:57:48 +01:00
|
|
|
|
|
|
|
/// Complete request creation and generate `ServiceFromRequest` instance
|
|
|
|
pub fn to_from(mut self) -> ServiceFromRequest<PayloadStream> {
|
|
|
|
let req = self.req.finish();
|
|
|
|
|
|
|
|
let req = ServiceRequest::new(
|
|
|
|
Path::new(Url::new(req.uri().clone())),
|
|
|
|
req,
|
|
|
|
Rc::new(self.extensions),
|
|
|
|
);
|
|
|
|
ServiceFromRequest::new(req, None)
|
|
|
|
}
|
2019-03-04 22:25:35 +01:00
|
|
|
|
|
|
|
/// 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: Future,
|
|
|
|
{
|
|
|
|
block_on(f)
|
|
|
|
}
|
2019-03-03 01:24:14 +01:00
|
|
|
}
|