mockd-http 0.2.0

Lightweight standalone mock HTTP server for local development, integration tests and CI/CD.
Documentation
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
//! Route matching.
//!
//! The [`Router`] owns the compiled set of mock routes and answers the
//! question *"given this request, which route matches and what should the
//! response be?"*.
//!
//! ## Matching algorithm
//!
//! Routes are evaluated in declaration order; the first matching route wins.
//! A route matches when:
//!
//! 1. the HTTP [`Method`] equals the request method,
//! 2. the path pattern matches the request path segment by segment
//!    (capturing `{param}` segments), and
//! 3. every rule in the optional `when` block ([`RequestMatch`]) is
//!    satisfied: required query parameters, headers (case-insensitively)
//!    and a JSON body subset.

use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use serde_json::Value;

use crate::config::{Method, RequestMatch, ResponseConfig, Route};

/// A compiled set of routes ready to answer requests.
#[derive(Debug, Clone)]
pub struct Router {
    routes: Vec<CompiledRoute>,
}

#[derive(Debug, Clone)]
struct CompiledRoute {
    method: Method,
    segments: Vec<Segment>,
    when: Option<RequestMatch>,
    /// The list of responses for this route. A single-element vec means a
    /// plain static route; a longer vec is a sequence whose counter advances
    /// on each match (last item is sticky).
    responses: Vec<ResponseConfig>,
    /// Per-route counter for sequence responses. Shared across `Router`
    /// clones so that all callers observe the same progression.
    counter: Arc<AtomicUsize>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Segment {
    Literal(String),
    Param(String),
}

/// The result of matching a request against the [`Router`].
#[derive(Debug, Clone)]
pub struct Match {
    /// Captured path parameters (e.g. `{"id": "42"}`).
    pub path_params: HashMap<String, String>,
    /// The response that should be produced.
    pub response: ResponseConfig,
}

impl Router {
    /// Compile a set of routes.
    ///
    /// Returns an error if any route has an invalid path pattern or an empty
    /// `sequence: []` response spec.
    pub fn new(routes: Vec<Route>) -> Result<Self, RouterError> {
        let mut compiled = Vec::with_capacity(routes.len());
        for (index, route) in routes.into_iter().enumerate() {
            let segments = compile_path(&route.path).map_err(|e| RouterError::InvalidPath {
                route_index: index,
                source: e,
            })?;
            let responses = route.response.into_responses();
            if responses.is_empty() {
                return Err(RouterError::EmptySequence { route_index: index });
            }
            compiled.push(CompiledRoute {
                method: route.method,
                segments,
                when: route.when,
                responses,
                counter: Arc::new(AtomicUsize::new(0)),
            });
        }
        Ok(Router { routes: compiled })
    }

    /// Number of compiled routes.
    pub fn len(&self) -> usize {
        self.routes.len()
    }

    /// Whether the router has no routes.
    pub fn is_empty(&self) -> bool {
        self.routes.is_empty()
    }

    /// Resolve a request to a [`Match`].
    ///
    /// All inputs use plain, server-agnostic types. `headers` should use
    /// lower-cased header names; header *matching* against route rules is
    /// performed case-insensitively regardless.
    ///
    /// For sequence routes, each successful match advances the internal
    /// counter; the last response in the sequence is repeated forever.
    pub fn resolve(
        &self,
        method: Method,
        path: &str,
        query: &HashMap<String, String>,
        headers: &HashMap<String, String>,
        body: &Value,
    ) -> Option<Match> {
        let request_segments: Vec<&str> = path_segments(path).collect();

        for route in &self.routes {
            if route.method != method {
                continue;
            }
            if let Some(path_params) = match_path(&route.segments, &request_segments) {
                if match_when(route.when.as_ref(), query, headers, body) {
                    let response = pick_response(route);
                    return Some(Match {
                        path_params,
                        response,
                    });
                }
            }
        }
        None
    }
}

/// Select the response for a matched route.
///
/// For a single-response route this is the only item. For a sequence route,
/// each call returns the next item until the last is reached, after which the
/// last item is returned on every subsequent call (sticky last).
fn pick_response(route: &CompiledRoute) -> ResponseConfig {
    let n = route.responses.len();
    if n == 1 {
        return route.responses[0].clone();
    }
    let idx = route.counter.fetch_add(1, Ordering::Relaxed);
    // Once we've passed the end, keep returning the last response.
    let clamped = idx.min(n - 1);
    route.responses[clamped].clone()
}

/// Split a request path into non-empty segments, ignoring the leading slash.
fn path_segments(path: &str) -> impl Iterator<Item = &str> {
    path.trim_end_matches('/')
        .split('/')
        .filter(|s| !s.is_empty())
}

/// Compile a path pattern into [`Segment`]s.
///
/// Patterns look like `/users/{id}/items/{itemId}`. A `{name}` placeholder
/// captures a single path segment. The pattern must be well-formed: balanced
/// braces and a non-empty name.
fn compile_path(pattern: &str) -> Result<Vec<Segment>, PathError> {
    let mut segments = Vec::new();
    for raw in pattern.split('/') {
        if raw.is_empty() {
            continue;
        }
        if let Some(name) = raw.strip_prefix('{').and_then(|r| r.strip_suffix('}')) {
            if name.is_empty() || name.contains('{') || name.contains('}') {
                return Err(PathError::InvalidParam(raw.to_string()));
            }
            segments.push(Segment::Param(name.to_string()));
        } else if raw.contains('{') || raw.contains('}') {
            return Err(PathError::UnbalancedBraces(raw.to_string()));
        } else {
            segments.push(Segment::Literal(raw.to_string()));
        }
    }
    if segments.is_empty() {
        return Err(PathError::EmptyPattern);
    }
    Ok(segments)
}

/// Match compiled segments against request segments, capturing params.
///
/// Returns the captured params if the segments match, or `None` otherwise.
fn match_path(segments: &[Segment], request: &[&str]) -> Option<HashMap<String, String>> {
    if segments.len() != request.len() {
        return None;
    }
    let mut params = HashMap::with_capacity(segments.len());
    for (seg, req) in segments.iter().zip(request.iter()) {
        match seg {
            Segment::Literal(lit) => {
                if lit != req {
                    return None;
                }
            }
            Segment::Param(name) => {
                params.insert(name.clone(), (*req).to_string());
            }
        }
    }
    Some(params)
}

/// Evaluate the optional `when` block.
fn match_when(
    when: Option<&RequestMatch>,
    query: &HashMap<String, String>,
    headers: &HashMap<String, String>,
    body: &Value,
) -> bool {
    let Some(when) = when else {
        return true;
    };
    when.query
        .iter()
        .all(|(k, v)| query.get(k).map(|actual| actual == v).unwrap_or(false))
        && when.headers.iter().all(|(k, v)| {
            let lower = k.to_ascii_lowercase();
            headers
                .get(&lower)
                .map(|actual| actual.eq_ignore_ascii_case(v))
                .unwrap_or(false)
        })
        && when
            .body
            .as_ref()
            .map(|pattern| body_matches(pattern, body))
            .unwrap_or(true)
}

/// Subset match between a JSON pattern and the actual request body.
///
/// - Objects: every key in the pattern must be present and recursively match.
/// - Arrays: must have the same length and match element by element.
/// - Scalars: equality.
fn body_matches(pattern: &Value, actual: &Value) -> bool {
    match (pattern, actual) {
        (Value::Object(pattern), Value::Object(actual)) => pattern.iter().all(|(key, value)| {
            actual
                .get(key)
                .map(|a| body_matches(value, a))
                .unwrap_or(false)
        }),
        (Value::Array(pattern), Value::Array(actual)) => {
            pattern.len() == actual.len()
                && pattern.iter().zip(actual).all(|(p, a)| body_matches(p, a))
        }
        _ => pattern == actual,
    }
}

/// Errors that can occur while building a [`Router`].
#[derive(Debug, thiserror::Error)]
pub enum RouterError {
    /// A route's path pattern could not be compiled.
    #[error("invalid path pattern in route {route_index}: {source}")]
    InvalidPath {
        route_index: usize,
        #[source]
        source: PathError,
    },

    /// A `response.sequence` was empty.
    #[error("empty `sequence` in route {route_index}; expected at least one item")]
    EmptySequence { route_index: usize },
}

/// Errors in an individual path pattern.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PathError {
    /// A `{...}` placeholder was malformed.
    #[error("invalid path parameter `{0}`")]
    InvalidParam(String),
    /// Curly braces appear outside a placeholder.
    #[error("unbalanced braces in segment `{0}`")]
    UnbalancedBraces(String),
    /// The pattern contained no segments.
    #[error("path pattern is empty")]
    EmptyPattern,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{Method, RequestMatch, ResponseConfig, ResponseSpec};
    use serde_json::json;

    fn route(method: Method, path: &str) -> Route {
        Route {
            method,
            path: path.to_string(),
            when: None,
            response: ResponseSpec::Single(ResponseConfig::default()),
        }
    }

    /// Helper: build a route whose single response has the given status.
    fn route_with_status(method: Method, path: &str, status: u16) -> Route {
        let mut r = route(method, path);
        if let ResponseSpec::Single(resp) = &mut r.response {
            resp.status = status;
        }
        r
    }

    fn empty_inputs() -> (HashMap<String, String>, HashMap<String, String>, Value) {
        (HashMap::new(), HashMap::new(), Value::Null)
    }

    #[test]
    fn literal_path_matches() {
        let router = Router::new(vec![route(Method::Get, "/users")]).unwrap();
        let (q, h, b) = empty_inputs();
        let m = router.resolve(Method::Get, "/users", &q, &h, &b);
        assert!(m.is_some());
    }

    #[test]
    fn leading_slash_optional() {
        let router = Router::new(vec![route(Method::Get, "/users")]).unwrap();
        let (q, h, b) = empty_inputs();
        assert!(router.resolve(Method::Get, "users", &q, &h, &b).is_some());
    }

    #[test]
    fn method_must_match() {
        let router = Router::new(vec![route(Method::Get, "/users")]).unwrap();
        let (q, h, b) = empty_inputs();
        assert!(router.resolve(Method::Post, "/users", &q, &h, &b).is_none());
    }

    #[test]
    fn captures_path_params() {
        let router = Router::new(vec![route(Method::Get, "/users/{id}/items/{itemId}")]).unwrap();
        let (q, h, b) = empty_inputs();
        let m = router
            .resolve(Method::Get, "/users/42/items/7", &q, &h, &b)
            .unwrap();
        assert_eq!(m.path_params.get("id").unwrap(), "42");
        assert_eq!(m.path_params.get("itemId").unwrap(), "7");
    }

    #[test]
    fn segment_count_must_match() {
        let router = Router::new(vec![route(Method::Get, "/users/{id}")]).unwrap();
        let (q, h, b) = empty_inputs();
        assert!(router
            .resolve(Method::Get, "/users/42/items", &q, &h, &b)
            .is_none());
    }

    #[test]
    fn first_match_wins() {
        let r1 = route_with_status(Method::Get, "/users/{id}", 200);
        let r2 = route_with_status(Method::Get, "/users/{id}", 201);
        let router = Router::new(vec![r1, r2]).unwrap();
        let (q, h, b) = empty_inputs();
        let m = router.resolve(Method::Get, "/users/1", &q, &h, &b).unwrap();
        assert_eq!(m.response.status, 200);
    }

    #[test]
    fn matches_query_param() {
        let mut r = route(Method::Get, "/users");
        r.when = Some(RequestMatch {
            query: [("role".to_string(), "admin".to_string())].into(),
            ..Default::default()
        });
        let router = Router::new(vec![r]).unwrap();
        let (mut q, h, b) = empty_inputs();
        assert!(router.resolve(Method::Get, "/users", &q, &h, &b).is_none());
        q.insert("role".into(), "admin".into());
        assert!(router.resolve(Method::Get, "/users", &q, &h, &b).is_some());
    }

    #[test]
    fn matches_header_case_insensitively() {
        let mut r = route(Method::Get, "/users");
        r.when = Some(RequestMatch {
            headers: [("X-Tenant-Id".to_string(), "tenant-a".to_string())].into(),
            ..Default::default()
        });
        let router = Router::new(vec![r]).unwrap();
        let (q, mut h, b) = empty_inputs();
        h.insert("x-tenant-id".into(), "TENANT-A".into());
        assert!(router.resolve(Method::Get, "/users", &q, &h, &b).is_some());
    }

    #[test]
    fn matches_body_subset() {
        let mut r = route(Method::Post, "/login");
        r.when = Some(RequestMatch {
            body: Some(json!({"username": "admin"})),
            ..Default::default()
        });
        let router = Router::new(vec![r]).unwrap();
        let (q, h, _) = empty_inputs();
        let body = json!({"username": "admin", "password": "secret"});
        assert!(router
            .resolve(Method::Post, "/login", &q, &h, &body)
            .is_some());
        let other = json!({"username": "guest"});
        assert!(router
            .resolve(Method::Post, "/login", &q, &h, &other)
            .is_none());
    }

    #[test]
    fn when_block_can_disambiguate_same_path() {
        // Two routes with the same path: the one without `when` is a fallback,
        // the one with `when` is more specific. Declaring the specific one
        // first makes it win for matching requests.
        let mut admin = route_with_status(Method::Get, "/users", 201);
        admin.when = Some(RequestMatch {
            query: [("role".to_string(), "admin".to_string())].into(),
            ..Default::default()
        });
        let generic = route_with_status(Method::Get, "/users", 200);
        let router = Router::new(vec![admin, generic]).unwrap();

        let (mut q, h, b) = empty_inputs();
        q.insert("role".into(), "admin".into());
        let m = router.resolve(Method::Get, "/users", &q, &h, &b).unwrap();
        assert_eq!(m.response.status, 201);

        q.clear();
        let m = router.resolve(Method::Get, "/users", &q, &h, &b).unwrap();
        assert_eq!(m.response.status, 200);
    }

    #[test]
    fn rejects_invalid_path_pattern_empty_param() {
        let routes = vec![route(Method::Get, "/users/{}")];
        assert!(Router::new(routes).is_err());
    }

    #[test]
    fn rejects_invalid_path_pattern_unbalanced() {
        let routes = vec![route(Method::Get, "/users/{id")];
        assert!(Router::new(routes).is_err());
    }

    #[test]
    fn rejects_empty_pattern() {
        let routes = vec![route(Method::Get, "/")];
        assert!(Router::new(routes).is_err());
    }

    // -----------------------------------------------------------------
    // Sequence responses
    // -----------------------------------------------------------------

    fn sequence_route(method: Method, path: &str, statuses: Vec<u16>) -> Route {
        let sequence = statuses
            .into_iter()
            .map(|status| ResponseConfig {
                status,
                ..ResponseConfig::default()
            })
            .collect();
        Route {
            method,
            path: path.to_string(),
            when: None,
            response: ResponseSpec::Sequence { sequence },
        }
    }

    #[test]
    fn sequence_returns_responses_in_order() {
        let router = Router::new(vec![sequence_route(
            Method::Get,
            "/flaky",
            vec![500, 500, 200],
        )])
        .unwrap();
        let (q, h, b) = empty_inputs();

        assert_eq!(
            router
                .resolve(Method::Get, "/flaky", &q, &h, &b)
                .unwrap()
                .response
                .status,
            500
        );
        assert_eq!(
            router
                .resolve(Method::Get, "/flaky", &q, &h, &b)
                .unwrap()
                .response
                .status,
            500
        );
        assert_eq!(
            router
                .resolve(Method::Get, "/flaky", &q, &h, &b)
                .unwrap()
                .response
                .status,
            200
        );
    }

    #[test]
    fn sequence_sticks_on_last_response_after_exhausting() {
        let router =
            Router::new(vec![sequence_route(Method::Get, "/retry", vec![500, 200])]).unwrap();
        let (q, h, b) = empty_inputs();

        // Consume the whole sequence.
        router.resolve(Method::Get, "/retry", &q, &h, &b);
        router.resolve(Method::Get, "/retry", &q, &h, &b);

        // Subsequent calls keep returning the last one.
        for _ in 0..5 {
            assert_eq!(
                router
                    .resolve(Method::Get, "/retry", &q, &h, &b)
                    .unwrap()
                    .response
                    .status,
                200
            );
        }
    }

    #[test]
    fn sequence_state_is_shared_between_router_clones() {
        // The Router is cloned per Axum worker; all clones must observe the
        // same sequence progression.
        let router = Router::new(vec![sequence_route(Method::Get, "/x", vec![1, 2, 3])]).unwrap();
        let cloned = router.clone();
        let (q, h, b) = empty_inputs();

        // Interleave calls from both clones.
        assert_eq!(
            router
                .resolve(Method::Get, "/x", &q, &h, &b)
                .unwrap()
                .response
                .status,
            1
        );
        assert_eq!(
            cloned
                .resolve(Method::Get, "/x", &q, &h, &b)
                .unwrap()
                .response
                .status,
            2
        );
        assert_eq!(
            router
                .resolve(Method::Get, "/x", &q, &h, &b)
                .unwrap()
                .response
                .status,
            3
        );
        // Exhausted -> sticks on 3.
        assert_eq!(
            cloned
                .resolve(Method::Get, "/x", &q, &h, &b)
                .unwrap()
                .response
                .status,
            3
        );
    }

    #[test]
    fn empty_sequence_is_rejected_at_compile_time() {
        let r = Route {
            method: Method::Get,
            path: "/x".to_string(),
            when: None,
            response: ResponseSpec::Sequence { sequence: vec![] },
        };
        let err = Router::new(vec![r]).unwrap_err();
        assert!(matches!(err, RouterError::EmptySequence { route_index: 0 }));
    }
}