use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Api {
#[serde(default, rename = "HTTPHeaders")]
pub http_headers: HashMap<String, Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub authorizations: Option<HashMap<String, RpcAuthScope>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RpcAuthScope {
pub auth_secret: String,
pub allowed_paths: Vec<String>,
}
impl Api {
pub fn default_http_headers() -> HashMap<String, Vec<String>> {
let mut headers = HashMap::new();
headers.insert(
"Access-Control-Allow-Origin".to_string(),
vec!["*".to_string()],
);
headers
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_api_default() {
let api = Api::default();
assert!(api.http_headers.is_empty());
assert!(api.authorizations.is_none());
}
#[test]
fn test_rpc_auth_scope() {
let scope = RpcAuthScope {
auth_secret: "bearer:mytoken".to_string(),
allowed_paths: vec!["/api/v0/id".to_string()],
};
let json = serde_json::to_string(&scope).unwrap();
assert!(json.contains("AuthSecret"));
assert!(json.contains("AllowedPaths"));
}
}