Skip to content

Commit

Permalink
add async handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 25, 2019
1 parent e18227c commit 8d1195d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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())),
),
Expand Down Expand Up @@ -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(
Expand Down
47 changes: 37 additions & 10 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down

0 comments on commit 8d1195d

Please sign in to comment.