use std::cell::RefCell;
thread_local! {
static SCRIPT_PREFIX: RefCell<String> = RefCell::new(String::from("/"));
}
pub fn set_script_prefix(prefix: &str) {
SCRIPT_PREFIX.with(|p| {
*p.borrow_mut() = prefix.to_string();
});
}
pub fn get_script_prefix() -> String {
SCRIPT_PREFIX.with(|p| p.borrow().clone())
}
pub fn clear_script_prefix() {
SCRIPT_PREFIX.with(|p| {
*p.borrow_mut() = String::from("/");
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_script_prefix() {
clear_script_prefix();
assert_eq!(get_script_prefix(), "/");
}
#[test]
fn test_set_and_get_script_prefix() {
set_script_prefix("/myapp/");
assert_eq!(get_script_prefix(), "/myapp/");
clear_script_prefix();
}
#[test]
fn test_clear_script_prefix() {
set_script_prefix("/test/");
assert_eq!(get_script_prefix(), "/test/");
clear_script_prefix();
assert_eq!(get_script_prefix(), "/");
}
#[test]
fn test_multiple_set_operations() {
set_script_prefix("/app1/");
assert_eq!(get_script_prefix(), "/app1/");
set_script_prefix("/app2/");
assert_eq!(get_script_prefix(), "/app2/");
set_script_prefix("/");
assert_eq!(get_script_prefix(), "/");
clear_script_prefix();
}
#[test]
fn test_empty_prefix() {
set_script_prefix("");
assert_eq!(get_script_prefix(), "");
clear_script_prefix();
}
#[test]
fn test_prefix_without_trailing_slash() {
set_script_prefix("/myapp");
assert_eq!(get_script_prefix(), "/myapp");
clear_script_prefix();
}
}