armature-framework 0.5.0

A modern, type-safe HTTP framework for Rust inspired by Angular and NestJS. Features dependency injection, decorators, middleware, authentication (JWT/OAuth2/SAML), validation, OpenAPI/Swagger, caching, job queues, and observability.
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
//! Integration tests for common Armature workflows.
//!
//! These tests verify that the most common use cases work correctly.

#![allow(clippy::get_first)]
#![allow(clippy::unnecessary_get_then_check)]

use armature_core::*;
use bytes::Bytes;

// =============================================================================
// HTTP Response Tests
// =============================================================================

#[test]
fn test_http_response_convenience_methods() {
    // Test JSON response shorthand
    let response = HttpResponse::json(&serde_json::json!({"message": "hello"})).unwrap();
    assert_eq!(response.status, 200);
    assert_eq!(
        response.headers.get("Content-Type"),
        Some(&"application/json".to_string())
    );

    // Test HTML response
    let response = HttpResponse::html("<h1>Hello</h1>");
    assert_eq!(response.status, 200);
    assert_eq!(
        response.headers.get("Content-Type"),
        Some(&"text/html; charset=utf-8".to_string())
    );
    assert_eq!(response.body, Bytes::from_static(b"<h1>Hello</h1>"));

    // Test text response
    let response = HttpResponse::text("Hello, World!");
    assert_eq!(response.status, 200);
    assert_eq!(
        response.headers.get("Content-Type"),
        Some(&"text/plain; charset=utf-8".to_string())
    );

    // Test redirect
    let response = HttpResponse::redirect("https://example.com");
    assert_eq!(response.status, 302);
    assert_eq!(
        response.headers.get("Location"),
        Some(&"https://example.com".to_string())
    );

    // Test permanent redirect
    let response = HttpResponse::redirect_permanent("https://example.com");
    assert_eq!(response.status, 301);

    // Test status code helpers
    assert_eq!(HttpResponse::unauthorized().status, 401);
    assert_eq!(HttpResponse::forbidden().status, 403);
    assert_eq!(HttpResponse::conflict().status, 409);
    assert_eq!(HttpResponse::service_unavailable().status, 503);
    assert_eq!(HttpResponse::accepted().status, 202);
    assert_eq!(HttpResponse::empty().status, 204);

    // Test fluent builder methods
    let response = HttpResponse::ok()
        .content_type("application/xml")
        .cache_control("max-age=3600")
        .with_body(b"<xml/>".to_vec());
    assert_eq!(
        response.headers.get("Content-Type"),
        Some(&"application/xml".to_string())
    );
    assert_eq!(
        response.headers.get("Cache-Control"),
        Some(&"max-age=3600".to_string())
    );

    // Test no_cache
    let response = HttpResponse::ok().no_cache();
    assert!(
        response
            .headers
            .get("Cache-Control")
            .unwrap()
            .contains("no-store")
    );

    // Test cookie — armature-core 0.2.3 introduced multi-cookie support,
    // which moved Set-Cookie out of the headers map and into a dedicated
    // `cookies` Vec so multiple cookies can be emitted on one response.
    let response = HttpResponse::ok().cookie("session", "abc123; HttpOnly");
    assert_eq!(response.cookies.len(), 1);
    assert!(response.cookies[0].starts_with("session=abc123"));

    // Test status checks
    let ok = HttpResponse::ok();
    assert!(ok.is_success());
    assert!(!ok.is_redirect());
    assert!(!ok.is_client_error());
    assert!(!ok.is_server_error());

    let redirect = HttpResponse::redirect("/");
    assert!(!redirect.is_success());
    assert!(redirect.is_redirect());

    let not_found = HttpResponse::not_found();
    assert!(not_found.is_client_error());

    let error = HttpResponse::internal_server_error();
    assert!(error.is_server_error());
}

// =============================================================================
// Container Tests
// =============================================================================

#[test]
fn test_container_convenience_methods() {
    let container = Container::new();

    // Test register and resolve
    #[derive(Clone, Default)]
    struct Config {
        debug: bool,
    }

    container.register(Config { debug: true });

    // Test require (should not panic)
    let config = container.require::<Config>();
    assert!(config.debug);

    // Test get_or_default
    #[derive(Clone, Default)]
    struct OtherConfig {
        timeout: u32,
    }

    let other = container.get_or_default::<OtherConfig>();
    assert_eq!(other.timeout, 0); // Default value

    // Test register_if_missing
    assert!(!container.register_if_missing(Config { debug: false }));
    assert!(container.require::<Config>().debug); // Still true

    #[derive(Clone)]
    struct NewService;
    assert!(container.register_if_missing(NewService));
    assert!(container.has::<NewService>());
}

// =============================================================================
// Error Tests
// =============================================================================

#[test]
fn test_error_convenience_methods() {
    // Test convenience constructors
    let err = Error::bad_request("Invalid input");
    assert_eq!(err.status_code(), 400);

    let err = Error::unauthorized("No token");
    assert_eq!(err.status_code(), 401);

    let err = Error::forbidden("Access denied");
    assert_eq!(err.status_code(), 403);

    let err = Error::not_found("User not found");
    assert_eq!(err.status_code(), 404);

    let err = Error::conflict("Resource already exists");
    assert_eq!(err.status_code(), 409);

    let err = Error::internal("Something went wrong");
    assert_eq!(err.status_code(), 500);

    let err = Error::validation("Email is required");
    assert_eq!(err.status_code(), 400);

    let err = Error::timeout("Request took too long");
    assert_eq!(err.status_code(), 408);

    let err = Error::rate_limited("Too many requests");
    assert_eq!(err.status_code(), 429);

    let err = Error::unavailable("Under maintenance");
    assert_eq!(err.status_code(), 503);
}

#[test]
fn test_error_help_messages() {
    let err = Error::ProviderNotFound("MyService".to_string());
    assert!(err.help().is_some());
    assert!(err.help().unwrap().contains("register"));

    let err = Error::RouteNotFound("/api/users".to_string());
    assert!(err.help().is_some());
    assert!(err.help().unwrap().contains("controller"));

    let err = Error::Deserialization("invalid JSON".to_string());
    assert!(err.help().is_some());
    assert!(err.help().unwrap().contains("JSON"));

    let err = Error::Unauthorized("Invalid token".to_string());
    assert!(err.help().is_some());
    assert!(err.help().unwrap().contains("Authorization"));

    let err = Error::TooManyRequests("Rate limit exceeded".to_string());
    assert!(err.help().is_some());
    assert!(err.help().unwrap().contains("retry"));

    // Not all errors have help
    let err = Error::Internal("Unknown error".to_string());
    assert!(err.help().is_none());
}

// =============================================================================
// HTTP Request Tests
// =============================================================================

#[test]
fn test_http_request_helpers() {
    let mut request = HttpRequest::new("GET", "/api/users/123".to_string());
    request.push_param("id", "123");
    request.push_query_param("format", "json");
    request
        .headers
        .insert("Content-Type", "application/json".to_string());
    request.body = Bytes::from_static(b"{\"name\":\"John\"}");

    // Test param helper
    assert_eq!(request.param("id"), Some("123"));
    assert_eq!(request.param("unknown"), None);

    // Test query helper
    assert_eq!(request.query_param("format"), Some("json"));
    assert_eq!(request.query_param("unknown"), None);

    // Test json deserialization
    #[derive(serde::Deserialize)]
    struct UserInput {
        name: String,
    }
    let user: UserInput = request.json().unwrap();
    assert_eq!(user.name, "John");
}

// =============================================================================
// Router Round-Trip
// =============================================================================

/// A request actually dispatched through a `Router`.
///
/// Every other test in this file constructs a type and asserts on the value it
/// just built. This one registers a route, sends a real request through
/// `Router::route`, and asserts on what the handler saw and what came back -
/// path-param capture, query parsing on a target that carries a query string,
/// status, and 404 for an unregistered path.
#[tokio::test]
async fn test_router_dispatches_a_request_end_to_end() {
    async fn show_user(req: HttpRequest) -> Result<HttpResponse, Error> {
        // The handler reads what routing captured and what the target carried.
        let id = req.param("id").unwrap_or("<none>").to_owned();
        let verbose = req.query_param("verbose").unwrap_or("<none>").to_owned();
        Ok(HttpResponse::ok().with_body(format!("{id}|{verbose}").into_bytes()))
    }

    let mut router = Router::new();
    router.add_route(Route::new(HttpMethod::GET, "/users/:id", show_user));

    // A query string must not affect matching, must not be captured into the
    // path param, and must be readable through `query_param`.
    let response = router
        .route(HttpRequest::new("GET", "/users/1?verbose=true"))
        .await
        .expect("GET /users/1?verbose=true must dispatch");
    assert_eq!(response.status, 200);
    assert_eq!(response.body, Bytes::from_static(b"1|true"));

    // Same route, no query string.
    let response = router
        .route(HttpRequest::new("GET", "/users/42"))
        .await
        .expect("GET /users/42 must dispatch");
    assert_eq!(response.status, 200);
    assert_eq!(response.body, Bytes::from_static(b"42|<none>"));

    // An unregistered path is a routing error, not a 200.
    assert!(
        router
            .route(HttpRequest::new("GET", "/nope"))
            .await
            .is_err(),
        "an unregistered path must not dispatch"
    );
}

// =============================================================================
// Circuit Breaker Tests
// =============================================================================

#[test]
fn test_circuit_breaker_basic() {
    use armature_core::resilience::{CircuitBreaker, CircuitBreakerConfig, CircuitState};

    let config = CircuitBreakerConfig::default();

    let cb = CircuitBreaker::new(config);
    assert_eq!(cb.state(), CircuitState::Closed);

    // Record failures to trip the circuit
    for _ in 0..5 {
        cb.record_failure();
    }

    // After enough failures, circuit should be open
    assert_eq!(cb.state(), CircuitState::Open);
}

// =============================================================================
// Retry Tests
// =============================================================================

#[test]
fn test_retry_config() {
    use armature_core::resilience::{BackoffStrategy, RetryConfig};
    use std::time::Duration;

    // Test exponential backoff
    let config = RetryConfig::default();
    assert!(config.max_attempts > 0);

    // Destructuring a `Constant` built one line earlier only asserts that
    // `match` works, so assert what the strategy actually computes instead:
    // a constant backoff returns the same delay for every attempt.
    let backoff = BackoffStrategy::Constant(Duration::from_millis(500));
    let delays: Vec<Duration> = (1..=4)
        .map(|attempt| backoff.delay_for_attempt(attempt))
        .collect();
    assert_eq!(delays, vec![Duration::from_millis(500); 4]);
}

// =============================================================================
// Bulkhead Tests
// =============================================================================

#[tokio::test]
async fn test_bulkhead_basic() {
    use armature_core::resilience::{Bulkhead, BulkheadConfig};

    let config = BulkheadConfig::new("test", 2);
    let bulkhead = Bulkhead::new(config);

    // Check initial capacity
    assert!(bulkhead.has_capacity());
    assert_eq!(bulkhead.available_permits(), 2);

    // Stats should show proper counts
    let stats = bulkhead.stats();
    assert_eq!(stats.name, "test");
    assert_eq!(stats.max_concurrent, 2);
}

/// A bulkhead that never has a permit taken never demonstrates it bounds
/// anything. Hold both permits of a size-2 bulkhead and assert the third
/// caller is refused rather than admitted.
#[tokio::test]
async fn test_bulkhead_refuses_calls_beyond_capacity() {
    use armature_core::resilience::{Bulkhead, BulkheadConfig, BulkheadError};
    use tokio::sync::oneshot;

    let bulkhead = Bulkhead::new(BulkheadConfig::new("saturate", 2));

    // Two in-flight calls, each parked on a channel we control, so both permits
    // are genuinely held while the third attempt is made.
    let mut releases = Vec::new();
    let mut holders = Vec::new();
    for _ in 0..2 {
        let (release_tx, release_rx) = oneshot::channel::<()>();
        let (entered_tx, entered_rx) = oneshot::channel::<()>();
        let bulkhead = bulkhead.clone();

        holders.push(tokio::spawn(async move {
            bulkhead
                .call(|| async move {
                    let _ = entered_tx.send(());
                    let _ = release_rx.await;
                    Ok::<(), std::convert::Infallible>(())
                })
                .await
        }));
        entered_rx.await.expect("call must enter the bulkhead");
        releases.push(release_tx);
    }

    assert_eq!(bulkhead.available_permits(), 0);
    assert!(!bulkhead.has_capacity());

    // Third caller: no permit is free, so it is rejected outright.
    let rejected = bulkhead
        .try_call(|| async { Ok::<(), std::convert::Infallible>(()) })
        .await;
    assert!(
        matches!(rejected, Err(BulkheadError::Full)),
        "a third concurrent call must be refused, got {rejected:?}"
    );

    // Release both holders; capacity comes back.
    for release in releases {
        let _ = release.send(());
    }
    for holder in holders {
        holder
            .await
            .expect("holder task")
            .expect("call must succeed");
    }
    assert_eq!(bulkhead.available_permits(), 2);
    assert!(
        bulkhead
            .try_call(|| async { Ok::<(), std::convert::Infallible>(()) })
            .await
            .is_ok(),
        "a freed permit must be reusable"
    );
}