From db69279557b769884e7aa7e5310f7b253c96aef2 Mon Sep 17 00:00:00 2001 From: Jon Lim Date: Sun, 10 Sep 2023 16:13:12 -0700 Subject: [PATCH] started some test code --- actix-web-codegen/src/lib.rs | 20 +++----- actix-web-codegen/src/route.rs | 4 +- actix-web-codegen/tests/test_macro.rs | 66 ++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 16 deletions(-) diff --git a/actix-web-codegen/src/lib.rs b/actix-web-codegen/src/lib.rs index 129919e65..ee04fd947 100644 --- a/actix-web-codegen/src/lib.rs +++ b/actix-web-codegen/src/lib.rs @@ -255,23 +255,17 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream { /// # Example /// /// ```rust -/// use actix_web_cute_codegen::{scope}; +/// use actix_web_codegen::{scope}; /// /// #[scope("/scope")] /// const mod_inner: () = { -/// use actix_web_cute_codegen::{get, hook}; -/// use actix_web::{HttpResponse, Responder}; -/// use futures::{Future, future}; +/// use actix_web::{HttpResponse, HttpRequest, Responder, get }; +/// use actix_web::web::Json; /// -/// #[get("/test")] -/// pub fn test() -> impl Responder { -/// HttpResponse::Ok() -/// } -/// -/// #[get("/test_async")] -/// pub fn auto_sync() -> impl Future { -/// future::ok(HttpResponse::Ok().finish()) -/// } +/// #[get("/test")] +/// pub async fn test() -> impl Responder { +/// actix_web::HttpResponse::Ok().finish() +/// } /// }; /// ``` /// diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index 1b5b4e2f0..ce80210e7 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -662,7 +662,7 @@ impl fmt::Display for ScopeArgs { writeln!(f, "#[allow(non_camel_case_types)]")?; writeln!(f, "struct {};\n", self.name)?; writeln!(f, "impl actix_web::dev::HttpServiceFactory for {} {{", self.name)?; - writeln!(f, " fn register(self, config: &mut actix_web::dev::AppService) {{")?; + writeln!(f, " fn register(self, __config: &mut actix_web::dev::AppService) {{")?; write!(f, " let scope = actix_web::Scope::new(\"{}\")", self.path)?; for handler in self.scope_items.handlers.iter() { @@ -670,7 +670,7 @@ impl fmt::Display for ScopeArgs { } writeln!(f, ";\n")?; - writeln!(f, " actix_web::dev::HttpServiceFactory::register(scope, config)")?; + writeln!(f, " actix_web::dev::HttpServiceFactory::register(scope, __config)")?; writeln!(f, " }}\n}}") } } \ No newline at end of file diff --git a/actix-web-codegen/tests/test_macro.rs b/actix-web-codegen/tests/test_macro.rs index fb50d4ae0..e4e326671 100644 --- a/actix-web-codegen/tests/test_macro.rs +++ b/actix-web-codegen/tests/test_macro.rs @@ -11,7 +11,7 @@ use actix_web::{ web, App, Error, HttpRequest, HttpResponse, Responder, }; use actix_web_codegen::{ - connect, delete, get, head, options, patch, post, put, route, routes, trace, + connect, delete, get, head, options, patch, post, put, route, routes, trace, scope }; use futures_core::future::LocalBoxFuture; @@ -384,3 +384,67 @@ async fn test_wrap() { let body = String::from_utf8(body.to_vec()).unwrap(); assert!(body.contains("wrong number of parameters")); } + +#[scope("/works")] +const mod_inner: () = { + use actix_web::{HttpResponse, HttpRequest, Responder, put, head, connect, options, trace, patch, delete, web }; + use actix_web::web::Json; + + #[actix_web::get("/test")] + pub async fn test() -> impl Responder { + mod_test2() + } + + #[actix_web::get("/twicetest/{value}")] + pub async fn test_twice(value: web::Path) -> 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() + } +}; + +// todo: tests to call each endpoint... \ No newline at end of file