rust-webx-host 0.3.0

rust-webx HTTP layer: Host builder, middleware pipeline, Trie-based router, hyper integration
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
//! Tests for IMediator send/publish using HandlerCache + DI resolution.
//!
//! `send` tests verify that the Mediator correctly resolves handlers via the
//! `HandlerCache` (populated by `HandlerRegistration` inventory submissions)
//! and dispatches requests through the factory + call bridge.
//!
//! `publish` tests verify event-handler resolution from the rust_dix
//! ServiceProvider.
//!
//! Handlers are registered manually via `inventory::submit!` with
//! `HandlerRegistration` (same mechanism as `#[handler]` macro) using
//! `rust_webx_core::` paths directly, since `rust-webx-host` cannot
//! depend on the `rust_webx` umbrella crate (circular dependency).

// Manual handler-bridge functions mimic `#[handler]` macro output, whose
// `Pin<Box<dyn Future<Output = Result<Box<dyn Any + Send>>> + Send>>` return
// type is inherent to the dispatch bridge pattern.
#![allow(clippy::type_complexity)]

use rust_dix::ServiceCollection;
use rust_webx_core::error::{Error, Result as LrwfResult};
use rust_webx_core::handler::{IEventHandler, IRequestHandler};
use rust_webx_core::mediator::{IEventRequest, IMediator, IRequest};
use rust_webx_core::mediator::Mediator;
use rust_webx_core::route::scan::HandlerRegistration;
use std::sync::{Arc, Mutex};

// --- Request / Response Types ---

struct HelloRequest;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct HelloResponse {
    message: String,
}

impl IRequest<HelloResponse> for HelloRequest {}

// --- Handlers ---
//
// Manually registered via `inventory::submit!` — equivalent to what the
// `#[handler]` macro generates, but using `rust_webx_core::` paths.

#[derive(Default)]
struct HelloHandler;

#[async_trait::async_trait]
impl IRequestHandler<HelloRequest, HelloResponse> for HelloHandler {
    async fn handle(&mut self, _req: HelloRequest) -> LrwfResult<HelloResponse> {
        Ok(HelloResponse {
            message: "hello".into(),
        })
    }
}

fn __factory_hello_handler(
    _resolver: &dyn rust_dix::IServiceResolver,
) -> LrwfResult<Box<dyn std::any::Any + Send>> {
    Ok(Box::new(HelloHandler) as Box<dyn std::any::Any + Send>)
}

fn __call_hello_handler(
    handler: Box<dyn std::any::Any + Send>,
    request: Box<dyn std::any::Any + Send>,
) -> std::pin::Pin<
    Box<
        dyn std::future::Future<Output = LrwfResult<Box<dyn std::any::Any + Send>>>
            + Send,
    >,
> {
    Box::pin(async move {
        let mut h = *handler
            .downcast::<HelloHandler>()
            .map_err(|_| Error::Internal("Handler downcast failed".into()))?;
        let req = *request
            .downcast::<HelloRequest>()
            .map_err(|_| Error::Internal("Request downcast failed".into()))?;
        let result: HelloResponse = h.handle(req).await?;
        Ok(Box::new(result) as Box<dyn std::any::Any + Send>)
    })
}

inventory::submit! {
    HandlerRegistration {
        req_type_id: std::any::TypeId::of::<HelloRequest>(),
        req_type_name: "HelloRequest",
        factory: __factory_hello_handler,
        call: __call_hello_handler,
    }
}

#[derive(Default)]
struct FailingHandler;

#[async_trait::async_trait]
impl IRequestHandler<HelloRequest, HelloResponse> for FailingHandler {
    async fn handle(&mut self, _req: HelloRequest) -> LrwfResult<HelloResponse> {
        Err(Error::Internal("handler failure".into()))
    }
}

fn __factory_failing_handler(
    _resolver: &dyn rust_dix::IServiceResolver,
) -> LrwfResult<Box<dyn std::any::Any + Send>> {
    Ok(Box::new(FailingHandler) as Box<dyn std::any::Any + Send>)
}

fn __call_failing_handler(
    handler: Box<dyn std::any::Any + Send>,
    request: Box<dyn std::any::Any + Send>,
) -> std::pin::Pin<
    Box<
        dyn std::future::Future<Output = LrwfResult<Box<dyn std::any::Any + Send>>>
            + Send,
    >,
> {
    Box::pin(async move {
        let mut h = *handler
            .downcast::<FailingHandler>()
            .map_err(|_| Error::Internal("Handler downcast failed".into()))?;
        let req = *request
            .downcast::<HelloRequest>()
            .map_err(|_| Error::Internal("Request downcast failed".into()))?;
        let result: HelloResponse = h.handle(req).await?;
        Ok(Box::new(result) as Box<dyn std::any::Any + Send>)
    })
}

inventory::submit! {
    HandlerRegistration {
        req_type_id: std::any::TypeId::of::<HelloRequest>(),
        req_type_name: "HelloRequest",
        factory: __factory_failing_handler,
        call: __call_failing_handler,
    }
}

// --- Event types ---

#[derive(Clone)]
struct TestEvent {
    payload: String,
}
impl IEventRequest for TestEvent {}

struct CountingEventHandler {
    counter: Arc<Mutex<Vec<String>>>,
}

#[async_trait::async_trait]
impl IEventHandler<TestEvent> for CountingEventHandler {
    async fn handle(&self, event: TestEvent) -> LrwfResult<()> {
        self.counter.lock().unwrap().push(event.payload);
        Ok(())
    }
}

struct FailingEventHandler;

#[async_trait::async_trait]
impl IEventHandler<TestEvent> for FailingEventHandler {
    async fn handle(&self, _event: TestEvent) -> LrwfResult<()> {
        Err(Error::Internal("event handler failure".into()))
    }
}

// --- Mediator::send tests ---
//
// Note: HandlerCache is process-global and keyed by request type name. With
// both HelloHandler and FailingHandler registered for HelloRequest, the
// last-submitted entry wins (inventory iteration order is deterministic per
// build but not guaranteed across rebuilds). These tests therefore only assert
// the success/failure shape, not which handler ran. The
// `mediator_send_handler_not_registered` test uses a dedicated request type
// with no registration to verify the not-registered error path.

fn build_provider() -> Arc<rust_dix::ServiceProvider> {
    ServiceCollection::new().build().unwrap()
}

#[tokio::test]
async fn mediator_send_success_or_failure() {
    let mediator = Mediator::new(build_provider());
    let result = mediator.send(HelloRequest).await;
    match result {
        Ok(rsp) => assert_eq!(rsp.message, "hello"),
        Err(Error::Internal(msg)) => assert_eq!(msg, "handler failure"),
        Err(other) => panic!("Unexpected error variant: {:?}", other),
    }
}

// Dedicated request type with NO handler registration.
struct UnregisteredRequest;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct UnregisteredResponse;
impl IRequest<UnregisteredResponse> for UnregisteredRequest {}

#[tokio::test]
async fn mediator_send_handler_not_registered() {
    let mediator = Mediator::new(build_provider());
    let result = mediator.send(UnregisteredRequest).await;
    assert!(result.is_err());
    match result.unwrap_err() {
        Error::Di(msg) => assert!(msg.contains("No #[handler] registered")),
        other => panic!("Expected Di error, got {:?}", other),
    }
}

// --- Mediator::publish tests ---

#[tokio::test]
async fn mediator_publish_single_handler() {
    let counter = Arc::new(Mutex::new(Vec::new()));
    let counter_clone = Arc::clone(&counter);

    let provider = ServiceCollection::new()
            .singleton::<dyn IEventHandler<TestEvent>>(move |_| {
                Arc::new(CountingEventHandler {
                    counter: Arc::clone(&counter_clone),
                })
            })
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);
    mediator
        .publish(TestEvent {
            payload: "event-1".into(),
        })
        .await
        .unwrap();

    let events = counter.lock().unwrap();
    assert_eq!(events.len(), 1);
    assert_eq!(events[0], "event-1");
}

#[tokio::test]
async fn mediator_publish_multiple_handlers() {
    let counter = Arc::new(Mutex::new(Vec::new()));
    let c1 = Arc::clone(&counter);
    let c2 = Arc::clone(&counter);

    let provider = ServiceCollection::new()
            .singleton::<dyn IEventHandler<TestEvent>>(move |_| {
                Arc::new(CountingEventHandler {
                    counter: Arc::clone(&c1),
                })
            })
            .singleton::<dyn IEventHandler<TestEvent>>(move |_| {
                Arc::new(CountingEventHandler {
                    counter: Arc::clone(&c2),
                })
            })
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);
    mediator
        .publish(TestEvent {
            payload: "multi".into(),
        })
        .await
        .unwrap();

    let events = counter.lock().unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0], "multi");
    assert_eq!(events[1], "multi");
}

#[tokio::test]
async fn mediator_publish_handler_returns_error() {
    let provider = ServiceCollection::new()
            .singleton::<dyn IEventHandler<TestEvent>>(|_| Arc::new(FailingEventHandler))
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);
    let result = mediator
        .publish(TestEvent {
            payload: "will-fail".into(),
        })
        .await;
    assert!(result.is_err());
}

#[tokio::test]
async fn mediator_publish_empty_handler_list() {
    let mediator = Mediator::new(build_provider());
    let result = mediator
        .publish(TestEvent {
            payload: "no-handlers".into(),
        })
        .await;
    assert!(result.is_ok());
}

// --- Mediator::send scope provider test ---
//
// Verifies P0-5 fix: `Mediator::send` creates a per-call scope so that Scoped
// services resolve to a single shared instance within one send invocation
// (matching the HTTP dispatch path). Before the fix, send used the root
// provider, which made Scoped services degrade to transient (fresh instance
// per resolution, no within-call caching).

use std::sync::atomic::{AtomicU32, Ordering};

static SCOPED_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Scoped service that records the order in which it was constructed.
struct ScopedService {
    instance_id: u32,
}

struct ScopeProbeRequest;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct ScopeProbeResponse {
    /// "same" if two resolutions within one send returned the same instance,
    /// "different" otherwise.
    within_call: String,
    /// The instance id observed on the first resolution.
    first_id: u32,
}

impl IRequest<ScopeProbeResponse> for ScopeProbeRequest {}

/// Factory that resolves `ScopedService` twice from the same resolver and
/// records whether the two resolutions returned the same instance.
fn __factory_scope_probe_handler(
    resolver: &dyn rust_dix::IServiceResolver,
) -> LrwfResult<Box<dyn std::any::Any + Send>> {
    // Use get_any (the only non-Sized-bound resolver method) + downcast.
    let key = std::any::type_name::<ScopedService>();
    let a: Arc<ScopedService> = resolver
        .get_any(key)
        .and_then(|a| a.downcast::<Arc<ScopedService>>().ok())
        .map(|d| Arc::clone(&*d))
        .ok_or_else(|| Error::Internal("ScopedService not registered".into()))?;
    let b: Arc<ScopedService> = resolver
        .get_any(key)
        .and_then(|a| a.downcast::<Arc<ScopedService>>().ok())
        .map(|d| Arc::clone(&*d))
        .ok_or_else(|| Error::Internal("ScopedService not registered".into()))?;
    let within_call = if std::ptr::eq(Arc::as_ptr(&a), Arc::as_ptr(&b)) {
        "same"
    } else {
        "different"
    };
    let first_id = a.instance_id;
    Ok(Box::new(ScopeProbeResult {
        within_call: within_call.to_string(),
        first_id,
    }) as Box<dyn std::any::Any + Send>)
}

struct ScopeProbeResult {
    within_call: String,
    first_id: u32,
}

fn __call_scope_probe_handler(
    handler: Box<dyn std::any::Any + Send>,
    _request: Box<dyn std::any::Any + Send>,
) -> std::pin::Pin<
    Box<
        dyn std::future::Future<Output = LrwfResult<Box<dyn std::any::Any + Send>>>
            + Send,
    >,
> {
    Box::pin(async move {
        let h = *handler
            .downcast::<ScopeProbeResult>()
            .map_err(|_| Error::Internal("ScopeProbeResult downcast failed".into()))?;
        let rsp = ScopeProbeResponse {
            within_call: h.within_call,
            first_id: h.first_id,
        };
        Ok(Box::new(rsp) as Box<dyn std::any::Any + Send>)
    })
}

inventory::submit! {
    HandlerRegistration {
        req_type_id: std::any::TypeId::of::<ScopeProbeRequest>(),
        req_type_name: "ScopeProbeRequest",
        factory: __factory_scope_probe_handler,
        call: __call_scope_probe_handler,
    }
}

#[tokio::test]
async fn mediator_send_uses_per_call_scope_for_scoped_services() {
    SCOPED_COUNTER.store(0, Ordering::SeqCst);

    let provider = ServiceCollection::new()
            .scoped::<ScopedService>(|_| {
                let id = SCOPED_COUNTER.fetch_add(1, Ordering::SeqCst) + 1;
                Arc::new(ScopedService { instance_id: id })
            })
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);

    let r1 = mediator.send(ScopeProbeRequest).await.expect("send #1 failed");
    let r2 = mediator.send(ScopeProbeRequest).await.expect("send #2 failed");

    // Within a single send, both resolutions must return the same instance
    // (Scoped caching within the per-call scope).
    assert_eq!(r1.within_call, "same", "first send: scope not caching scoped service");
    assert_eq!(r2.within_call, "same", "second send: scope not caching scoped service");

    // Across sends, a new scope means a fresh scoped instance.
    assert_eq!(r1.first_id, 1, "first send should observe instance #1");
    assert_eq!(r2.first_id, 2, "second send should observe instance #2");
}

// --- Pipeline behavior chain tests ---
//
// Verifies P0-3: IPipelineBehavior chain construction and execution.
// Behaviors wrap the handler in a MediatR-style chain: each behavior can
// inspect/modify the request, short-circuit, or pass through to the next.

use rust_webx_core::pipeline::{BoxedNextFn, IPipelineBehavior};

struct BehaviorProbeRequest;

#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)]
struct BehaviorProbeResponse {
    message: String,
    source: String,
}

impl IRequest<BehaviorProbeResponse> for BehaviorProbeRequest {}

#[derive(Default)]
struct BehaviorProbeHandler;

#[async_trait::async_trait]
impl IRequestHandler<BehaviorProbeRequest, BehaviorProbeResponse> for BehaviorProbeHandler {
    async fn handle(&mut self, _req: BehaviorProbeRequest) -> LrwfResult<BehaviorProbeResponse> {
        Ok(BehaviorProbeResponse {
            message: "from-handler".into(),
            source: "handler".into(),
        })
    }
}

fn __factory_behavior_probe_handler(
    _resolver: &dyn rust_dix::IServiceResolver,
) -> LrwfResult<Box<dyn std::any::Any + Send>> {
    Ok(Box::new(BehaviorProbeHandler) as Box<dyn std::any::Any + Send>)
}

fn __call_behavior_probe_handler(
    handler: Box<dyn std::any::Any + Send>,
    request: Box<dyn std::any::Any + Send>,
) -> std::pin::Pin<
    Box<
        dyn std::future::Future<Output = LrwfResult<Box<dyn std::any::Any + Send>>>
            + Send,
    >,
> {
    Box::pin(async move {
        let mut h = *handler
            .downcast::<BehaviorProbeHandler>()
            .map_err(|_| Error::Internal("Handler downcast failed".into()))?;
        let req = *request
            .downcast::<BehaviorProbeRequest>()
            .map_err(|_| Error::Internal("Request downcast failed".into()))?;
        let result: BehaviorProbeResponse = h.handle(req).await?;
        Ok(Box::new(result) as Box<dyn std::any::Any + Send>)
    })
}

inventory::submit! {
    HandlerRegistration {
        req_type_id: std::any::TypeId::of::<BehaviorProbeRequest>(),
        req_type_name: "BehaviorProbeRequest",
        factory: __factory_behavior_probe_handler,
        call: __call_behavior_probe_handler,
    }
}

/// Behavior that records the order it was called relative to the handler.
struct TrackingBehavior {
    called: Arc<AtomicU32>,
}

#[async_trait::async_trait]
impl IPipelineBehavior for TrackingBehavior {
    async fn handle(
        &self,
        req: Box<dyn std::any::Any + Send>,
        next: BoxedNextFn,
    ) -> LrwfResult<Box<dyn std::any::Any + Send>> {
        self.called.fetch_add(1, Ordering::SeqCst);
        next(req).await
    }
}

/// Behavior that short-circuits the chain without calling next.
struct ShortCircuitBehavior;

#[async_trait::async_trait]
impl IPipelineBehavior for ShortCircuitBehavior {
    async fn handle(
        &self,
        _req: Box<dyn std::any::Any + Send>,
        _next: BoxedNextFn,
    ) -> LrwfResult<Box<dyn std::any::Any + Send>> {
        Ok(Box::new(BehaviorProbeResponse {
            message: "short-circuited".into(),
            source: "behavior".into(),
        }))
    }
}

#[tokio::test]
async fn mediator_send_pipeline_behavior_executes_before_handler() {
    let behavior_called = Arc::new(AtomicU32::new(0));
    let behavior = Arc::new(TrackingBehavior {
        called: Arc::clone(&behavior_called),
    });

    let provider = ServiceCollection::new()
            .singleton::<dyn IPipelineBehavior>(move |_| {
                Arc::clone(&behavior) as Arc<dyn IPipelineBehavior>
            })
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);

    let rsp = mediator
        .send(BehaviorProbeRequest)
        .await
        .expect("send failed");

    assert_eq!(behavior_called.load(Ordering::SeqCst), 1, "behavior should be called once");
    assert_eq!(rsp.source, "handler", "response should come from handler");
    assert_eq!(rsp.message, "from-handler");
}

#[tokio::test]
async fn mediator_send_pipeline_behavior_can_short_circuit() {
    let provider = ServiceCollection::new()
            .singleton::<dyn IPipelineBehavior>(|_| {
                Arc::new(ShortCircuitBehavior) as Arc<dyn IPipelineBehavior>
            })
            .build()
            .unwrap();
    let mediator = Mediator::new(provider);

    let rsp = mediator
        .send(BehaviorProbeRequest)
        .await
        .expect("send failed");

    assert_eq!(rsp.source, "behavior", "response should come from behavior (short-circuit)");
    assert_eq!(rsp.message, "short-circuited");
}

#[tokio::test]
async fn mediator_send_pipeline_empty_chain_works() {
    // No behaviors registered — chain is just the terminal handler.
    let provider = build_provider();
    let mediator = Mediator::new(provider);

    let rsp = mediator
        .send(BehaviorProbeRequest)
        .await
        .expect("send failed");

    assert_eq!(rsp.source, "handler");
    assert_eq!(rsp.message, "from-handler");
}