mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-23 16:21:06 +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"
|
||||
env_logger = "0.6"
|
||||
serde_derive = "1.0"
|
||||
tokio-timer = "0.2.8"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
@ -542,8 +542,11 @@ impl<P: 'static> NewService for ResourceEndpoint<P> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_service::Service;
|
||||
use futures::{Future, IntoFuture};
|
||||
use tokio_timer::sleep;
|
||||
|
||||
use crate::http::{header, HeaderValue, Method, StatusCode};
|
||||
use crate::service::{ServiceRequest, ServiceResponse};
|
||||
@ -573,6 +576,7 @@ mod tests {
|
||||
let mut srv = init_service(
|
||||
App::new().service(
|
||||
web::resource("/test")
|
||||
.name("test")
|
||||
.wrap(md)
|
||||
.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]
|
||||
fn test_default_resource() {
|
||||
let mut srv = init_service(
|
||||
|
47
src/route.rs
47
src/route.rs
@ -410,21 +410,36 @@ where
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::Future;
|
||||
use tokio_timer::sleep;
|
||||
|
||||
use crate::http::{Method, StatusCode};
|
||||
use crate::test::{call_success, init_service, TestRequest};
|
||||
use crate::{web, App, Error, HttpResponse};
|
||||
use crate::{error, web, App, HttpResponse};
|
||||
|
||||
#[test]
|
||||
fn test_route() {
|
||||
let mut srv = init_service(
|
||||
App::new().service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
.route(
|
||||
web::post().to_async(|| Ok::<_, Error>(HttpResponse::Created())),
|
||||
),
|
||||
),
|
||||
);
|
||||
let mut srv =
|
||||
init_service(
|
||||
App::new().service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
.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")
|
||||
.method(Method::GET)
|
||||
@ -438,6 +453,18 @@ mod tests {
|
||||
let resp = call_success(&mut srv, req);
|
||||
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")
|
||||
.method(Method::HEAD)
|
||||
.to_request();
|
||||
|
Loading…
Reference in New Issue
Block a user