Skip to content

Commit

Permalink
use new header signatures on test structures
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Jan 11, 2021
1 parent abd3353 commit b459959
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 68 deletions.
74 changes: 31 additions & 43 deletions actix-http/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::{
cell::{Ref, RefCell},
convert::TryFrom,
io::{self, Read, Write},
pin::Pin,
rc::Rc,
Expand All @@ -12,14 +11,17 @@ use std::{

use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
use bytes::{Bytes, BytesMut};
use http::header::{self, HeaderName, HeaderValue};
use http::{Error as HttpError, Method, Uri, Version};
use http::{
header::{self, HeaderValue},
Error as HttpError, Method, Uri, Version,
};

use crate::cookie::{Cookie, CookieJar};
use crate::header::HeaderMap;
use crate::header::{Header, IntoHeaderValue};
use crate::payload::Payload;
use crate::Request;
use crate::{
cookie::{Cookie, CookieJar},
header::{HeaderMap, IntoHeaderPair},
payload::Payload,
Request,
};

/// Test `Request` builder
///
Expand Down Expand Up @@ -69,76 +71,62 @@ impl Default for TestRequest {
}

impl TestRequest {
/// Create TestRequest and set request uri
/// Create a default TestRequest and then set its URI.
pub fn with_uri(path: &str) -> TestRequest {
TestRequest::default().uri(path).take()
}

/// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
TestRequest::default().set(hdr).take()
}

/// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
/// Create a default TestRequest and then set a header.
pub fn with_header<H>(header: H) -> TestRequest
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
H::Error: Into<HttpError>,
{
TestRequest::default().header(key, value).take()
TestRequest::default().header(header).take()
}

/// Set HTTP version of this request
/// Set HTTP version of this request.
pub fn version(&mut self, ver: Version) -> &mut Self {
parts(&mut self.0).version = ver;
self
}

/// Set HTTP method of this request
/// Set HTTP method of this request.
pub fn method(&mut self, meth: Method) -> &mut Self {
parts(&mut self.0).method = meth;
self
}

/// Set HTTP Uri of this request
/// Set HTTP Uri of this request.
pub fn uri(&mut self, path: &str) -> &mut Self {
parts(&mut self.0).uri = Uri::from_str(path).unwrap();
self
}

/// Set a header
pub fn set<H: Header>(&mut self, hdr: H) -> &mut Self {
if let Ok(value) = hdr.try_into_value() {
parts(&mut self.0).headers.append(H::name(), value);
return self;
}
panic!("Can not set header");
}

/// Set a header
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
/// Set a header.
pub fn header<H>(&mut self, header: H) -> &mut Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
H::Error: Into<HttpError>,
{
if let Ok(key) = HeaderName::try_from(key) {
if let Ok(value) = value.try_into_value() {
match header.try_into_header_pair() {
Ok((key, value)) => {
parts(&mut self.0).headers.append(key, value);
return self;
}
Err(err) => {
panic!("Error setting test header: {}.", err.into());
}
}
panic!("Can not create header");
}

/// Set cookie for this request
/// Set cookie for this request.
pub fn cookie<'a>(&mut self, cookie: Cookie<'a>) -> &mut Self {
parts(&mut self.0).cookies.add(cookie.into_owned());
self
}

/// Set request payload
/// Set request payload.
pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self {
let mut payload = crate::h1::Payload::empty();
payload.unread_data(data.into());
Expand All @@ -150,7 +138,7 @@ impl TestRequest {
TestRequest(self.0.take())
}

/// Complete request creation and generate `Request` instance
/// Complete request creation and generate `Request` instance.
pub fn finish(&mut self) -> Request {
let inner = self.0.take().expect("cannot reuse test request builder");

Expand Down
37 changes: 12 additions & 25 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Various helpers for Actix applications to use during testing.
use std::convert::TryFrom;
use std::net::SocketAddr;
use std::rc::Rc;
use std::sync::mpsc;
use std::{fmt, net, thread, time};

use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::http::header::{ContentType, Header, HeaderName, IntoHeaderValue};
use actix_http::http::header::{ContentType, IntoHeaderPair};
use actix_http::http::{Error as HttpError, Method, StatusCode, Uri, Version};
use actix_http::test::TestRequest as HttpTestRequest;
use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
Expand Down Expand Up @@ -390,18 +390,12 @@ impl TestRequest {
}

/// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
TestRequest::default().set(hdr)
}

/// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
pub fn with_header<H>(header: H) -> TestRequest
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
H::Error: Into<HttpError>,
{
TestRequest::default().header(key, value)
TestRequest::default().header(header)
}

/// Create TestRequest and set method to `Method::GET`
Expand Down Expand Up @@ -448,19 +442,12 @@ impl TestRequest {
}

/// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self {
self.req.set(hdr);
self
}

/// Set a header
pub fn header<K, V>(mut self, key: K, value: V) -> Self
pub fn header<H>(mut self, header: H) -> Self
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
H: IntoHeaderPair,
H::Error: Into<HttpError>,
{
self.req.header(key, value);
self.req.header(header);
self
}

Expand Down Expand Up @@ -494,7 +481,7 @@ impl TestRequest {
let bytes = serde_urlencoded::to_string(data)
.expect("Failed to serialize test data as a urlencoded form");
self.req.set_payload(bytes);
self.req.set(ContentType::form_url_encoded());
self.req.header(ContentType::form_url_encoded());
self
}

Expand All @@ -504,7 +491,7 @@ impl TestRequest {
let bytes =
serde_json::to_string(data).expect("Failed to serialize test data to json");
self.req.set_payload(bytes);
self.req.set(ContentType::json());
self.req.header(ContentType::json());
self
}

Expand Down

0 comments on commit b459959

Please sign in to comment.