1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/test.rs

162 lines
4.1 KiB
Rust
Raw Normal View History

//! Various helpers for Actix applications to use during testing.
2019-03-03 01:24:14 +01:00
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
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};
use actix_http::test::TestRequest;
use actix_http::{Extensions, PayloadStream};
2019-03-02 07:51:32 +01:00
use actix_router::{Path, Url};
2019-03-03 01:24:14 +01:00
use bytes::Bytes;
2019-03-03 01:24:14 +01:00
use crate::request::HttpRequest;
2019-03-02 07:51:32 +01:00
use crate::service::ServiceRequest;
2018-03-20 19:23:35 +01:00
2019-03-02 07:51:32 +01:00
/// Test `Request` builder
2018-03-29 06:49:50 +02:00
///
2019-03-02 07:51:32 +01:00
/// ```rust,ignore
2017-12-27 04:48:02 +01:00
/// # extern crate http;
/// # extern crate actix_web;
/// # use http::{header, StatusCode};
/// # use actix_web::*;
/// use actix_web::test::TestRequest;
///
2018-06-25 06:58:04 +02:00
/// fn index(req: &HttpRequest) -> HttpResponse {
2017-12-27 04:48:02 +01:00
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
/// HttpResponse::Ok().into()
2017-12-27 04:48:02 +01:00
/// } else {
/// HttpResponse::BadRequest().into()
2017-12-27 04:48:02 +01:00
/// }
/// }
///
/// fn main() {
/// let resp = TestRequest::with_header("content-type", "text/plain")
2018-06-21 19:21:28 +02:00
/// .run(&index)
2018-06-01 18:36:16 +02:00
/// .unwrap();
2017-12-27 04:48:02 +01:00
/// assert_eq!(resp.status(), StatusCode::OK);
///
2018-06-21 19:21:28 +02:00
/// let resp = TestRequest::default().run(&index).unwrap();
2017-12-27 04:48:02 +01:00
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
/// }
/// ```
2019-03-03 01:24:14 +01:00
pub struct TestServiceRequest {
req: TestRequest,
extensions: Extensions,
2017-12-27 04:48:02 +01:00
}
2019-03-03 01:24:14 +01:00
impl Default for TestServiceRequest {
fn default() -> TestServiceRequest {
TestServiceRequest {
req: TestRequest::default(),
extensions: Extensions::new(),
2017-12-27 04:48:02 +01:00
}
}
}
2019-03-03 01:24:14 +01:00
impl TestServiceRequest {
2018-01-15 22:47:25 +01:00
/// Create TestRequest and set request uri
2019-03-03 01:24:14 +01:00
pub fn with_uri(path: &str) -> TestServiceRequest {
TestServiceRequest {
req: TestRequest::default().uri(path).take(),
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 01:24:14 +01:00
pub fn with_hdr<H: Header>(hdr: H) -> TestServiceRequest {
TestServiceRequest {
req: TestRequest::default().set(hdr).take(),
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 01:24:14 +01:00
pub fn with_header<K, V>(key: K, value: V) -> TestServiceRequest
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
TestServiceRequest {
req: TestRequest::default().header(key, value).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-03 01:24:14 +01:00
/// Complete request creation and generate `ServiceRequest` instance
pub fn finish(mut self) -> ServiceRequest<PayloadStream> {
let req = self.req.finish();
ServiceRequest::new(
Path::new(Url::new(req.uri().clone())),
req,
Rc::new(self.extensions),
)
}
2017-12-27 04:48:02 +01:00
/// Complete request creation and generate `HttpRequest` instance
2019-03-03 01:24:14 +01:00
pub fn request(mut self) -> HttpRequest {
let req = self.req.finish();
ServiceRequest::new(
Path::new(Url::new(req.uri().clone())),
req,
Rc::new(self.extensions),
)
.into_request()
}
}
impl Deref for TestServiceRequest {
type Target = TestRequest;
2018-07-04 18:52:49 +02:00
2019-03-03 01:24:14 +01:00
fn deref(&self) -> &TestRequest {
&self.req
2017-12-27 04:48:02 +01:00
}
2019-03-03 01:24:14 +01:00
}
2019-03-03 01:24:14 +01:00
impl DerefMut for TestServiceRequest {
fn deref_mut(&mut self) -> &mut TestRequest {
&mut self.req
}
2017-12-27 04:48:02 +01:00
}