mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
update tests
This commit is contained in:
parent
0cf73f1a04
commit
81273f71ef
50
src/app.rs
50
src/app.rs
@ -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(
|
||||||
|
App::new()
|
||||||
.resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
.resource("/test", |r| r.to(|| HttpResponse::Ok()))
|
||||||
.resource("/test2", |r| {
|
.resource("/test2", |r| {
|
||||||
r.default_resource(|r| r.to(|| HttpResponse::Created()))
|
r.default_resource(|r| r.to(|| HttpResponse::Created()))
|
||||||
.route(web::get().to(|| HttpResponse::Ok()))
|
.route(web::get().to(|| HttpResponse::Ok()))
|
||||||
})
|
})
|
||||||
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed()))
|
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
|
||||||
.into_new_service();
|
);
|
||||||
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(
|
||||||
|
App::new()
|
||||||
.state(10usize)
|
.state(10usize)
|
||||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||||
.into_new_service();
|
);
|
||||||
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(
|
||||||
|
App::new()
|
||||||
.state(10u32)
|
.state(10u32)
|
||||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||||
.into_new_service();
|
);
|
||||||
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(
|
||||||
|
App::new()
|
||||||
.state_factory(|| Ok::<_, ()>(10usize))
|
.state_factory(|| Ok::<_, ()>(10usize))
|
||||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||||
.into_new_service();
|
);
|
||||||
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(
|
||||||
|
App::new()
|
||||||
.state_factory(|| Ok::<_, ()>(10u32))
|
.state_factory(|| Ok::<_, ()>(10u32))
|
||||||
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok()))
|
.resource("/", |r| r.to(|_: State<usize>| HttpResponse::Ok())),
|
||||||
.into_new_service();
|
);
|
||||||
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();
|
||||||
|
23
src/scope.rs
23
src/scope.rs
@ -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();
|
||||||
|
10
src/test.rs
10
src/test.rs
@ -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);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
Loading…
Reference in New Issue
Block a user