switchy_web_server 0.2.0

Switchy Web Server package
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
//! Test response handling and assertion utilities.
//!
//! This module provides [`TestResponse`], a unified response wrapper for testing HTTP handlers,
//! along with [`TestResponseExt`], an extension trait providing assertion methods for
//! validating response status codes, headers, and body content.
//!
//! # Overview
//!
//! The test response utilities enable expressive, fluent assertions in tests:
//!
//! ```rust,ignore
//! use switchy_web_server::test_client::{TestResponse, TestResponseExt};
//!
//! // Chain assertions fluently
//! response
//!     .assert_success()
//!     .assert_header("Content-Type", "application/json")
//!     .assert_text_contains("success");
//! ```
//!
//! # Features
//!
//! * Status code classification (`is_success()`, `is_client_error()`, etc.)
//! * Body parsing (`text()`, `json()`)
//! * Fluent assertions (`assert_status()`, `assert_header()`, `assert_text_contains()`)
//! * JSON assertions when `serde` feature is enabled

use std::collections::BTreeMap;

#[cfg(feature = "serde")]
use serde::de::DeserializeOwned;

/// Unified test response wrapper for both Actix and Simulator backends
#[derive(Debug, Clone)]
pub struct TestResponse {
    /// HTTP status code (e.g., 200, 404, 500)
    pub status: u16,
    /// Response headers as key-value pairs
    pub headers: BTreeMap<String, String>,
    /// Response body as raw bytes
    pub body: Vec<u8>,
}

impl TestResponse {
    /// Create a new test response
    #[must_use]
    pub const fn new(status: u16, headers: BTreeMap<String, String>, body: Vec<u8>) -> Self {
        Self {
            status,
            headers,
            body,
        }
    }

    /// Get the response status code
    #[must_use]
    pub const fn status(&self) -> u16 {
        self.status
    }

    /// Get response headers
    #[must_use]
    pub const fn headers(&self) -> &BTreeMap<String, String> {
        &self.headers
    }

    /// Get a specific header value
    #[must_use]
    pub fn header(&self, name: &str) -> Option<&String> {
        self.headers.get(name)
    }

    /// Get the response body as bytes
    #[must_use]
    pub fn body(&self) -> &[u8] {
        &self.body
    }

    /// Get the response body as a UTF-8 string
    ///
    /// # Errors
    /// * Returns error if the body is not valid UTF-8
    pub fn text(&self) -> Result<String, std::str::Utf8Error> {
        std::str::from_utf8(&self.body).map(ToString::to_string)
    }

    /// Parse the response body as JSON
    ///
    /// # Errors
    /// * Returns error if JSON parsing fails
    #[cfg(feature = "serde")]
    pub fn json<T: DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_slice(&self.body)
    }

    /// Check if the response status is successful (2xx)
    #[must_use]
    pub fn is_success(&self) -> bool {
        (200..300).contains(&self.status)
    }

    /// Check if the response status is a client error (4xx)
    #[must_use]
    pub fn is_client_error(&self) -> bool {
        (400..500).contains(&self.status)
    }

    /// Check if the response status is a server error (5xx)
    #[must_use]
    pub fn is_server_error(&self) -> bool {
        (500..600).contains(&self.status)
    }

    /// Check if the response status is a redirection (3xx)
    #[must_use]
    pub fn is_redirection(&self) -> bool {
        (300..400).contains(&self.status)
    }
}

/// Extension trait for `TestResponse` with assertion helpers
pub trait TestResponseExt {
    /// Assert that the response has the expected status code
    ///
    /// # Panics
    /// * Panics if the status code doesn't match
    fn assert_status(&self, expected: u16) -> &Self;

    /// Assert that the response is successful (2xx)
    ///
    /// # Panics
    /// * Panics if the status is not in the 2xx range
    fn assert_success(&self) -> &Self;

    /// Assert that the response is a client error (4xx)
    ///
    /// # Panics
    /// * Panics if the status is not in the 4xx range
    fn assert_client_error(&self) -> &Self;

    /// Assert that the response is a server error (5xx)
    ///
    /// # Panics
    /// * Panics if the status is not in the 5xx range
    fn assert_server_error(&self) -> &Self;

    /// Assert that the response has a specific header
    ///
    /// # Panics
    /// * Panics if the header is not present
    fn assert_header(&self, name: &str, expected: &str) -> &Self;

    /// Assert that the response has a specific header (case-insensitive)
    ///
    /// # Panics
    /// * Panics if the header is not present
    fn assert_header_contains(&self, name: &str, expected: &str) -> &Self;

    /// Assert that the response body contains the expected text
    ///
    /// # Panics
    /// * Panics if the body doesn't contain the expected text
    /// * Panics if the body is not valid UTF-8
    fn assert_text_contains(&self, expected: &str) -> &Self;

    /// Assert that the response body equals the expected text
    ///
    /// # Panics
    /// * Panics if the body doesn't equal the expected text
    /// * Panics if the body is not valid UTF-8
    fn assert_text_equals(&self, expected: &str) -> &Self;

    /// Assert that the response body can be parsed as JSON and equals the expected value
    ///
    /// # Panics
    /// * Panics if JSON parsing fails
    /// * Panics if the parsed JSON doesn't equal the expected value
    #[cfg(feature = "serde")]
    fn assert_json_equals<T: DeserializeOwned + PartialEq + std::fmt::Debug>(
        &self,
        expected: &T,
    ) -> &Self;

    /// Assert that the response body can be parsed as JSON and contains the expected fields
    ///
    /// # Panics
    /// * Panics if JSON parsing fails
    /// * Panics if the expected fields are not present
    #[cfg(feature = "serde")]
    fn assert_json_contains(&self, expected: &serde_json::Value) -> &Self;
}

impl TestResponseExt for TestResponse {
    fn assert_status(&self, expected: u16) -> &Self {
        assert_eq!(
            self.status, expected,
            "Expected status {expected}, got {}",
            self.status
        );
        self
    }

    fn assert_success(&self) -> &Self {
        assert!(
            self.is_success(),
            "Expected successful status (2xx), got {}",
            self.status
        );
        self
    }

    fn assert_client_error(&self) -> &Self {
        assert!(
            self.is_client_error(),
            "Expected client error status (4xx), got {}",
            self.status
        );
        self
    }

    fn assert_server_error(&self) -> &Self {
        assert!(
            self.is_server_error(),
            "Expected server error status (5xx), got {}",
            self.status
        );
        self
    }

    fn assert_header(&self, name: &str, expected: &str) -> &Self {
        let actual = self
            .header(name)
            .unwrap_or_else(|| panic!("Header '{name}' not found"));
        assert_eq!(
            actual, expected,
            "Expected header '{name}' to be '{expected}', got '{actual}'"
        );
        self
    }

    fn assert_header_contains(&self, name: &str, expected: &str) -> &Self {
        let actual = self
            .header(name)
            .unwrap_or_else(|| panic!("Header '{name}' not found"));
        assert!(
            actual.to_lowercase().contains(&expected.to_lowercase()),
            "Expected header '{name}' to contain '{expected}', got '{actual}'"
        );
        self
    }

    fn assert_text_contains(&self, expected: &str) -> &Self {
        let text = self.text().expect("Response body is not valid UTF-8");
        assert!(
            text.contains(expected),
            "Expected response body to contain '{expected}', got: {text}"
        );
        self
    }

    fn assert_text_equals(&self, expected: &str) -> &Self {
        let text = self.text().expect("Response body is not valid UTF-8");
        assert_eq!(
            text, expected,
            "Expected response body to equal '{expected}', got: {text}"
        );
        self
    }

    #[cfg(feature = "serde")]
    fn assert_json_equals<T: DeserializeOwned + PartialEq + std::fmt::Debug>(
        &self,
        expected: &T,
    ) -> &Self {
        let actual: T = self.json().expect("Failed to parse response body as JSON");
        assert_eq!(
            &actual, expected,
            "Expected JSON to equal {expected:?}, got {actual:?}"
        );
        self
    }

    #[cfg(feature = "serde")]
    fn assert_json_contains(&self, expected: &serde_json::Value) -> &Self {
        fn contains_value(actual: &serde_json::Value, expected: &serde_json::Value) -> bool {
            match (actual, expected) {
                (
                    serde_json::Value::Object(actual_obj),
                    serde_json::Value::Object(expected_obj),
                ) => expected_obj.iter().all(|(key, expected_val)| {
                    actual_obj
                        .get(key)
                        .is_some_and(|actual_val| contains_value(actual_val, expected_val))
                }),
                (actual_val, expected_val) => actual_val == expected_val,
            }
        }

        let actual: serde_json::Value = self.json().expect("Failed to parse response body as JSON");

        assert!(
            contains_value(&actual, expected),
            "Expected JSON to contain {expected}, got {actual}"
        );
        self
    }
}

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

    fn create_response(status: u16, body: &[u8]) -> TestResponse {
        TestResponse::new(status, BTreeMap::new(), body.to_vec())
    }

    fn create_response_with_headers(
        status: u16,
        headers: &[(&str, &str)],
        body: &[u8],
    ) -> TestResponse {
        let headers = headers
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect();
        TestResponse::new(status, headers, body.to_vec())
    }

    #[test_log::test]
    fn test_response_status_classification_success() {
        assert!(create_response(200, b"").is_success());
        assert!(create_response(201, b"").is_success());
        assert!(create_response(204, b"").is_success());
        assert!(create_response(299, b"").is_success());

        assert!(!create_response(199, b"").is_success());
        assert!(!create_response(300, b"").is_success());
    }

    #[test_log::test]
    fn test_response_status_classification_client_error() {
        assert!(create_response(400, b"").is_client_error());
        assert!(create_response(401, b"").is_client_error());
        assert!(create_response(404, b"").is_client_error());
        assert!(create_response(499, b"").is_client_error());

        assert!(!create_response(399, b"").is_client_error());
        assert!(!create_response(500, b"").is_client_error());
    }

    #[test_log::test]
    fn test_response_status_classification_server_error() {
        assert!(create_response(500, b"").is_server_error());
        assert!(create_response(502, b"").is_server_error());
        assert!(create_response(503, b"").is_server_error());
        assert!(create_response(599, b"").is_server_error());

        assert!(!create_response(499, b"").is_server_error());
        assert!(!create_response(600, b"").is_server_error());
    }

    #[test_log::test]
    fn test_response_status_classification_redirection() {
        assert!(create_response(301, b"").is_redirection());
        assert!(create_response(302, b"").is_redirection());
        assert!(create_response(307, b"").is_redirection());
        assert!(create_response(399, b"").is_redirection());

        assert!(!create_response(299, b"").is_redirection());
        assert!(!create_response(400, b"").is_redirection());
    }

    #[test_log::test]
    fn test_response_text_valid_utf8() {
        let response = create_response(200, b"Hello, World!");
        let text = response.text().unwrap();
        assert_eq!(text, "Hello, World!");
    }

    #[test_log::test]
    fn test_response_text_invalid_utf8() {
        // Invalid UTF-8 sequence
        let invalid_utf8 = vec![0xff, 0xfe, 0x00, 0x01];
        let response = create_response(200, &invalid_utf8);
        let result = response.text();
        assert!(result.is_err());
    }

    #[test_log::test]
    fn test_response_header_retrieval() {
        let response = create_response_with_headers(
            200,
            &[
                ("Content-Type", "application/json"),
                ("X-Custom", "custom-value"),
            ],
            b"{}",
        );

        assert_eq!(
            response.header("Content-Type"),
            Some(&"application/json".to_string())
        );
        assert_eq!(
            response.header("X-Custom"),
            Some(&"custom-value".to_string())
        );
        assert_eq!(response.header("X-Missing"), None);
    }

    #[test_log::test]
    #[cfg(feature = "serde")]
    fn test_response_json_parsing_valid() {
        let json_body = r#"{"name": "test", "value": 123}"#;
        let response = create_response(200, json_body.as_bytes());

        let parsed: serde_json::Value = response.json().unwrap();
        assert_eq!(parsed["name"], "test");
        assert_eq!(parsed["value"], 123);
    }

    #[test_log::test]
    #[cfg(feature = "serde")]
    fn test_response_json_parsing_invalid() {
        let invalid_json = r#"{"name": invalid}"#;
        let response = create_response(200, invalid_json.as_bytes());

        let result: Result<serde_json::Value, _> = response.json();
        assert!(result.is_err());
    }

    #[test_log::test]
    fn test_assert_status() {
        let response = create_response(200, b"OK");
        response.assert_status(200);
    }

    #[test_log::test]
    #[should_panic(expected = "Expected status 404, got 200")]
    fn test_assert_status_fails() {
        let response = create_response(200, b"OK");
        response.assert_status(404);
    }

    #[test_log::test]
    fn test_assert_success() {
        create_response(200, b"").assert_success();
        create_response(201, b"").assert_success();
    }

    #[test_log::test]
    #[should_panic(expected = "Expected successful status (2xx), got 404")]
    fn test_assert_success_fails() {
        create_response(404, b"").assert_success();
    }

    #[test_log::test]
    fn test_assert_client_error() {
        create_response(400, b"").assert_client_error();
        create_response(404, b"").assert_client_error();
    }

    #[test_log::test]
    #[should_panic(expected = "Expected client error status (4xx), got 200")]
    fn test_assert_client_error_fails() {
        create_response(200, b"").assert_client_error();
    }

    #[test_log::test]
    fn test_assert_server_error() {
        create_response(500, b"").assert_server_error();
        create_response(503, b"").assert_server_error();
    }

    #[test_log::test]
    #[should_panic(expected = "Expected server error status (5xx), got 200")]
    fn test_assert_server_error_fails() {
        create_response(200, b"").assert_server_error();
    }

    #[test_log::test]
    fn test_assert_header() {
        let response =
            create_response_with_headers(200, &[("Content-Type", "application/json")], b"{}");
        response.assert_header("Content-Type", "application/json");
    }

    #[test_log::test]
    #[should_panic(expected = "Header 'X-Missing' not found")]
    fn test_assert_header_missing() {
        let response = create_response(200, b"");
        response.assert_header("X-Missing", "value");
    }

    #[test_log::test]
    fn test_assert_header_contains() {
        let response = create_response_with_headers(
            200,
            &[("Content-Type", "application/json; charset=utf-8")],
            b"{}",
        );
        response.assert_header_contains("Content-Type", "json");
    }

    #[test_log::test]
    fn test_assert_text_contains() {
        let response = create_response(200, b"Hello, World!");
        response.assert_text_contains("World");
    }

    #[test_log::test]
    #[should_panic(expected = "Expected response body to contain 'missing'")]
    fn test_assert_text_contains_fails() {
        let response = create_response(200, b"Hello, World!");
        response.assert_text_contains("missing");
    }

    #[test_log::test]
    fn test_assert_text_equals() {
        let response = create_response(200, b"exact match");
        response.assert_text_equals("exact match");
    }

    #[test_log::test]
    #[should_panic(expected = "Expected response body to equal 'different'")]
    fn test_assert_text_equals_fails() {
        let response = create_response(200, b"actual");
        response.assert_text_equals("different");
    }

    #[test_log::test]
    #[cfg(feature = "serde")]
    fn test_assert_json_equals() {
        let json_body = r#"{"name":"test","value":123}"#;
        let response = create_response(200, json_body.as_bytes());

        let expected: serde_json::Value = serde_json::json!({"name": "test", "value": 123});
        response.assert_json_equals(&expected);
    }

    #[test_log::test]
    #[cfg(feature = "serde")]
    fn test_assert_json_contains() {
        let json_body = r#"{"name":"test","value":123,"extra":"ignored"}"#;
        let response = create_response(200, json_body.as_bytes());

        let expected = serde_json::json!({"name": "test"});
        response.assert_json_contains(&expected);
    }

    #[test_log::test]
    #[cfg(feature = "serde")]
    fn test_assert_json_contains_nested() {
        let json_body = r#"{"user":{"name":"test","age":30},"active":true}"#;
        let response = create_response(200, json_body.as_bytes());

        // Test that nested objects work correctly
        let expected = serde_json::json!({"user": {"name": "test"}});
        response.assert_json_contains(&expected);
    }
}