use crate::extensions::flow_state::{CasOutcome, FlowStore};
use crate::imposter::ResponseMode;
use anyhow::{Result, anyhow};
use rhai::{AST, Dynamic, Engine, Map, Scope};
use serde_json::Value;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use super::{
FaultDecision, ScriptCtxExtras, ScriptCtxInput, ScriptFlowStore, ScriptRequest, ScriptResult,
ScriptResultBody, entrypoints,
};
fn is_leap_year(year: u64) -> bool {
(year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}
#[derive(Clone)]
pub struct RhaiEngine {
ast: Arc<AST>, rule_id: String,
}
impl RhaiEngine {
pub fn new(script: &str, rule_id: &str) -> Result<Self> {
let engine = Self::create_engine();
let ast = engine
.compile(script)
.map_err(|e| anyhow!("Failed to compile script: {e}"))?;
Ok(Self {
ast: Arc::new(ast), rule_id: rule_id.to_string(),
})
}
pub fn ast(&self) -> &Arc<AST> {
&self.ast
}
pub fn rule_id(&self) -> &str {
&self.rule_id
}
pub fn create_engine() -> Engine {
let mut engine = crate::scripting::rift_rhai_engine();
engine
.register_type::<ScriptFlowStore>()
.register_fn("get", ScriptFlowStore::get)
.register_fn("set", ScriptFlowStore::set)
.register_fn("exists", ScriptFlowStore::exists)
.register_fn("delete", ScriptFlowStore::delete)
.register_fn("increment", ScriptFlowStore::increment)
.register_fn("set_ttl", ScriptFlowStore::set_ttl)
.register_fn("last_error", ScriptFlowStore::last_error);
engine.register_fn("timestamp_header", || -> String {
let secs = u64::try_from(crate::scripting::env::now_ms() / 1000).unwrap_or(0);
let days_since_epoch = secs / 86400;
let time_of_day = secs % 86400;
let hours = time_of_day / 3600;
let minutes = (time_of_day % 3600) / 60;
let seconds = time_of_day % 60;
let day_of_week = (days_since_epoch + 4) % 7;
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let mut year = 1970;
let mut remaining_days = days_since_epoch;
loop {
let days_in_year = if is_leap_year(year) { 366 } else { 365 };
if remaining_days < days_in_year {
break;
}
remaining_days -= days_in_year;
year += 1;
}
let months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let days_in_months = [
31,
if is_leap_year(year) { 29 } else { 28 },
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
let mut month = 0;
let mut day = remaining_days + 1;
for (i, &days_in_month) in days_in_months.iter().enumerate() {
if day <= days_in_month {
month = i;
break;
}
day -= days_in_month;
}
format!(
"{}, {:02} {} {} {:02}:{:02}:{:02} GMT",
days[day_of_week as usize], day, months[month], year, hours, minutes, seconds
)
});
register_v2_api(&mut engine);
engine
}
pub fn should_inject_fault(
&self,
request: &ScriptRequest,
flow_store: Arc<dyn FlowStore>,
) -> Result<FaultDecision> {
self.should_inject_fault_with_ctx(request, flow_store, &ScriptCtxExtras::default())
}
pub fn should_inject_fault_with_ctx(
&self,
request: &ScriptRequest,
flow_store: Arc<dyn FlowStore>,
extra: &ScriptCtxExtras,
) -> Result<FaultDecision> {
let engine = Self::create_engine();
let ctx_input = extra.build_ctx_input(request);
call_respond(
&engine,
self.ast.as_ref(),
&ctx_input,
flow_store,
&self.rule_id,
)
}
}
fn register_v2_api(engine: &mut Engine) {
engine
.register_type::<RhaiStateHandle>()
.register_fn("get", RhaiStateHandle::get)
.register_fn("set", RhaiStateHandle::set)
.register_fn("incr", RhaiStateHandle::incr)
.register_fn("exists", RhaiStateHandle::exists)
.register_fn("delete", RhaiStateHandle::delete)
.register_fn("get_or", RhaiStateHandle::get_or)
.register_fn("incr_by", RhaiStateHandle::incr_by)
.register_fn("cas", RhaiStateHandle::cas)
.register_fn("ttl", RhaiStateHandle::ttl)
.register_fn("ttl", RhaiStateHandle::ttl_key)
.register_fn("clear", RhaiStateHandle::clear);
engine
.register_type::<RhaiStoreHandle>()
.register_fn("flow", RhaiStoreHandle::flow);
engine
.register_type::<RhaiLogger>()
.register_fn("debug", RhaiLogger::debug)
.register_fn("info", RhaiLogger::info)
.register_fn("warn", RhaiLogger::warn)
.register_fn("error", RhaiLogger::error);
engine
.register_type::<RhaiScriptResult>()
.register_fn("header", RhaiScriptResult::header);
engine.register_fn("http", |status: i64| {
RhaiScriptResult(ScriptResult::http(clamp_u16(status), None))
});
engine.register_fn("http", |status: i64, body: Dynamic| {
RhaiScriptResult(ScriptResult::http(
clamp_u16(status),
Some(dynamic_to_script_result_body(body)),
))
});
engine.register_fn("delay", |ms: i64| {
RhaiScriptResult(ScriptResult::Delay(ms.max(0) as u64))
});
engine.register_fn("reset", || RhaiScriptResult(ScriptResult::Reset));
engine.register_fn("pass", || RhaiScriptResult(ScriptResult::Pass));
engine.register_fn("header", |m: &mut Map, name: &str| -> Dynamic {
let Some(headers_val) = m.get("headers") else {
return Dynamic::UNIT;
};
let Some(headers_map) = headers_val.clone().try_cast::<Map>() else {
return Dynamic::UNIT;
};
headers_map
.iter()
.find(|(k, _)| k.as_str().eq_ignore_ascii_case(name))
.map(|(_, v)| v.clone())
.unwrap_or(Dynamic::UNIT)
});
}
fn clamp_u16(n: i64) -> u16 {
n.clamp(0, i64::from(u16::MAX)) as u16
}
fn dynamic_to_script_result_body(value: Dynamic) -> ScriptResultBody {
if let Some(s) = value.clone().try_cast::<String>() {
ScriptResultBody::Str(s)
} else {
ScriptResultBody::Json(dynamic_to_json(value))
}
}
#[derive(Clone)]
pub struct RhaiStateHandle {
inner: ScriptFlowStore,
flow_id: String,
}
impl RhaiStateHandle {
fn new(store: Arc<dyn FlowStore>, flow_id: String) -> Self {
Self {
inner: ScriptFlowStore::new(store),
flow_id,
}
}
pub fn get(&mut self, key: String) -> std::result::Result<Dynamic, Box<rhai::EvalAltResult>> {
self.inner.get(self.flow_id.clone(), key)
}
pub fn set(
&mut self,
key: String,
value: Dynamic,
) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.set(self.flow_id.clone(), key, value)
}
pub fn incr(&mut self, key: String) -> std::result::Result<i64, Box<rhai::EvalAltResult>> {
self.inner.increment(self.flow_id.clone(), key)
}
pub fn exists(&mut self, key: String) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.exists(self.flow_id.clone(), key)
}
pub fn delete(&mut self, key: String) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.delete(self.flow_id.clone(), key)
}
pub fn get_or(
&mut self,
key: String,
default: Dynamic,
) -> std::result::Result<Dynamic, Box<rhai::EvalAltResult>> {
self.inner.get_or(self.flow_id.clone(), key, default)
}
pub fn incr_by(
&mut self,
key: String,
by: i64,
) -> std::result::Result<i64, Box<rhai::EvalAltResult>> {
self.inner.increment_by(self.flow_id.clone(), key, by)
}
pub fn cas(
&mut self,
key: String,
expected: Dynamic,
new: Dynamic,
) -> std::result::Result<Dynamic, Box<rhai::EvalAltResult>> {
let outcome = self.inner.cas(self.flow_id.clone(), key, expected, new)?;
Ok(cas_outcome_to_dynamic(outcome))
}
pub fn ttl(&mut self, seconds: i64) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.ttl(self.flow_id.clone(), seconds)
}
pub fn ttl_key(
&mut self,
key: String,
seconds: i64,
) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.key_ttl(self.flow_id.clone(), key, seconds)
}
pub fn clear(&mut self) -> std::result::Result<bool, Box<rhai::EvalAltResult>> {
self.inner.clear(self.flow_id.clone())
}
}
fn cas_outcome_to_dynamic(outcome: CasOutcome) -> Dynamic {
let mut map = Map::new();
match outcome {
CasOutcome::Applied => {
map.insert("applied".into(), Dynamic::from(true));
map.insert("current".into(), Dynamic::UNIT);
}
CasOutcome::Conflict(current) => {
map.insert("applied".into(), Dynamic::from(false));
map.insert(
"current".into(),
current.map(json_to_dynamic).unwrap_or(Dynamic::UNIT),
);
}
}
Dynamic::from(map)
}
#[derive(Clone)]
pub struct RhaiStoreHandle {
store: Arc<dyn FlowStore>,
}
impl RhaiStoreHandle {
fn new(store: Arc<dyn FlowStore>) -> Self {
Self { store }
}
pub fn flow(&mut self, flow_id: String) -> RhaiStateHandle {
RhaiStateHandle::new(Arc::clone(&self.store), flow_id)
}
}
#[derive(Clone)]
pub struct RhaiLogger {
port: u16,
stub_id: Option<String>,
}
impl RhaiLogger {
fn log(&self, level: tracing::Level, message: &str) {
let port = self.port;
let stub_id = self.stub_id.as_deref().unwrap_or("");
match level {
tracing::Level::DEBUG => {
tracing::debug!(target: "rift::script", port, stub_id, "{message}")
}
tracing::Level::INFO => {
tracing::info!(target: "rift::script", port, stub_id, "{message}")
}
tracing::Level::WARN => {
tracing::warn!(target: "rift::script", port, stub_id, "{message}")
}
_ => tracing::error!(target: "rift::script", port, stub_id, "{message}"),
}
}
pub fn debug(&mut self, message: String) {
self.log(tracing::Level::DEBUG, &message);
}
pub fn info(&mut self, message: String) {
self.log(tracing::Level::INFO, &message);
}
pub fn warn(&mut self, message: String) {
self.log(tracing::Level::WARN, &message);
}
pub fn error(&mut self, message: String) {
self.log(tracing::Level::ERROR, &message);
}
}
#[derive(Clone)]
pub struct RhaiScriptResult(pub ScriptResult);
impl RhaiScriptResult {
pub fn header(&mut self, key: String, value: String) -> Self {
self.0.add_header(key, value);
self.clone()
}
}
fn run_entrypoint(
engine: &Engine,
ast: &AST,
entrypoint: &str,
ctx_input: &ScriptCtxInput,
flow_store: Arc<dyn FlowStore>,
) -> Result<Dynamic> {
let mut scope = Scope::new();
let ctx_map = build_ctx_map(ctx_input, flow_store);
let has_named = ast.iter_functions().any(|f| f.name == entrypoint);
if has_named {
engine
.run_ast_with_scope(&mut scope, ast)
.map_err(|e| anyhow!("Script execution error: {e}"))?;
let result: Dynamic = engine
.call_fn(&mut scope, ast, entrypoint, (ctx_map,))
.map_err(|e| anyhow!("Failed to call {entrypoint}(ctx): {e}"))?;
Ok(result)
} else {
let has_any_function = ast.iter_functions().next().is_some();
scope.push("ctx", ctx_map);
let result: Dynamic = engine
.eval_ast_with_scope(&mut scope, ast)
.map_err(|e| anyhow!("Script execution error: {e}"))?;
if result.is_unit() && has_any_function {
return Err(anyhow!(
"script defines function(s) but none is the `{entrypoint}` entrypoint \
(and there is no bare expression to evaluate); did you mean `{entrypoint}`?"
));
}
Ok(result)
}
}
fn dynamic_to_fault_decision(result: Dynamic, rule_id: &str) -> Result<FaultDecision> {
if result.is_unit() {
return Ok(FaultDecision::None);
}
let script_result = result.try_cast::<RhaiScriptResult>().ok_or_else(|| {
anyhow!("respond(ctx) must return http(...)/delay(...)/reset()/pass() or nothing")
})?;
Ok(script_result.0.into_fault_decision(rule_id))
}
pub fn call_respond(
engine: &Engine,
ast: &AST,
ctx_input: &ScriptCtxInput,
flow_store: Arc<dyn FlowStore>,
rule_id: &str,
) -> Result<FaultDecision> {
let result = run_entrypoint(engine, ast, entrypoints::RESPOND, ctx_input, flow_store)?;
dynamic_to_fault_decision(result, rule_id)
}
pub(crate) fn values_to_dynamic(values: &[String]) -> Dynamic {
match values {
[one] => Dynamic::from(one.clone()),
many => Dynamic::from_array(many.iter().cloned().map(Dynamic::from).collect()),
}
}
pub(crate) fn values_map_to_rhai(map: &crate::util::MultiValueMap) -> Map {
map.iter()
.map(|(k, v)| (k.as_str().into(), values_to_dynamic(v)))
.collect()
}
fn header_map_lowercased(headers: &crate::util::MultiValueMap) -> Map {
headers
.iter()
.map(|(k, v)| (k.to_ascii_lowercase().into(), values_to_dynamic(v)))
.collect()
}
fn parse_json_or_unit(raw: &str) -> Dynamic {
serde_json::from_str::<Value>(raw)
.map(json_to_dynamic)
.unwrap_or(Dynamic::UNIT)
}
fn build_request_ctx_map(request: &ScriptRequest) -> Map {
let mut m = Map::new();
m.insert("method".into(), Dynamic::from(request.method.clone()));
m.insert("path".into(), Dynamic::from(request.path.clone()));
let mut path_params = Map::new();
for (k, v) in &request.path_params {
path_params.insert(k.clone().into(), Dynamic::from(v.clone()));
}
m.insert("pathParams".into(), Dynamic::from(path_params));
m.insert(
"query".into(),
Dynamic::from(values_map_to_rhai(&request.query)),
);
m.insert(
"headers".into(),
Dynamic::from(header_map_lowercased(&request.headers)),
);
let raw = request.raw_body.clone().unwrap_or_else(|| {
if request.body.is_null() {
String::new()
} else {
serde_json::to_string(&request.body).unwrap_or_default()
}
});
m.insert("json".into(), parse_json_or_unit(&raw));
m.insert("body".into(), Dynamic::from(raw));
m.insert(
"isBinary".into(),
Dynamic::from(request.mode == ResponseMode::Binary),
);
m
}
fn build_stub_ctx_map(stub: &super::ScriptStubContext) -> Map {
let mut m = Map::new();
m.insert(
"scenarioName".into(),
stub.scenario_name
.clone()
.map_or(Dynamic::UNIT, Dynamic::from),
);
m.insert(
"scenarioState".into(),
stub.scenario_state
.clone()
.map_or(Dynamic::UNIT, Dynamic::from),
);
m.insert(
"id".into(),
stub.stub_id.clone().map_or(Dynamic::UNIT, Dynamic::from),
);
m
}
fn build_ctx_map(input: &ScriptCtxInput, flow_store: Arc<dyn FlowStore>) -> Map {
let mut ctx = Map::new();
ctx.insert(
"request".into(),
Dynamic::from(build_request_ctx_map(input.request)),
);
ctx.insert("flowId".into(), Dynamic::from(input.flow_id.clone()));
ctx.insert(
"stub".into(),
Dynamic::from(build_stub_ctx_map(&input.stub)),
);
ctx.insert(
"state".into(),
Dynamic::from(RhaiStateHandle::new(
Arc::clone(&flow_store),
input.flow_id.clone(),
)),
);
ctx.insert(
"store".into(),
Dynamic::from(RhaiStoreHandle::new(Arc::clone(&flow_store))),
);
ctx.insert(
"logger".into(),
Dynamic::from(RhaiLogger {
port: input.port,
stub_id: input.stub.stub_id.clone(),
}),
);
ctx
}
pub(super) fn json_to_dynamic(value: Value) -> Dynamic {
match value {
Value::Null => Dynamic::UNIT,
Value::Bool(b) => Dynamic::from(b),
Value::Number(n) => {
if let Some(i) = n.as_i64() {
Dynamic::from(i)
} else if let Some(f) = n.as_f64() {
Dynamic::from(f)
} else {
Dynamic::UNIT
}
}
Value::String(s) => Dynamic::from(s),
Value::Array(arr) => {
let vec: Vec<Dynamic> = arr.into_iter().map(json_to_dynamic).collect();
Dynamic::from(vec)
}
Value::Object(obj) => {
let mut map = Map::new();
for (k, v) in obj {
map.insert(k.into(), json_to_dynamic(v));
}
Dynamic::from(map)
}
}
}
pub(super) fn dynamic_to_json(value: Dynamic) -> Value {
if value.is_unit() {
Value::Null
} else if let Ok(b) = value.as_bool() {
Value::Bool(b)
} else if let Ok(i) = value.as_int() {
Value::Number(i.into())
} else if let Ok(f) = value.as_float() {
Value::Number(serde_json::Number::from_f64(f).unwrap_or(0.into()))
} else if let Some(s) = value.clone().try_cast::<String>() {
Value::String(s)
} else {
match value.clone().try_cast::<Vec<Dynamic>>() {
Some(arr) => Value::Array(arr.into_iter().map(dynamic_to_json).collect()),
_ => match value.clone().try_cast::<Map>() {
Some(map) => {
let mut obj = serde_json::Map::new();
for (k, v) in map {
obj.insert(k.to_string(), dynamic_to_json(v));
}
Value::Object(obj)
}
_ => Value::String(format!("{value}")),
},
}
}
}
pub fn run_should_inject_with_abort_rhai(
code: &str,
rule_id: &str,
request: &ScriptRequest,
flow_store: Arc<dyn FlowStore>,
abort: &Arc<AtomicBool>,
ctx_extra: &ScriptCtxExtras,
) -> Result<FaultDecision> {
let ast = super::compiled_cache::cached_rhai_ast(code)?;
let mut engine = RhaiEngine::create_engine();
let flag = Arc::clone(abort);
engine.on_progress(move |_ops| {
if flag.load(Ordering::Relaxed) {
Some(Dynamic::TRUE)
} else {
None
}
});
let ctx_input = ctx_extra.build_ctx_input(request);
call_respond(&engine, &ast, &ctx_input, flow_store, rule_id)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::backends::InMemoryFlowStore;
use serde_json::json;
use std::collections::HashMap;
#[test]
fn rhai_import_resolves_no_module() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::write(
dir.path().join("leak.rhai"),
"export const X = \"CANARY_RHAI_1611\";",
)
.unwrap();
let module = dir.path().join("leak");
let import = format!("import {:?} as m; m::X", module.to_str().unwrap());
let err = RhaiEngine::create_engine()
.eval::<String>(&import)
.expect_err("the runtime engine must not load a module");
assert!(!err.to_string().contains("CANARY_RHAI_1611"), "{err}");
let mut headers = crate::util::OrderedMap::default();
let decorate = format!(
"import {:?} as m; response.body = m::X;",
module.to_str().unwrap()
);
let decorated = crate::behaviors::apply_decorate(
&decorate,
&crate::behaviors::RequestContext::default(),
"orig",
&crate::behaviors::ResponseStatus::Code(200),
&mut headers,
);
assert!(
decorated.is_err(),
"a Rhai decorate must not load a module: {decorated:?}"
);
}
#[tokio::test]
async fn test_simple_fault_injection() {
let script = r#"
fn respond(ctx) {
if ctx.request.method == "POST" {
return http(503, "Service unavailable");
}
pass()
}
"#;
let engine = RhaiEngine::new(script, "test-rule").unwrap();
let store: Arc<dyn FlowStore> = Arc::new(InMemoryFlowStore::new(300));
let request = ScriptRequest {
mode: ResponseMode::Text,
raw_body: None,
method: "POST".to_string(),
path: "/test".to_string(),
headers: crate::util::OrderedMap::default(),
body: json!({}),
query: Default::default(),
path_params: HashMap::new(),
};
let decision = engine.should_inject_fault(&request, store).unwrap();
match decision {
FaultDecision::Error {
status,
body,
rule_id,
headers,
} => {
assert_eq!(status, 503);
assert_eq!(body, "Service unavailable");
assert_eq!(rule_id, "test-rule");
assert!(headers.is_empty()); }
_ => panic!("Expected Error fault decision"),
}
}
#[tokio::test]
async fn test_latency_fault() {
let script = r#"
fn respond(ctx) {
delay(500)
}
"#;
let engine = RhaiEngine::new(script, "latency-rule").unwrap();
let store: Arc<dyn FlowStore> = Arc::new(InMemoryFlowStore::new(300));
let request = ScriptRequest {
mode: ResponseMode::Text,
raw_body: None,
method: "GET".to_string(),
path: "/api/test".to_string(),
headers: crate::util::OrderedMap::default(),
body: json!({}),
query: Default::default(),
path_params: HashMap::new(),
};
let decision = engine.should_inject_fault(&request, store).unwrap();
match decision {
FaultDecision::Latency {
duration_ms,
rule_id,
} => {
assert_eq!(duration_ms, 500);
assert_eq!(rule_id, "latency-rule");
}
_ => panic!("Expected Latency fault decision"),
}
}
#[tokio::test]
async fn test_flow_store_increment() {
let script = r#"
fn respond(ctx) {
let attempts = ctx.state.incr("attempts");
if attempts <= 2 {
return http(503, "Retry later");
}
pass()
}
"#;
let engine = RhaiEngine::new(script, "retry-rule").unwrap();
let store: Arc<dyn FlowStore> = Arc::new(InMemoryFlowStore::new(300));
let mut headers = crate::util::OrderedMap::default();
headers.insert("x-flow-id".to_string(), vec!["flow123".to_string()]);
let request = ScriptRequest {
mode: ResponseMode::Text,
raw_body: None,
method: "GET".to_string(),
path: "/test".to_string(),
headers: headers.clone(),
body: json!({}),
query: Default::default(),
path_params: HashMap::new(),
};
let decision1 = engine
.should_inject_fault(&request, Arc::clone(&store))
.unwrap();
assert!(matches!(decision1, FaultDecision::Error { .. }));
let decision2 = engine
.should_inject_fault(&request, Arc::clone(&store))
.unwrap();
assert!(matches!(decision2, FaultDecision::Error { .. }));
let decision3 = engine.should_inject_fault(&request, store).unwrap();
assert!(matches!(decision3, FaultDecision::None));
}
#[tokio::test]
async fn test_header_based_routing() {
let script = r#"
fn respond(ctx) {
let user_id = ctx.request.header("x-user-id");
if user_id.starts_with("beta-") {
return delay(1000);
}
pass()
}
"#;
let engine = RhaiEngine::new(script, "beta-users").unwrap();
let store: Arc<dyn FlowStore> = Arc::new(InMemoryFlowStore::new(300));
let mut headers1 = crate::util::OrderedMap::default();
headers1.insert("x-user-id".to_string(), vec!["beta-user-123".to_string()]);
let request1 = ScriptRequest {
mode: ResponseMode::Text,
raw_body: None,
method: "GET".to_string(),
path: "/test".to_string(),
headers: headers1,
body: json!({}),
query: Default::default(),
path_params: HashMap::new(),
};
let decision1 = engine
.should_inject_fault(&request1, Arc::clone(&store))
.unwrap();
assert!(matches!(decision1, FaultDecision::Latency { .. }));
let mut headers2 = crate::util::OrderedMap::default();
headers2.insert(
"x-user-id".to_string(),
vec!["regular-user-456".to_string()],
);
let request2 = ScriptRequest {
mode: ResponseMode::Text,
raw_body: None,
method: "GET".to_string(),
path: "/test".to_string(),
headers: headers2,
body: json!({}),
query: Default::default(),
path_params: HashMap::new(),
};
let decision2 = engine.should_inject_fault(&request2, store).unwrap();
assert!(matches!(decision2, FaultDecision::None));
}
mod v2 {
use super::*;
use crate::backends::InMemoryFlowStore;
fn req(headers: HashMap<String, String>, raw_body: Option<&str>) -> ScriptRequest {
ScriptRequest {
mode: ResponseMode::Text,
method: "POST".to_string(),
path: "/api/orders".to_string(),
headers: headers.into_iter().map(|(k, v)| (k, vec![v])).collect(),
body: serde_json::Value::Null,
query: crate::util::OrderedMap::from_iter([(
"page".to_string(),
vec!["2".to_string()],
)]),
path_params: HashMap::from([("id".to_string(), "42".to_string())]),
raw_body: raw_body.map(|s| s.to_string()),
}
}
fn store() -> Arc<dyn FlowStore> {
Arc::new(InMemoryFlowStore::new(300))
}
fn run_respond(script: &str, request: &ScriptRequest) -> Result<FaultDecision> {
let engine = RhaiEngine::new(script, "v2-rule").unwrap();
engine.should_inject_fault(request, store())
}
fn run_body(script: &str) -> String {
match run_respond(script, &req(HashMap::new(), None)).unwrap() {
FaultDecision::Error { body, .. } => body,
other => panic!("expected Error(200) carrier, got {other:?}"),
}
}
#[test]
fn per_key_ttl_no_longer_type_errors_and_returns_true() {
let body = run_body(
r#"fn respond(ctx) {
ctx.state.set("k", "v");
http(200, #{ ok: ctx.state.ttl("k", 100) })
}"#,
);
assert!(
body.contains("true"),
"per-key ttl on existing key -> true, got {body}"
);
}
#[test]
fn per_key_ttl_absent_key_returns_false() {
let body =
run_body(r#"fn respond(ctx) { http(200, #{ ok: ctx.state.ttl("absent", 60) }) }"#);
assert!(
body.contains("false"),
"per-key ttl on absent key -> false, got {body}"
);
}
#[test]
fn per_key_ttl_zero_deletes_the_key() {
let body = run_body(
r#"fn respond(ctx) {
ctx.state.set("k", "v");
ctx.state.ttl("k", 0);
http(200, #{ present: ctx.state.exists("k") })
}"#,
);
assert!(
body.contains("false"),
"ttl(key, 0) deletes the key, got {body}"
);
}
#[test]
fn flow_level_ttl_still_dispatches() {
let body = run_body(r#"fn respond(ctx) { http(200, #{ ok: ctx.state.ttl(60) }) }"#);
assert!(
body.contains("true"),
"flow-level ttl(seconds) still works, got {body}"
);
}
#[test]
fn clear_empties_the_flow() {
let body = run_body(
r#"fn respond(ctx) {
ctx.state.set("a", 1);
ctx.state.set("b", 2);
ctx.state.clear();
http(200, #{ a: ctx.state.exists("a"), b: ctx.state.exists("b") })
}"#,
);
assert!(
!body.contains("true"),
"clear() removes every key, got {body}"
);
}
#[test]
fn ttl_bad_arg_raises() {
let result = run_respond(
r#"fn respond(ctx) { http(200, #{ v: ctx.state.ttl(true) }) }"#,
&req(HashMap::new(), None),
);
assert!(result.is_err(), "ttl(<bool>) must raise, got {result:?}");
}
#[test]
fn ctx_request_header_is_case_insensitive() {
let headers = HashMap::from([("X-Flow-Id".to_string(), "flow-9".to_string())]);
let script = r#"
fn respond(ctx) {
let v = ctx.request.header("x-flow-id");
http(200, #{ seen: v })
}
"#;
let decision = run_respond(script, &req(headers, None)).unwrap();
match decision {
FaultDecision::Error { body, .. } => assert!(body.contains("flow-9")),
other => panic!("expected Error(200) carrier, got {other:?}"),
}
}
#[test]
fn ctx_request_headers_map_is_lowercased() {
let headers = HashMap::from([("X-Flow-Id".to_string(), "flow-9".to_string())]);
let script = r#"
fn respond(ctx) {
if ctx.request.headers.contains("x-flow-id") {
http(200)
} else {
http(500)
}
}
"#;
let decision = run_respond(script, &req(headers, None)).unwrap();
match decision {
FaultDecision::Error { status, .. } => assert_eq!(status, 200),
other => panic!("expected 200, got {other:?}"),
}
}
#[test]
fn ctx_request_json_lazy_parse_valid() {
let script = r#"
fn respond(ctx) {
http(200, #{ n: ctx.request.json.n })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), Some(r#"{"n": 7}"#))).unwrap();
match decision {
FaultDecision::Error { body, .. } => assert!(body.contains('7')),
other => panic!("expected Error(200) carrier, got {other:?}"),
}
}
#[test]
fn ctx_request_json_is_unit_for_non_json_body() {
let script = r#"
fn respond(ctx) {
if ctx.request.json == () {
http(200)
} else {
http(500)
}
}
"#;
let decision =
run_respond(script, &req(HashMap::new(), Some("not json at all"))).unwrap();
match decision {
FaultDecision::Error { status, .. } => assert_eq!(status, 200),
other => panic!("expected 200 (json is unit), got {other:?}"),
}
}
#[test]
fn ctx_request_path_params_and_query() {
let script = r#"
fn respond(ctx) {
http(200, #{ id: ctx.request.pathParams.id, page: ctx.request.query.page })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { body, .. } => {
assert!(body.contains("42"));
assert!(body.contains('2'));
}
other => panic!("expected Error(200) carrier, got {other:?}"),
}
}
#[test]
fn ctx_request_body_is_raw_string() {
let script = r#"
fn respond(ctx) {
http(200, ctx.request.body)
}
"#;
let decision =
run_respond(script, &req(HashMap::new(), Some("plain text, not json"))).unwrap();
match decision {
FaultDecision::Error { body, .. } => assert_eq!(body, "plain text, not json"),
other => panic!("expected Error(200) carrier, got {other:?}"),
}
}
#[test]
fn respond_named_wrapper() {
let script = r#"
fn respond(ctx) {
http(503, #{ error: "unavailable" })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error {
status,
body,
headers,
..
} => {
assert_eq!(status, 503);
assert!(body.contains("unavailable"));
assert_eq!(
headers.get("Content-Type").map(String::as_str),
Some("application/json")
);
}
other => panic!("expected Error, got {other:?}"),
}
}
#[test]
fn respond_bare_expression() {
let script = r#"
if ctx.request.method == "POST" {
http(503, #{ error: "unavailable" })
} else {
pass()
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::Error { status: 503, .. }));
}
#[test]
fn http_json_body_sets_content_type_unless_overridden() {
let script = r#"
fn respond(ctx) {
http(503, #{ error: "x" }).header("Retry-After", "1")
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error {
status,
body,
headers,
..
} => {
assert_eq!(status, 503);
assert_eq!(body, r#"{"error":"x"}"#);
assert_eq!(headers.get("Retry-After").map(String::as_str), Some("1"));
assert_eq!(
headers.get("Content-Type").map(String::as_str),
Some("application/json")
);
}
other => panic!("expected Error, got {other:?}"),
}
}
#[test]
fn http_explicit_content_type_wins() {
let script = r#"
fn respond(ctx) {
http(200, #{ a: 1 }).header("Content-Type", "application/vnd.custom+json")
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { headers, .. } => {
assert_eq!(
headers.get("Content-Type").map(String::as_str),
Some("application/vnd.custom+json")
);
}
other => panic!("expected Error, got {other:?}"),
}
}
#[test]
fn http_string_body_passes_through_verbatim() {
let script = r#"
fn respond(ctx) {
http(200, "hello world")
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { body, headers, .. } => {
assert_eq!(body, "hello world");
assert!(!headers.contains_key("Content-Type"));
}
other => panic!("expected Error, got {other:?}"),
}
}
#[test]
fn delay_constructor() {
let script = " fn respond(ctx) { delay(42) } ";
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Latency { duration_ms, .. } => assert_eq!(duration_ms, 42),
other => panic!("expected Latency, got {other:?}"),
}
}
#[test]
fn reset_constructor() {
let script = " fn respond(ctx) { reset() } ";
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::Reset { .. }));
}
#[test]
fn pass_constructor_and_bare_nothing_both_mean_none() {
let decision =
run_respond(" fn respond(ctx) { pass() } ", &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::None));
let decision2 =
run_respond(" fn respond(ctx) { () } ", &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision2, FaultDecision::None));
}
#[test]
fn should_inject_only_script_is_misnamed_entrypoint_error() {
let script = r#"
fn should_inject(request, flow_store) {
#{ inject: true, fault: "error", status: 418, body: "teapot" }
}
"#;
let err = run_respond(script, &req(HashMap::new(), None)).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("entrypoint") && msg.contains("respond"),
"expected the standard misnamed-entrypoint error naming `respond`, got: {msg}"
);
}
#[test]
fn retry_example_end_to_end_with_ctx_state_incr() {
let script = r#"
fn respond(ctx) {
let n = ctx.state.incr("attempts");
if n <= 2 {
http(503, #{ error: "unavailable", attempt: n }).header("Retry-After", "1")
} else {
http(200, #{ ok: true, succeededOnAttempt: n })
}
}
"#;
let engine = RhaiEngine::new(script, "retry-rule").unwrap();
let shared_store = store();
let request = req(HashMap::new(), None);
let d1 = engine
.should_inject_fault(&request, Arc::clone(&shared_store))
.unwrap();
assert!(matches!(d1, FaultDecision::Error { status: 503, .. }));
let d2 = engine
.should_inject_fault(&request, Arc::clone(&shared_store))
.unwrap();
assert!(matches!(d2, FaultDecision::Error { status: 503, .. }));
let d3 = engine.should_inject_fault(&request, shared_store).unwrap();
match d3 {
FaultDecision::Error { status, body, .. } => {
assert_eq!(status, 200);
assert!(body.contains("succeededOnAttempt"));
}
other => panic!("expected 200 on third attempt, got {other:?}"),
}
}
#[test]
fn ctx_store_flow_scopes_to_another_flow_id() {
let script = r#"
fn respond(ctx) {
ctx.store.flow("other-flow").set("shared", 99);
let v = ctx.store.flow("other-flow").get("shared");
http(200, #{ v: v })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { status, body, .. } => {
assert_eq!(status, 200);
assert!(body.contains("99"));
}
other => panic!("expected 200, got {other:?}"),
}
}
#[test]
fn get_or_returns_default_when_absent_then_stored_value() {
let script = r#"
fn respond(ctx) {
let n = ctx.state.get_or("count", 0);
ctx.state.set("count", 7);
let n2 = ctx.state.get_or("count", 0);
http(200, #{ first: n, second: n2 })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { body, .. } => {
assert!(body.contains("\"first\":0"), "got {body}");
assert!(body.contains("\"second\":7"), "got {body}");
}
other => panic!("expected Error(200), got {other:?}"),
}
}
#[test]
fn incr_by_is_atomic_and_starts_at_zero() {
let script = r#"
fn respond(ctx) {
let n = ctx.state.incr_by("hits", 5);
http(200, #{ n: n })
}
"#;
let engine = RhaiEngine::new(script, "incr-by-rule").unwrap();
let shared_store = store();
let request = req(HashMap::new(), None);
let d1 = engine
.should_inject_fault(&request, Arc::clone(&shared_store))
.unwrap();
match d1 {
FaultDecision::Error { body, .. } => assert!(body.contains("\"n\":5")),
other => panic!("expected Error(200), got {other:?}"),
}
let d2 = engine.should_inject_fault(&request, shared_store).unwrap();
match d2 {
FaultDecision::Error { body, .. } => assert!(body.contains("\"n\":10")),
other => panic!("expected Error(200), got {other:?}"),
}
}
#[test]
fn cas_distinguishes_applied_from_conflict() {
let shared_store = store();
let request = req(HashMap::new(), None);
let applied_script = r#"
fn respond(ctx) {
let outcome = ctx.state.cas("status", (), "paid");
http(200, #{ applied: outcome.applied, current: outcome.current })
}
"#;
let applied_engine = RhaiEngine::new(applied_script, "cas-applied").unwrap();
let d1 = applied_engine
.should_inject_fault(&request, Arc::clone(&shared_store))
.unwrap();
match d1 {
FaultDecision::Error { body, .. } => {
assert!(body.contains("\"applied\":true"), "got {body}");
}
other => panic!("expected 200, got {other:?}"),
}
let conflict_script = r#"
fn respond(ctx) {
let outcome = ctx.state.cas("status", "pending", "shipped");
http(200, #{ applied: outcome.applied, current: outcome.current })
}
"#;
let conflict_engine = RhaiEngine::new(conflict_script, "cas-conflict").unwrap();
let d2 = conflict_engine
.should_inject_fault(&request, shared_store)
.unwrap();
match d2 {
FaultDecision::Error { body, .. } => {
assert!(body.contains("\"applied\":false"), "got {body}");
assert!(body.contains("\"current\":\"paid\""), "got {body}");
}
other => panic!("expected 200, got {other:?}"),
}
}
#[test]
fn ttl_sets_per_flow_expiry() {
let script = r#"
fn respond(ctx) {
ctx.state.set("k", 1);
let applied = ctx.state.ttl(3600);
http(200, #{ applied: applied })
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
match decision {
FaultDecision::Error { body, .. } => {
assert!(body.contains("\"applied\":true"), "got {body}");
}
other => panic!("expected 200, got {other:?}"),
}
}
#[test]
fn atomic_ops_propagate_store_errors_fail_loud() {
use crate::extensions::flow_state::FailingFlowStore;
let failing_store: Arc<dyn FlowStore> = Arc::new(FailingFlowStore);
let request = req(HashMap::new(), None);
let get_or_result = RhaiEngine::new(
r#"fn respond(ctx) { ctx.state.get_or("k", 0) }"#,
"fail-get-or",
)
.unwrap()
.should_inject_fault(&request, Arc::clone(&failing_store));
assert!(
get_or_result.is_err(),
"get_or must propagate a store failure, not a default"
);
let incr_by_result = RhaiEngine::new(
r#"fn respond(ctx) { ctx.state.incr_by("k", 1) }"#,
"fail-incr-by",
)
.unwrap()
.should_inject_fault(&request, Arc::clone(&failing_store));
assert!(
incr_by_result.is_err(),
"incr_by must propagate a store failure"
);
let cas_result = RhaiEngine::new(
r#"fn respond(ctx) { ctx.state.cas("k", (), "v") }"#,
"fail-cas",
)
.unwrap()
.should_inject_fault(&request, Arc::clone(&failing_store));
assert!(cas_result.is_err(), "cas must propagate a store failure");
let ttl_result =
RhaiEngine::new(r#"fn respond(ctx) { ctx.state.ttl(60) }"#, "fail-ttl")
.unwrap()
.should_inject_fault(&request, failing_store);
assert!(ttl_result.is_err(), "ttl must propagate a store failure");
}
#[test]
fn state_ops_fail_loud_on_backend_failure() {
use crate::extensions::flow_state::FailingFlowStore;
let request = req(HashMap::new(), None);
let get_err = RhaiEngine::new(r#"fn respond(ctx) { ctx.state.get("k") }"#, "v2-get")
.unwrap()
.should_inject_fault(&request, Arc::new(FailingFlowStore));
assert!(get_err.is_err(), "v2 ctx.state.get must fail loud");
let incr_err = RhaiEngine::new(r#"fn respond(ctx) { ctx.state.incr("k") }"#, "v2-incr")
.unwrap()
.should_inject_fault(&request, Arc::new(FailingFlowStore));
assert!(incr_err.is_err(), "v2 ctx.state.incr must fail loud");
}
#[test]
fn ctx_stub_none_when_unavailable() {
let script = r#"
fn respond(ctx) {
if ctx.stub.scenarioName == () && ctx.stub.id == () {
http(200)
} else {
http(500)
}
}
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::Error { status: 200, .. }));
}
#[test]
fn misnamed_entrypoint_errors_not_none() {
let script = r#"
fn respnod(ctx) {
http(500)
}
"#;
let result = run_respond(script, &req(HashMap::new(), None));
assert!(
result.is_err(),
"a misnamed entrypoint must surface an error, got {result:?}"
);
}
#[test]
fn genuine_bare_expression_still_ok_after_b1() {
let decision = run_respond("http(503)", &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::Error { status: 503, .. }));
}
#[test]
fn helper_function_plus_bare_expression_ok() {
let script = r#"
fn helper() { 503 }
http(helper())
"#;
let decision = run_respond(script, &req(HashMap::new(), None)).unwrap();
assert!(matches!(decision, FaultDecision::Error { status: 503, .. }));
}
}
}