use super::extraction::{ExtractionMethod, XPathSelectorError};
use super::request::RequestContext;
use super::spliced::{
Spliced, SplicedHeaders, authored_headers, plain_headers, replace_response_token,
};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::{BTreeSet, HashMap};
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CopyBehavior {
pub from: CopySource,
pub into: String,
#[serde(rename = "using")]
pub extraction: ExtractionMethod,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum CopySource {
Simple(String),
#[cfg_attr(
feature = "schema",
schemars(extend("minProperties" = 1, "maxProperties" = 1))
)]
Nested(HashMap<String, String>),
}
impl CopySource {
pub fn extract(&self, request: &RequestContext) -> Option<String> {
match self {
CopySource::Simple(field) => {
let field = field.to_ascii_lowercase();
match field.as_str() {
"requestfrom" => request.request_from.clone(),
"path" => Some(request.path.clone()),
"method" => Some(request.method.clone()),
"body" if request.body_undefined => None,
"body" => Some(request.body.clone().unwrap_or_default()),
"ip" => request.ip.clone(),
_ => None,
}
}
CopySource::Nested(map) => {
if let Some(name) = map.get("query") {
ignoring_case(&request.query, name).and_then(|v| v.first().cloned())
} else if let Some(name) = map.get("headers") {
ignoring_case(&request.headers, name).and_then(|v| v.first().cloned())
} else if let Some(name) = map.get("form") {
request
.form
.as_ref()
.and_then(|form| ignoring_case(form, name))
.and_then(|v| v.first().cloned())
} else {
None
}
}
}
}
}
fn ignoring_case<'a, V: 'a>(
map: impl IntoIterator<Item = (&'a String, &'a V)> + Clone,
name: &str,
) -> Option<&'a V> {
map.clone()
.into_iter()
.find(|(k, _)| k.as_str() == name)
.or_else(|| map.into_iter().find(|(k, _)| k.eq_ignore_ascii_case(name)))
.map(|(_, v)| v)
}
pub fn apply_copy_behaviors(
body: &str,
headers: &mut crate::imposter::Headers,
behaviors: &[CopyBehavior],
request: &RequestContext,
stub: crate::imposter::headers::StubRef<'_>,
) -> Result<String, XPathSelectorError> {
let mut spliced_body = Spliced::authored(body.to_string());
let mut spliced_headers = authored_headers(std::mem::take(headers));
let applied = apply_copy_spliced(
&mut spliced_body,
&mut spliced_headers,
&mut None,
behaviors,
request,
stub,
);
*headers = plain_headers(spliced_headers);
applied.map(|()| spliced_body.into_text())
}
pub(crate) fn apply_copy_spliced(
body: &mut Spliced,
headers: &mut SplicedHeaders,
status: &mut Option<Spliced>,
behaviors: &[CopyBehavior],
request: &RequestContext,
stub: crate::imposter::headers::StubRef<'_>,
) -> Result<(), XPathSelectorError> {
for behavior in behaviors {
let Some(source) =
source_or_undefined(&behavior.extraction, behavior.from.extract(request))
else {
continue;
};
let indices = written_indices(&behavior.into, body, headers, status.as_ref());
if indices.is_empty() {
let Some(bare) = behavior.extraction.extract(&source)? else {
continue;
};
replace_response_token(body, headers, status, &behavior.into, &bare, stub);
continue;
}
let values = behavior.extraction.matches(&source)?;
let Some(bare) = bare_value(&behavior.extraction, &values) else {
continue;
};
for i in indices {
let Some(value) = values.get(i) else {
continue;
};
let token = format!("{}[{i}]", behavior.into);
replace_response_token(
body,
headers,
status,
&token,
value.as_deref().unwrap_or(""),
stub,
);
}
replace_response_token(body, headers, status, &behavior.into, bare, stub);
}
Ok(())
}
fn written_indices(
into: &str,
body: &Spliced,
headers: &SplicedHeaders,
status: Option<&Spliced>,
) -> BTreeSet<usize> {
let prefix = format!("{into}[");
let mut indices = BTreeSet::new();
let names = headers.keys().map(|name| name.as_str());
for text in std::iter::once(body.as_str())
.chain(headers.values().flatten().map(Spliced::as_str))
.chain(names)
.chain(status.map(Spliced::as_str))
{
for (at, _) in text.match_indices(&prefix) {
let rest = &text[at + prefix.len()..];
let len = rest.bytes().take_while(u8::is_ascii_digit).count();
let digits = &rest[..len];
let canonical = len > 0 && (digits == "0" || !digits.starts_with('0'));
if canonical
&& rest[len..].starts_with(']')
&& let Ok(i) = digits.parse()
{
indices.insert(i);
}
}
}
indices
}
fn bare_value<'a>(method: &ExtractionMethod, values: &'a [Option<String>]) -> Option<&'a str> {
let whole = values.first().and_then(Option::as_deref);
match method {
ExtractionMethod::Regex { .. } => values.get(1).and_then(Option::as_deref).or(whole),
ExtractionMethod::JsonPath { .. } | ExtractionMethod::XPath { .. } => whole,
}
}
pub(crate) fn source_or_undefined(
extraction: &ExtractionMethod,
extracted: Option<String>,
) -> Option<Cow<'static, str>> {
match (extracted, extraction) {
(Some(source), _) => Some(Cow::Owned(source)),
(None, ExtractionMethod::Regex { .. }) => Some(Cow::Borrowed("undefined")),
(None, ExtractionMethod::JsonPath { .. } | ExtractionMethod::XPath { .. }) => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
const FIXTURE_STUB: crate::imposter::headers::StubRef<'static> =
crate::imposter::headers::StubRef {
port: 0,
index: 0,
id: None,
};
fn ctx_1590() -> RequestContext {
RequestContext {
method: "POST".into(),
path: "/Pth".into(),
query: crate::util::OrderedMap::from_iter([(
"q".into(),
vec!["qv".into(), "second".into()],
)]),
headers: crate::util::OrderedMap::from_iter([("X-Test".into(), vec!["hv".into()])]),
body: Some("b".into()),
ip: Some("::ffff:127.0.0.1".into()),
request_from: Some("::ffff:127.0.0.1:55333".into()),
form: Some(crate::util::OrderedMap::from_iter([(
"f".into(),
vec!["fv".into()],
)])),
..Default::default()
}
}
fn nested(container: &str, name: &str) -> CopySource {
CopySource::Nested(HashMap::from([(container.into(), name.into())]))
}
#[test]
fn a_simple_from_matches_the_field_name_case_insensitively() {
let r = ctx_1590();
for (from, want) in [
("BODY", "b"),
("Path", "/Pth"),
("METHOD", "POST"),
("ip", "::ffff:127.0.0.1"),
("IP", "::ffff:127.0.0.1"),
("requestFrom", "::ffff:127.0.0.1:55333"),
("REQUESTFROM", "::ffff:127.0.0.1:55333"),
] {
assert_eq!(
CopySource::Simple(from.into()).extract(&r).as_deref(),
Some(want),
"from {from}"
);
}
assert_eq!(CopySource::Simple("nope".into()).extract(&r), None);
}
#[test]
fn a_nested_from_matches_the_inner_name_case_insensitively_and_the_container_exactly() {
let r = ctx_1590();
assert_eq!(nested("query", "Q").extract(&r).as_deref(), Some("qv"));
assert_eq!(
nested("headers", "X-TEST").extract(&r).as_deref(),
Some("hv")
);
assert_eq!(nested("form", "f").extract(&r).as_deref(), Some("fv"));
assert_eq!(nested("form", "F").extract(&r).as_deref(), Some("fv"));
assert_eq!(nested("QUERY", "q").extract(&r), None);
assert_eq!(nested("Headers", "x-test").extract(&r), None);
assert_eq!(nested("form", "missing").extract(&r), None);
}
#[test]
fn a_form_or_ip_source_without_a_client_is_none() {
let r = RequestContext::default();
assert_eq!(nested("form", "f").extract(&r), None);
assert_eq!(CopySource::Simple("ip".into()).extract(&r), None);
}
#[test]
fn a_lookup_key_from_reads_the_field_name_case_insensitively() {
let key = super::super::lookup::LookupKey {
from: CopySource::Simple("PATH".into()),
extraction: ExtractionMethod::Regex {
selector: "^/(.+)$".into(),
options: None,
},
index: None,
};
assert_eq!(
key.extract(&ctx_1590())
.expect("a regex never fails")
.as_deref(),
Some("Pth")
);
}
#[test]
fn test_copy_source_simple() {
let request = RequestContext {
method: "GET".to_string(),
path: "/users/123".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: Some("test body".to_string()),
..Default::default()
};
let source = CopySource::Simple("path".to_string());
assert_eq!(source.extract(&request), Some("/users/123".to_string()));
let source = CopySource::Simple("method".to_string());
assert_eq!(source.extract(&request), Some("GET".to_string()));
let source = CopySource::Simple("body".to_string());
assert_eq!(source.extract(&request), Some("test body".to_string()));
}
#[test]
fn an_undefined_body_is_no_copy_source() {
let source = CopySource::Simple("body".to_string());
let undefined = RequestContext {
body_undefined: true,
..Default::default()
};
assert_eq!(source.extract(&undefined), None);
assert_eq!(
source.extract(&RequestContext::default()),
Some(String::new())
);
}
#[test]
fn test_copy_source_nested() {
let mut query = crate::util::OrderedMap::default();
query.insert("name".to_string(), vec!["Alice".to_string()]);
let mut headers = crate::util::OrderedMap::default();
headers.insert(
"content-type".to_string(),
vec!["application/json".to_string()],
);
let request = RequestContext {
method: "GET".to_string(),
path: "/test".to_string(),
query,
headers,
body: None,
..Default::default()
};
let mut map = HashMap::new();
map.insert("query".to_string(), "name".to_string());
let source = CopySource::Nested(map);
assert_eq!(source.extract(&request), Some("Alice".to_string()));
let mut map = HashMap::new();
map.insert("headers".to_string(), "Content-Type".to_string());
let source = CopySource::Nested(map);
assert_eq!(
source.extract(&request),
Some("application/json".to_string())
);
}
#[test]
fn test_apply_copy_behaviors() {
let mut query = crate::util::OrderedMap::default();
query.insert("name".to_string(), vec!["Alice".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/users/123".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![
CopyBehavior {
from: CopySource::Simple("path".to_string()),
into: "${PATH}".to_string(),
extraction: ExtractionMethod::Regex {
selector: r"/users/(\d+)".to_string(),
options: None,
},
},
CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "name".to_string());
CopySource::Nested(map)
},
into: "${NAME}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
},
];
let body = r#"{"userId": "${PATH}", "greeting": "Hello, ${NAME}!"}"#;
let mut headers = crate::imposter::Headers::new();
let result = apply_copy_behaviors(body, &mut headers, &behaviors, &request, FIXTURE_STUB)
.expect("copies");
assert_eq!(result, r#"{"userId": "123", "greeting": "Hello, Alice!"}"#);
}
#[test]
fn copy_repairs_a_header_value_it_rewrote_from_request_data() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["one\rtwo".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Echo".to_string(), vec!["v=${q}".to_string()]);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["X-Echo"],
vec!["v=onetwo".to_string()],
"the CR the client supplied is removed; without the repair this value is unrepresentable"
);
}
#[test]
fn copy_into_a_body_only_stub_does_not_warn_about_headers() {
let request = RequestContext {
method: "POST".to_string(),
path: "/x".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: Some("one\r\ntwo".to_string()),
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: CopySource::Simple("body".to_string()),
into: "${b}".to_string(),
extraction: ExtractionMethod::Regex {
selector: "(?s).*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Plain".to_string(), vec!["no token here".to_string()]);
let mut body = String::new();
let events = crate::test_support::captured_logs(|| {
body = apply_copy_behaviors("x=${b}", &mut headers, &behaviors, &request, FIXTURE_STUB)
.expect("copies");
});
assert_eq!(
body, "x=one\r\ntwo",
"the body keeps the bytes the client sent"
);
assert_eq!(headers["X-Plain"], vec!["no token here".to_string()]);
assert!(
events.is_empty(),
"no header used this token, so the header repair must not have run, but it logged {} event(s)",
events.len()
);
}
#[test]
fn every_repaired_value_of_a_multi_value_header_names_the_same_stub() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["one\rtwo".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert(
"Set-Cookie".to_string(),
vec!["a=${q}".to_string(), "b=${q}".to_string()],
);
let events = crate::test_support::captured_logs(|| {
apply_copy_behaviors(
"",
&mut headers,
&behaviors,
&request,
crate::imposter::headers::StubRef {
port: 4545,
index: 2,
id: Some("echo"),
},
)
.expect("copies");
});
assert_eq!(
headers["Set-Cookie"],
vec!["a=onetwo".to_string(), "b=onetwo".to_string()],
"both cookie lines survive and are both repaired"
);
assert_eq!(
events.len(),
1,
"the replacement is repaired once, not once per header line"
);
assert_eq!(events[0].field("port"), Some("4545"));
assert_eq!(events[0].field("stub"), Some("2"));
assert_eq!(events[0].field("stub_id"), Some("echo"));
}
#[test]
fn copy_repair_warning_names_the_stub() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["one\rtwo".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Echo".to_string(), vec!["v=${q}".to_string()]);
let events = crate::test_support::captured_logs(|| {
apply_copy_behaviors(
"",
&mut headers,
&behaviors,
&request,
crate::imposter::headers::StubRef {
port: 4545,
index: 2,
id: Some("echo"),
},
)
.expect("copies");
});
assert_eq!(headers["X-Echo"], vec!["v=onetwo".to_string()]);
assert_eq!(events.len(), 1, "one repair, one warning");
assert_eq!(events[0].field("port"), Some("4545"));
assert_eq!(events[0].field("stub"), Some("2"));
assert_eq!(events[0].field("stub_id"), Some("echo"));
}
#[test]
fn copy_repairs_the_substituted_text_but_not_a_literal_control_char_beside_it() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["one\rtwo".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Echo".to_string(), vec!["bad\nvalue=${q}".to_string()]);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["X-Echo"],
vec!["bad\nvalue=onetwo".to_string()],
"the CR from the request is gone; the author's literal newline is not"
);
}
#[test]
fn copy_leaves_a_header_value_without_the_token_untouched() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["hi".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Literal".to_string(), vec!["bad\nvalue".to_string()]);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["X-Literal"],
vec!["bad\nvalue".to_string()],
"a literal config value must reach the builder unrepaired and fail loudly"
);
}
#[test]
fn copy_with_a_missing_source_does_not_repair_a_literal_config_value() {
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: r"\d+".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-Echo".to_string(), vec!["bad\nvalue=${q}".to_string()]);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["X-Echo"],
vec!["bad\nvalue=${q}".to_string()],
"the token is kept; the literal newline beside it is left to fail loudly"
);
}
#[test]
fn copy_preserves_multi_value_headers_and_substitutes_each() {
let mut query = crate::util::OrderedMap::default();
query.insert("q".to_string(), vec!["hi".to_string()]);
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query,
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: ".*".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert(
"Set-Cookie".to_string(),
vec!["a=1".to_string(), "b=${q}".to_string()],
);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["Set-Cookie"],
vec!["a=1".to_string(), "b=hi".to_string()]
);
}
#[test]
fn copy_missing_source_keeps_token_per_value_without_folding() {
let request = RequestContext {
method: "GET".to_string(),
path: "/x".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let behaviors = vec![CopyBehavior {
from: {
let mut map = HashMap::new();
map.insert("query".to_string(), "q".to_string());
CopySource::Nested(map)
},
into: "${q}".to_string(),
extraction: ExtractionMethod::Regex {
selector: r"\d+".to_string(),
options: None,
},
}];
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert(
"Set-Cookie".to_string(),
vec!["a=1".to_string(), "b=${q}".to_string()],
);
apply_copy_behaviors("", &mut headers, &behaviors, &request, FIXTURE_STUB).expect("copies");
assert_eq!(
headers["Set-Cookie"],
vec!["a=1".to_string(), "b=${q}".to_string()]
);
}
fn copy_once(
from: &str,
extraction: serde_json::Value,
request: &RequestContext,
body: &str,
) -> (String, Vec<String>) {
let behavior: CopyBehavior = serde_json::from_value(serde_json::json!({
"from": serde_json::from_str::<serde_json::Value>(from).expect("from is JSON"),
"into": "$C",
"using": extraction,
}))
.expect("the copy parses");
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert("X-C".to_string(), vec!["<$C>".to_string()]);
let served = apply_copy_behaviors(body, &mut headers, &[behavior], request, FIXTURE_STUB)
.expect("copies");
(served, headers.shift_remove("X-C").expect("header kept"))
}
fn request_with_body(body: &str) -> RequestContext {
RequestContext {
method: "POST".to_string(),
path: "/abc".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: Some(body.to_string()),
..Default::default()
}
}
#[test]
fn a_regex_that_does_not_match_leaves_the_token_in_body_and_header() {
let (body, header) = copy_once(
r#""path""#,
serde_json::json!({"method": "regex", "selector": "\\d+"}),
&request_with_body(""),
"[abc][$C]",
);
assert_eq!(body, "[abc][$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
}
#[test]
fn a_regex_that_matches_only_under_ignore_case_leaves_the_token_without_it() {
let using = |ignore_case: bool| {
serde_json::json!({
"method": "regex",
"selector": "ABC",
"options": {"ignoreCase": ignore_case}
})
};
let (body, header) = copy_once(r#""path""#, using(false), &request_with_body(""), "[$C]");
assert_eq!(body, "[$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
let (body, header) = copy_once(r#""path""#, using(true), &request_with_body(""), "[$C]");
assert_eq!(body, "[abc]");
assert_eq!(header, vec!["<abc>".to_string()]);
}
#[test]
fn a_jsonpath_that_selects_nothing_leaves_the_token() {
let (body, header) = copy_once(
r#""body""#,
serde_json::json!({"method": "jsonpath", "selector": "$.missing"}),
&request_with_body(r#"{"present": 1}"#),
"[$C]",
);
assert_eq!(body, "[$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
}
#[test]
fn an_xpath_that_selects_nothing_leaves_the_token() {
let (body, header) = copy_once(
r#""body""#,
serde_json::json!({"method": "xpath", "selector": "//missing"}),
&request_with_body("<doc><present>1</present></doc>"),
"[$C]",
);
assert_eq!(body, "[$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
}
#[test]
fn an_absent_query_source_leaves_the_token() {
let (body, header) = copy_once(
r#"{"query": "id"}"#,
serde_json::json!({"method": "regex", "selector": "(\\d+)"}),
&request_with_body(""),
"[$C]",
);
assert_eq!(body, "[$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
}
#[test]
fn a_request_without_a_body_copies_from_the_empty_body() {
let request = RequestContext {
method: "GET".to_string(),
path: "/".to_string(),
query: Default::default(),
headers: crate::util::OrderedMap::default(),
body: None,
..Default::default()
};
let (body, header) = copy_once(
r#""body""#,
serde_json::json!({"method": "regex", "selector": ".*"}),
&request,
"[$C]",
);
assert_eq!(body, "[]");
assert_eq!(header, vec!["<>".to_string()]);
}
#[test]
fn a_copy_that_extracts_nothing_does_not_stop_the_next_copy() {
let behaviors: Vec<CopyBehavior> = serde_json::from_value(serde_json::json!([
{"from": "path", "into": "$A", "using": {"method": "regex", "selector": "\\d+"}},
{"from": "path", "into": "$B", "using": {"method": "regex", "selector": "abc"}}
]))
.expect("the copies parse");
let served = apply_copy_behaviors(
"$A/$B",
&mut crate::imposter::Headers::new(),
&behaviors,
&request_with_body(""),
FIXTURE_STUB,
)
.expect("copies");
assert_eq!(served, "$A/abc");
}
fn regex(selector: &str) -> serde_json::Value {
serde_json::json!({"method": "regex", "selector": selector})
}
#[test]
fn indexed_tokens_take_each_capture_group() {
let (body, header) = copy_once(
r#""body""#,
regex(r"(\w+)-(\w+)"),
&request_with_body("ab-cd"),
"$C[1]-$C[2]|$C[0]|$C",
);
assert_eq!(body, "ab-cd|ab-cd|ab");
assert_eq!(header, vec!["<ab>".to_string()]);
}
#[test]
fn index_zero_is_the_whole_match() {
let (body, _) = copy_once(
r#""body""#,
regex(r"\d+"),
&request_with_body("x42"),
"$C[0]",
);
assert_eq!(body, "42");
}
#[test]
fn a_jsonpath_index_selects_that_node() {
let (body, _) = copy_once(
r#""body""#,
serde_json::json!({"method": "jsonpath", "selector": "$..id"}),
&request_with_body(r#"[{"id":1},{"id":2}]"#),
"$C[0],$C[1]|$C",
);
assert_eq!(body, "1,2|1");
}
#[test]
fn an_xpath_index_selects_that_node_in_document_order() {
let (body, _) = copy_once(
r#""body""#,
serde_json::json!({"method": "xpath", "selector": "//n"}),
&request_with_body("<d><n>a</n><n>b</n><n>c</n></d>"),
"$C[2]$C[1]$C[0]|$C",
);
assert_eq!(body, "cba|a");
}
#[test]
fn an_out_of_range_or_non_canonical_index_has_its_prefix_rewritten() {
let (body, _) = copy_once(
r#""body""#,
regex(r"(\w+)-(\w+)"),
&request_with_body("ab-cd"),
"$C[5]|$C[01]",
);
assert_eq!(body, "ab[5]|ab[01]");
}
#[test]
fn a_non_participating_group_is_empty() {
let (body, _) = copy_once(
r#""body""#,
regex("(a)?(b)"),
&request_with_body("b"),
"[$C[1]][$C[2]][$C]",
);
assert_eq!(body, "[][b][b]");
}
#[test]
fn indexed_tokens_are_replaced_in_each_value_of_a_multi_value_header() {
let behavior: CopyBehavior = serde_json::from_value(serde_json::json!({
"from": "body", "into": "$C", "using": regex(r"(\w+)-(\w+)")
}))
.expect("the copy parses");
let mut headers: crate::imposter::Headers = crate::imposter::Headers::new();
headers.insert(
"Set-Cookie".to_string(),
vec![
"a=$C[1]".to_string(),
"b=$C[2]".to_string(),
"c=1".to_string(),
],
);
apply_copy_behaviors(
"",
&mut headers,
&[behavior],
&request_with_body("ab-cd"),
FIXTURE_STUB,
)
.expect("copies");
assert_eq!(
headers["Set-Cookie"],
vec!["a=ab".to_string(), "b=cd".to_string(), "c=1".to_string()]
);
}
#[test]
fn a_request_value_spelling_an_indexed_token_is_not_expanded() {
let (body, _) = copy_once(
r#""body""#,
regex("(.*)"),
&request_with_body("$C[1]$C"),
"$C[0]",
);
assert_eq!(body, "$C[1]$C");
}
#[test]
fn an_index_inserted_by_an_earlier_copy_composes() {
let behaviors: Vec<CopyBehavior> = serde_json::from_value(serde_json::json!([
{"from": {"query": "i"}, "into": "${I}", "using": regex(r"\d")},
{"from": "body", "into": "$C", "using": regex(r"(\w+)-(\w+)")}
]))
.expect("the copies parse");
let mut request = request_with_body("ab-cd");
request.query.insert("i".to_string(), vec!["2".to_string()]);
let served = apply_copy_behaviors(
"$C[${I}]",
&mut crate::imposter::Headers::new(),
&behaviors,
&request,
FIXTURE_STUB,
)
.expect("copies");
assert_eq!(served, "cd");
}
#[test]
fn written_indices_finds_only_canonical_indexed_tokens() {
let body = Spliced::authored(
"$C[0] $C[12] $C[01] $C[] $C[x] $C[3 $D[4] $C[18446744073709551616]".to_string(),
);
let headers = authored_headers(crate::imposter::Headers::from([(
"X-C".to_string(),
vec!["$C[7]".to_string()],
)]));
assert_eq!(
written_indices("$C", &body, &headers, None),
BTreeSet::from([0, 7, 12])
);
}
#[test]
fn a_missing_source_runs_a_regex_over_the_text_undefined() {
let request = request_with_body("");
for (from, selector, expected) in [
(r#"{"query": "nosuch"}"#, ".*", "undefined"),
(r#"{"headers": "X-None"}"#, ".*", "undefined"),
(r#"{"query": "nosuch"}"#, r"\w+", "undefined"),
(r#"{"query": "nosuch"}"#, "und", "und"),
(r#""nosuch""#, ".*", "undefined"),
(r#"{"query": "nosuch"}"#, r"\d+", "$C"),
] {
let (body, header) = copy_once(from, regex(selector), &request, "[$C]");
assert_eq!(body, format!("[{expected}]"), "{from} {selector}");
assert_eq!(header, vec![format!("<{expected}>")], "{from} {selector}");
}
}
#[test]
fn a_missing_source_leaves_the_token_for_jsonpath_and_xpath() {
let request = request_with_body("");
for using in [
serde_json::json!({"method": "jsonpath", "selector": "$.a"}),
serde_json::json!({"method": "xpath", "selector": "//a"}),
] {
let (body, header) = copy_once(r#"{"query": "nosuch"}"#, using, &request, "[$C]");
assert_eq!(body, "[$C]");
assert_eq!(header, vec!["<$C>".to_string()]);
}
}
fn copy_path_into_headers(path: &str, headers: &[(&str, &str)]) -> crate::imposter::Headers {
let behavior: CopyBehavior = serde_json::from_value(serde_json::json!({
"from": "path", "into": "${v}", "using": regex(r"[^/]+")
}))
.expect("the copy parses");
let mut request = request_with_body("");
request.path = path.to_string();
let mut map: crate::imposter::Headers = headers
.iter()
.map(|(k, v)| ((*k).to_string(), vec![(*v).to_string()]))
.collect();
apply_copy_behaviors("", &mut map, &[behavior], &request, FIXTURE_STUB).expect("copies");
map
}
#[test]
fn a_token_in_a_header_name_is_replaced() {
let headers = copy_path_into_headers("/p", &[("X-${v}", "1")]);
assert_eq!(
headers,
crate::imposter::Headers::from([("X-p".to_string(), vec!["1".to_string()])])
);
}
#[test]
fn a_header_name_and_its_value_are_both_replaced() {
let headers = copy_path_into_headers("/p", &[("X-${v}", "v=${v}")]);
assert_eq!(
headers,
crate::imposter::Headers::from([("X-p".to_string(), vec!["v=p".to_string()])])
);
}
#[test]
fn a_renamed_header_replaces_one_already_of_that_name() {
let headers = copy_path_into_headers("/p", &[("X-${v}", "renamed"), ("x-P", "authored")]);
assert_eq!(
headers,
crate::imposter::Headers::from([("X-p".to_string(), vec!["renamed".to_string()])])
);
}
#[test]
fn a_header_name_whose_token_has_no_value_keeps_it() {
let behavior: CopyBehavior = serde_json::from_value(serde_json::json!({
"from": "path", "into": "${v}", "using": regex(r"\d+")
}))
.expect("the copy parses");
let mut headers =
crate::imposter::Headers::from([("X-${v}".to_string(), vec!["1".to_string()])]);
apply_copy_behaviors(
"",
&mut headers,
&[behavior],
&request_with_body(""),
FIXTURE_STUB,
)
.expect("copies");
assert_eq!(
headers,
crate::imposter::Headers::from([("X-${v}".to_string(), vec!["1".to_string()])])
);
}
#[test]
fn an_indexed_token_in_a_header_name_is_replaced() {
let behavior: CopyBehavior = serde_json::from_value(serde_json::json!({
"from": "path", "into": "$C", "using": regex(r"(\w+)-(\w+)")
}))
.expect("the copy parses");
let mut request = request_with_body("");
request.path = "/ab-cd".to_string();
let mut headers =
crate::imposter::Headers::from([("X-$C[2]".to_string(), vec!["1".to_string()])]);
apply_copy_behaviors("", &mut headers, &[behavior], &request, FIXTURE_STUB)
.expect("copies");
assert_eq!(
headers,
crate::imposter::Headers::from([("X-cd".to_string(), vec!["1".to_string()])])
);
}
}