mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
update tests
This commit is contained in:
parent
0cf73f1a04
commit
81273f71ef
68
src/app.rs
68
src/app.rs
@ -902,16 +902,14 @@ mod tests {
|
||||
use actix_http::http::{Method, StatusCode};
|
||||
|
||||
use super::*;
|
||||
use crate::test::{block_on, TestRequest};
|
||||
use crate::test::{self, block_on, TestRequest};
|
||||
use crate::{web, HttpResponse, State};
|
||||
|
||||
#[test]
|
||||
fn test_default_resource() {
|
||||
let app = App::new()
|
||||
.resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
|
||||
let mut srv = test::init_service(
|
||||
App::new().resource("/test", |r| r.to(|| HttpResponse::Ok())),
|
||||
);
|
||||
let req = TestRequest::with_uri("/test").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
@ -920,15 +918,15 @@ mod tests {
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
|
||||
let app = App::new()
|
||||
.resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
||||
.resource("/test2", |r| {
|
||||
r.default_resource(|r| r.to(|| HttpResponse::Created()))
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
||||
.resource("/test2", |r| {
|
||||
r.default_resource(|r| r.to(|| HttpResponse::Created()))
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
|
||||
);
|
||||
|
||||
let req = TestRequest::with_uri("/blah").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
@ -947,21 +945,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_state() {
|
||||
let app = App::new()
|
||||
.state(10usize)
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.state(10usize)
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let app = App::new()
|
||||
.state(10u32)
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.state(10u32)
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
@ -970,21 +968,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_state_factory() {
|
||||
let app = App::new()
|
||||
.state_factory(|| Ok::<_, ()>(10usize))
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.state_factory(|| Ok::<_, ()>(10usize))
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let app = App::new()
|
||||
.state_factory(|| Ok::<_, ()>(10u32))
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(
|
||||
App::new()
|
||||
.state_factory(|| Ok::<_, ()>(10u32))
|
||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||
);
|
||||
|
||||
let req = TestRequest::default().to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
|
33
src/scope.rs
33
src/scope.rs
@ -509,17 +509,14 @@ mod tests {
|
||||
use actix_service::{IntoNewService, NewService, Service};
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::test::{block_on, TestRequest};
|
||||
use crate::test::{self, block_on, TestRequest};
|
||||
use crate::{guard, web, App, HttpRequest, HttpResponse};
|
||||
|
||||
#[test]
|
||||
fn test_scope() {
|
||||
let app = App::new()
|
||||
.scope("/app", |scope| {
|
||||
scope.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(App::new().scope("/app", |scope| {
|
||||
scope.resource("/path1", |r| r.to(|| HttpResponse::Ok()))
|
||||
}));
|
||||
|
||||
let req = TestRequest::with_uri("/app/path1").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
@ -528,14 +525,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_scope_root() {
|
||||
let app = App::new()
|
||||
.scope("/app", |scope| {
|
||||
scope
|
||||
.resource("", |r| r.to(|| HttpResponse::Ok()))
|
||||
.resource("/", |r| r.to(|| HttpResponse::Created()))
|
||||
})
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(App::new().scope("/app", |scope| {
|
||||
scope
|
||||
.resource("", |r| r.to(|| HttpResponse::Ok()))
|
||||
.resource("/", |r| r.to(|| HttpResponse::Created()))
|
||||
}));
|
||||
|
||||
let req = TestRequest::with_uri("/app").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
@ -548,12 +542,9 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_scope_root2() {
|
||||
let app = App::new()
|
||||
.scope("/app/", |scope| {
|
||||
scope.resource("", |r| r.to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.into_new_service();
|
||||
let mut srv = block_on(app.new_service(&())).unwrap();
|
||||
let mut srv = test::init_service(App::new().scope("/app/", |scope| {
|
||||
scope.resource("", |r| r.to(|| HttpResponse::Ok()))
|
||||
}));
|
||||
|
||||
let req = TestRequest::with_uri("/app").to_request();
|
||||
let resp = block_on(srv.call(req)).unwrap();
|
||||
|
10
src/test.rs
10
src/test.rs
@ -41,7 +41,7 @@ where
|
||||
/// This method accepts application builder instance, and constructs
|
||||
/// service.
|
||||
///
|
||||
/// ```rust
|
||||
/// ```rust,ignore
|
||||
/// use actix_web::{test, App, HttpResponse, http::StatusCode};
|
||||
/// use actix_service::Service;
|
||||
///
|
||||
@ -80,7 +80,9 @@ where
|
||||
/// * `TestRequest::to_http_request` creates `HttpRequest` instance, which is used for testing handlers.
|
||||
///
|
||||
/// ```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 {
|
||||
/// 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")
|
||||
/// .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);
|
||||
///
|
||||
/// 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);
|
||||
/// }
|
||||
/// ```
|
||||
|
Loading…
Reference in New Issue
Block a user