1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-03-19 19:32:41 +01:00
actix-extras/src/test.rs

190 lines
5.3 KiB
Rust
Raw Normal View History

2019-01-27 10:59:07 -08:00
//! Test Various helpers for Actix applications to use during testing.
2019-03-10 17:00:03 -07:00
use std::fmt::Write as FmtWrite;
2017-12-26 19:48:02 -08:00
use std::str::FromStr;
2018-06-01 09:36:16 -07:00
use bytes::Bytes;
2019-03-10 17:00:03 -07:00
use cookie::{Cookie, CookieJar};
use http::header::{self, HeaderName, HeaderValue};
2018-04-13 16:02:01 -07:00
use http::{HeaderMap, HttpTryFrom, Method, Uri, Version};
2019-03-10 17:00:03 -07:00
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
2018-12-06 14:32:52 -08:00
use crate::header::{Header, IntoHeaderValue};
2019-02-07 13:39:15 -08:00
use crate::payload::Payload;
2019-01-27 10:59:07 -08:00
use crate::Request;
2017-12-26 19:48:02 -08:00
2018-10-05 11:04:59 -07:00
/// Test `Request` builder
2017-12-26 19:48:02 -08:00
///
2018-10-05 11:04:59 -07:00
/// ```rust,ignore
2017-12-26 19:48:02 -08:00
/// # extern crate http;
/// # extern crate actix_web;
/// # use http::{header, StatusCode};
/// # use actix_web::*;
/// use actix_web::test::TestRequest;
///
2018-10-05 11:04:59 -07:00
/// fn index(req: &HttpRequest) -> Response {
2017-12-26 19:48:02 -08:00
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
2018-10-05 11:04:59 -07:00
/// Response::Ok().into()
2017-12-26 19:48:02 -08:00
/// } else {
2018-10-05 11:04:59 -07:00
/// Response::BadRequest().into()
2017-12-26 19:48:02 -08:00
/// }
/// }
///
/// fn main() {
/// let resp = TestRequest::with_header("content-type", "text/plain")
2018-06-21 23:21:28 +06:00
/// .run(&index)
2018-06-01 09:36:16 -07:00
/// .unwrap();
2017-12-26 19:48:02 -08:00
/// assert_eq!(resp.status(), StatusCode::OK);
///
2018-06-21 23:21:28 +06:00
/// let resp = TestRequest::default().run(&index).unwrap();
2017-12-26 19:48:02 -08:00
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
/// }
/// ```
2019-03-02 15:59:05 -08:00
pub struct TestRequest(Option<Inner>);
struct Inner {
2017-12-26 19:48:02 -08:00
version: Version,
method: Method,
uri: Uri,
headers: HeaderMap,
2019-03-10 17:00:03 -07:00
cookies: CookieJar,
2017-12-26 19:48:02 -08:00
payload: Option<Payload>,
}
2018-10-04 21:14:18 -07:00
impl Default for TestRequest {
fn default() -> TestRequest {
2019-03-02 15:59:05 -08:00
TestRequest(Some(Inner {
2017-12-26 19:48:02 -08:00
method: Method::GET,
uri: Uri::from_str("/").unwrap(),
version: Version::HTTP_11,
headers: HeaderMap::new(),
2019-03-10 17:00:03 -07:00
cookies: CookieJar::new(),
2017-12-26 19:48:02 -08:00
payload: None,
2019-03-02 15:59:05 -08:00
}))
2017-12-26 19:48:02 -08:00
}
}
2018-10-04 21:14:18 -07:00
impl TestRequest {
2018-01-16 00:47:25 +03:00
/// Create TestRequest and set request uri
2018-10-04 21:14:18 -07:00
pub fn with_uri(path: &str) -> TestRequest {
2019-03-02 15:59:05 -08:00
TestRequest::default().uri(path).take()
2017-12-26 19:48:02 -08:00
}
2018-03-05 19:28:42 -08:00
/// Create TestRequest and set header
2018-10-04 21:14:18 -07:00
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
2019-03-02 15:59:05 -08:00
TestRequest::default().set(hdr).take()
2018-03-05 19:28:42 -08:00
}
2018-01-16 00:47:25 +03:00
/// Create TestRequest and set header
2018-10-04 21:14:18 -07:00
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
2018-04-13 16:02:01 -07:00
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
2017-12-26 19:48:02 -08:00
{
2019-03-02 15:59:05 -08:00
TestRequest::default().header(key, value).take()
2017-12-26 19:48:02 -08:00
}
/// Set HTTP version of this request
2019-03-02 15:59:05 -08:00
pub fn version(&mut self, ver: Version) -> &mut Self {
parts(&mut self.0).version = ver;
2017-12-26 19:48:02 -08:00
self
}
/// Set HTTP method of this request
2019-03-02 15:59:05 -08:00
pub fn method(&mut self, meth: Method) -> &mut Self {
parts(&mut self.0).method = meth;
2017-12-26 19:48:02 -08:00
self
}
/// Set HTTP Uri of this request
2019-03-02 15:59:05 -08:00
pub fn uri(&mut self, path: &str) -> &mut Self {
parts(&mut self.0).uri = Uri::from_str(path).unwrap();
2017-12-26 19:48:02 -08:00
self
}
2018-03-05 19:28:42 -08:00
/// Set a header
2019-03-02 15:59:05 -08:00
pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self {
2018-03-05 19:28:42 -08:00
if let Ok(value) = hdr.try_into() {
2019-03-02 15:59:05 -08:00
parts(&mut self.0).headers.append(H::name(), value);
2018-04-13 16:02:01 -07:00
return self;
2018-03-05 19:28:42 -08:00
}
panic!("Can not set header");
}
2017-12-26 19:48:02 -08:00
/// Set a header
2019-03-02 15:59:05 -08:00
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
2018-04-13 16:02:01 -07:00
where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
2017-12-26 19:48:02 -08:00
{
if let Ok(key) = HeaderName::try_from(key) {
2018-03-06 00:43:25 -08:00
if let Ok(value) = value.try_into() {
2019-03-02 15:59:05 -08:00
parts(&mut self.0).headers.append(key, value);
2018-04-13 16:02:01 -07:00
return self;
2017-12-26 19:48:02 -08:00
}
}
panic!("Can not create header");
}
2019-03-10 17:00:03 -07:00
/// Set cookie for this request
2019-03-10 17:02:14 -07:00
pub fn cookie<'a>(&mut self, cookie: Cookie<'a>) -> &mut Self {
2019-03-10 17:00:03 -07:00
parts(&mut self.0).cookies.add(cookie.into_owned());
self
}
2018-02-19 20:01:38 -08:00
/// Set request payload
2019-03-02 15:59:05 -08:00
pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
2019-02-07 13:39:15 -08:00
let mut payload = crate::h1::Payload::empty();
payload.unread_data(data.into());
2019-03-02 15:59:05 -08:00
parts(&mut self.0).payload = Some(payload.into());
2018-02-19 20:01:38 -08:00
self
}
2018-03-01 19:12:59 -08:00
2019-03-02 16:04:43 -08:00
pub fn take(&mut self) -> TestRequest {
2019-03-02 15:59:05 -08:00
TestRequest(self.0.take())
2018-07-15 16:24:22 +06:00
}
2018-10-04 21:14:18 -07:00
/// Complete request creation and generate `Request` instance
2019-03-02 15:59:05 -08:00
pub fn finish(&mut self) -> Request {
let Inner {
2018-04-13 16:02:01 -07:00
method,
uri,
version,
headers,
payload,
2019-03-10 17:00:03 -07:00
cookies,
2019-03-02 15:59:05 -08:00
} = self.0.take().expect("cannot reuse test request builder");;
2018-03-07 14:56:53 -08:00
2019-02-06 11:44:15 -08:00
let mut req = if let Some(pl) = payload {
Request::with_payload(pl)
} else {
2019-02-07 13:39:15 -08:00
Request::with_payload(crate::h1::Payload::empty().into())
2019-02-06 11:44:15 -08:00
};
2019-02-07 21:16:46 -08:00
let head = req.head_mut();
head.uri = uri;
head.method = method;
head.version = version;
head.headers = headers;
2019-02-06 11:44:15 -08:00
2019-03-10 17:00:03 -07:00
let mut cookie = String::new();
for c in cookies.delta() {
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
let _ = write!(&mut cookie, "; {}={}", name, value);
}
2019-03-10 17:06:43 -07:00
if !cookie.is_empty() {
head.headers.insert(
header::COOKIE,
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
);
}
2019-03-10 17:00:03 -07:00
2018-06-25 10:58:04 +06:00
req
}
2017-12-26 19:48:02 -08:00
}
2019-03-02 15:59:05 -08:00
#[inline]
fn parts<'a>(parts: &'a mut Option<Inner>) -> &'a mut Inner {
parts.as_mut().expect("cannot reuse test request builder")
}