2019-03-27 16:24:55 +00:00
|
|
|
//! Test helpers for actix http client to use during testing.
|
2019-12-05 17:35:43 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-03-30 04:13:39 +00:00
|
|
|
|
2021-02-13 15:08:43 +00:00
|
|
|
use actix_http::http::header::{Header, IntoHeaderValue};
|
2019-12-05 17:35:43 +00:00
|
|
|
use actix_http::http::{Error as HttpError, HeaderName, StatusCode, Version};
|
2021-02-13 15:08:43 +00:00
|
|
|
#[cfg(feature = "cookies")]
|
|
|
|
use actix_http::{
|
|
|
|
cookie::{Cookie, CookieJar},
|
|
|
|
http::header::{self, HeaderValue},
|
|
|
|
};
|
2019-03-27 04:54:57 +00:00
|
|
|
use actix_http::{h1, Payload, ResponseHead};
|
|
|
|
use bytes::Bytes;
|
|
|
|
|
|
|
|
use crate::ClientResponse;
|
|
|
|
|
|
|
|
/// Test `ClientResponse` builder
|
2019-03-27 05:03:00 +00:00
|
|
|
pub struct TestResponse {
|
2019-03-27 04:54:57 +00:00
|
|
|
head: ResponseHead,
|
2021-02-13 15:08:43 +00:00
|
|
|
#[cfg(feature = "cookies")]
|
2019-03-27 04:54:57 +00:00
|
|
|
cookies: CookieJar,
|
|
|
|
payload: Option<Payload>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestResponse {
|
|
|
|
fn default() -> TestResponse {
|
2019-03-27 05:03:00 +00:00
|
|
|
TestResponse {
|
2019-04-08 06:06:21 +00:00
|
|
|
head: ResponseHead::new(StatusCode::OK),
|
2021-02-13 15:08:43 +00:00
|
|
|
#[cfg(feature = "cookies")]
|
2019-03-27 04:54:57 +00:00
|
|
|
cookies: CookieJar::new(),
|
|
|
|
payload: None,
|
2019-03-27 05:03:00 +00:00
|
|
|
}
|
2019-03-27 04:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestResponse {
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Create TestResponse and set header
|
2019-03-27 04:54:57 +00:00
|
|
|
pub fn with_header<K, V>(key: K, value: V) -> Self
|
|
|
|
where
|
2019-12-05 17:35:43 +00:00
|
|
|
HeaderName: TryFrom<K>,
|
|
|
|
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
2019-03-27 04:54:57 +00:00
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
2019-03-27 05:03:00 +00:00
|
|
|
Self::default().header(key, value)
|
2019-03-27 04:54:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Set HTTP version of this response
|
|
|
|
pub fn version(mut self, ver: Version) -> Self {
|
|
|
|
self.head.version = ver;
|
2019-03-27 04:54:57 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a header
|
2019-03-27 05:03:00 +00:00
|
|
|
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
2021-01-15 02:11:10 +00:00
|
|
|
if let Ok(value) = hdr.try_into_value() {
|
2019-03-27 05:03:00 +00:00
|
|
|
self.head.headers.append(H::name(), value);
|
2019-03-27 04:54:57 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
panic!("Can not set header");
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Append a header
|
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
2019-03-27 04:54:57 +00:00
|
|
|
where
|
2019-12-05 17:35:43 +00:00
|
|
|
HeaderName: TryFrom<K>,
|
|
|
|
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
|
2019-03-27 04:54:57 +00:00
|
|
|
V: IntoHeaderValue,
|
|
|
|
{
|
|
|
|
if let Ok(key) = HeaderName::try_from(key) {
|
2021-01-15 02:11:10 +00:00
|
|
|
if let Ok(value) = value.try_into_value() {
|
2019-03-27 05:03:00 +00:00
|
|
|
self.head.headers.append(key, value);
|
2019-03-27 04:54:57 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Can not create header");
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Set cookie for this response
|
2021-02-13 15:08:43 +00:00
|
|
|
#[cfg(feature = "cookies")]
|
2019-07-17 09:08:30 +00:00
|
|
|
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
2019-03-27 05:03:00 +00:00
|
|
|
self.cookies.add(cookie.into_owned());
|
2019-03-27 04:54:57 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Set response's payload
|
|
|
|
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
2019-03-27 04:54:57 +00:00
|
|
|
let mut payload = h1::Payload::empty();
|
|
|
|
payload.unread_data(data.into());
|
2019-03-27 05:03:00 +00:00
|
|
|
self.payload = Some(payload.into());
|
2019-03-27 04:54:57 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
/// Complete response creation and generate `ClientResponse` instance
|
|
|
|
pub fn finish(self) -> ClientResponse {
|
2021-02-13 15:08:43 +00:00
|
|
|
// allow unused mut when cookies feature is disabled
|
|
|
|
#[allow(unused_mut)]
|
2019-03-27 05:03:00 +00:00
|
|
|
let mut head = self.head;
|
2019-03-27 04:54:57 +00:00
|
|
|
|
2021-02-13 15:08:43 +00:00
|
|
|
#[cfg(feature = "cookies")]
|
2020-06-19 13:34:14 +00:00
|
|
|
for cookie in self.cookies.delta() {
|
2019-03-30 04:13:39 +00:00
|
|
|
head.headers.insert(
|
|
|
|
header::SET_COOKIE,
|
2020-06-19 13:34:14 +00:00
|
|
|
HeaderValue::from_str(&cookie.encoded().to_string()).unwrap(),
|
2019-03-30 04:13:39 +00:00
|
|
|
);
|
2019-03-27 04:54:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:03:00 +00:00
|
|
|
if let Some(pl) = self.payload {
|
2019-03-27 04:54:57 +00:00
|
|
|
ClientResponse::new(head, pl)
|
|
|
|
} else {
|
|
|
|
ClientResponse::new(head, h1::Payload::empty().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 03:20:33 +00: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);
|
|
|
|
}
|
|
|
|
}
|