fhttp_core/preprocessing/
request_preprocessor.rs1use anyhow::Result;
2use linked_hash_set::LinkedHashSet;
3
4use crate::execution::execution_order::plan_request_order;
5use crate::path_utils::CanonicalizedPathBuf;
6use crate::request_sources::Preprocessed;
7use crate::Config;
8use crate::Profile;
9use crate::RequestSource;
10use crate::ResponseStore;
11
12pub struct Requestpreprocessor {
14 profile: Profile,
15 config: Config,
16 requests: LinkedHashSet<RequestSource>,
17 response_data: ResponseStore,
18}
19
20impl Requestpreprocessor {
21 pub fn new(profile: Profile, requests: Vec<RequestSource>, config: Config) -> Result<Self> {
22 let requests_in_order = plan_request_order(requests, &profile)?;
23
24 Ok(Requestpreprocessor {
25 profile,
26 config,
27 requests: requests_in_order,
28 response_data: ResponseStore::new(),
29 })
30 }
31
32 pub fn is_empty(&self) -> bool {
33 self.requests.is_empty()
34 }
35
36 pub fn notify_response(&mut self, path: &CanonicalizedPathBuf, response: &str) {
37 self.response_data.store(path.clone(), response);
38 }
39}
40
41impl Iterator for Requestpreprocessor {
42 type Item = Result<RequestSource<Preprocessed>>;
43
44 fn next(&mut self) -> Option<Self::Item> {
45 self.requests
46 .pop_front()
47 .map(|req| req.replace_variables(&self.profile, &self.config, &self.response_data))
48 }
49}
50
51#[cfg(test)]
52mod dependencies {
53 use std::env;
54
55 use crate::test_utils::root;
56 use crate::RequestSource;
57
58 use super::*;
59
60 #[test]
61 fn should_replace_dependencies_on_next_calls() -> Result<()> {
62 let root = root().join("resources/test/requests/nested_dependencies");
63 let init_path = root.join("4.http");
64 let dep_path = root.join("5.http");
65
66 let init_request = RequestSource::from_file(init_path, false)?;
67
68 let mut preprocessor = Requestpreprocessor::new(
69 Profile::empty(env::current_dir().unwrap()),
70 vec![init_request],
71 Config::default(),
72 )?;
73
74 preprocessor.next();
75 preprocessor.notify_response(&dep_path, "dependency");
76 let result = preprocessor.next().unwrap().unwrap();
77 let req = result.parse()?;
78 assert_eq!(req.request.url, "dependency");
79
80 Ok(())
81 }
82}