use crate::SessionId;
pub const SESSION_ID_HEADER: &str = "x-backtest-session-id";
const SESSION_PATH_PREFIX: &str = "/backtest/";
pub fn prefixed_path(session_id: &SessionId, method_path: &str) -> String {
format!("{SESSION_PATH_PREFIX}{session_id}{method_path}")
}
pub fn split_prefixed_path(path: &str) -> Option<(&str, String)> {
let (session_id, method) = path.strip_prefix(SESSION_PATH_PREFIX)?.split_once('/')?;
Some((session_id, format!("/{method}")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_prefixed_path_splits_back_into_its_parts() {
for method in [
"/geyser.Geyser/Subscribe",
"/geyser.Geyser/GetVersion",
"/geyser.Geyser/SubscribeReplayInfo",
] {
let session_id = SessionId::new();
let path = prefixed_path(&session_id, method);
let (parsed, rest) =
split_prefixed_path(&path).unwrap_or_else(|| panic!("{method} did not split"));
assert_eq!(parsed, session_id.to_string(), "{method}");
assert_eq!(rest, method, "{method}");
}
}
#[test]
fn a_path_without_a_session_does_not_split() {
for (name, path) in [
("no prefix", "/geyser.Geyser/Subscribe"),
("prefix only", "/backtest/"),
("session with no method", "/backtest/abc"),
("a different route", "/available-ranges"),
] {
assert!(split_prefixed_path(path).is_none(), "{name}");
}
}
}