ringo-flow 0.10.1

Declarative telephony scenario test runner for baresip, built on ringo-core
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Language-neutral HTTP mock server: a scenario starts one, registers per-route
//! responses (static, or a dynamic responder supplied by the language adapter),
//! and inspects the requests it received. It backs the webhook-driven e2e pattern
//! — a telephony API calls our webhook and we answer with the actions it should
//! perform.
//!
//! The server runs on the shared tokio runtime (`Ctx::rt`); the script thread only
//! ever touches the route table and the recorded requests behind a `Mutex`, so
//! waiting for a webhook is just polling `request_count(...)` via `await_until` —
//! no second blocking mechanism.

use super::ctx::Ctx;
use crate::runtime::report::Event;
use axum::body::{Body, to_bytes};
use axum::extract::State;
use axum::http::{Request, Response, StatusCode, header};
use axum::{Router, routing::any};
use regex::Regex;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::sync::oneshot;
use tokio::task::JoinHandle;

/// Largest request body the mock will buffer (webhooks are small JSON payloads).
const MAX_BODY: usize = 1 << 20; // 1 MiB

/// A request the mock server received, exposed to scenarios for assertions.
#[derive(Clone, Debug)]
pub struct MockRequest {
    pub method: String,
    pub path: String,
    /// Query parameters (percent-decoded), last value wins on duplicates.
    pub query: HashMap<String, String>,
    /// Request headers, names lower-cased (like [`super::http::HttpResponse`]).
    pub headers: HashMap<String, String>,
    pub body: String,
}

/// The response the mock returns for a matched route.
#[derive(Clone)]
pub struct MockResponse {
    pub status: u16,
    pub content_type: Option<String>,
    pub headers: Vec<(String, String)>,
    pub body: String,
}

/// What to answer for a route: a fixed response, or a closure the language adapter
/// supplies (e.g. a Rhai responder). The closure must be pure — request in,
/// response out — and must not touch agent sessions (it runs on a runtime worker,
/// concurrently with the script thread).
pub enum Responder {
    Static(MockResponse),
    /// A responder closure. On `Err`, the failure is logged to the scenario (never
    /// exposed over HTTP); the caller gets a bare `500`.
    Dynamic(Box<dyn Fn(MockRequest) -> Result<MockResponse, String> + Send + Sync>),
}

/// How a route matches a request path: an exact path, or an (anchored) regex.
#[derive(Clone)]
pub enum PathMatcher {
    Exact(String),
    Regex(Regex),
}

impl PathMatcher {
    /// A regex matcher, anchored to the whole path so `"/calls/.*"` matches
    /// `"/calls/123"` but not a longer string containing it. Errors on a bad pattern.
    pub fn regex(pattern: &str) -> Result<Self, String> {
        Regex::new(&format!("^(?:{pattern})$"))
            .map(PathMatcher::Regex)
            .map_err(|e| format!("invalid path regex `{pattern}`: {e}"))
    }
    /// Whether this matcher accepts `path`.
    pub fn matches(&self, path: &str) -> bool {
        match self {
            PathMatcher::Exact(p) => p == path,
            PathMatcher::Regex(re) => re.is_match(path),
        }
    }
    /// A stable identity for replace-on-re-register (kind + pattern text).
    fn identity(&self) -> (u8, &str) {
        match self {
            PathMatcher::Exact(p) => (0, p.as_str()),
            PathMatcher::Regex(re) => (1, re.as_str()),
        }
    }
    /// A human label, e.g. `/voice` or `~^/calls/.*$`.
    pub fn label(&self) -> String {
        match self {
            PathMatcher::Exact(p) => p.clone(),
            PathMatcher::Regex(re) => format!("~{}", re.as_str()),
        }
    }
}

/// One registered route. `method` is `None` to match any HTTP method.
struct Route {
    method: Option<String>, // UPPERCASE, or None = any
    path: PathMatcher,
    responder: Arc<Responder>,
}

/// The shared state of a running mock server. Cheap to clone via `Arc`; the script
/// handle, the serving task and the [`Ctx`] teardown list all hold one.
pub struct MockServerInner {
    port: u16,
    routes: Mutex<Vec<Route>>,
    recorded: Mutex<Vec<MockRequest>>,
    shutdown: Mutex<Option<oneshot::Sender<()>>>,
    /// The serving task, awaited at teardown so the port is provably released
    /// before the next scenario can rebind it.
    task: Mutex<Option<JoinHandle<()>>>,
}

impl MockServerInner {
    pub fn port(&self) -> u16 {
        self.port
    }

    /// The base URL to hand to the system under test, e.g. `http://127.0.0.1:8080`.
    pub fn url(&self) -> String {
        format!("http://127.0.0.1:{}", self.port)
    }

    /// Register (or replace) the responder for `method` (None = any) and `path`.
    /// Re-registering the same route stages the next answer in place (order kept).
    pub fn set_route(&self, method: Option<String>, path: PathMatcher, responder: Responder) {
        let method = method.map(|m| m.to_uppercase());
        let mut routes = self.routes.lock().unwrap_or_else(|e| e.into_inner());
        let id = path.identity();
        if let Some(slot) = routes
            .iter_mut()
            .find(|r| r.method == method && r.path.identity() == id)
        {
            slot.responder = Arc::new(responder);
        } else {
            routes.push(Route {
                method,
                path,
                responder: Arc::new(responder),
            });
        }
    }

    /// The responder for `method path`, by specificity: an exact path beats a regex,
    /// and within the same path kind an exact method beats an any-method route; ties
    /// go to the most recently registered route.
    fn find_responder(&self, method: &str, path: &str) -> Option<Arc<Responder>> {
        let routes = self.routes.lock().unwrap_or_else(|e| e.into_inner());
        let mut best: Option<(u8, usize)> = None;
        for (i, r) in routes.iter().enumerate() {
            let method_ok = r.method.as_deref().is_none_or(|m| m == method);
            if !method_ok || !r.path.matches(path) {
                continue;
            }
            let path_rank = match r.path {
                PathMatcher::Exact(_) => 0,
                PathMatcher::Regex(_) => 2,
            };
            let score = path_rank + u8::from(r.method.is_none());
            if best.is_none_or(|(bs, bi)| score < bs || (score == bs && i > bi)) {
                best = Some((score, i));
            }
        }
        best.map(|(_, i)| routes[i].responder.clone())
    }

    /// Number of recorded requests whose path matches — pollable in `await_until`.
    pub fn request_count(&self, path: &PathMatcher) -> i64 {
        self.recorded
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .iter()
            .filter(|r| path.matches(&r.path))
            .count() as i64
    }

    /// The most recent recorded request whose path matches, or `None`.
    pub fn last_request(&self, path: &PathMatcher) -> Option<MockRequest> {
        self.recorded
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .iter()
            .rev()
            .find(|r| path.matches(&r.path))
            .cloned()
    }

    /// All recorded requests whose path matches, in arrival order.
    pub fn requests(&self, path: &PathMatcher) -> Vec<MockRequest> {
        self.recorded
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .iter()
            .filter(|r| path.matches(&r.path))
            .cloned()
            .collect()
    }

    /// Signal the serving task to stop (idempotent). Called by the `stop()` verb and
    /// by [`Ctx::reset_sessions`]; the latter then [`take_task`](Self::take_task)s the
    /// handle to await actual release.
    pub fn shutdown(&self) {
        if let Some(tx) = self
            .shutdown
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .take()
        {
            let _ = tx.send(());
        }
    }

    /// Take the serving task handle so it can be awaited after `shutdown()`, ensuring
    /// the listening socket is closed (port freed) before returning.
    pub fn take_task(&self) -> Option<JoinHandle<()>> {
        self.task.lock().unwrap_or_else(|e| e.into_inner()).take()
    }
}

/// State shared with the serving task: the server's tables plus the context to
/// report received requests through. Holds the context by `Weak` so the
/// `Ctx → mock_servers → task → Ctx` chain can't form a reference cycle.
#[derive(Clone)]
struct Handler {
    inner: Arc<MockServerInner>,
    ctx: std::sync::Weak<Ctx>,
}

/// Start a mock server. `port` is the requested port, or `None` to let the OS pick
/// a free one (the chosen port is then read back via [`MockServerInner::url`]). The
/// server runs on `ctx.rt`; the returned handle is registered for teardown by the
/// caller.
pub fn start(ctx: &Arc<Ctx>, port: Option<u16>) -> Result<Arc<MockServerInner>, String> {
    // Bind synchronously so a port clash is a clear error at start time, then hand
    // the listener to tokio.
    let std_listener = std::net::TcpListener::bind(("127.0.0.1", port.unwrap_or(0)))
        .map_err(|e| format!("mock_server: bind 127.0.0.1:{}: {e}", port.unwrap_or(0)))?;
    std_listener
        .set_nonblocking(true)
        .map_err(|e| format!("mock_server: set_nonblocking: {e}"))?;
    let bound_port = std_listener
        .local_addr()
        .map_err(|e| format!("mock_server: local_addr: {e}"))?
        .port();

    let (tx, rx) = oneshot::channel::<()>();
    let inner = Arc::new(MockServerInner {
        port: bound_port,
        routes: Mutex::new(Vec::new()),
        recorded: Mutex::new(Vec::new()),
        shutdown: Mutex::new(Some(tx)),
        task: Mutex::new(None),
    });

    let handler = Handler {
        inner: inner.clone(),
        ctx: Arc::downgrade(ctx),
    };
    let task = ctx.rt.spawn(async move {
        let listener = match tokio::net::TcpListener::from_std(std_listener) {
            Ok(l) => l,
            Err(e) => {
                eprintln!("mock_server: listener: {e}");
                return;
            }
        };
        let app = Router::new().fallback(any(serve)).with_state(handler);
        let _ = axum::serve(listener, app)
            .with_graceful_shutdown(async move {
                let _ = rx.await;
            })
            .await;
    });
    *inner.task.lock().unwrap_or_else(|e| e.into_inner()) = Some(task);

    Ok(inner)
}

/// The single catch-all handler: record the request, report it, look up a route
/// and run its responder (404 if none matches).
async fn serve(State(state): State<Handler>, req: Request<Body>) -> Response<Body> {
    let (parts, body) = req.into_parts();
    // A body over MAX_BODY (or an I/O error) reads as empty rather than failing the
    // request — webhooks are small, so this only bites pathological payloads.
    let body = to_bytes(body, MAX_BODY)
        .await
        .map(|b| String::from_utf8_lossy(&b).into_owned())
        .unwrap_or_default();
    let method = parts.method.as_str().to_uppercase();
    let path = parts.uri.path().to_string();
    let query = parse_query(parts.uri.query().unwrap_or(""));
    let headers = parts
        .headers
        .iter()
        .map(|(k, v)| {
            (
                k.as_str().to_lowercase(),
                v.to_str().unwrap_or("").to_string(),
            )
        })
        .collect();

    let mreq = MockRequest {
        method: method.clone(),
        path: path.clone(),
        query,
        headers,
        body,
    };

    // Record before responding so a synchronous responder still sees it counted.
    state
        .inner
        .recorded
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .push(mreq.clone());

    let responder = state.inner.find_responder(&method, &path);

    if let Some(ctx) = state.ctx.upgrade() {
        ctx.emit(&Event::MockRequest {
            method: &method,
            path: &path,
            matched: responder.is_some(),
        });
    }

    let resp = match responder {
        Some(r) => match &*r {
            Responder::Static(resp) => resp.clone(),
            // A responder failure is the scenario's bug, not the API's: log it and
            // answer with a bare 500 so the error text isn't leaked over HTTP.
            Responder::Dynamic(f) => f(mreq).unwrap_or_else(|error| {
                if let Some(ctx) = state.ctx.upgrade() {
                    ctx.emit(&Event::MockError {
                        method: &method,
                        path: &path,
                        error: &error,
                    });
                }
                MockResponse {
                    status: 500,
                    content_type: None,
                    headers: Vec::new(),
                    body: String::new(),
                }
            }),
        },
        None => MockResponse {
            status: 404,
            content_type: Some("text/plain".into()),
            headers: Vec::new(),
            body: format!("no mock route for {method} {path}"),
        },
    };
    build_response(resp)
}

/// Turn a [`MockResponse`] into an HTTP response, degrading gracefully if a header
/// name/value is invalid rather than panicking the handler.
fn build_response(r: MockResponse) -> Response<Body> {
    let mut builder = Response::builder()
        .status(StatusCode::from_u16(r.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR));
    if let Some(ct) = &r.content_type {
        builder = builder.header(header::CONTENT_TYPE, ct);
    }
    for (k, v) in &r.headers {
        builder = builder.header(k.as_str(), v.as_str());
    }
    // An invalid status/header makes `body()` error; fall back to an empty 200 so a
    // malformed response map can't panic the worker (it surfaces as a blank reply).
    builder
        .body(Body::from(r.body))
        .unwrap_or_else(|_| Response::new(Body::empty()))
}

/// Parse a URL query string into a map (last value wins), percent-decoding keys and
/// values and treating `+` as a space.
fn parse_query(q: &str) -> HashMap<String, String> {
    q.split('&')
        .filter(|s| !s.is_empty())
        .map(|pair| match pair.split_once('=') {
            Some((k, v)) => (percent_decode(k), percent_decode(v)),
            None => (percent_decode(pair), String::new()),
        })
        .collect()
}

/// Minimal `application/x-www-form-urlencoded` decode: `+` → space, `%XX` → byte.
fn percent_decode(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            b'%' if i + 2 < bytes.len() => {
                let hi = (bytes[i + 1] as char).to_digit(16);
                let lo = (bytes[i + 2] as char).to_digit(16);
                if let (Some(hi), Some(lo)) = (hi, lo) {
                    out.push((hi * 16 + lo) as u8);
                    i += 3;
                } else {
                    out.push(bytes[i]);
                    i += 1;
                }
            }
            b => {
                out.push(b);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

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

    #[test]
    fn parse_query_decodes_and_splits() {
        let q = parse_query("event=incoming+call&id=%2B49&flag");
        assert_eq!(q.get("event").unwrap(), "incoming call");
        assert_eq!(q.get("id").unwrap(), "+49");
        assert_eq!(q.get("flag").unwrap(), "");
        assert!(parse_query("").is_empty());
    }

    /// A reporter that drops events (the test only cares about HTTP behaviour).
    struct Silent;
    impl crate::runtime::report::Reporter for Silent {
        fn emit(&mut self, _: &crate::runtime::report::Event) {}
    }

    fn test_ctx() -> (tokio::runtime::Runtime, Arc<Ctx>) {
        let rt = tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .unwrap();
        let ctx = Arc::new(Ctx::new(
            rt.handle().clone(),
            Box::new(Silent),
            std::time::Duration::from_secs(5),
        ));
        (rt, ctx)
    }

    fn exact(p: &str) -> PathMatcher {
        PathMatcher::Exact(p.into())
    }
    fn static_resp(status: u16, body: &str) -> Responder {
        Responder::Static(MockResponse {
            status,
            content_type: Some("text/plain".into()),
            headers: vec![],
            body: body.into(),
        })
    }

    #[test]
    fn static_dynamic_and_unmatched_routes() {
        let (_rt, ctx) = test_ctx();
        let server = start(&ctx, None).unwrap();
        server.set_route(
            Some("POST".into()),
            exact("/voice"),
            Responder::Static(MockResponse {
                status: 200,
                content_type: Some("application/json".into()),
                headers: vec![("X-Mock".into(), "1".into())],
                body: r#"{"ok":true}"#.into(),
            }),
        );
        // A dynamic responder echoes the request body back with a 201.
        server.set_route(
            Some("POST".into()),
            exact("/echo"),
            Responder::Dynamic(Box::new(|req| {
                Ok(MockResponse {
                    status: 201,
                    content_type: Some("text/plain".into()),
                    headers: vec![],
                    body: req.body,
                })
            })),
        );
        // A failing responder must yield a bare 500 — no error text over HTTP.
        server.set_route(
            Some("POST".into()),
            exact("/boom"),
            Responder::Dynamic(Box::new(|_| Err("secret internal detail".into()))),
        );

        let client = reqwest::blocking::Client::new();

        let r = client
            .post(format!("{}/voice", server.url()))
            .header("X-Test", "abc")
            .body(r#"{"event":"incoming_call"}"#)
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 200);
        assert_eq!(r.headers()["x-mock"], "1");
        assert_eq!(r.text().unwrap(), r#"{"ok":true}"#);

        let r = client
            .post(format!("{}/echo", server.url()))
            .body("ping")
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 201);
        assert_eq!(r.text().unwrap(), "ping");

        // No route → 404.
        let r = client
            .get(format!("{}/missing", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 404);

        // Failing responder → bare 500, error text not leaked to the caller.
        let r = client
            .post(format!("{}/boom", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 500);
        assert!(r.text().unwrap().is_empty(), "500 body must be empty");

        // Recording: the two POSTs are captured per path; headers are lower-cased.
        assert_eq!(server.request_count(&exact("/voice")), 1);
        assert_eq!(server.request_count(&exact("/echo")), 1);
        let req = server.last_request(&exact("/voice")).unwrap();
        assert_eq!(req.method, "POST");
        assert_eq!(req.headers.get("x-test").map(String::as_str), Some("abc"));
        assert_eq!(req.body, r#"{"event":"incoming_call"}"#);

        server.shutdown();
    }

    #[test]
    fn regex_path_and_any_method_routing() {
        let (_rt, ctx) = test_ctx();
        let server = start(&ctx, None).unwrap();
        // Any method on an exact path.
        server.set_route(None, exact("/any"), static_resp(200, "any"));
        // A regex path (anchored), POST only.
        server.set_route(
            Some("POST".into()),
            PathMatcher::regex("/calls/.*").unwrap(),
            static_resp(202, "call"),
        );
        // A more specific exact route wins over the regex for the same path.
        server.set_route(
            Some("POST".into()),
            exact("/calls/special"),
            static_resp(200, "special"),
        );

        let client = reqwest::blocking::Client::new();

        // Any-method route answers GET and DELETE alike.
        for m in ["GET", "DELETE"] {
            let r = client
                .request(m.parse().unwrap(), format!("{}/any", server.url()))
                .send()
                .unwrap();
            assert_eq!(r.status().as_u16(), 200, "method {m}");
        }

        // The regex matches concrete sub-paths.
        let r = client
            .post(format!("{}/calls/123", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 202);
        assert_eq!(r.text().unwrap(), "call");
        let r = client
            .post(format!("{}/calls/123/extra", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 202);
        // The exact route is preferred over the regex for its path.
        let r = client
            .post(format!("{}/calls/special", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.text().unwrap(), "special");
        // GET on the POST-only regex route → no match → 404.
        let r = client
            .get(format!("{}/calls/123", server.url()))
            .send()
            .unwrap();
        assert_eq!(r.status().as_u16(), 404);

        // A regex matcher also queries the recording (path-only, any method): the
        // three POSTs plus the unmatched GET all hit /calls/* and were recorded.
        let re = PathMatcher::regex("/calls/.*").unwrap();
        assert_eq!(server.request_count(&re), 4);

        server.shutdown();
    }

    #[test]
    fn explicit_port_is_freed_after_awaiting_task() {
        let (rt, ctx) = test_ctx();
        // A free port to reuse (the throwaway listener is dropped at the semicolon).
        let port = std::net::TcpListener::bind("127.0.0.1:0")
            .unwrap()
            .local_addr()
            .unwrap()
            .port();

        let s1 = start(&ctx, Some(port)).unwrap();
        assert_eq!(s1.port(), port);
        s1.shutdown();
        rt.block_on(async {
            if let Some(t) = s1.take_task() {
                let _ = t.await;
            }
        });

        // Rebinding the same port must succeed now that the task has ended.
        let s2 = start(&ctx, Some(port)).expect("port should be free after awaiting the task");
        s2.shutdown();
        rt.block_on(async {
            if let Some(t) = s2.take_task() {
                let _ = t.await;
            }
        });
    }
}