1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

update tests

This commit is contained in:
Nikolay Kim 2019-03-05 19:03:59 -08:00
parent 0cf73f1a04
commit 81273f71ef
3 changed files with 51 additions and 60 deletions

View File

@ -902,16 +902,14 @@ mod tests {
use actix_http::http::{Method, StatusCode}; use actix_http::http::{Method, StatusCode};
use super::*; use super::*;
use crate::test::{block_on, TestRequest}; use crate::test::{self, block_on, TestRequest};
use crate::{web, HttpResponse, State}; use crate::{web, HttpResponse, State};
#[test] #[test]
fn test_default_resource() { fn test_default_resource() {
let app = App::new() let mut srv = test::init_service(
.resource("/test", |r| r.to(|| HttpResponse::Ok())) App::new().resource("/test", |r| r.to(|| HttpResponse::Ok())),
.into_new_service(); );
let mut srv = block_on(app.new_service(&())).unwrap();
let req = TestRequest::with_uri("/test").to_request(); let req = TestRequest::with_uri("/test").to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
@ -920,15 +918,15 @@ mod tests {
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND); assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let app = App::new() let mut srv = test::init_service(
.resource("/test", |r| r.to(|| HttpResponse::Ok())) App::new()
.resource("/test2", |r| { .resource("/test", |r| r.to(|| HttpResponse::Ok()))
r.default_resource(|r| r.to(|| HttpResponse::Created())) .resource("/test2", |r| {
.route(web::get().to(|| HttpResponse::Ok())) r.default_resource(|r| r.to(|| HttpResponse::Created()))
}) .route(web::get().to(|| HttpResponse::Ok()))
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())) })
.into_new_service(); .default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
let mut srv = block_on(app.new_service(&())).unwrap(); );
let req = TestRequest::with_uri("/blah").to_request(); let req = TestRequest::with_uri("/blah").to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
@ -947,21 +945,21 @@ mod tests {
#[test] #[test]
fn test_state() { fn test_state() {
let app = App::new() let mut srv = test::init_service(
.state(10usize) App::new()
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())) .state(10usize)
.into_new_service(); .resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
let mut srv = block_on(app.new_service(&())).unwrap(); );
let req = TestRequest::default().to_request(); let req = TestRequest::default().to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
let app = App::new() let mut srv = test::init_service(
.state(10u32) App::new()
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())) .state(10u32)
.into_new_service(); .resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
let mut srv = block_on(app.new_service(&())).unwrap(); );
let req = TestRequest::default().to_request(); let req = TestRequest::default().to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
@ -970,21 +968,21 @@ mod tests {
#[test] #[test]
fn test_state_factory() { fn test_state_factory() {
let app = App::new() let mut srv = test::init_service(
.state_factory(|| Ok::<_, ()>(10usize)) App::new()
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())) .state_factory(|| Ok::<_, ()>(10usize))
.into_new_service(); .resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
let mut srv = block_on(app.new_service(&())).unwrap(); );
let req = TestRequest::default().to_request(); let req = TestRequest::default().to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
let app = App::new() let mut srv = test::init_service(
.state_factory(|| Ok::<_, ()>(10u32)) App::new()
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())) .state_factory(|| Ok::<_, ()>(10u32))
.into_new_service(); .resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
let mut srv = block_on(app.new_service(&())).unwrap(); );
let req = TestRequest::default().to_request(); let req = TestRequest::default().to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();

View File

@ -509,17 +509,14 @@ mod tests {
use actix_service::{IntoNewService, NewService, Service}; use actix_service::{IntoNewService, NewService, Service};
use bytes::Bytes; use bytes::Bytes;
use crate::test::{block_on, TestRequest}; use crate::test::{self, block_on, TestRequest};
use crate::{guard, web, App, HttpRequest, HttpResponse}; use crate::{guard, web, App, HttpRequest, HttpResponse};
#[test] #[test]
fn test_scope() { fn test_scope() {
let app = App::new() let mut srv = test::init_service(App::new().scope("/app", |scope| {
.scope("/app", |scope| { scope.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
scope.resource("/path1", |r| r.to(|| HttpResponse::Ok())) }));
})
.into_new_service();
let mut srv = block_on(app.new_service(&())).unwrap();
let req = TestRequest::with_uri("/app/path1").to_request(); let req = TestRequest::with_uri("/app/path1").to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
@ -528,14 +525,11 @@ mod tests {
#[test] #[test]
fn test_scope_root() { fn test_scope_root() {
let app = App::new() let mut srv = test::init_service(App::new().scope("/app", |scope| {
.scope("/app", |scope| { scope
scope .resource("", |r| r.to(|| HttpResponse::Ok()))
.resource("", |r| r.to(|| HttpResponse::Ok())) .resource("/", |r| r.to(|| HttpResponse::Created()))
.resource("/", |r| r.to(|| HttpResponse::Created())) }));
})
.into_new_service();
let mut srv = block_on(app.new_service(&())).unwrap();
let req = TestRequest::with_uri("/app").to_request(); let req = TestRequest::with_uri("/app").to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();
@ -548,12 +542,9 @@ mod tests {
#[test] #[test]
fn test_scope_root2() { fn test_scope_root2() {
let app = App::new() let mut srv = test::init_service(App::new().scope("/app/", |scope| {
.scope("/app/", |scope| { scope.resource("", |r| r.to(|| HttpResponse::Ok()))
scope.resource("", |r| r.to(|| HttpResponse::Ok())) }));
})
.into_new_service();
let mut srv = block_on(app.new_service(&())).unwrap();
let req = TestRequest::with_uri("/app").to_request(); let req = TestRequest::with_uri("/app").to_request();
let resp = block_on(srv.call(req)).unwrap(); let resp = block_on(srv.call(req)).unwrap();

View File

@ -41,7 +41,7 @@ where
/// This method accepts application builder instance, and constructs /// This method accepts application builder instance, and constructs
/// service. /// service.
/// ///
/// ```rust /// ```rust,ignore
/// use actix_web::{test, App, HttpResponse, http::StatusCode}; /// use actix_web::{test, App, HttpResponse, http::StatusCode};
/// use actix_service::Service; /// use actix_service::Service;
/// ///
@ -80,7 +80,9 @@ where
/// * `TestRequest::to_http_request` creates `HttpRequest` instance, which is used for testing handlers. /// * `TestRequest::to_http_request` creates `HttpRequest` instance, which is used for testing handlers.
/// ///
/// ```rust,ignore /// ```rust,ignore
/// use actix_web::test; /// # use futures::IntoFuture;
/// use actix_web::{test, HttpRequest, HttpResponse, HttpMessage};
/// use actix_web::http::{header, StatusCode};
/// ///
/// fn index(req: HttpRequest) -> HttpResponse { /// fn index(req: HttpRequest) -> HttpResponse {
/// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) { /// if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
@ -94,11 +96,11 @@ where
/// let req = test::TestRequest::with_header("content-type", "text/plain") /// let req = test::TestRequest::with_header("content-type", "text/plain")
/// .to_http_request(); /// .to_http_request();
/// ///
/// let resp = test::block_on(index(req)); /// let resp = test::block_on(index(req).into_future()).unwrap();
/// assert_eq!(resp.status(), StatusCode::OK); /// assert_eq!(resp.status(), StatusCode::OK);
/// ///
/// let req = test::TestRequest::default().to_http_request(); /// let req = test::TestRequest::default().to_http_request();
/// let resp = test::block_on(index(req)); /// let resp = test::block_on(index(req).into_future()).unwrap();
/// assert_eq!(resp.status(), StatusCode::BAD_REQUEST); /// assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
/// } /// }
/// ``` /// ```