mockforge-sdk 0.3.115

Developer SDK for embedding MockForge in tests and applications
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
//! Verification API for `MockForge` SDK
//!
//! Provides methods to verify that specific requests were made (or not made)
//! during test execution.
#![allow(async_fn_in_trait)]

use crate::Error;
use mockforge_core::{
    request_logger::get_global_logger, verify_at_least, verify_never, verify_requests,
    verify_sequence, VerificationCount, VerificationRequest, VerificationResult,
};

/// Extension trait for verification methods on `MockServer`
pub trait Verification {
    /// Verify requests against a pattern and count assertion
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use mockforge_sdk::MockServer;
    /// use mockforge_sdk::verification::Verification;
    /// use mockforge_core::verification::{VerificationRequest, VerificationCount};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut server = MockServer::new().port(3000).start().await?;
    ///
    /// // Make some requests...
    ///
    /// let pattern = VerificationRequest {
    ///     method: Some("GET".to_string()),
    ///     path: Some("/api/users".to_string()),
    ///     query_params: std::collections::HashMap::new(),
    ///     headers: std::collections::HashMap::new(),
    ///     body_pattern: None,
    /// };
    ///
    /// let result = server.verify(&pattern, VerificationCount::Exactly(3)).await?;
    /// assert!(result.matched, "Expected GET /api/users to be called exactly 3 times");
    /// # Ok(())
    /// # }
    /// ```
    async fn verify(
        &self,
        pattern: &VerificationRequest,
        expected: VerificationCount,
    ) -> Result<VerificationResult, Error>;

    /// Verify that a request was never made
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use mockforge_sdk::MockServer;
    /// use mockforge_sdk::verification::Verification;
    /// use mockforge_core::verification::VerificationRequest;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut server = MockServer::new().port(3000).start().await?;
    ///
    /// // Make some requests...
    ///
    /// let pattern = VerificationRequest {
    ///     method: Some("DELETE".to_string()),
    ///     path: Some("/api/users/1".to_string()),
    ///     query_params: std::collections::HashMap::new(),
    ///     headers: std::collections::HashMap::new(),
    ///     body_pattern: None,
    /// };
    ///
    /// let result = server.verify_never(&pattern).await?;
    /// assert!(result.matched, "Expected DELETE /api/users/1 to never be called");
    /// # Ok(())
    /// # }
    /// ```
    async fn verify_never(
        &self,
        pattern: &VerificationRequest,
    ) -> Result<VerificationResult, Error>;

    /// Verify that a request was made at least N times
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use mockforge_sdk::MockServer;
    /// use mockforge_sdk::verification::Verification;
    /// use mockforge_core::verification::VerificationRequest;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut server = MockServer::new().port(3000).start().await?;
    ///
    /// // Make some requests...
    ///
    /// let pattern = VerificationRequest {
    ///     method: Some("POST".to_string()),
    ///     path: Some("/api/orders".to_string()),
    ///     query_params: std::collections::HashMap::new(),
    ///     headers: std::collections::HashMap::new(),
    ///     body_pattern: None,
    /// };
    ///
    /// let result = server.verify_at_least(&pattern, 2).await?;
    /// assert!(result.matched, "Expected POST /api/orders to be called at least 2 times");
    /// # Ok(())
    /// # }
    /// ```
    async fn verify_at_least(
        &self,
        pattern: &VerificationRequest,
        min: usize,
    ) -> Result<VerificationResult, Error>;

    /// Verify that requests occurred in a specific sequence
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use mockforge_sdk::MockServer;
    /// use mockforge_sdk::verification::Verification;
    /// use mockforge_core::verification::VerificationRequest;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut server = MockServer::new().port(3000).start().await?;
    ///
    /// // Make some requests in sequence...
    ///
    /// let patterns = vec![
    ///     VerificationRequest {
    ///         method: Some("POST".to_string()),
    ///         path: Some("/api/users".to_string()),
    ///         query_params: std::collections::HashMap::new(),
    ///         headers: std::collections::HashMap::new(),
    ///         body_pattern: None,
    ///     },
    ///     VerificationRequest {
    ///         method: Some("GET".to_string()),
    ///         path: Some("/api/users/1".to_string()),
    ///         query_params: std::collections::HashMap::new(),
    ///         headers: std::collections::HashMap::new(),
    ///         body_pattern: None,
    ///     },
    /// ];
    ///
    /// let result = server.verify_sequence(&patterns).await?;
    /// assert!(result.matched, "Expected requests to occur in sequence");
    /// # Ok(())
    /// # }
    /// ```
    async fn verify_sequence(
        &self,
        patterns: &[VerificationRequest],
    ) -> Result<VerificationResult, Error>;
}

impl Verification for crate::server::MockServer {
    async fn verify(
        &self,
        pattern: &VerificationRequest,
        expected: VerificationCount,
    ) -> Result<VerificationResult, Error> {
        let logger = get_global_logger()
            .ok_or_else(|| Error::General("Request logger not initialized".to_string()))?;

        Ok(verify_requests(logger, pattern, expected).await)
    }

    async fn verify_never(
        &self,
        pattern: &VerificationRequest,
    ) -> Result<VerificationResult, Error> {
        let logger = get_global_logger()
            .ok_or_else(|| Error::General("Request logger not initialized".to_string()))?;

        Ok(verify_never(logger, pattern).await)
    }

    async fn verify_at_least(
        &self,
        pattern: &VerificationRequest,
        min: usize,
    ) -> Result<VerificationResult, Error> {
        let logger = get_global_logger()
            .ok_or_else(|| Error::General("Request logger not initialized".to_string()))?;

        Ok(verify_at_least(logger, pattern, min).await)
    }

    async fn verify_sequence(
        &self,
        patterns: &[VerificationRequest],
    ) -> Result<VerificationResult, Error> {
        let logger = get_global_logger()
            .ok_or_else(|| Error::General("Request logger not initialized".to_string()))?;

        Ok(verify_sequence(logger, patterns).await)
    }
}

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

    // Helper function to create a verification request
    fn create_verification_request(method: &str, path: &str) -> VerificationRequest {
        VerificationRequest {
            method: Some(method.to_string()),
            path: Some(path.to_string()),
            query_params: HashMap::new(),
            headers: HashMap::new(),
            body_pattern: None,
        }
    }

    #[test]
    fn test_verification_request_creation() {
        let request = create_verification_request("GET", "/api/users");
        assert_eq!(request.method, Some("GET".to_string()));
        assert_eq!(request.path, Some("/api/users".to_string()));
        assert!(request.query_params.is_empty());
        assert!(request.headers.is_empty());
        assert!(request.body_pattern.is_none());
    }

    #[test]
    fn test_verification_request_with_query_params() {
        let mut query_params = HashMap::new();
        query_params.insert("page".to_string(), "1".to_string());
        query_params.insert("limit".to_string(), "10".to_string());

        let request = VerificationRequest {
            method: Some("GET".to_string()),
            path: Some("/api/users".to_string()),
            query_params,
            headers: HashMap::new(),
            body_pattern: None,
        };

        assert_eq!(request.query_params.len(), 2);
        assert_eq!(request.query_params.get("page"), Some(&"1".to_string()));
        assert_eq!(request.query_params.get("limit"), Some(&"10".to_string()));
    }

    #[test]
    fn test_verification_request_with_headers() {
        let mut headers = HashMap::new();
        headers.insert("Authorization".to_string(), "Bearer token".to_string());
        headers.insert("Content-Type".to_string(), "application/json".to_string());

        let request = VerificationRequest {
            method: Some("POST".to_string()),
            path: Some("/api/users".to_string()),
            query_params: HashMap::new(),
            headers,
            body_pattern: None,
        };

        assert_eq!(request.headers.len(), 2);
        assert_eq!(request.headers.get("Authorization"), Some(&"Bearer token".to_string()));
    }

    #[test]
    fn test_verification_request_with_body_pattern() {
        let request = VerificationRequest {
            method: Some("POST".to_string()),
            path: Some("/api/users".to_string()),
            query_params: HashMap::new(),
            headers: HashMap::new(),
            body_pattern: Some(r#"{"name":".*"}"#.to_string()),
        };

        assert_eq!(request.body_pattern, Some(r#"{"name":".*"}"#.to_string()));
    }

    #[test]
    fn test_verification_count_exactly() {
        let count = VerificationCount::Exactly(3);
        match count {
            VerificationCount::Exactly(n) => assert_eq!(n, 3),
            _ => panic!("Expected Exactly variant"),
        }
    }

    #[test]
    fn test_verification_count_at_least() {
        let count = VerificationCount::AtLeast(2);
        match count {
            VerificationCount::AtLeast(n) => assert_eq!(n, 2),
            _ => panic!("Expected AtLeast variant"),
        }
    }

    #[test]
    fn test_verification_count_at_most() {
        let count = VerificationCount::AtMost(5);
        match count {
            VerificationCount::AtMost(n) => assert_eq!(n, 5),
            _ => panic!("Expected AtMost variant"),
        }
    }

    #[test]
    fn test_verification_count_never() {
        let count = VerificationCount::Never;
        match count {
            VerificationCount::Never => (),
            _ => panic!("Expected Never variant"),
        }
    }

    #[tokio::test]
    async fn test_verify_error_when_logger_not_initialized() {
        let server = crate::server::MockServer::default();
        let request = create_verification_request("GET", "/api/test");

        // Without initializing the global logger, this should fail
        let result = server.verify(&request, VerificationCount::Exactly(1)).await;

        // The result depends on whether the global logger is initialized
        // In test environments, it might be initialized by other tests
        if result.is_err() {
            match result {
                Err(Error::General(msg)) => {
                    assert!(msg.contains("Request logger not initialized"));
                }
                _ => panic!("Expected General error about logger"),
            }
        }
    }

    #[tokio::test]
    async fn test_verify_never_error_when_logger_not_initialized() {
        let server = crate::server::MockServer::default();
        let request = create_verification_request("DELETE", "/api/users/1");

        let result = server.verify_never(&request).await;

        if result.is_err() {
            match result {
                Err(Error::General(msg)) => {
                    assert!(msg.contains("Request logger not initialized"));
                }
                _ => panic!("Expected General error about logger"),
            }
        }
    }

    #[tokio::test]
    async fn test_verify_at_least_error_when_logger_not_initialized() {
        let server = crate::server::MockServer::default();
        let request = create_verification_request("POST", "/api/orders");

        let result = server.verify_at_least(&request, 2).await;

        if result.is_err() {
            match result {
                Err(Error::General(msg)) => {
                    assert!(msg.contains("Request logger not initialized"));
                }
                _ => panic!("Expected General error about logger"),
            }
        }
    }

    #[tokio::test]
    async fn test_verify_sequence_error_when_logger_not_initialized() {
        let server = crate::server::MockServer::default();
        let patterns = [
            create_verification_request("POST", "/api/users"),
            create_verification_request("GET", "/api/users/1"),
        ];

        let result = server.verify_sequence(&patterns).await;

        if result.is_err() {
            match result {
                Err(Error::General(msg)) => {
                    assert!(msg.contains("Request logger not initialized"));
                }
                _ => panic!("Expected General error about logger"),
            }
        }
    }

    #[test]
    fn test_verification_request_all_fields() {
        let mut query_params = HashMap::new();
        query_params.insert("id".to_string(), "123".to_string());

        let mut headers = HashMap::new();
        headers.insert("X-Custom".to_string(), "value".to_string());

        let request = VerificationRequest {
            method: Some("PUT".to_string()),
            path: Some("/api/users/123".to_string()),
            query_params,
            headers,
            body_pattern: Some(r#"{"name":"test"}"#.to_string()),
        };

        assert_eq!(request.method, Some("PUT".to_string()));
        assert_eq!(request.path, Some("/api/users/123".to_string()));
        assert_eq!(request.query_params.len(), 1);
        assert_eq!(request.headers.len(), 1);
        assert!(request.body_pattern.is_some());
    }

    #[test]
    fn test_verification_request_minimal() {
        let request = VerificationRequest {
            method: None,
            path: None,
            query_params: HashMap::new(),
            headers: HashMap::new(),
            body_pattern: None,
        };

        assert!(request.method.is_none());
        assert!(request.path.is_none());
        assert!(request.query_params.is_empty());
        assert!(request.headers.is_empty());
        assert!(request.body_pattern.is_none());
    }

    #[test]
    fn test_verification_sequence_empty() {
        let patterns: Vec<VerificationRequest> = vec![];
        assert_eq!(patterns.len(), 0);
    }

    #[test]
    fn test_verification_sequence_single() {
        let patterns = [create_verification_request("GET", "/api/test")];
        assert_eq!(patterns.len(), 1);
    }

    #[test]
    fn test_verification_sequence_multiple() {
        let patterns = [
            create_verification_request("POST", "/api/users"),
            create_verification_request("GET", "/api/users/1"),
            create_verification_request("PUT", "/api/users/1"),
            create_verification_request("DELETE", "/api/users/1"),
        ];
        assert_eq!(patterns.len(), 4);
    }

    #[test]
    fn test_verification_with_complex_pattern() {
        let mut query_params = HashMap::new();
        query_params.insert("filter".to_string(), "active".to_string());
        query_params.insert("sort".to_string(), "name".to_string());

        let mut headers = HashMap::new();
        headers.insert("Accept".to_string(), "application/json".to_string());
        headers.insert("X-API-Key".to_string(), "secret".to_string());

        let request = VerificationRequest {
            method: Some("GET".to_string()),
            path: Some("/api/users".to_string()),
            query_params,
            headers,
            body_pattern: None,
        };

        assert_eq!(request.method, Some("GET".to_string()));
        assert_eq!(request.query_params.len(), 2);
        assert_eq!(request.headers.len(), 2);
    }
}