From 615fbb49bdeb3fd8efa83672a7b71caf4f146465 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 10 Mar 2019 17:00:03 -0700 Subject: [PATCH] support cookies in TestRequest --- src/test.rs | 117 +++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 93 deletions(-) diff --git a/src/test.rs b/src/test.rs index e2e0fd76e..4ac30be00 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,10 +1,12 @@ //! Test Various helpers for Actix applications to use during testing. +use std::fmt::Write as FmtWrite; use std::str::FromStr; use bytes::Bytes; -use cookie::Cookie; -use http::header::HeaderName; +use cookie::{Cookie, CookieJar}; +use http::header::{self, HeaderName, HeaderValue}; use http::{HeaderMap, HttpTryFrom, Method, Uri, Version}; +use percent_encoding::{percent_encode, USERINFO_ENCODE_SET}; use crate::header::{Header, IntoHeaderValue}; use crate::payload::Payload; @@ -44,7 +46,7 @@ struct Inner { method: Method, uri: Uri, headers: HeaderMap, - _cookies: Option>>, + cookies: CookieJar, payload: Option, } @@ -55,7 +57,7 @@ impl Default for TestRequest { uri: Uri::from_str("/").unwrap(), version: Version::HTTP_11, headers: HeaderMap::new(), - _cookies: None, + cookies: CookieJar::new(), payload: None, })) } @@ -123,6 +125,12 @@ impl TestRequest { panic!("Can not create header"); } + /// Set cookie for this request + pub fn cookie<'a>(mut self, cookie: Cookie<'a>) -> Self { + parts(&mut self.0).cookies.add(cookie.into_owned()); + self + } + /// Set request payload pub fn set_payload>(&mut self, data: B) -> &mut Self { let mut payload = crate::h1::Payload::empty(); @@ -143,7 +151,7 @@ impl TestRequest { version, headers, payload, - .. + cookies, } = self.0.take().expect("cannot reuse test request builder");; let mut req = if let Some(pl) = payload { @@ -158,96 +166,19 @@ impl TestRequest { head.version = version; head.headers = headers; - // req.set_cookies(cookies); + let mut cookie = String::new(); + for c in cookies.delta() { + let name = percent_encode(c.name().as_bytes(), USERINFO_ENCODE_SET); + let value = percent_encode(c.value().as_bytes(), USERINFO_ENCODE_SET); + let _ = write!(&mut cookie, "; {}={}", name, value); + } + head.headers.insert( + header::COOKIE, + HeaderValue::from_str(&cookie.as_str()[2..]).unwrap(), + ); + req } - - // /// This method generates `HttpRequest` instance and runs handler - // /// with generated request. - // pub fn run>(self, h: &H) -> Result { - // let req = self.finish(); - // let resp = h.handle(&req); - - // match resp.respond_to(&req) { - // Ok(resp) => match resp.into().into() { - // AsyncResultItem::Ok(resp) => Ok(resp), - // AsyncResultItem::Err(err) => Err(err), - // AsyncResultItem::Future(fut) => { - // let mut sys = System::new("test"); - // sys.block_on(fut) - // } - // }, - // Err(err) => Err(err.into()), - // } - // } - - // /// This method generates `HttpRequest` instance and runs handler - // /// with generated request. - // /// - // /// This method panics is handler returns actor. - // pub fn run_async(self, h: H) -> Result - // where - // H: Fn(HttpRequest) -> F + 'static, - // F: Future + 'static, - // R: Responder + 'static, - // E: Into + 'static, - // { - // let req = self.finish(); - // let fut = h(req.clone()); - - // let mut sys = System::new("test"); - // match sys.block_on(fut) { - // Ok(r) => match r.respond_to(&req) { - // Ok(reply) => match reply.into().into() { - // AsyncResultItem::Ok(resp) => Ok(resp), - // _ => panic!("Nested async replies are not supported"), - // }, - // Err(e) => Err(e), - // }, - // Err(err) => Err(err), - // } - // } - - // /// This method generates `HttpRequest` instance and executes handler - // pub fn run_async_result(self, f: F) -> Result - // where - // F: FnOnce(&HttpRequest) -> R, - // R: Into>, - // { - // let req = self.finish(); - // let res = f(&req); - - // match res.into().into() { - // AsyncResultItem::Ok(resp) => Ok(resp), - // AsyncResultItem::Err(err) => Err(err), - // AsyncResultItem::Future(fut) => { - // let mut sys = System::new("test"); - // sys.block_on(fut) - // } - // } - // } - - // /// This method generates `HttpRequest` instance and executes handler - // pub fn execute(self, f: F) -> Result - // where - // F: FnOnce(&HttpRequest) -> R, - // R: Responder + 'static, - // { - // let req = self.finish(); - // let resp = f(&req); - - // match resp.respond_to(&req) { - // Ok(resp) => match resp.into().into() { - // AsyncResultItem::Ok(resp) => Ok(resp), - // AsyncResultItem::Err(err) => Err(err), - // AsyncResultItem::Future(fut) => { - // let mut sys = System::new("test"); - // sys.block_on(fut) - // } - // }, - // Err(err) => Err(err.into()), - // } - // } } #[inline]