1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

provide during test request construction

This commit is contained in:
Nikolay Kim 2019-04-12 11:22:18 -07:00
parent 5cfba5ff16
commit 3fb7343e73

View File

@ -9,34 +9,35 @@ use actix_router::{Path, Url};
use crate::{FramedRequest, State}; use crate::{FramedRequest, State};
/// Test `Request` builder. /// Test `Request` builder.
pub struct TestRequest { pub struct TestRequest<S = ()> {
req: HttpTestRequest, req: HttpTestRequest,
path: Path<Url>, path: Path<Url>,
state: State<S>,
} }
impl Default for TestRequest { impl Default for TestRequest<()> {
fn default() -> TestRequest { fn default() -> TestRequest {
TestRequest { TestRequest {
req: HttpTestRequest::default(), req: HttpTestRequest::default(),
path: Path::new(Url::new(Uri::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 /// Create TestRequest and set request uri
pub fn with_uri(path: &str) -> TestRequest { pub fn with_uri(path: &str) -> Self {
Self::get().uri(path) Self::get().uri(path)
} }
/// Create TestRequest and set header /// Create TestRequest and set header
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest { pub fn with_hdr<H: Header>(hdr: H) -> Self {
Self::default().set(hdr) Self::default().set(hdr)
} }
/// Create TestRequest and set header /// Create TestRequest and set header
pub fn with_header<K, V>(key: K, value: V) -> TestRequest pub fn with_header<K, V>(key: K, value: V) -> Self
where where
HeaderName: HttpTryFrom<K>, HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue, V: IntoHeaderValue,
@ -45,14 +46,26 @@ impl TestRequest {
} }
/// Create TestRequest and set method to `Method::GET` /// Create TestRequest and set method to `Method::GET`
pub fn get() -> TestRequest { pub fn get() -> Self {
Self::default().method(Method::GET) Self::default().method(Method::GET)
} }
/// Create TestRequest and set method to `Method::POST` /// Create TestRequest and set method to `Method::POST`
pub fn post() -> TestRequest { pub fn post() -> Self {
Self::default().method(Method::POST) Self::default().method(Method::POST)
} }
}
impl<S> TestRequest<S> {
/// Create TestRequest and set request uri
pub fn with_state(state: S) -> TestRequest<S> {
let req = TestRequest::get();
TestRequest {
state: State::new(state),
req: req.req,
path: req.path,
}
}
/// Set HTTP version of this request /// Set HTTP version of this request
pub fn version(mut self, ver: Version) -> Self { pub fn version(mut self, ver: Version) -> Self {
@ -95,16 +108,11 @@ impl TestRequest {
} }
/// Complete request creation and generate `Request` instance /// Complete request creation and generate `Request` instance
pub fn finish(self) -> FramedRequest<TestBuffer, ()> { pub fn finish(mut self) -> FramedRequest<TestBuffer, S> {
self.finish_with_state(())
}
/// Complete request creation and generate `Request` instance
pub fn finish_with_state<S>(mut self, state: S) -> FramedRequest<TestBuffer, S> {
let req = self.req.finish(); let req = self.req.finish();
self.path.get_mut().update(req.uri()); self.path.get_mut().update(req.uri());
let framed = Framed::new(TestBuffer::empty(), Codec::default()); 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)
} }
} }