1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Add more tests for route

This commit is contained in:
Nikolay Kim 2019-03-10 18:33:47 -07:00
parent b8829bbf22
commit 9680423025
3 changed files with 40 additions and 81 deletions

View File

@ -526,84 +526,4 @@ mod tests {
let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}
// #[test]
// fn test_handler() {
// let app = App::new()
// .handler("/test", |_: &_| HttpResponse::Ok())
// .finish();
// let req = TestRequest::with_uri("/test").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/test/").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/test/app").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/testapp").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
// let req = TestRequest::with_uri("/blah").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
// }
// #[test]
// fn test_handler2() {
// let app = App::new()
// .handler("test", |_: &_| HttpResponse::Ok())
// .finish();
// let req = TestRequest::with_uri("/test").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/test/").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/test/app").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/testapp").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
// let req = TestRequest::with_uri("/blah").request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
// }
// #[test]
// fn test_route() {
// let app = App::new()
// .route("/test", Method::GET, |_: HttpRequest| HttpResponse::Ok())
// .route("/test", Method::POST, |_: HttpRequest| {
// HttpResponse::Created()
// })
// .finish();
// let req = TestRequest::with_uri("/test").method(Method::GET).request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::OK);
// let req = TestRequest::with_uri("/test")
// .method(Method::POST)
// .request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::CREATED);
// let req = TestRequest::with_uri("/test")
// .method(Method::HEAD)
// .request();
// let resp = app.run(req);
// assert_eq!(resp.as_msg().status(), StatusCode::NOT_FOUND);
// }
}

View File

@ -79,7 +79,7 @@ pub mod dev {
}
pub mod web {
use actix_http::{http::Method, Error, Response};
use actix_http::{http::Method, Response};
use futures::IntoFuture;
pub use actix_http::Response as HttpResponse;
@ -93,6 +93,7 @@ pub mod web {
use crate::route::Route;
use crate::scope::Scope;
pub use crate::error::Error;
pub use crate::extract::{Form, Json, Path, Payload, Query};
pub use crate::extract::{FormConfig, JsonConfig, PayloadConfig};
pub use crate::request::HttpRequest;

View File

@ -424,3 +424,41 @@ where
}))
}
}
#[cfg(test)]
mod tests {
use crate::http::{Method, StatusCode};
use crate::test::{call_success, init_service, TestRequest};
use crate::{web, App, Error, 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 req = TestRequest::with_uri("/test")
.method(Method::GET)
.to_request();
let resp = call_success(&mut srv, req);
assert_eq!(resp.status(), StatusCode::OK);
let req = TestRequest::with_uri("/test")
.method(Method::POST)
.to_request();
let resp = call_success(&mut srv, req);
assert_eq!(resp.status(), StatusCode::CREATED);
let req = TestRequest::with_uri("/test")
.method(Method::HEAD)
.to_request();
let resp = call_success(&mut srv, req);
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
}