#![allow(dead_code)]
#![allow(unused_variables)]
use server_less::{http, jsonrpc, serve, ws};
#[derive(Clone)]
struct MultiService {
name: String,
}
impl MultiService {
fn new(name: &str) -> Self {
Self {
name: name.to_string(),
}
}
}
#[http(prefix = "/api")]
#[ws(path = "/ws")]
#[serve(http, ws)]
impl MultiService {
pub fn get_info(&self) -> String {
format!("Service: {}", self.name)
}
pub fn list_items(&self) -> Vec<String> {
vec!["item1".to_string(), "item2".to_string()]
}
}
#[test]
fn test_serve_router_created() {
let service = MultiService::new("test");
let router = service.router();
let _ = router;
}
#[test]
fn test_serve_router_has_health() {
let service = MultiService::new("test");
let _router = service.router();
}
#[derive(Clone)]
struct HttpOnlyService;
#[http]
#[serve(http)]
impl HttpOnlyService {
pub fn list_things(&self) -> Vec<String> {
vec![]
}
}
#[test]
fn test_serve_http_only() {
let service = HttpOnlyService;
let _router = service.router();
}
#[derive(Clone)]
struct WsOnlyService;
#[ws(path = "/ws")]
#[serve(ws)]
impl WsOnlyService {
pub fn echo(&self, msg: String) -> String {
msg
}
}
#[test]
fn test_serve_ws_only() {
let service = WsOnlyService;
let _router = service.router();
}
#[derive(Clone)]
struct CustomHealthService;
#[http]
#[serve(http, health = "/healthz")]
impl CustomHealthService {
pub fn list_items(&self) -> Vec<String> {
vec![]
}
}
#[test]
fn test_serve_custom_health() {
let service = CustomHealthService;
let _router = service.router();
}
#[derive(Clone)]
struct MinimalService;
#[serve()]
impl MinimalService {
pub fn _internal(&self) {}
}
#[test]
fn test_serve_minimal() {
let service = MinimalService;
let _router = service.router();
}
#[derive(Clone)]
struct CombinedRpcService;
#[http]
#[jsonrpc(path = "/rpc")]
#[serve(http, jsonrpc)]
impl CombinedRpcService {
pub fn add(&self, a: i32, b: i32) -> i32 {
a + b
}
pub fn get_status(&self) -> String {
"ok".to_string()
}
}
#[test]
fn test_serve_http_jsonrpc() {
let service = CombinedRpcService;
let _router = service.router();
}
#[derive(Clone)]
struct JsonRpcOnlyService;
#[jsonrpc]
#[serve(jsonrpc)]
impl JsonRpcOnlyService {
pub fn ping(&self) -> String {
"pong".to_string()
}
}
#[test]
fn test_serve_jsonrpc_only() {
let service = JsonRpcOnlyService;
let _router = service.router();
}
#[test]
fn test_serve_combined_openapi_spec() {
let spec = MultiService::openapi_spec();
assert_eq!(spec["openapi"], "3.0.0");
assert_eq!(spec["info"]["title"], "MultiService");
assert!(
spec["paths"].is_object(),
"Should have paths object. Spec: {}",
serde_json::to_string_pretty(&spec).unwrap()
);
}
#[test]
fn test_serve_http_openapi_has_paths() {
let spec = HttpOnlyService::openapi_spec();
let paths = &spec["paths"];
assert!(
paths.is_object(),
"Should have paths. Spec: {}",
serde_json::to_string_pretty(&spec).unwrap()
);
}
#[test]
fn test_serve_combined_openapi_includes_jsonrpc() {
let spec = CombinedRpcService::openapi_spec();
let paths = &spec["paths"];
assert!(paths.is_object(), "Should have paths object");
assert!(
paths["/rpc"].is_object(),
"Should have /rpc JSON-RPC endpoint. Paths: {}",
serde_json::to_string_pretty(paths).unwrap()
);
}
#[derive(Clone)]
struct NoOpenApiService;
#[http]
#[serve(http, openapi = false)]
impl NoOpenApiService {
pub fn list_items(&self) -> Vec<String> {
vec![]
}
}
#[test]
fn test_serve_openapi_disabled() {
let service = NoOpenApiService;
let _router = service.router();
}