From 3fb7343e73240ae17e98ee249c93635169295b63 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 12 Apr 2019 11:22:18 -0700 Subject: [PATCH] provide during test request construction --- actix-framed/src/test.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/actix-framed/src/test.rs b/actix-framed/src/test.rs index c890662e..34a15727 100644 --- a/actix-framed/src/test.rs +++ b/actix-framed/src/test.rs @@ -9,34 +9,35 @@ use actix_router::{Path, Url}; use crate::{FramedRequest, State}; /// Test `Request` builder. -pub struct TestRequest { +pub struct TestRequest { req: HttpTestRequest, path: Path, + state: State, } -impl Default for TestRequest { +impl Default for TestRequest<()> { fn default() -> TestRequest { TestRequest { req: HttpTestRequest::default(), path: Path::new(Url::new(Uri::default())), + state: State::new(()), } } } -#[allow(clippy::wrong_self_convention)] -impl TestRequest { +impl TestRequest<()> { /// Create TestRequest and set request uri - pub fn with_uri(path: &str) -> TestRequest { + pub fn with_uri(path: &str) -> Self { Self::get().uri(path) } /// Create TestRequest and set header - pub fn with_hdr(hdr: H) -> TestRequest { + pub fn with_hdr(hdr: H) -> Self { Self::default().set(hdr) } /// Create TestRequest and set header - pub fn with_header(key: K, value: V) -> TestRequest + pub fn with_header(key: K, value: V) -> Self where HeaderName: HttpTryFrom, V: IntoHeaderValue, @@ -45,14 +46,26 @@ impl TestRequest { } /// Create TestRequest and set method to `Method::GET` - pub fn get() -> TestRequest { + pub fn get() -> Self { Self::default().method(Method::GET) } /// Create TestRequest and set method to `Method::POST` - pub fn post() -> TestRequest { + pub fn post() -> Self { Self::default().method(Method::POST) } +} + +impl TestRequest { + /// Create TestRequest and set request uri + pub fn with_state(state: S) -> TestRequest { + let req = TestRequest::get(); + TestRequest { + state: State::new(state), + req: req.req, + path: req.path, + } + } /// Set HTTP version of this request pub fn version(mut self, ver: Version) -> Self { @@ -95,16 +108,11 @@ impl TestRequest { } /// Complete request creation and generate `Request` instance - pub fn finish(self) -> FramedRequest { - self.finish_with_state(()) - } - - /// Complete request creation and generate `Request` instance - pub fn finish_with_state(mut self, state: S) -> FramedRequest { + pub fn finish(mut self) -> FramedRequest { let req = self.req.finish(); self.path.get_mut().update(req.uri()); let framed = Framed::new(TestBuffer::empty(), Codec::default()); - FramedRequest::new(req, framed, self.path, State::new(state)) + FramedRequest::new(req, framed, self.path, self.state) } }