use potato::{HttpServer, OnceCache};
#[potato::controller]
pub struct NoAuthController<'a> {
pub once_cache: &'a OnceCache,
}
#[potato::controller("/api/no-auth")]
impl<'a> NoAuthController<'a> {
#[potato::http_get("/data")]
pub async fn get_data(&self) -> anyhow::Result<&'static str> {
Ok("Data accessed without auth")
}
#[potato::http_post("/update")]
pub async fn update_data(&mut self) -> anyhow::Result<&'static str> {
Ok("Data updated without auth")
}
#[potato::http_get("/public")]
pub async fn get_public() -> anyhow::Result<&'static str> {
Ok("Public data")
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[tokio::test]
async fn test_controller_without_session_cache() {
let port = 18891;
let server_addr = format!("127.0.0.1:{}", port);
let mut server = HttpServer::new(&server_addr);
server.configure(|ctx| {
ctx.use_handlers();
});
let server_handle = tokio::spawn(async move {
let _ = server.serve_http().await;
});
tokio::time::sleep(Duration::from_millis(500)).await;
let url = format!("http://{}/api/no-auth/data", server_addr);
match potato::get(&url, vec![]).await {
Ok(res) => {
assert_eq!(res.http_code, 200, "Should return 200 without Authorization header");
println!("✅ GET /api/no-auth/data correctly returns 200 without auth");
}
Err(e) => {
panic!("Request failed: {}", e);
}
}
let url = format!("http://{}/api/no-auth/update", server_addr);
match potato::post(&url, vec![], vec![]).await {
Ok(res) => {
assert_eq!(res.http_code, 200, "Should return 200 without Authorization header");
println!("✅ POST /api/no-auth/update correctly returns 200 without auth");
}
Err(e) => {
panic!("Request failed: {}", e);
}
}
let url = format!("http://{}/api/no-auth/public", server_addr);
match potato::get(&url, vec![]).await {
Ok(res) => {
assert_eq!(res.http_code, 200, "Public endpoint should return 200");
println!("✅ GET /api/no-auth/public correctly returns 200");
}
Err(e) => {
panic!("Request failed: {}", e);
}
}
server_handle.abort();
}
}