1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
use std::fs; use std::path::Path; use crate::Config; use crate::ResponseStore; use crate::Request; use crate::VariableSupport; use crate::Result; use crate::execution_order::plan_request_order; use crate::Profile; #[derive(Debug)] pub struct Requestpreprocessor { profile: Profile, config: Config, requests: Vec<Request>, response_data: ResponseStore, } impl Requestpreprocessor { pub fn new( profile: Profile, requests: Vec<Request>, config: Config, ) -> Result<Self> { let requests_in_order = plan_request_order(requests, &profile, &config)?; Ok( Requestpreprocessor { profile, config, requests: requests_in_order, response_data: ResponseStore::new(), } ) } pub fn is_empty(&self) -> bool { self.requests.is_empty() } pub fn notify_response( &mut self, path: &Path, response: &str ) { let path = fs::canonicalize(&path).unwrap(); self.response_data.store(path, response); } } impl Iterator for Requestpreprocessor { type Item = Result<Request>; fn next(&mut self) -> Option<Self::Item> { if self.requests.len() > 0 { let mut req = self.requests.remove(0); let req = req.replace_variables(&self.profile, &self.config, &self.response_data) .map(|_| req); Some(req) } else { None } } } #[cfg(test)] mod dependencies { use std::env; use crate::Request; use crate::test_utils::root; use super::*; #[test] fn should_replace_dependencies_on_next_calls() -> Result<()> { let root = root() .join("resources/test/requests/nested_dependencies"); let init_path = root.join("4.http"); let dep_path = root.join("5.http"); let init_request = Request::from_file(&init_path, false)?; let mut preprocessor = Requestpreprocessor::new( Profile::empty(env::current_dir().unwrap()), vec![init_request], Config::default() )?; preprocessor.next(); preprocessor.notify_response(&dep_path, "dependency"); let result = preprocessor.next().unwrap().unwrap(); assert_eq!(result.url()?, "dependency"); Ok(()) } }