1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

improve ergomonics of TestRequest

This commit is contained in:
Nikolay Kim 2019-03-02 15:59:05 -08:00
parent de9b38295f
commit 0081b9d446
2 changed files with 30 additions and 26 deletions

View File

@ -669,7 +669,6 @@ impl ResponseBuilder {
} }
#[inline] #[inline]
#[allow(clippy::borrowed_box)]
fn parts<'a>( fn parts<'a>(
parts: &'a mut Option<Message<ResponseHead>>, parts: &'a mut Option<Message<ResponseHead>>,
err: &Option<HttpError>, err: &Option<HttpError>,

View File

@ -37,7 +37,9 @@ use crate::Request;
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST); /// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
/// } /// }
/// ``` /// ```
pub struct TestRequest { pub struct TestRequest(Option<Inner>);
struct Inner {
version: Version, version: Version,
method: Method, method: Method,
uri: Uri, uri: Uri,
@ -49,7 +51,7 @@ pub struct TestRequest {
impl Default for TestRequest { impl Default for TestRequest {
fn default() -> TestRequest { fn default() -> TestRequest {
TestRequest { TestRequest(Some(Inner {
method: Method::GET, method: Method::GET,
uri: Uri::from_str("/").unwrap(), uri: Uri::from_str("/").unwrap(),
version: Version::HTTP_11, version: Version::HTTP_11,
@ -57,19 +59,19 @@ impl Default for TestRequest {
_cookies: None, _cookies: None,
payload: None, payload: None,
prefix: 0, prefix: 0,
} }))
} }
} }
impl TestRequest { impl TestRequest {
/// Create TestRequest and set request uri /// Create TestRequest and set request uri
pub fn with_uri(path: &str) -> TestRequest { pub fn with_uri(path: &str) -> TestRequest {
TestRequest::default().uri(path) TestRequest::default().uri(path).take()
} }
/// Create TestRequest and set header /// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest { pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
TestRequest::default().set(hdr) TestRequest::default().set(hdr).take()
} }
/// Create TestRequest and set header /// Create TestRequest and set header
@ -78,45 +80,45 @@ impl TestRequest {
HeaderName: HttpTryFrom<K>, HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue, V: IntoHeaderValue,
{ {
TestRequest::default().header(key, value) TestRequest::default().header(key, value).take()
} }
/// Set HTTP version of this request /// Set HTTP version of this request
pub fn version(mut self, ver: Version) -> Self { pub fn version(&mut self, ver: Version) -> &mut Self {
self.version = ver; parts(&mut self.0).version = ver;
self self
} }
/// Set HTTP method of this request /// Set HTTP method of this request
pub fn method(mut self, meth: Method) -> Self { pub fn method(&mut self, meth: Method) -> &mut Self {
self.method = meth; parts(&mut self.0).method = meth;
self self
} }
/// Set HTTP Uri of this request /// Set HTTP Uri of this request
pub fn uri(mut self, path: &str) -> Self { pub fn uri(&mut self, path: &str) -> &mut Self {
self.uri = Uri::from_str(path).unwrap(); parts(&mut self.0).uri = Uri::from_str(path).unwrap();
self self
} }
/// Set a header /// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self { pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self {
if let Ok(value) = hdr.try_into() { if let Ok(value) = hdr.try_into() {
self.headers.append(H::name(), value); parts(&mut self.0).headers.append(H::name(), value);
return self; return self;
} }
panic!("Can not set header"); panic!("Can not set header");
} }
/// Set a header /// Set a header
pub fn header<K, V>(mut self, key: K, value: V) -> Self pub fn header<K, V>(&mut self, key: K, value: V) -> &mut 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() {
self.headers.append(key, value); parts(&mut self.0).headers.append(key, value);
return self; return self;
} }
} }
@ -124,29 +126,27 @@ impl TestRequest {
} }
/// Set request payload /// Set request payload
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self { pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
let mut payload = crate::h1::Payload::empty(); let mut payload = crate::h1::Payload::empty();
payload.unread_data(data.into()); payload.unread_data(data.into());
self.payload = Some(payload.into()); parts(&mut self.0).payload = Some(payload.into());
self self
} }
/// Set request's prefix pub(crate) fn take(&mut self) -> TestRequest {
pub fn prefix(mut self, prefix: u16) -> Self { TestRequest(self.0.take())
self.prefix = prefix;
self
} }
/// Complete request creation and generate `Request` instance /// Complete request creation and generate `Request` instance
pub fn finish(self) -> Request { pub fn finish(&mut self) -> Request {
let TestRequest { let Inner {
method, method,
uri, uri,
version, version,
headers, headers,
payload, payload,
.. ..
} = self; } = self.0.take().expect("cannot reuse test request builder");;
let mut req = if let Some(pl) = payload { let mut req = if let Some(pl) = payload {
Request::with_payload(pl) Request::with_payload(pl)
@ -251,3 +251,8 @@ impl TestRequest {
// } // }
// } // }
} }
#[inline]
fn parts<'a>(parts: &'a mut Option<Inner>) -> &'a mut Inner {
parts.as_mut().expect("cannot reuse test request builder")
}