simulator_api/
geyser_route.rs1use crate::SessionId;
9
10pub const SESSION_ID_HEADER: &str = "x-backtest-session-id";
12
13const SESSION_PATH_PREFIX: &str = "/backtest/";
15
16pub fn prefixed_path(session_id: &SessionId, method_path: &str) -> String {
18 format!("{SESSION_PATH_PREFIX}{session_id}{method_path}")
19}
20
21pub fn split_prefixed_path(path: &str) -> Option<(&str, String)> {
25 let (session_id, method) = path.strip_prefix(SESSION_PATH_PREFIX)?.split_once('/')?;
26 Some((session_id, format!("/{method}")))
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
35 fn a_prefixed_path_splits_back_into_its_parts() {
36 for method in [
37 "/geyser.Geyser/Subscribe",
38 "/geyser.Geyser/GetVersion",
39 "/geyser.Geyser/SubscribeReplayInfo",
40 ] {
41 let session_id = SessionId::new();
42 let path = prefixed_path(&session_id, method);
43 let (parsed, rest) =
44 split_prefixed_path(&path).unwrap_or_else(|| panic!("{method} did not split"));
45 assert_eq!(parsed, session_id.to_string(), "{method}");
46 assert_eq!(rest, method, "{method}");
47 }
48 }
49
50 #[test]
51 fn a_path_without_a_session_does_not_split() {
52 for (name, path) in [
53 ("no prefix", "/geyser.Geyser/Subscribe"),
54 ("prefix only", "/backtest/"),
55 ("session with no method", "/backtest/abc"),
56 ("a different route", "/available-ranges"),
57 ] {
58 assert!(split_prefixed_path(path).is_none(), "{name}");
59 }
60 }
61}