1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00
actix-web/awc/src/test.rs

120 lines
3.4 KiB
Rust
Raw Normal View History

2019-03-27 17:24:55 +01:00
//! Test helpers for actix http client to use during testing.
use actix_http::{h1, header::TryIntoHeaderPair, Payload, ResponseHead, StatusCode, Version};
2019-03-27 05:54:57 +01:00
use bytes::Bytes;
#[cfg(feature = "cookies")]
use crate::cookie::{Cookie, CookieJar};
2019-03-27 05:54:57 +01:00
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,
2021-02-13 16:08:43 +01:00
#[cfg(feature = "cookies")]
2019-03-27 05:54:57 +01:00
cookies: CookieJar,
payload: Option<Payload>,
}
impl Default for TestResponse {
fn default() -> TestResponse {
2019-03-27 06:03:00 +01:00
TestResponse {
head: ResponseHead::new(StatusCode::OK),
2021-02-13 16:08:43 +01:00
#[cfg(feature = "cookies")]
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
pub fn with_header(header: impl TryIntoHeaderPair) -> Self {
Self::default().insert_header(header)
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
}
/// Insert a header
pub fn insert_header(mut self, header: impl TryIntoHeaderPair) -> Self {
if let Ok((key, value)) = header.try_into_pair() {
self.head.headers.insert(key, 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 append_header(mut self, header: impl TryIntoHeaderPair) -> Self {
if let Ok((key, value)) = header.try_into_pair() {
self.head.headers.append(key, value);
return self;
2019-03-27 05:54:57 +01:00
}
panic!("Can not create header");
}
2019-03-27 06:03:00 +01:00
/// Set cookie for this response
2021-02-13 16:08:43 +01:00
#[cfg(feature = "cookies")]
2019-07-17 11:08:30 +02:00
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
2019-03-27 06:03:00 +01:00
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 {
let (_, mut payload) = h1::Payload::create(true);
2019-03-27 05:54:57 +01:00
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 {
2021-02-13 16:08:43 +01:00
// allow unused mut when cookies feature is disabled
#[allow(unused_mut)]
2019-03-27 06:03:00 +01:00
let mut head = self.head;
2019-03-27 05:54:57 +01:00
2021-02-13 16:08:43 +01:00
#[cfg(feature = "cookies")]
for cookie in self.cookies.delta() {
use actix_http::header::{self, HeaderValue};
2019-03-30 05:13:39 +01:00
head.headers.insert(
header::SET_COOKIE,
HeaderValue::from_str(&cookie.encoded().to_string()).unwrap(),
2019-03-30 05:13:39 +01:00
);
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 {
let (_, payload) = h1::Payload::create(true);
ClientResponse::new(head, payload.into())
2019-03-27 05:54:57 +01:00
}
}
}
2019-04-15 05:20:33 +02:00
#[cfg(test)]
mod tests {
use std::time::SystemTime;
use actix_http::header::HttpDate;
2019-04-15 05:20:33 +02:00
use super::*;
use crate::{cookie, http::header};
#[test]
fn test_basics() {
let res = TestResponse::default()
.version(Version::HTTP_2)
.insert_header((header::DATE, HttpDate::from(SystemTime::now())))
2019-04-15 05:20:33 +02:00
.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);
}
}