mirror of
https://github.com/actix/actix-web.git
synced 2024-11-20 08:31:09 +00:00
simplify TestResponse builder
This commit is contained in:
parent
d49a8ba53b
commit
959aebb24f
1 changed files with 24 additions and 62 deletions
|
@ -10,35 +10,7 @@ use cookie::{Cookie, CookieJar};
|
||||||
use crate::ClientResponse;
|
use crate::ClientResponse;
|
||||||
|
|
||||||
/// Test `ClientResponse` builder
|
/// Test `ClientResponse` builder
|
||||||
///
|
pub struct TestResponse {
|
||||||
/// ```rust,ignore
|
|
||||||
/// # extern crate http;
|
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// # use http::{header, StatusCode};
|
|
||||||
/// # use actix_web::*;
|
|
||||||
/// use actix_web::test::TestRequest;
|
|
||||||
///
|
|
||||||
/// fn index(req: &HttpRequest) -> Response {
|
|
||||||
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
|
|
||||||
/// Response::Ok().into()
|
|
||||||
/// } else {
|
|
||||||
/// Response::BadRequest().into()
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn main() {
|
|
||||||
/// let resp = TestRequest::with_header("content-type", "text/plain")
|
|
||||||
/// .run(&index)
|
|
||||||
/// .unwrap();
|
|
||||||
/// assert_eq!(resp.status(), StatusCode::OK);
|
|
||||||
///
|
|
||||||
/// let resp = TestRequest::default().run(&index).unwrap();
|
|
||||||
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
pub struct TestResponse(Option<Inner>);
|
|
||||||
|
|
||||||
struct Inner {
|
|
||||||
head: ResponseHead,
|
head: ResponseHead,
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: CookieJar,
|
cookies: CookieJar,
|
||||||
|
@ -47,78 +19,73 @@ struct Inner {
|
||||||
|
|
||||||
impl Default for TestResponse {
|
impl Default for TestResponse {
|
||||||
fn default() -> TestResponse {
|
fn default() -> TestResponse {
|
||||||
TestResponse(Some(Inner {
|
TestResponse {
|
||||||
head: ResponseHead::default(),
|
head: ResponseHead::default(),
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
cookies: CookieJar::new(),
|
cookies: CookieJar::new(),
|
||||||
payload: None,
|
payload: None,
|
||||||
}))
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestResponse {
|
impl TestResponse {
|
||||||
/// Create TestRequest and set header
|
/// Create TestResponse and set header
|
||||||
pub fn with_header<K, V>(key: K, value: V) -> Self
|
pub fn with_header<K, V>(key: K, value: V) -> Self
|
||||||
where
|
where
|
||||||
HeaderName: HttpTryFrom<K>,
|
HeaderName: HttpTryFrom<K>,
|
||||||
V: IntoHeaderValue,
|
V: IntoHeaderValue,
|
||||||
{
|
{
|
||||||
Self::default().header(key, value).take()
|
Self::default().header(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set HTTP version of this request
|
/// Set HTTP version of this response
|
||||||
pub fn version(&mut self, ver: Version) -> &mut Self {
|
pub fn version(mut self, ver: Version) -> Self {
|
||||||
parts(&mut self.0).head.version = ver;
|
self.head.version = ver;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a header
|
/// Set a header
|
||||||
pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self {
|
pub fn set<H: Header>(mut self, hdr: H) -> Self {
|
||||||
if let Ok(value) = hdr.try_into() {
|
if let Ok(value) = hdr.try_into() {
|
||||||
parts(&mut self.0).head.headers.append(H::name(), value);
|
self.head.headers.append(H::name(), value);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
panic!("Can not set header");
|
panic!("Can not set header");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set a header
|
/// Append a header
|
||||||
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
||||||
where
|
where
|
||||||
HeaderName: HttpTryFrom<K>,
|
HeaderName: HttpTryFrom<K>,
|
||||||
V: IntoHeaderValue,
|
V: IntoHeaderValue,
|
||||||
{
|
{
|
||||||
if let Ok(key) = HeaderName::try_from(key) {
|
if let Ok(key) = HeaderName::try_from(key) {
|
||||||
if let Ok(value) = value.try_into() {
|
if let Ok(value) = value.try_into() {
|
||||||
parts(&mut self.0).head.headers.append(key, value);
|
self.head.headers.append(key, value);
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic!("Can not create header");
|
panic!("Can not create header");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set cookie for this request
|
/// Set cookie for this response
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
pub fn cookie<'a>(&mut self, cookie: Cookie<'a>) -> &mut Self {
|
pub fn cookie<'a>(mut self, cookie: Cookie<'a>) -> Self {
|
||||||
parts(&mut self.0).cookies.add(cookie.into_owned());
|
self.cookies.add(cookie.into_owned());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set request payload
|
/// Set response's payload
|
||||||
pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
|
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
||||||
let mut payload = h1::Payload::empty();
|
let mut payload = h1::Payload::empty();
|
||||||
payload.unread_data(data.into());
|
payload.unread_data(data.into());
|
||||||
parts(&mut self.0).payload = Some(payload.into());
|
self.payload = Some(payload.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn take(&mut self) -> Self {
|
/// Complete response creation and generate `ClientResponse` instance
|
||||||
Self(self.0.take())
|
pub fn finish(self) -> ClientResponse {
|
||||||
}
|
let mut head = self.head;
|
||||||
|
|
||||||
/// Complete request creation and generate `Request` instance
|
|
||||||
pub fn finish(&mut self) -> ClientResponse {
|
|
||||||
let inner = self.0.take().expect("cannot reuse test request builder");;
|
|
||||||
let mut head = inner.head;
|
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
{
|
{
|
||||||
|
@ -128,7 +95,7 @@ impl TestResponse {
|
||||||
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
|
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
|
||||||
|
|
||||||
let mut cookie = String::new();
|
let mut cookie = String::new();
|
||||||
for c in inner.cookies.delta() {
|
for c in self.cookies.delta() {
|
||||||
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
|
let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET);
|
||||||
let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
|
let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET);
|
||||||
let _ = write!(&mut cookie, "; {}={}", name, value);
|
let _ = write!(&mut cookie, "; {}={}", name, value);
|
||||||
|
@ -141,15 +108,10 @@ impl TestResponse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(pl) = inner.payload {
|
if let Some(pl) = self.payload {
|
||||||
ClientResponse::new(head, pl)
|
ClientResponse::new(head, pl)
|
||||||
} else {
|
} else {
|
||||||
ClientResponse::new(head, h1::Payload::empty().into())
|
ClientResponse::new(head, h1::Payload::empty().into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn parts<'a>(parts: &'a mut Option<Inner>) -> &'a mut Inner {
|
|
||||||
parts.as_mut().expect("cannot reuse test request builder")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue