#[cfg(feature = "derive")]
mod tests {
use elif_http::{
controller::{ControllerRoute, ElifController},
request::ElifRequest,
response::ElifResponse,
routing::HttpMethod,
HttpResult,
};
use elif_http_derive::{controller, get, post};
#[derive(Clone)]
pub struct TestController {
pub name: String,
}
#[controller("/test")]
impl TestController {
#[get("/hello")]
pub async fn hello(&self, _req: ElifRequest) -> HttpResult<ElifResponse> {
Ok(ElifResponse::ok().text(&format!("Hello from {}", self.name)))
}
#[post("/echo")]
pub async fn echo(&self, _req: ElifRequest) -> HttpResult<ElifResponse> {
Ok(ElifResponse::ok().text(&format!("{} echoes something", self.name)))
}
}
#[tokio::test]
async fn test_async_dispatch() {
let controller = TestController {
name: "TestController".to_string(),
};
let _ = &controller;
}
#[test]
fn test_controller_trait_implementation() {
let controller = TestController {
name: "Test".to_string(),
};
assert_eq!(controller.name(), "TestController");
assert_eq!(controller.base_path(), "/test");
let routes = controller.routes();
assert_eq!(routes.len(), 2);
assert_eq!(routes[0].method, HttpMethod::GET);
assert_eq!(routes[0].path, "/hello");
assert_eq!(routes[0].handler_name, "hello");
assert_eq!(routes[1].method, HttpMethod::POST);
assert_eq!(routes[1].path, "/echo");
assert_eq!(routes[1].handler_name, "echo");
}
}