diff --git a/src/app.rs b/src/app.rs index d9219091..ae510621 100644 --- a/src/app.rs +++ b/src/app.rs @@ -29,7 +29,8 @@ pub trait HttpServiceFactory { fn create(self) -> Self::Factory; } -/// Application builder +/// Application builder - structure that follows the builder pattern +/// for building application instances. pub struct App where T: NewService, Response = ServiceRequest

>, @@ -69,11 +70,8 @@ where InitError = (), >, { - /// Create application with specified state. Application can be - /// configured with a builder-like pattern. - /// - /// State is shared with all resources within same application and - /// could be accessed with `HttpRequest::state()` method. + /// Set application state. Applicatin state could be accessed + /// by using `State` extractor where `T` is state type. /// /// **Note**: http server accepts an application factory rather than /// an application instance. Http server constructs an application @@ -86,7 +84,7 @@ where self } - /// Set application state. This function is + /// Set application state factory. This function is /// similar to `.state()` but it accepts state factory. State get /// constructed asynchronously during application initialization. pub fn state_factory(mut self, state: F) -> Self @@ -119,14 +117,14 @@ where /// `/users/{userid}/{friend}` and store `userid` and `friend` in /// the exposed `Params` object: /// - /// ```rust,ignore + /// ```rust /// # extern crate actix_web; - /// use actix_web::{http, App, HttpResponse}; + /// use actix_web::{web, http, App, HttpResponse}; /// /// fn main() { /// let app = App::new().resource("/users/{userid}/{friend}", |r| { - /// r.get(|r| r.to(|_| HttpResponse::Ok())); - /// r.head(|r| r.to(|_| HttpResponse::MethodNotAllowed())) + /// r.route(web::get().to(|| HttpResponse::Ok())) + /// .route(web::head().to(|| HttpResponse::MethodNotAllowed())) /// }); /// } /// ``` @@ -294,15 +292,17 @@ where /// `/users/{userid}/{friend}` and store `userid` and `friend` in /// the exposed `Params` object: /// - /// ```rust,ignore - /// # extern crate actix_web; - /// use actix_web::{http, App, HttpResponse}; + /// ```rust + /// use actix_web::{web, http, App, HttpResponse}; /// /// fn main() { - /// let app = App::new().resource("/users/{userid}/{friend}", |r| { - /// r.get(|r| r.to(|_| HttpResponse::Ok())); - /// r.head(|r| r.to(|_| HttpResponse::MethodNotAllowed())) - /// }); + /// let app = App::new() + /// .resource("/users/{userid}/{friend}", |r| { + /// r.route(web::to(|| HttpResponse::Ok())) + /// }) + /// .resource("/index.html", |r| { + /// r.route(web::head().to(|| HttpResponse::MethodNotAllowed())) + /// }); /// } /// ``` pub fn resource(mut self, path: &str, f: F) -> Self diff --git a/src/extractor.rs b/src/extractor.rs index 522ce721..04dfb81a 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -999,73 +999,60 @@ tuple_from_req!( (9, J) ); -// #[cfg(test)] -// mod tests { -// use super::*; -// use actix_http::http::header; -// use actix_http::test::TestRequest; -// use bytes::Bytes; -// use futures::{Async, Future}; -// use mime; -// use serde::{Deserialize, Serialize}; +#[cfg(test)] +mod tests { + use actix_http::http::header; + use bytes::Bytes; + use serde_derive::Deserialize; -// use crate::resource::Resource; -// // use crate::router::{ResourceDef, Router}; + use super::*; + use crate::test::TestRequest; -// #[derive(Deserialize, Debug, PartialEq)] -// struct Info { -// hello: String, -// } + #[derive(Deserialize, Debug, PartialEq)] + struct Info { + hello: String, + } -// #[test] -// fn test_bytes() { -// let cfg = PayloadConfig::default(); -// let req = TestRequest::with_header(header::CONTENT_LENGTH, "11") -// .set_payload(Bytes::from_static(b"hello=world")) -// .finish(); + #[test] + fn test_bytes() { + let mut rt = actix_rt::Runtime::new().unwrap(); + let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") + .set_payload(Bytes::from_static(b"hello=world")) + .finish() + .into(); -// match Bytes::from_request(&req, &cfg).unwrap().poll().unwrap() { -// Async::Ready(s) => { -// assert_eq!(s, Bytes::from_static(b"hello=world")); -// } -// _ => unreachable!(), -// } -// } + let s = rt.block_on(Bytes::from_request(&mut req)).unwrap(); + assert_eq!(s, Bytes::from_static(b"hello=world")); + } -// #[test] -// fn test_string() { -// let cfg = PayloadConfig::default(); -// let req = TestRequest::with_header(header::CONTENT_LENGTH, "11") -// .set_payload(Bytes::from_static(b"hello=world")) -// .finish(); + #[test] + fn test_string() { + let mut rt = actix_rt::Runtime::new().unwrap(); + let mut req = TestRequest::with_header(header::CONTENT_LENGTH, "11") + .set_payload(Bytes::from_static(b"hello=world")) + .finish() + .into(); -// match String::from_request(&req, &cfg).unwrap().poll().unwrap() { -// Async::Ready(s) => { -// assert_eq!(s, "hello=world"); -// } -// _ => unreachable!(), -// } -// } + let s = rt.block_on(String::from_request(&mut req)).unwrap(); + assert_eq!(s, "hello=world"); + } -// #[test] -// fn test_form() { -// let req = TestRequest::with_header( -// header::CONTENT_TYPE, -// "application/x-www-form-urlencoded", -// ) -// .header(header::CONTENT_LENGTH, "11") -// .set_payload(Bytes::from_static(b"hello=world")) -// .finish(); + #[test] + fn test_form() { + let mut rt = actix_rt::Runtime::new().unwrap(); + let mut req = TestRequest::with_header( + header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header(header::CONTENT_LENGTH, "11") + .set_payload(Bytes::from_static(b"hello=world")) + .finish() + .into(); -// let mut cfg = FormConfig::default(); -// cfg.limit(4096); -// match Form::::from_request(&req, &cfg).poll().unwrap() { -// Async::Ready(s) => { -// assert_eq!(s.hello, "world"); -// } -// _ => unreachable!(), -// } -// } + let s = rt.block_on(Form::::from_request(&mut req)).unwrap(); + assert_eq!(s.hello, "world"); + } +} // #[test] // fn test_option() { diff --git a/src/filter.rs b/src/filter.rs index e3d87b76..9b49c9dd 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -230,14 +230,14 @@ impl Filter for HeaderFilter { #[cfg(test)] mod tests { - use crate::test::TestServiceRequest; use actix_http::http::{header, Method}; use super::*; + use crate::test::TestRequest; #[test] fn test_header() { - let req = TestServiceRequest::with_header(header::TRANSFER_ENCODING, "chunked") + let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked") .finish() .into_request(); @@ -269,8 +269,8 @@ mod tests { #[test] fn test_methods() { - let req = TestServiceRequest::default().finish().into_request(); - let req2 = TestServiceRequest::default() + let req = TestRequest::default().finish().into_request(); + let req2 = TestRequest::default() .method(Method::POST) .finish() .into_request(); @@ -280,46 +280,38 @@ mod tests { assert!(Post().check(&req2)); assert!(!Post().check(&req)); - let r = TestServiceRequest::default().method(Method::PUT).finish(); + let r = TestRequest::default().method(Method::PUT).finish(); assert!(Put().check(&r,)); assert!(!Put().check(&req,)); - let r = TestServiceRequest::default() - .method(Method::DELETE) - .finish(); + let r = TestRequest::default().method(Method::DELETE).finish(); assert!(Delete().check(&r,)); assert!(!Delete().check(&req,)); - let r = TestServiceRequest::default().method(Method::HEAD).finish(); + let r = TestRequest::default().method(Method::HEAD).finish(); assert!(Head().check(&r,)); assert!(!Head().check(&req,)); - let r = TestServiceRequest::default() - .method(Method::OPTIONS) - .finish(); + let r = TestRequest::default().method(Method::OPTIONS).finish(); assert!(Options().check(&r,)); assert!(!Options().check(&req,)); - let r = TestServiceRequest::default() - .method(Method::CONNECT) - .finish(); + let r = TestRequest::default().method(Method::CONNECT).finish(); assert!(Connect().check(&r,)); assert!(!Connect().check(&req,)); - let r = TestServiceRequest::default().method(Method::PATCH).finish(); + let r = TestRequest::default().method(Method::PATCH).finish(); assert!(Patch().check(&r,)); assert!(!Patch().check(&req,)); - let r = TestServiceRequest::default().method(Method::TRACE).finish(); + let r = TestRequest::default().method(Method::TRACE).finish(); assert!(Trace().check(&r,)); assert!(!Trace().check(&req,)); } #[test] fn test_preds() { - let r = TestServiceRequest::default() - .method(Method::TRACE) - .request(); + let r = TestRequest::default().method(Method::TRACE).request(); assert!(Not(Get()).check(&r,)); assert!(!Not(Trace()).check(&r,)); diff --git a/src/middleware/defaultheaders.rs b/src/middleware/defaultheaders.rs index 83bb94c6..fa287b28 100644 --- a/src/middleware/defaultheaders.rs +++ b/src/middleware/defaultheaders.rs @@ -138,7 +138,7 @@ mod tests { use actix_service::FnService; use super::*; - use crate::test::TestServiceRequest; + use crate::test::TestRequest; use crate::{HttpResponse, ServiceRequest}; #[test] @@ -149,11 +149,11 @@ mod tests { req.into_response(HttpResponse::Ok().finish()) }); - let req = TestServiceRequest::default().finish(); + let req = TestRequest::default().finish(); let resp = rt.block_on(mw.call(req, &mut srv)).unwrap(); assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); - let req = TestServiceRequest::default().finish(); + let req = TestRequest::default().finish(); let mut srv = FnService::new(|req: ServiceRequest<_>| { req.into_response(HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish()) }); @@ -169,7 +169,7 @@ mod tests { req.into_response(HttpResponse::Ok().finish()) }); - let req = TestServiceRequest::default().finish(); + let req = TestRequest::default().finish(); let resp = rt.block_on(mw.call(req, &mut srv)).unwrap(); assert_eq!( resp.headers().get(CONTENT_TYPE).unwrap(), diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 8ef316b4..85127ee2 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -34,19 +34,6 @@ where } } -impl Clone for MiddlewareFactory -where - T: Transform + Clone, - S: Service, -{ - fn clone(&self) -> Self { - Self { - tr: self.tr.clone(), - _t: PhantomData, - } - } -} - impl NewTransform for MiddlewareFactory where T: Transform + Clone, diff --git a/src/test.rs b/src/test.rs index d67696b1..d6caf897 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,10 +1,9 @@ //! Various helpers for Actix applications to use during testing. -use std::ops::{Deref, DerefMut}; use std::rc::Rc; use actix_http::http::header::{Header, HeaderName, IntoHeaderValue}; use actix_http::http::{HttpTryFrom, Method, Version}; -use actix_http::test::TestRequest; +use actix_http::test::TestRequest as HttpTestRequest; use actix_http::{Extensions, PayloadStream}; use actix_router::{Path, Url}; use bytes::Bytes; @@ -39,45 +38,45 @@ use crate::service::ServiceRequest; /// assert_eq!(resp.status(), StatusCode::BAD_REQUEST); /// } /// ``` -pub struct TestServiceRequest { - req: TestRequest, +pub struct TestRequest { + req: HttpTestRequest, extensions: Extensions, } -impl Default for TestServiceRequest { - fn default() -> TestServiceRequest { - TestServiceRequest { - req: TestRequest::default(), +impl Default for TestRequest { + fn default() -> TestRequest { + TestRequest { + req: HttpTestRequest::default(), extensions: Extensions::new(), } } } -impl TestServiceRequest { +impl TestRequest { /// Create TestRequest and set request uri - pub fn with_uri(path: &str) -> TestServiceRequest { - TestServiceRequest { - req: TestRequest::default().uri(path).take(), + pub fn with_uri(path: &str) -> TestRequest { + TestRequest { + req: HttpTestRequest::default().uri(path).take(), extensions: Extensions::new(), } } /// Create TestRequest and set header - pub fn with_hdr(hdr: H) -> TestServiceRequest { - TestServiceRequest { - req: TestRequest::default().set(hdr).take(), + pub fn with_hdr(hdr: H) -> TestRequest { + TestRequest { + req: HttpTestRequest::default().set(hdr).take(), extensions: Extensions::new(), } } /// Create TestRequest and set header - pub fn with_header(key: K, value: V) -> TestServiceRequest + pub fn with_header(key: K, value: V) -> TestRequest where HeaderName: HttpTryFrom, V: IntoHeaderValue, { - TestServiceRequest { - req: TestRequest::default().header(key, value).take(), + TestRequest { + req: HttpTestRequest::default().header(key, value).take(), extensions: Extensions::new(), } } @@ -145,17 +144,3 @@ impl TestServiceRequest { .into_request() } } - -impl Deref for TestServiceRequest { - type Target = TestRequest; - - fn deref(&self) -> &TestRequest { - &self.req - } -} - -impl DerefMut for TestServiceRequest { - fn deref_mut(&mut self) -> &mut TestRequest { - &mut self.req - } -}