2019-03-27 17:24:55 +01:00
|
|
|
//! Test helpers for actix http client to use during testing.
|
2019-03-30 05:13:39 +01:00
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
|
|
|
|
use actix_http::cookie::{Cookie, CookieJar};
|
|
|
|
use actix_http::http::header::{self, Header, HeaderValue, IntoHeaderValue};
|
2019-04-08 08:06:21 +02:00
|
|
|
use actix_http::http::{HeaderName, HttpTryFrom, StatusCode, Version};
|
2019-03-27 05:54:57 +01:00
|
|
|
use actix_http::{h1, Payload, ResponseHead};
|
|
|
|
use bytes::Bytes;
|
2019-04-01 20:51:18 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
use futures::Future;
|
2019-03-30 05:13:39 +01:00
|
|
|
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
|
2019-03-27 05:54:57 +01:00
|
|
|
|
|
|
|
use crate::ClientResponse;
|
|
|
|
|
2019-03-29 05:48:35 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
thread_local! {
|
|
|
|
static RT: std::cell::RefCell<actix_rt::Runtime> = {
|
|
|
|
std::cell::RefCell::new(actix_rt::Runtime::new().unwrap())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-04-01 20:51:18 +02:00
|
|
|
pub(crate) fn run_on<F, R>(f: F) -> R
|
2019-03-29 05:48:35 +01:00
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
{
|
|
|
|
RT.with(move |rt| {
|
|
|
|
rt.borrow_mut()
|
|
|
|
.block_on(futures::future::lazy(|| Ok::<_, ()>(f())))
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2019-04-01 20:51:18 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) 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-27 05:54:57 +01:00
|
|
|
/// Test `ClientResponse` builder
|
2019-03-27 06:03:00 +01:00
|
|
|
pub struct TestResponse {
|
2019-03-27 05:54:57 +01:00
|
|
|
head: ResponseHead,
|
|
|
|
cookies: CookieJar,
|
|
|
|
payload: Option<Payload>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestResponse {
|
|
|
|
fn default() -> TestResponse {
|
2019-03-27 06:03:00 +01:00
|
|
|
TestResponse {
|
2019-04-08 08:06:21 +02:00
|
|
|
head: ResponseHead::new(StatusCode::OK),
|
2019-03-27 05:54:57 +01:00
|
|
|
cookies: CookieJar::new(),
|
|
|
|
payload: None,
|
2019-03-27 06:03:00 +01:00
|
|
|
}
|
2019-03-27 05:54:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestResponse {
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Create TestResponse and set header
|
2019-03-27 05:54:57 +01:00
|
|
|
pub fn with_header<K, V>(key: K, value: V) -> Self
|
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
2019-03-27 06:03:00 +01:00
|
|
|
Self::default().header(key, value)
|
2019-03-27 05:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Set HTTP version of this response
|
|
|
|
pub fn version(mut self, ver: Version) -> Self {
|
|
|
|
self.head.version = ver;
|
2019-03-27 05:54:57 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a header
|
2019-03-27 06:03:00 +01:00
|
|
|
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
2019-03-27 05:54:57 +01:00
|
|
|
if let Ok(value) = hdr.try_into() {
|
2019-03-27 06:03:00 +01:00
|
|
|
self.head.headers.append(H::name(), value);
|
2019-03-27 05:54:57 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
panic!("Can not set header");
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Append a header
|
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
2019-03-27 05:54:57 +01:00
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
if let Ok(key) = HeaderName::try_from(key) {
|
|
|
|
if let Ok(value) = value.try_into() {
|
2019-03-27 06:03:00 +01:00
|
|
|
self.head.headers.append(key, value);
|
2019-03-27 05:54:57 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Can not create header");
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Set cookie for this response
|
|
|
|
pub fn cookie<'a>(mut self, cookie: Cookie<'a>) -> Self {
|
|
|
|
self.cookies.add(cookie.into_owned());
|
2019-03-27 05:54:57 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Set response's payload
|
|
|
|
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
2019-03-27 05:54:57 +01:00
|
|
|
let mut payload = h1::Payload::empty();
|
|
|
|
payload.unread_data(data.into());
|
2019-03-27 06:03:00 +01:00
|
|
|
self.payload = Some(payload.into());
|
2019-03-27 05:54:57 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
/// Complete response creation and generate `ClientResponse` instance
|
|
|
|
pub fn finish(self) -> ClientResponse {
|
|
|
|
let mut head = self.head;
|
2019-03-27 05:54:57 +01:00
|
|
|
|
2019-03-30 05:13:39 +01:00
|
|
|
let mut cookie = String::new();
|
|
|
|
for c in self.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);
|
|
|
|
}
|
|
|
|
if !cookie.is_empty() {
|
|
|
|
head.headers.insert(
|
|
|
|
header::SET_COOKIE,
|
|
|
|
HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(),
|
|
|
|
);
|
2019-03-27 05:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 06:03:00 +01:00
|
|
|
if let Some(pl) = self.payload {
|
2019-03-27 05:54:57 +01:00
|
|
|
ClientResponse::new(head, pl)
|
|
|
|
} else {
|
|
|
|
ClientResponse::new(head, h1::Payload::empty().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 05:20:33 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::{cookie, http::header};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basics() {
|
|
|
|
let res = TestResponse::default()
|
|
|
|
.version(Version::HTTP_2)
|
|
|
|
.set(header::Date(SystemTime::now().into()))
|
|
|
|
.cookie(cookie::Cookie::build("name", "value").finish())
|
|
|
|
.finish();
|
|
|
|
assert!(res.headers().contains_key(header::SET_COOKIE));
|
|
|
|
assert!(res.headers().contains_key(header::DATE));
|
|
|
|
assert_eq!(res.version(), Version::HTTP_2);
|
|
|
|
}
|
|
|
|
}
|