rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
Documentation
//! Request context for behavior processing.

/// Convert a header name to title case (e.g., "content-type" -> "Content-Type").
///
/// This is used for Mountebank compatibility, which expects title-cased header names.
pub fn header_to_title_case(name: &str) -> String {
    let mut title_case = String::with_capacity(name.len());
    for part in name.split_inclusive('-') {
        let mut chars = part.chars();
        if let Some(first_char) = chars.next() {
            title_case.push(first_char.to_ascii_uppercase());
        }
        title_case.push_str(chars.as_str());
    }
    title_case
}

/// Request context for behavior processing
#[derive(Debug, Clone, Default)]
pub struct RequestContext {
    pub method: String,
    pub path: String,
    /// Every value of each query key, in request order (issue #1553). `copy` and `lookup` read a
    /// repeated key's first value, as Mountebank's `getFrom` does; `decorate` and `shellTransform`
    /// see an array (issue #1629, `docs/mountebank/behaviors.md`).
    pub query: crate::util::MultiValueMap,
    /// Every value of each header, names in the order the client sent them (issue #1606). `copy`
    /// and `lookup` read the first value; `decorate` and `shellTransform` see a repeated header as
    /// an array (issue #1629).
    pub headers: crate::util::MultiValueMap,
    pub body: Option<String>,
    /// The request's `gzip` / `br` body did not decode (issue #1584), so it has no body at all:
    /// `body` is `None`, `copy`/`lookup` find no `body` source, and `decorate`/`shellTransform` see
    /// no `body` key, as Mountebank's `request.body` is `undefined`. When `false`, a `None` body is
    /// a request without one, which every consumer reads as `""`.
    pub body_undefined: bool,
    /// `requestFrom` / `ip` / `form` for `decorate` and `MB_REQUEST`, as Mountebank's
    /// `httpRequest.transform` builds them (issue #1451). `None` when there is no client socket,
    /// and `form` only for a non-empty urlencoded body.
    pub request_from: Option<String>,
    pub ip: Option<String>,
    pub form: Option<crate::util::MultiValueMap>,
}

impl RequestContext {
    /// The first value of header `name`, matched ignoring ASCII case: what `copy` and `lookup`
    /// read, as Mountebank's `getFrom` does for a repeated field.
    #[must_use]
    pub fn first_header(&self, name: &str) -> Option<&String> {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(name))
            .and_then(|(_, values)| values.first())
    }
}

impl RequestContext {
    /// Build the behavior-facing view of a request.
    ///
    /// `headers` is the map `collect_request_headers` already built for this request — Title-Case
    /// names, every value the client sent, non-UTF-8 values already dropped with one warning. This
    /// function only *copies* it, every value (issue #1629); it does not re-derive it from the raw
    /// `HeaderMap` (issue #1040). Deriving rather than re-collecting is what stops behaviors
    /// disagreeing with predicates, proxy forwarding, the journal and inject about what the client
    /// sent: before, this was a second pass with a worse rule, coercing an undecodable value to
    /// `""` and keeping a repeated header's *last* value while every other surface took the first.
    ///
    /// A name is absent from the result when it carries no values, never present-but-empty — that
    /// conflation is the defect, not an edge case of it.
    ///
    /// **The caller owns the key casing.** This used to title-case internally, so it was correct
    /// for any input; it now passes keys straight through, which makes it correct only for a map
    /// collected with [`header_to_title_case`]. That matters because a same-typed map with
    /// *lowercase* keys exists one crate away — the intercept listener calls the same collector
    /// with `str::to_string` — and handing that one over would compile and then quietly give
    /// `decorate` and `MB_REQUEST` lowercase header names. `copy` and `lookup` would survive it
    /// (they scan case-insensitively), so the breakage would be partial and silent. Pass the
    /// imposter handler's map.
    pub fn from_request<SH: std::hash::BuildHasher>(
        method: &str,
        uri: &hyper::Uri,
        headers: &indexmap::IndexMap<String, Vec<String>, SH>,
        body: Option<&str>,
        client_addr: Option<std::net::SocketAddr>,
    ) -> Self {
        // The shared parser (issue #1153) — this used to be an inline third copy of it. It keeps
        // request order (issue #1596), which `decorate` and `shellTransform` can show.
        let query_map = uri
            .query()
            .map(crate::util::parse_query_string)
            .unwrap_or_default();

        // Every value (issue #1629): scripts see a repeated header as an array, and `copy` /
        // `lookup` take the first one ([`Self::first_header`]). A name with no values is dropped,
        // never kept present-but-empty.
        let header_map = headers
            .iter()
            .filter(|(_, values)| !values.is_empty())
            .map(|(name, values)| (name.clone(), values.clone()))
            .collect();

        Self {
            method: method.to_string(),
            path: uri.path().to_string(),
            query: query_map,
            headers: header_map,
            body: body.map(|s| s.to_string()),
            body_undefined: false,
            request_from: client_addr.map(|addr| addr.to_string()),
            ip: client_addr.map(|addr| addr.ip().to_string()),
            form: crate::imposter::Imposter::request_form(headers, body),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Issue #611: an undecodable percent-sequence used to blank the value (`unwrap_or_default`),
    // silently destroying text a predicate matches on. Every sibling decode site in the repo
    // (request_filter.rs, intercept.rs, scenarios.rs) passes the raw value through instead.
    #[test]
    fn from_request_passes_through_an_undecodable_query_value() {
        let uri: hyper::Uri = "/p?k=%FF".parse().unwrap();
        let ctx =
            RequestContext::from_request("GET", &uri, &crate::imposter::Headers::new(), None, None);
        assert_eq!(
            ctx.query
                .get("k")
                .and_then(|v| v.first())
                .map(String::as_str),
            Some("%FF"),
            "an undecodable value must pass through raw, not become an empty string"
        );
    }

    // Issue #1596: `decorate` and `shellTransform` see the client's key order, request after request.
    #[test]
    fn from_request_keeps_query_and_form_in_request_order() {
        let uri: hyper::Uri = "/p?b=1&a=2&c=3&a=4".parse().unwrap();
        let form_headers = crate::imposter::Headers::from([(
            "Content-Type".to_string(),
            vec!["application/x-www-form-urlencoded".to_string()],
        )]);
        for _ in 0..20 {
            let ctx = RequestContext::from_request(
                "POST",
                &uri,
                &form_headers,
                Some("z=1&y=2&x=3"),
                None,
            );
            assert_eq!(ctx.query.keys().collect::<Vec<_>>(), ["b", "a", "c"]);
            let form = ctx.form.expect("form");
            assert_eq!(form.keys().collect::<Vec<_>>(), ["z", "y", "x"]);
        }
    }

    #[test]
    fn from_request_still_decodes_a_valid_query_value() {
        let uri: hyper::Uri = "/p?k=hello%20world".parse().unwrap();
        let ctx =
            RequestContext::from_request("GET", &uri, &crate::imposter::Headers::new(), None, None);
        assert_eq!(
            ctx.query
                .get("k")
                .and_then(|v| v.first())
                .map(String::as_str),
            Some("hello world")
        );
    }

    // Issue #614: the key was left raw while the value was decoded, so a behavior reading the
    // request context saw a different parameter name than predicate matching did.
    #[test]
    fn from_request_decodes_an_encoded_query_key() {
        let uri: hyper::Uri = "/p?a%20b=1".parse().unwrap();
        let ctx =
            RequestContext::from_request("GET", &uri, &crate::imposter::Headers::new(), None, None);
        assert_eq!(
            ctx.query
                .get("a b")
                .and_then(|v| v.first())
                .map(String::as_str),
            Some("1"),
            "an encoded key must be decoded, matching imposter::parse_query_string"
        );
    }

    #[test]
    fn from_request_passes_through_an_undecodable_query_key() {
        let uri: hyper::Uri = "/p?%FF=v".parse().unwrap();
        let ctx =
            RequestContext::from_request("GET", &uri, &crate::imposter::Headers::new(), None, None);
        assert_eq!(
            ctx.query
                .get("%FF")
                .and_then(|v| v.first())
                .map(String::as_str),
            Some("v"),
            "an undecodable key must pass through raw, not collapse to an empty key"
        );
    }

    /// The collector's output shape: Title-Case names, every value the client sent, in order.
    fn collected(pairs: &[(&str, &[&str])]) -> crate::imposter::Headers {
        pairs
            .iter()
            .map(|(k, vs)| {
                (
                    header_to_title_case(k),
                    vs.iter().map(|v| (*v).to_string()).collect(),
                )
            })
            .collect()
    }

    // Issue #480 established that context keys are Title-Case. Since #1040 the casing is applied
    // once, by `collect_request_headers`, and `from_request` passes it through — so this now guards
    // that the projection does not re-case or otherwise disturb the collector's key shape.
    #[test]
    fn from_request_preserves_the_collectors_title_case_keys() {
        let headers = collected(&[
            ("content-type", &["application/json"]),
            ("x-custom-header", &["v"]),
        ]);
        let uri: hyper::Uri = "/p".parse().unwrap();

        let ctx = RequestContext::from_request("GET", &uri, &headers, None, None);

        assert_eq!(
            ctx.first_header("Content-Type").map(String::as_str),
            Some("application/json"),
            "header names reaching a behavior must be Title-Case"
        );
        assert_eq!(
            ctx.first_header("X-Custom-Header").map(String::as_str),
            Some("v")
        );
    }

    // Issue #1040: this inverts the pre-#1040 rule. A value that is not valid UTF-8 must reach a
    // behavior as an ABSENT name, not as `""` — an empty string the client never sent, which made
    // `copy` substitute emptiness and `lookup` match an empty key, silently.
    //
    // Driven through the real `collect_request_headers` from a real `HeaderMap` rather than a
    // hand-built map: `from_request` alone can never observe raw bytes (it is handed `String`s), so
    // asserting the absence against a map that simply omits the name would pass against any
    // implementation at all — including one that took the last value. The guarantee only means
    // something as a property of the composed path, so that is what this exercises.
    #[test]
    fn a_header_the_collector_dropped_is_absent_from_the_behavior_context() {
        let mut raw = hyper::HeaderMap::new();
        raw.insert(
            hyper::header::HeaderName::from_static("x-bin"),
            hyper::header::HeaderValue::from_bytes(&[0xff, 0xfe]).unwrap(),
        );
        raw.insert(
            hyper::header::HeaderName::from_static("x-id"),
            hyper::header::HeaderValue::from_static("a"),
        );
        let headers: crate::imposter::Headers =
            crate::imposter::headers::collect_request_headers(&raw, header_to_title_case);
        let uri: hyper::Uri = "/p".parse().unwrap();

        let ctx = RequestContext::from_request("GET", &uri, &headers, None, None);

        assert!(
            !ctx.headers.contains_key("X-Bin"),
            "a dropped header must be absent, not present as an empty string"
        );
        assert_eq!(
            ctx.first_header("X-Id").map(String::as_str),
            Some("a"),
            "the decodable header alongside it is unaffected"
        );
    }

    // The same composed path, for the value-selection half: two values on the wire, first wins.
    // Pinning it here as well as on the hand-built map catches a collector that stopped preserving
    // send order, which would silently change which value every behavior sees.
    #[test]
    fn a_repeated_header_reaches_the_behavior_context_as_its_first_wire_value() {
        let mut raw = hyper::HeaderMap::new();
        raw.append(
            hyper::header::HeaderName::from_static("x-id"),
            hyper::header::HeaderValue::from_static("a"),
        );
        raw.append(
            hyper::header::HeaderName::from_static("x-id"),
            hyper::header::HeaderValue::from_static("b"),
        );
        let headers: crate::imposter::Headers =
            crate::imposter::headers::collect_request_headers(&raw, header_to_title_case);
        let uri: hyper::Uri = "/p".parse().unwrap();

        let ctx = RequestContext::from_request("GET", &uri, &headers, None, None);

        assert_eq!(ctx.first_header("X-Id").map(String::as_str), Some("a"));
    }

    // Issue #1040: `HashMap::insert` was last-wins, so a repeated header exposed its LAST value to
    // behaviors while predicates, `parse_form_data` (#1038) and the inject path all used the FIRST.
    // One request, two answers. First wins everywhere now.
    #[test]
    fn from_request_takes_the_first_value_of_a_repeated_header() {
        let headers = collected(&[("x-id", &["a", "b", "c"])]);
        let uri: hyper::Uri = "/p".parse().unwrap();

        let ctx = RequestContext::from_request("GET", &uri, &headers, None, None);

        assert_eq!(
            ctx.first_header("X-Id").map(String::as_str),
            Some("a"),
            "first value, matching predicates and copy/lookup"
        );
        // Issue #1629: every value is kept, in order, for the scripts that see an array.
        assert_eq!(
            ctx.headers.get("X-Id").map(Vec::as_slice),
            Some(["a".to_string(), "b".to_string(), "c".to_string()].as_slice())
        );
    }

    // A name mapped to no values cannot come from the collector, but `RequestContext` is public
    // API and an embedder can build the map by hand. It must read as absent — never as `""`, which
    // is the exact confusion #1040 removes.
    #[test]
    fn from_request_treats_a_name_with_no_values_as_absent() {
        let headers: crate::imposter::Headers =
            crate::imposter::Headers::from([("X-Empty".to_string(), Vec::new())]);
        let uri: hyper::Uri = "/p".parse().unwrap();

        let ctx = RequestContext::from_request("GET", &uri, &headers, None, None);

        assert!(!ctx.headers.contains_key("X-Empty"));
    }

    #[test]
    fn from_request_with_no_headers_yields_an_empty_map() {
        let uri: hyper::Uri = "/p".parse().unwrap();
        let ctx =
            RequestContext::from_request("GET", &uri, &crate::imposter::Headers::new(), None, None);
        assert!(ctx.headers.is_empty());
    }
}