use super::xpath;
#[derive(Default)]
pub struct BodyCache {
xml: Option<xpath::Document>,
json: Option<serde_json::Value>,
}
impl BodyCache {
pub fn new() -> Self {
BodyCache::default()
}
pub fn xml(&self) -> Option<&xpath::Document> {
self.xml.as_ref()
}
pub fn set_xml(&mut self, xml: xpath::Document) {
self.xml = Some(xml);
}
pub fn json(&self) -> Option<&serde_json::Value> {
self.json.as_ref()
}
pub fn set_json(&mut self, json: serde_json::Value) {
self.json = Some(json);
}
}
#[cfg(test)]
mod tests {
use crate::runner::Value;
use crate::runner::cache::BodyCache;
use crate::runner::xpath::{Document, Format};
#[test]
fn add_and_retry_html() {
let html = "<!DOCTYPE html> \
<html> \
<body> \
<h1>My First Heading</h1> \
<p>My first paragraph.</p> \
</body> \
</html>";
let doc = Document::parse(html, Format::Html).unwrap();
assert_eq!(
doc.eval_xpath("string(//h1)").unwrap(),
Value::String("My First Heading".to_string())
);
let mut cache = BodyCache::new();
assert!(cache.xml().is_none());
cache.set_xml(doc);
let doc = cache.xml().unwrap();
assert_eq!(
doc.eval_xpath("string(//h1)").unwrap(),
Value::String("My First Heading".to_string())
);
}
}