From 0e2f9a91061b083d4129ea62d49c340da5d79c85 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 7 Jan 2021 01:04:35 +0000 Subject: [PATCH] use new header signatures on test structures --- actix-http/src/test.rs | 74 ++++++++++++++++++------------------------ src/test.rs | 37 +++++++-------------- 2 files changed, 43 insertions(+), 68 deletions(-) diff --git a/actix-http/src/test.rs b/actix-http/src/test.rs index 8761b96cc..0b99ec41e 100644 --- a/actix-http/src/test.rs +++ b/actix-http/src/test.rs @@ -2,7 +2,6 @@ use std::{ cell::{Ref, RefCell}, - convert::TryFrom, io::{self, Read, Write}, pin::Pin, rc::Rc, @@ -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 /// @@ -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(hdr: H) -> TestRequest { - TestRequest::default().set(hdr).take() - } - - /// Create TestRequest and set header - pub fn with_header(key: K, value: V) -> TestRequest + /// Create a default TestRequest and then set a header. + pub fn with_header(header: H) -> TestRequest where - HeaderName: TryFrom, - >::Error: Into, - V: IntoHeaderValue, + H: IntoHeaderPair, + H::Error: Into, { - 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(&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(&mut self, key: K, value: V) -> &mut Self + /// Set a header. + pub fn header(&mut self, header: H) -> &mut Self where - HeaderName: TryFrom, - >::Error: Into, - V: IntoHeaderValue, + H: IntoHeaderPair, + H::Error: Into, { - 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>(&mut self, data: B) -> &mut Self { let mut payload = crate::h1::Payload::empty(); payload.unread_data(data.into()); @@ -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"); diff --git a/src/test.rs b/src/test.rs index f8b789d1b..64bfe35cf 100644 --- a/src/test.rs +++ b/src/test.rs @@ -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}; @@ -390,18 +390,12 @@ impl TestRequest { } /// Create TestRequest and set header - pub fn with_hdr(hdr: H) -> TestRequest { - TestRequest::default().set(hdr) - } - - /// Create TestRequest and set header - pub fn with_header(key: K, value: V) -> TestRequest + pub fn with_header(header: H) -> TestRequest where - HeaderName: TryFrom, - >::Error: Into, - V: IntoHeaderValue, + H: IntoHeaderPair, + H::Error: Into, { - TestRequest::default().header(key, value) + TestRequest::default().header(header) } /// Create TestRequest and set method to `Method::GET` @@ -448,19 +442,12 @@ impl TestRequest { } /// Set a header - pub fn set(mut self, hdr: H) -> Self { - self.req.set(hdr); - self - } - - /// Set a header - pub fn header(mut self, key: K, value: V) -> Self + pub fn header(mut self, header: H) -> Self where - HeaderName: TryFrom, - >::Error: Into, - V: IntoHeaderValue, + H: IntoHeaderPair, + H::Error: Into, { - self.req.header(key, value); + self.req.header(header); self } @@ -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 } @@ -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 }