1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-01 02:44:37 +01:00
actix-extras/awc/src/test.rs

118 lines
3.4 KiB
Rust
Raw Normal View History

2019-03-27 05:54:57 +01:00
//! Test Various helpers for Actix applications to use during testing.
use actix_http::http::header::{Header, IntoHeaderValue};
use actix_http::http::{HeaderName, HttpTryFrom, Version};
use actix_http::{h1, Payload, ResponseHead};
use bytes::Bytes;
#[cfg(feature = "cookies")]
use cookie::{Cookie, CookieJar};
use crate::ClientResponse;
/// Test `ClientResponse` builder
2019-03-27 06:03:00 +01:00
pub struct TestResponse {
2019-03-27 05:54:57 +01:00
head: ResponseHead,
#[cfg(feature = "cookies")]
cookies: CookieJar,
payload: Option<Payload>,
}
impl Default for TestResponse {
fn default() -> TestResponse {
2019-03-27 06:03:00 +01:00
TestResponse {
2019-03-27 05:54:57 +01:00
head: ResponseHead::default(),
#[cfg(feature = "cookies")]
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
2019-03-27 05:54:57 +01:00
#[cfg(feature = "cookies")]
2019-03-27 06:03:00 +01:00
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
#[cfg(feature = "cookies")]
{
use std::fmt::Write as FmtWrite;
use actix_http::http::header::{self, HeaderValue};
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
let mut cookie = String::new();
2019-03-27 06:03:00 +01:00
for c in self.cookies.delta() {
2019-03-27 05:54:57 +01:00
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 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())
}
}
}