use new header signatures on test structures

This commit is contained in:
Rob Ede 2021-01-07 01:04:35 +00:00
parent 90fb1513cc
commit 0e2f9a9106
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
2 changed files with 43 additions and 68 deletions

View File

@ -2,7 +2,6 @@
use std::{ use std::{
cell::{Ref, RefCell}, cell::{Ref, RefCell},
convert::TryFrom,
io::{self, Read, Write}, io::{self, Read, Write},
pin::Pin, pin::Pin,
rc::Rc, rc::Rc,
@ -12,14 +11,17 @@ use std::{
use actix_codec::{AsyncRead, AsyncWrite, ReadBuf}; use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use http::header::{self, HeaderName, HeaderValue}; use http::{
use http::{Error as HttpError, Method, Uri, Version}; header::{self, HeaderValue},
Error as HttpError, Method, Uri, Version,
};
use crate::cookie::{Cookie, CookieJar}; use crate::{
use crate::header::HeaderMap; cookie::{Cookie, CookieJar},
use crate::header::{Header, IntoHeaderValue}; header::{HeaderMap, IntoHeaderPair},
use crate::payload::Payload; payload::Payload,
use crate::Request; Request,
};
/// Test `Request` builder /// Test `Request` builder
/// ///
@ -69,76 +71,62 @@ impl Default for TestRequest {
} }
impl 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 { pub fn with_uri(path: &str) -> TestRequest {
TestRequest::default().uri(path).take() TestRequest::default().uri(path).take()
} }
/// Create TestRequest and set header /// Create a default TestRequest and then set a header.
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest { pub fn with_header<H>(header: H) -> TestRequest
TestRequest::default().set(hdr).take()
}
/// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
where where
HeaderName: TryFrom<K>, H: IntoHeaderPair,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>, H::Error: Into<HttpError>,
V: IntoHeaderValue,
{ {
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 { pub fn version(&mut self, ver: Version) -> &mut Self {
parts(&mut self.0).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) -> &mut Self { pub fn method(&mut self, meth: Method) -> &mut Self {
parts(&mut self.0).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) -> &mut Self { pub fn uri(&mut self, path: &str) -> &mut Self {
parts(&mut self.0).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) -> &mut Self { pub fn header<H>(&mut self, header: 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
where where
HeaderName: TryFrom<K>, H: IntoHeaderPair,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>, H::Error: Into<HttpError>,
V: IntoHeaderValue,
{ {
if let Ok(key) = HeaderName::try_from(key) { match header.try_into_header_pair() {
if let Ok(value) = value.try_into_value() { Ok((key, value)) => {
parts(&mut self.0).headers.append(key, value); parts(&mut self.0).headers.append(key, value);
return self; 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 { pub fn cookie<'a>(&mut self, cookie: Cookie<'a>) -> &mut Self {
parts(&mut self.0).cookies.add(cookie.into_owned()); parts(&mut self.0).cookies.add(cookie.into_owned());
self self
} }
/// Set request payload /// Set request 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) -> &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());
@ -150,7 +138,7 @@ impl TestRequest {
TestRequest(self.0.take()) TestRequest(self.0.take())
} }
/// Complete request creation and generate `Request` instance /// Complete request creation and generate `Request` instance.
pub fn finish(&mut self) -> Request { pub fn finish(&mut self) -> Request {
let inner = self.0.take().expect("cannot reuse test request builder"); let inner = self.0.take().expect("cannot reuse test request builder");

View File

@ -1,12 +1,12 @@
//! Various helpers for Actix applications to use during testing. //! Various helpers for Actix applications to use during testing.
use std::convert::TryFrom;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::rc::Rc; use std::rc::Rc;
use std::sync::mpsc; use std::sync::mpsc;
use std::{fmt, net, thread, time}; use std::{fmt, net, thread, time};
use actix_codec::{AsyncRead, AsyncWrite, Framed}; 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::http::{Error as HttpError, Method, StatusCode, Uri, Version};
use actix_http::test::TestRequest as HttpTestRequest; use actix_http::test::TestRequest as HttpTestRequest;
use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request}; use actix_http::{cookie::Cookie, ws, Extensions, HttpService, Request};
@ -390,18 +390,12 @@ impl TestRequest {
} }
/// Create TestRequest and set header /// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest { pub fn with_header<H>(header: H) -> TestRequest
TestRequest::default().set(hdr)
}
/// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
where where
HeaderName: TryFrom<K>, H: IntoHeaderPair,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>, H::Error: Into<HttpError>,
V: IntoHeaderValue,
{ {
TestRequest::default().header(key, value) TestRequest::default().header(header)
} }
/// Create TestRequest and set method to `Method::GET` /// Create TestRequest and set method to `Method::GET`
@ -448,19 +442,12 @@ impl TestRequest {
} }
/// Set a header /// Set a header
pub fn set<H: Header>(mut self, hdr: H) -> Self { pub fn header<H>(mut self, header: H) -> Self
self.req.set(hdr);
self
}
/// Set a header
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where where
HeaderName: TryFrom<K>, H: IntoHeaderPair,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>, H::Error: Into<HttpError>,
V: IntoHeaderValue,
{ {
self.req.header(key, value); self.req.header(header);
self self
} }
@ -494,7 +481,7 @@ impl TestRequest {
let bytes = serde_urlencoded::to_string(data) let bytes = serde_urlencoded::to_string(data)
.expect("Failed to serialize test data as a urlencoded form"); .expect("Failed to serialize test data as a urlencoded form");
self.req.set_payload(bytes); self.req.set_payload(bytes);
self.req.set(ContentType::form_url_encoded()); self.req.header(ContentType::form_url_encoded());
self self
} }
@ -504,7 +491,7 @@ impl TestRequest {
let bytes = let bytes =
serde_json::to_string(data).expect("Failed to serialize test data to json"); serde_json::to_string(data).expect("Failed to serialize test data to json");
self.req.set_payload(bytes); self.req.set_payload(bytes);
self.req.set(ContentType::json()); self.req.header(ContentType::json());
self self
} }