mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
add async handler tests
This commit is contained in:
parent
e18227cc3d
commit
8d1195d8ac
@ -105,6 +105,7 @@ actix-http-test = { git = "https://github.com/actix/actix-http.git", features=["
|
|||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
tokio-timer = "0.2.8"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
@ -542,8 +542,11 @@ impl<P: 'static> NewService for ResourceEndpoint<P> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
use futures::{Future, IntoFuture};
|
use futures::{Future, IntoFuture};
|
||||||
|
use tokio_timer::sleep;
|
||||||
|
|
||||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
@ -573,6 +576,7 @@ mod tests {
|
|||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
|
.name("test")
|
||||||
.wrap(md)
|
.wrap(md)
|
||||||
.route(web::get().to(|| HttpResponse::Ok())),
|
.route(web::get().to(|| HttpResponse::Ok())),
|
||||||
),
|
),
|
||||||
@ -612,6 +616,17 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_to_async() {
|
||||||
|
let mut srv =
|
||||||
|
init_service(App::new().service(web::resource("/test").to_async(|| {
|
||||||
|
sleep(Duration::from_millis(100)).then(|_| HttpResponse::Ok())
|
||||||
|
})));
|
||||||
|
let req = TestRequest::with_uri("/test").to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_resource() {
|
fn test_default_resource() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
|
47
src/route.rs
47
src/route.rs
@ -410,21 +410,36 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use futures::Future;
|
||||||
|
use tokio_timer::sleep;
|
||||||
|
|
||||||
use crate::http::{Method, StatusCode};
|
use crate::http::{Method, StatusCode};
|
||||||
use crate::test::{call_success, init_service, TestRequest};
|
use crate::test::{call_success, init_service, TestRequest};
|
||||||
use crate::{web, App, Error, HttpResponse};
|
use crate::{error, web, App, HttpResponse};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_route() {
|
fn test_route() {
|
||||||
let mut srv = init_service(
|
let mut srv =
|
||||||
App::new().service(
|
init_service(
|
||||||
web::resource("/test")
|
App::new().service(
|
||||||
.route(web::get().to(|| HttpResponse::Ok()))
|
web::resource("/test")
|
||||||
.route(
|
.route(web::get().to(|| HttpResponse::Ok()))
|
||||||
web::post().to_async(|| Ok::<_, Error>(HttpResponse::Created())),
|
.route(web::put().to(|| {
|
||||||
),
|
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
||||||
),
|
}))
|
||||||
);
|
.route(web::post().to_async(|| {
|
||||||
|
sleep(Duration::from_millis(100))
|
||||||
|
.then(|_| HttpResponse::Created())
|
||||||
|
}))
|
||||||
|
.route(web::delete().to_async(|| {
|
||||||
|
sleep(Duration::from_millis(100)).then(|_| {
|
||||||
|
Err::<HttpResponse, _>(error::ErrorBadRequest("err"))
|
||||||
|
})
|
||||||
|
})),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::GET)
|
.method(Method::GET)
|
||||||
@ -438,6 +453,18 @@ mod tests {
|
|||||||
let resp = call_success(&mut srv, req);
|
let resp = call_success(&mut srv, req);
|
||||||
assert_eq!(resp.status(), StatusCode::CREATED);
|
assert_eq!(resp.status(), StatusCode::CREATED);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/test")
|
||||||
|
.method(Method::PUT)
|
||||||
|
.to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
|
let req = TestRequest::with_uri("/test")
|
||||||
|
.method(Method::DELETE)
|
||||||
|
.to_request();
|
||||||
|
let resp = call_success(&mut srv, req);
|
||||||
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::with_uri("/test")
|
||||||
.method(Method::HEAD)
|
.method(Method::HEAD)
|
||||||
.to_request();
|
.to_request();
|
||||||
|
Loading…
Reference in New Issue
Block a user