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
//! The exchange inspector seam (issue #966): a synchronous, per-imposter hook pair that sees a
//! request before it is matched and the response before it is written, and may replace either.
//!
//! What it is for: policy on **live** exchanges — request linting, contract validation against a
//! schema, compliance capture of request/response pairs, a chaos veto. Nothing upstream could stand
//! in that position before: [`NoMatchInterceptor`](crate::extensions::no_match::NoMatchInterceptor)
//! fires only on a genuine no-match, the
//! [`ResponseDecorator`](crate::extensions::decorate::ResponseDecorator) may only add headers, and
//! the admin authorizer is admin-time. It is also the primitive Mountebank's `matches` array
//! (`record_matches`, parsed and never consulted) needs: the response-side hook sees exactly the
//! request/response pair that array records.
//!
//! Design rulings:
//!
//! - **Inert by default.** [`Imposter`](crate::imposter::Imposter) carries an `Option`, the
//! manager's provider defaults to `None`, and an imposter with no inspector pays one `is_none`
//! check per phase. Nothing else changes: the existing suite is the proof.
//! - **Request side runs after journaling and before matching.** Rejecting before matching means a
//! rejected request never advances a cycler, a scenario FSM or a match counter — the only
//! defensible semantics for "this request was off-policy". The request *is* journaled (it
//! arrived; hiding it would falsify `savedRequests`), and its entry carries the rejection's
//! status and latency like any other.
//! - **Response side runs in the one funnel every path shares** — the serve loop, the `/__rift/`
//! gateway and an embedder's in-process dispatch — after the response is built and before the
//! decorator and CORS. It is not consulted for a response the request-side hook itself produced.
//! - **Early exits see neither hook.** A disabled imposter, a CORS preflight, a 413 and a body-read
//! error return before there is anything to inspect.
//! - **Synchronous, deliberately.** The no-match hook is a boxed future because it parks an
//! already-failed request to rescue it over the network; this one runs on every request of an
//! opted-in imposter, and an async signature invites I/O onto the hot path. Everything an
//! inspector legitimately needs (compiled validators, policy) is process-local.
//! - **Borrowed views.** Both views borrow what the handler already holds; nothing is cloned for an
//! inspector that is not installed, and only the built response body is materialised for one that
//! is.
use Arc;
use Bytes;
use crate;
/// The request-side view: the in-flight request after body collection, borrowed.
/// The response-side view: what the imposter built, after behaviors ran, before it is written.
/// An inspector's verdict.
/// A synchronous hook pair on an imposter's live exchanges. See the module doc for where each
/// hook runs and what a rejection means.
/// Decides, per imposter, whether it gets an inspector — mirroring
/// [`FlowStoreProvider`](crate::extensions::flow_state::FlowStoreProvider): consulted once when the
/// manager creates the imposter, with that imposter's config, and `None` means that imposter runs
/// with no hooks at all.