mirror of https://github.com/fafhrd91/actix-web
add some unit tests
This commit is contained in:
parent
db69279557
commit
6bedb958e4
|
@ -257,13 +257,64 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use actix_web_codegen::{scope};
|
/// use actix_web_codegen::{scope};
|
||||||
///
|
///
|
||||||
/// #[scope("/scope")]
|
/// #[scope("/test")]
|
||||||
/// const mod_inner: () = {
|
/// const mod_inner: () = {
|
||||||
/// use actix_web::{HttpResponse, HttpRequest, Responder, get };
|
/// use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web };
|
||||||
/// use actix_web::web::Json;
|
/// use actix_web::web::Json;
|
||||||
///
|
///
|
||||||
/// #[get("/test")]
|
/// #[actix_web::get("/test")]
|
||||||
/// pub async fn test() -> impl Responder {
|
/// pub async fn test() -> impl Responder {
|
||||||
|
/// mod_test2()
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[actix_web::get("/twicetest/{value}")]
|
||||||
|
/// pub async fn test_twice(value: web::Path<String>) -> impl actix_web::Responder {
|
||||||
|
/// let int_value: i32 = value.parse().unwrap_or(0);
|
||||||
|
/// let doubled = int_value * 2;
|
||||||
|
/// HttpResponse::Ok().body(format!("Twice value: {}", doubled))
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[actix_web::post("/test")]
|
||||||
|
/// pub async fn test_post() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body(format!("post works"))
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[put("/test")]
|
||||||
|
/// pub async fn test_put() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body(format!("put works"))
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[head("/test")]
|
||||||
|
/// pub async fn test_head() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().finish()
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[connect("/test")]
|
||||||
|
/// pub async fn test_connect() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body("CONNECT works")
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[options("/test")]
|
||||||
|
/// pub async fn test_options() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body("OPTIONS works")
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[trace("/test")]
|
||||||
|
/// pub async fn test_trace(req: HttpRequest) -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body(format!("TRACE works: {:?}", req))
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[patch("/test")]
|
||||||
|
/// pub async fn test_patch() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body(format!("patch works"))
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[delete("/test")]
|
||||||
|
/// pub async fn test_delete() -> impl Responder {
|
||||||
|
/// HttpResponse::Ok().body("DELETE works")
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// pub fn mod_test2() -> impl actix_web::Responder {
|
||||||
/// actix_web::HttpResponse::Ok().finish()
|
/// actix_web::HttpResponse::Ok().finish()
|
||||||
/// }
|
/// }
|
||||||
///};
|
///};
|
||||||
|
|
|
@ -385,7 +385,7 @@ async fn test_wrap() {
|
||||||
assert!(body.contains("wrong number of parameters"));
|
assert!(body.contains("wrong number of parameters"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[scope("/works")]
|
#[scope("/test")]
|
||||||
const mod_inner: () = {
|
const mod_inner: () = {
|
||||||
use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web };
|
use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web };
|
||||||
use actix_web::web::Json;
|
use actix_web::web::Json;
|
||||||
|
@ -419,17 +419,17 @@ const mod_inner: () = {
|
||||||
|
|
||||||
#[connect("/test")]
|
#[connect("/test")]
|
||||||
pub async fn test_connect() -> impl Responder {
|
pub async fn test_connect() -> impl Responder {
|
||||||
HttpResponse::Ok().body("CONNECT works")
|
HttpResponse::Ok().body("connect works")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[options("/test")]
|
#[options("/test")]
|
||||||
pub async fn test_options() -> impl Responder {
|
pub async fn test_options() -> impl Responder {
|
||||||
HttpResponse::Ok().body("OPTIONS works")
|
HttpResponse::Ok().body("options works")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[trace("/test")]
|
#[trace("/test")]
|
||||||
pub async fn test_trace(req: HttpRequest) -> impl Responder {
|
pub async fn test_trace(req: HttpRequest) -> impl Responder {
|
||||||
HttpResponse::Ok().body(format!("TRACE works: {:?}", req))
|
HttpResponse::Ok().body(format!("trace works: {:?}", req))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[patch("/test")]
|
#[patch("/test")]
|
||||||
|
@ -439,7 +439,7 @@ const mod_inner: () = {
|
||||||
|
|
||||||
#[delete("/test")]
|
#[delete("/test")]
|
||||||
pub async fn test_delete() -> impl Responder {
|
pub async fn test_delete() -> impl Responder {
|
||||||
HttpResponse::Ok().body("DELETE works")
|
HttpResponse::Ok().body("delete works")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mod_test2() -> impl actix_web::Responder {
|
pub fn mod_test2() -> impl actix_web::Responder {
|
||||||
|
@ -447,4 +447,108 @@ const mod_inner: () = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo: tests to call each endpoint...
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_get_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::GET, srv.url("/test/test"));
|
||||||
|
let response = request.send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_get_param_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::GET, srv.url("/test/twicetest/4"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "Twice value: 8");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_post_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::POST, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "post works");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_put_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::PUT, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "put works");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_head_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::HEAD, srv.url("/test/test"));
|
||||||
|
let response = request.send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_connect_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::CONNECT, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "connect works");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_options_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::OPTIONS, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "options works");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_trace_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::TRACE, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert!(body_str.contains("trace works"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_patch_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::PATCH, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "patch works");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_scope_delete_async() {
|
||||||
|
let srv = actix_test::start(|| App::new().service(mod_inner));
|
||||||
|
|
||||||
|
let request = srv.request(http::Method::DELETE, srv.url("/test/test"));
|
||||||
|
let mut response = request.send().await.unwrap();
|
||||||
|
let body = response.body().await.unwrap();
|
||||||
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
||||||
|
assert_eq!(body_str, "delete works");
|
||||||
|
}
|
Loading…
Reference in New Issue