#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]
use switchy_web_server::{Error, HttpRequest, HttpResponse, handler::IntoHandler};
#[cfg(feature = "serde")]
mod serde_test_data {
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
pub struct TestParams {
pub name: String,
pub age: Option<u32>,
pub active: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
pub struct TestUser {
pub id: u64,
pub name: String,
pub email: String,
}
}
#[derive(Debug, Clone)]
struct TestConfig {
app_name: String,
#[allow(dead_code)]
version: String,
}
mod test_utils {
use super::*;
#[cfg(any(feature = "simulator", not(feature = "actix")))]
use switchy_web_server::Method;
#[cfg(any(feature = "simulator", not(feature = "actix")))]
#[allow(dead_code)]
pub fn create_comprehensive_test_request() -> HttpRequest {
use bytes::Bytes;
use switchy_web_server::simulator::{SimulationRequest, SimulationStub};
let json_body = r#"{"id": 123, "name": "John Doe", "email": "john@example.com"}"#;
let body = Bytes::from(json_body);
let sim_req = SimulationRequest::new(Method::Post, "/api/users/123/posts/456")
.with_query_string("name=john&age=30&active=true")
.with_header("authorization", "Bearer token123")
.with_header("content-type", "application/json")
.with_header("content-length", "1024")
.with_header("upgrade", "websocket")
.with_body(body);
HttpRequest::new(SimulationStub::new(sim_req))
}
#[cfg(all(feature = "actix", not(feature = "simulator")))]
#[allow(dead_code)]
pub fn create_comprehensive_test_request() -> HttpRequest {
HttpRequest::new(switchy_web_server::EmptyRequest)
}
#[cfg(feature = "simulator")]
pub fn create_test_state() -> TestConfig {
TestConfig {
app_name: "MoosicBox".to_string(),
version: "1.0.0".to_string(),
}
}
pub fn ok_response() -> HttpResponse {
HttpResponse::ok()
}
#[cfg(feature = "serde")]
pub fn json_response<T: serde::Serialize>(data: &T) -> HttpResponse {
let _ = data; HttpResponse::ok()
}
pub fn text_response(text: &str) -> HttpResponse {
let _ = text; HttpResponse::ok()
}
}
mod basic_handlers {
use super::*;
pub async fn handler_0_params() -> Result<HttpResponse, Error> {
Ok(test_utils::ok_response())
}
pub async fn handler_state_only(
state: switchy_web_server::extractors::State<TestConfig>,
) -> Result<HttpResponse, Error> {
Ok(test_utils::text_response(&state.0.app_name))
}
}
#[cfg(feature = "serde")]
mod serde_handlers {
use super::*;
use crate::serde_test_data::{TestParams, TestUser};
use switchy_web_server::extractors::{Header, Json, Path, Query, State};
pub async fn handler_1_param_query(
Query(params): Query<TestParams>,
) -> Result<HttpResponse, Error> {
Ok(test_utils::json_response(¶ms))
}
#[cfg(any(feature = "actix", feature = "simulator"))]
pub async fn handler_1_param_json(Json(user): Json<TestUser>) -> Result<HttpResponse, Error> {
Ok(test_utils::json_response(&user))
}
#[cfg(any(feature = "actix", feature = "simulator"))]
pub async fn handler_1_param_path(Path(id): Path<u64>) -> Result<HttpResponse, Error> {
Ok(test_utils::json_response(&id))
}
#[cfg(any(feature = "actix", feature = "simulator"))]
pub async fn handler_1_param_header(
Header(auth): Header<String>,
) -> Result<HttpResponse, Error> {
Ok(test_utils::json_response(&auth))
}
#[cfg(any(feature = "actix", feature = "simulator"))]
pub async fn handler_1_param_state(
State(config): State<TestConfig>,
) -> Result<HttpResponse, Error> {
Ok(test_utils::json_response(&config.app_name))
}
pub async fn handler_2_params(
Query(params): Query<TestParams>,
Path(id): Path<u64>,
) -> Result<HttpResponse, Error> {
let response = format!("User {} with ID {}", params.name, id);
Ok(test_utils::json_response(&response))
}
pub async fn handler_3_params(
Query(params): Query<TestParams>,
Path(id): Path<u64>,
Header(auth): Header<String>,
) -> Result<HttpResponse, Error> {
let response = format!("User {} with ID {} (auth: {})", params.name, id, auth);
Ok(test_utils::json_response(&response))
}
pub async fn handler_4_params(
Query(params): Query<TestParams>,
Json(user): Json<TestUser>,
Path(id): Path<u64>,
Header(auth): Header<String>,
) -> Result<HttpResponse, Error> {
let response = format!(
"Query: {}, JSON: {}, Path: {}, Header: {}",
params.name, user.name, id, auth
);
Ok(test_utils::json_response(&response))
}
pub async fn handler_5_params(
Query(params): Query<TestParams>,
Json(user): Json<TestUser>,
Path(id): Path<u64>,
Header(auth): Header<String>,
State(config): State<TestConfig>,
) -> Result<HttpResponse, Error> {
let response = format!(
"Query: {}, JSON: {}, Path: {}, Header: {}, State: {}",
params.name, user.name, id, auth, config.app_name
);
Ok(test_utils::json_response(&response))
}
pub async fn handler_with_error(_query: Query<TestParams>) -> Result<HttpResponse, Error> {
Err(Error::internal_server_error("Test error"))
}
}
#[cfg(feature = "actix")]
mod basic_actix_tests {
use super::*;
#[test]
fn test_basic_handlers() {
let _h0 = basic_handlers::handler_0_params.into_handler();
let _h1 = basic_handlers::handler_state_only.into_handler();
}
}
#[cfg(all(feature = "actix", feature = "serde"))]
mod serde_actix_tests {
use super::*;
#[test]
fn test_serde_handlers() {
let _handler1 = serde_handlers::handler_1_param_query.into_handler();
let _handler2 = serde_handlers::handler_1_param_json.into_handler();
let _handler3 = serde_handlers::handler_1_param_path.into_handler();
let _handler4 = serde_handlers::handler_1_param_header.into_handler();
let _handler5 = serde_handlers::handler_1_param_state.into_handler();
}
#[test]
fn test_multi_param_handler_compilation() {
let _handler2 = serde_handlers::handler_2_params.into_handler();
let _handler3 = serde_handlers::handler_3_params.into_handler();
let _handler4 = serde_handlers::handler_4_params.into_handler();
let _handler5 = serde_handlers::handler_5_params.into_handler();
}
#[test]
fn test_error_handler_compilation() {
let _handler = serde_handlers::handler_with_error.into_handler();
}
}
#[cfg(feature = "simulator")]
mod basic_simulator_tests {
use super::*;
#[test]
fn test_basic_handlers() {
let _h0 = basic_handlers::handler_0_params.into_handler();
let _h1 = basic_handlers::handler_state_only.into_handler();
}
#[test]
fn test_state_container_functionality() {
use switchy_web_server::extractors::StateContainer;
let mut state_container = StateContainer::new();
let test_config = test_utils::create_test_state();
state_container.insert(test_config.clone());
let retrieved = state_container.get::<TestConfig>();
assert!(retrieved.is_some());
assert_eq!(retrieved.unwrap().app_name, test_config.app_name);
}
}
#[cfg(all(feature = "simulator", feature = "serde"))]
mod serde_simulator_tests {
use super::*;
#[test]
fn test_serde_handlers() {
let _handler1 = serde_handlers::handler_1_param_query.into_handler();
let _handler2 = serde_handlers::handler_1_param_json.into_handler();
let _handler3 = serde_handlers::handler_1_param_path.into_handler();
let _handler4 = serde_handlers::handler_1_param_header.into_handler();
let _handler5 = serde_handlers::handler_1_param_state.into_handler();
}
#[test]
fn test_multi_param_handler_compilation() {
let _handler2 = serde_handlers::handler_2_params.into_handler();
let _handler3 = serde_handlers::handler_3_params.into_handler();
let _handler4 = serde_handlers::handler_4_params.into_handler();
let _handler5 = serde_handlers::handler_5_params.into_handler();
}
#[test]
fn test_error_handler_compilation() {
let _handler = serde_handlers::handler_with_error.into_handler();
}
}
mod basic_consistency_tests {
use super::*;
#[test]
fn test_basic_handler_consistency() {
let _h0 = basic_handlers::handler_0_params.into_handler();
let _h1 = basic_handlers::handler_state_only.into_handler();
}
}
#[cfg(feature = "serde")]
mod serde_consistency_tests {
use super::*;
#[test]
fn test_serde_handler_consistency() {
let _h1 = serde_handlers::handler_1_param_query.into_handler();
let _h2 = serde_handlers::handler_2_params.into_handler();
let _h3 = serde_handlers::handler_3_params.into_handler();
let _h4 = serde_handlers::handler_4_params.into_handler();
let _h5 = serde_handlers::handler_5_params.into_handler();
}
#[test]
fn test_error_handler_consistency() {
let _handler = serde_handlers::handler_with_error.into_handler();
}
}
#[cfg(all(feature = "simulator", feature = "serde"))]
mod benchmarks {
use super::*;
#[test]
fn test_handler_compilation_performance() {
let _handler = serde_handlers::handler_5_params.into_handler();
}
}