use crate::common::HttpMethod;
use crate::domain::id::Id;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CacheMode {
#[default]
Enabled,
Disabled,
ReadOnly,
Bypass,
}
const UNCACHEABLE_SCHEMES: &[&str] = &[
"data:",
"blob:",
"javascript:",
"file:",
"vbscript:",
"about:",
];
#[derive(Debug, Clone)]
pub struct CacheContext {
pub team_id: Id,
pub url: String,
pub method: HttpMethod,
pub mode: CacheMode,
}
impl CacheContext {
#[must_use]
pub fn should_read(&self) -> bool {
match self.mode {
CacheMode::Enabled | CacheMode::ReadOnly => true,
CacheMode::Disabled | CacheMode::Bypass => false,
}
}
#[must_use]
pub fn should_write(&self) -> bool {
match self.mode {
CacheMode::Enabled | CacheMode::Bypass => true,
CacheMode::Disabled | CacheMode::ReadOnly => false,
}
}
#[must_use]
pub fn is_cacheable(&self) -> bool {
for scheme in UNCACHEABLE_SCHEMES {
if self.url.len() >= scheme.len()
&& self.url[..scheme.len()].eq_ignore_ascii_case(scheme)
{
return false;
}
}
!matches!(self.method, HttpMethod::Post)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_mode_default_is_enabled() {
assert_eq!(CacheMode::default(), CacheMode::Enabled);
}
#[test]
fn test_should_read_enabled_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(ctx.should_read());
}
#[test]
fn test_should_read_disabled_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Disabled,
};
assert!(!ctx.should_read());
}
#[test]
fn test_should_read_read_only_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::ReadOnly,
};
assert!(ctx.should_read());
}
#[test]
fn test_should_read_bypass_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Bypass,
};
assert!(!ctx.should_read());
}
#[test]
fn test_should_write_enabled_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(ctx.should_write());
}
#[test]
fn test_should_write_disabled_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Disabled,
};
assert!(!ctx.should_write());
}
#[test]
fn test_should_write_read_only_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::ReadOnly,
};
assert!(!ctx.should_write());
}
#[test]
fn test_should_write_bypass_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Bypass,
};
assert!(ctx.should_write());
}
#[test]
fn test_read_write_matrix_all_modes() {
let cases = [
(CacheMode::Enabled, true, true),
(CacheMode::Disabled, false, false),
(CacheMode::ReadOnly, true, false),
(CacheMode::Bypass, false, true),
];
for (mode, expected_read, expected_write) in cases {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode,
};
assert_eq!(
ctx.should_read(),
expected_read,
"should_read mismatch for mode {:?}",
mode
);
assert_eq!(
ctx.should_write(),
expected_write,
"should_write mismatch for mode {:?}",
mode
);
}
}
#[test]
fn test_is_cacheable_https_url_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com/page".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_http_url_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "http://example.com/page".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_data_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "data:text/plain;base64,SGVsbG8=".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_blob_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "blob:https://example.com/uuid".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_javascript_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "javascript:alert('xss')".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_file_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "file:///etc/passwd".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_vbscript_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "vbscript:msgbox('xss')".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_about_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "about:blank".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_extended_schemes_case_insensitive() {
let cases = [
"JAVASCRIPT:alert(1)",
"File:///etc/passwd",
"VBSCRIPT:msgbox(1)",
"ABOUT:blank",
];
for url in cases {
let ctx = CacheContext {
team_id: Id::nil(),
url: url.to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(
!ctx.is_cacheable(),
"scheme of {} should be case-insensitive rejected",
url
);
}
}
#[test]
fn test_is_cacheable_data_scheme_case_insensitive() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "DATA:text/plain,hello".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_blob_scheme_case_insensitive() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "BLOB:https://example.com/uuid".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_get_method_returns_true() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
assert!(ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_post_method_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com/api".to_string(),
method: HttpMethod::Post,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_post_with_data_scheme_returns_false() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "data:text/plain,hello".to_string(),
method: HttpMethod::Post,
mode: CacheMode::Enabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_is_cacheable_independent_of_mode() {
let modes = [
CacheMode::Enabled,
CacheMode::Disabled,
CacheMode::ReadOnly,
CacheMode::Bypass,
];
for mode in modes {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode,
};
assert!(
ctx.is_cacheable(),
"is_cacheable should be true for HTTPS GET regardless of mode {:?}",
mode
);
}
}
#[test]
fn test_is_cacheable_disabled_mode_still_checks_scheme() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "data:text/plain,hello".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Disabled,
};
assert!(!ctx.is_cacheable());
}
#[test]
fn test_cache_context_fields_accessible() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::ReadOnly,
};
assert_eq!(ctx.url, "https://example.com");
assert_eq!(ctx.method, HttpMethod::Get);
assert_eq!(ctx.mode, CacheMode::ReadOnly);
}
#[test]
fn test_cache_context_clone() {
let ctx = CacheContext {
team_id: Id::nil(),
url: "https://example.com".to_string(),
method: HttpMethod::Get,
mode: CacheMode::Enabled,
};
let cloned = ctx.clone();
assert_eq!(ctx.url, cloned.url);
assert_eq!(ctx.method, cloned.method);
assert_eq!(ctx.mode, cloned.mode);
}
#[test]
fn test_cache_mode_copy_and_eq() {
let mode1 = CacheMode::ReadOnly;
let mode2 = mode1; assert_eq!(mode1, mode2);
assert_ne!(CacheMode::ReadOnly, CacheMode::Bypass);
}
#[test]
fn test_cache_mode_serde_camel_case() {
let cases = [
(CacheMode::Enabled, "enabled"),
(CacheMode::Disabled, "disabled"),
(CacheMode::ReadOnly, "readOnly"),
(CacheMode::Bypass, "bypass"),
];
for (mode, expected) in cases {
let json = serde_json::to_string(&mode).expect("serialize failed");
assert_eq!(json, format!("\"{}\"", expected));
let deserialized: CacheMode = serde_json::from_str(&json).expect("deserialize failed");
assert_eq!(deserialized, mode);
}
}
#[test]
fn test_cache_mode_serde_rejects_write_only() {
let result: Result<CacheMode, _> = serde_json::from_str("\"writeOnly\"");
assert!(result.is_err(), "writeOnly should be rejected");
}
}