#![allow(dead_code)]
#![allow(unused_variables)]
use serde::{Deserialize, Serialize};
use server_less::{http, jsonrpc, ws};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct User {
id: String,
name: String,
}
#[derive(Clone)]
struct UserService;
#[http]
impl UserService {
pub fn create_user(&self, ctx: server_less::Context, name: String) -> User {
let request_id = ctx.request_id().unwrap_or("unknown");
User {
id: request_id.to_string(),
name,
}
}
pub fn get_user(&self, user_id: String) -> Option<User> {
Some(User {
id: user_id,
name: "Test".to_string(),
})
}
pub fn update_user(&self, ctx: server_less::Context, user_id: String, name: String) -> User {
let request_id = ctx.request_id().unwrap_or("unknown");
User { id: user_id, name }
}
}
#[test]
fn test_context_injection_compiles() {
let service = UserService;
let _router = service.http_router();
}
#[test]
fn test_openapi_with_context() {
let spec = UserService::openapi_spec();
let paths = spec.get("paths").unwrap().as_object().unwrap();
let create_path = paths.get("/users").unwrap().as_object().unwrap();
let post = create_path.get("post").unwrap().as_object().unwrap();
let params = post
.get("requestBody")
.and_then(|rb| rb.get("content"))
.and_then(|c| c.get("application/json"))
.and_then(|aj| aj.get("schema"))
.and_then(|s| s.get("properties"))
.and_then(|p| p.as_object());
if let Some(props) = params {
assert!(props.contains_key("name"), "Should have 'name' parameter");
assert!(!props.contains_key("ctx"), "Should NOT expose 'ctx' in API");
}
}
#[derive(Clone)]
struct InterpreterService;
#[http(prefix = "/api")]
impl InterpreterService {
pub fn eval_with_auth(&self, ctx: server_less::Context, expr: String) -> String {
let user = ctx.user_id().unwrap_or("anonymous");
format!("User {} evaluating: {}", user, expr)
}
pub fn eval_no_context(&self, expr: String) -> String {
format!("Evaluating: {}", expr)
}
}
#[test]
fn test_qualified_context_collision() {
let service = InterpreterService;
let _router = service.http_router();
}
#[derive(Clone)]
struct SimpleService;
#[http]
impl SimpleService {
pub fn hello(&self, name: String) -> String {
format!("Hello, {}!", name)
}
}
#[test]
fn test_no_context_works() {
let service = SimpleService;
let _router = service.http_router();
}
#[derive(Clone)]
struct CalculatorService;
#[jsonrpc(path = "/rpc")]
impl CalculatorService {
fn add(&self, ctx: server_less::Context, a: i32, b: i32) -> i32 {
let _request_id = ctx.request_id();
a + b
}
fn subtract(&self, a: i32, b: i32) -> i32 {
a - b
}
fn multiply(&self, ctx: server_less::Context, a: i32, b: Option<i32>) -> i32 {
let _request_id = ctx.request_id();
a * b.unwrap_or(1)
}
}
#[test]
fn test_jsonrpc_with_context() {
let service = CalculatorService;
let _router = service.jsonrpc_router();
}
#[derive(Serialize, Deserialize)]
struct Context {
interpreter_state: String,
}
#[derive(Clone)]
struct RpcInterpreter;
#[jsonrpc]
impl RpcInterpreter {
fn eval(&self, ctx: server_less::Context, code: String) -> String {
let _request_id = ctx.request_id();
format!("Evaluated: {}", code)
}
fn execute(&self, ctx: Context, code: String) -> String {
format!("Executed with {}: {}", ctx.interpreter_state, code)
}
}
#[test]
fn test_jsonrpc_context_collision() {
let service = RpcInterpreter;
let _router = service.jsonrpc_router();
}
#[derive(Clone)]
struct ChatService;
#[ws(path = "/chat")]
impl ChatService {
fn echo(&self, ctx: server_less::Context, message: String) -> String {
let request_id = ctx.request_id().unwrap_or("unknown");
format!("[{}] Echo: {}", request_id, message)
}
fn broadcast(&self, message: String) -> String {
format!("Broadcast: {}", message)
}
async fn async_echo(&self, ctx: server_less::Context, message: String) -> String {
let _request_id = ctx.request_id();
format!("Async: {}", message)
}
}
#[test]
fn test_ws_with_context() {
let service = ChatService;
let _router = service.ws_router();
}
#[derive(Clone)]
struct WsInterpreter;
#[ws]
impl WsInterpreter {
fn eval(&self, ctx: server_less::Context, code: String) -> String {
let _request_id = ctx.request_id();
format!("Evaluated: {}", code)
}
fn execute(&self, ctx: Context, code: String) -> String {
format!("Executed with {}: {}", ctx.interpreter_state, code)
}
}
#[test]
fn test_ws_context_collision() {
let service = WsInterpreter;
let _router = service.ws_router();
}