mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
update extractor tests
This commit is contained in:
parent
352e7b7a75
commit
d5c54a1867
36
src/app.rs
36
src/app.rs
@ -29,7 +29,8 @@ pub trait HttpServiceFactory<Request> {
|
||||
fn create(self) -> Self::Factory;
|
||||
}
|
||||
|
||||
/// Application builder
|
||||
/// Application builder - structure that follows the builder pattern
|
||||
/// for building application instances.
|
||||
pub struct App<P, T>
|
||||
where
|
||||
T: NewService<Request = ServiceRequest<PayloadStream>, Response = ServiceRequest<P>>,
|
||||
@ -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<T>` 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<S, F, Out>(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<F, U>(mut self, path: &str, f: F) -> Self
|
||||
|
105
src/extractor.rs
105
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::<Info>::from_request(&req, &cfg).poll().unwrap() {
|
||||
// Async::Ready(s) => {
|
||||
// assert_eq!(s.hello, "world");
|
||||
// }
|
||||
// _ => unreachable!(),
|
||||
// }
|
||||
// }
|
||||
let s = rt.block_on(Form::<Info>::from_request(&mut req)).unwrap();
|
||||
assert_eq!(s.hello, "world");
|
||||
}
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn test_option() {
|
||||
|
@ -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,));
|
||||
|
@ -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(),
|
||||
|
@ -34,19 +34,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Clone for MiddlewareFactory<T, S>
|
||||
where
|
||||
T: Transform<S> + Clone,
|
||||
S: Service,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
tr: self.tr.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, C> NewTransform<S, C> for MiddlewareFactory<T, S>
|
||||
where
|
||||
T: Transform<S> + Clone,
|
||||
|
49
src/test.rs
49
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<H: Header>(hdr: H) -> TestServiceRequest {
|
||||
TestServiceRequest {
|
||||
req: TestRequest::default().set(hdr).take(),
|
||||
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest {
|
||||
TestRequest {
|
||||
req: HttpTestRequest::default().set(hdr).take(),
|
||||
extensions: Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create TestRequest and set header
|
||||
pub fn with_header<K, V>(key: K, value: V) -> TestServiceRequest
|
||||
pub fn with_header<K, V>(key: K, value: V) -> TestRequest
|
||||
where
|
||||
HeaderName: HttpTryFrom<K>,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user