rs-mock-server 0.6.2

A simple, file-based mock API server that maps your directory structure to HTTP routes. Ideal for local development and testing.
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
589
590
591
592
593
594
use std::{ffi::OsString, fmt::Display};

use http::Method;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::{handlers::build_method_router, route_builder::{method_from_str, route_params::RouteParams, PrintRoute, Route, RouteGenerator, RouteRegistrator}};

static RE_FILE_METHODS: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"^(\$)?(get|post|put|patch|delete|options)(\{(.+)\})?$").unwrap()
});

const ELEMENT_IS_PROTECTED: usize = 1;
const ELEMENT_METHOD: usize = 2;
const ELEMENT_DESCRIPTOR: usize = 4;

#[derive(Debug, Default, Clone, PartialEq)]
pub enum SubRoute {
    #[default]
    None,
    Id,
    Range(u32, u32),
    Static(String)
}

impl SubRoute {
    pub fn from(pattern: Option<regex::Match<'_>>) -> Self {
        if pattern.is_none() {
            return Self::None;
        }

        let pattern = pattern.unwrap().as_str();
        if pattern == "id" {
            return Self::Id;
        }

        if pattern.contains('-') {
            if let Some((start_str, end_str)) = pattern.split_once('-') {
                if let (Ok(start), Ok(end)) = (start_str.parse::<u32>(), end_str.parse::<u32>()) {
                    return Self::Range(start, end);
                }
            }
        }

        Self::Static(pattern.to_string())
    }
}

impl Display for SubRoute {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SubRoute::None => write!(f, ""),
            SubRoute::Id => write!(f, "/{{id}}"),
            SubRoute::Static(value) => write!(f, "/{{{}}}", value),
            SubRoute::Range(start, end) => write!(f, "/{{{}-{}}}", start, end),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct RouteBasic {
    pub path: OsString,
    pub method: Method,
    pub route: String,
    pub sub_route: SubRoute,
    pub is_protected: bool,
}

impl RouteBasic {
    pub fn try_parse(route_params: RouteParams) -> Route {
        if let Some(captures) = RE_FILE_METHODS.captures(&route_params.file_stem) {
            let is_protected = route_params.is_protected || captures.get(ELEMENT_IS_PROTECTED).is_some();
            let method = captures.get(ELEMENT_METHOD).unwrap().as_str();
            let pattern = captures.get(ELEMENT_DESCRIPTOR);

            let route_basic = Self {
                path: route_params.file_path,
                method: method_from_str(method),
                route: route_params.full_route,
                sub_route: SubRoute::from(pattern),
                is_protected,
            };

            return Route::Basic(route_basic);
        }

        let route_basic = Self {
            path: route_params.file_path,
            method: Method::GET,
            route: format!("{}/{}", route_params.full_route, route_params.file_stem) ,
            sub_route: SubRoute::None,
            is_protected: route_params.is_protected,
        };

        Route::Basic(route_basic)
    }
}

impl RouteGenerator for RouteBasic {
    fn make_routes(&self, app: &mut crate::app::App) {
        let method = self.method.as_str();

        match &self.sub_route {
            SubRoute::None => {
                let router = build_method_router(&self.path, method);
                app.push_route(&self.route, router, Some(method), self.is_protected, None);
            },
            SubRoute::Id => {
                let route_path = format!("{}/{}", self.route, "{id}");
                let router = build_method_router(&self.path, method);
                app.push_route(&route_path, router, Some(method), self.is_protected, None);
            },
            SubRoute::Range(start, end) => {
                for i in *start..=*end {
                    let route_path = format!("{}/{}", self.route, i);
                    let router = build_method_router(&self.path, method);
                    app.push_route(&route_path, router, Some(method), self.is_protected, None);
                }
            },
            SubRoute::Static(end_point) => {
                let route_path = format!("{}/{}", self.route, end_point);
                let router = build_method_router(&self.path, method);
                app.push_route(&route_path, router, Some(method), self.is_protected, None);
            },
        }

    }
}

impl PrintRoute for RouteBasic {
    fn println(&self) {
        let path = &self.path.to_string_lossy();
        let method = self.method.as_str();
        let route = &self.route;
        let subroute = self.sub_route.to_string();

        println!("✔️ Mapped {} to {} {}{}", path, method, route, subroute);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::route_builder::route_params::RouteParams;
    use std::fs::File;
    use std::path::Path;
    use tempfile::TempDir;

    fn create_test_file(dir: &Path, filename: &str) -> std::fs::DirEntry {
        let file_path = dir.join(filename);
        File::create(&file_path).unwrap();
        let mut entries = dir.read_dir().unwrap();
        entries.find(|entry| {
            entry.as_ref().unwrap().file_name() == filename
        }).unwrap().unwrap()
    }

    #[test]
    fn test_try_parse_get_method() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "get.json");
        let route_params = RouteParams::new("/api", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_post_method() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "post.json");
        let route_params = RouteParams::new("/api/users", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::POST);
                assert_eq!(route_basic.route, "/api/users");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_protected_method() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "$put.json");
        let route_params = RouteParams::new("/api/data", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::PUT);
                assert_eq!(route_basic.route, "/api/data");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_method_with_id_descriptor() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "get{id}.json");
        let route_params = RouteParams::new("/api/items", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/items");
                assert_eq!(route_basic.sub_route, SubRoute::Id);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_method_with_range_descriptor() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "delete{1-5}.json");
        let route_params = RouteParams::new("/api/resources", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::DELETE);
                assert_eq!(route_basic.route, "/api/resources");
                assert_eq!(route_basic.sub_route, SubRoute::Range(1, 5));
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_method_with_static_descriptor() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "patch{admin}.json");
        let route_params = RouteParams::new("/api/config", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::PATCH);
                assert_eq!(route_basic.route, "/api/config");
                assert_eq!(route_basic.sub_route, SubRoute::Static("admin".to_string()));
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_protected_with_descriptor() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "$options{special}.json");
        let route_params = RouteParams::new("/api/auth", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::OPTIONS);
                assert_eq!(route_basic.route, "/api/auth");
                assert_eq!(route_basic.sub_route, SubRoute::Static("special".to_string()));
                assert!(route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_inherited_protection() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "get.json");
        let route_params = RouteParams::new("/api/secure", &entry, true);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/secure");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_all_http_methods() {
        let temp_dir = TempDir::new().unwrap();
        let methods = vec![
            ("get.json", Method::GET),
            ("post.json", Method::POST),
            ("put.json", Method::PUT),
            ("patch.json", Method::PATCH),
            ("delete.json", Method::DELETE),
            ("options.json", Method::OPTIONS),
        ];

        for (filename, expected_method) in methods {
            let entry = create_test_file(temp_dir.path(), filename);
            let route_params = RouteParams::new("/api/test", &entry, false);
            let result = RouteBasic::try_parse(route_params);

            match result {
                Route::Basic(route_basic) => {
                    assert_eq!(route_basic.method, expected_method, "Failed for {}", filename);
                }
                _ => panic!("Expected Route::Basic for {}", filename),
            }
        }
    }

    #[test]
    fn test_try_parse_non_method_file() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "config.json");
        let route_params = RouteParams::new("/api", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                // Now it should create a static GET route with the file stem
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/config");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic for non-method file"),
        }
    }

    #[test]
    fn test_try_parse_complex_range() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "get{10-20}.json");
        let route_params = RouteParams::new("/api/pages", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/pages");
                assert_eq!(route_basic.sub_route, SubRoute::Range(10, 20));
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    #[test]
    fn test_try_parse_file_path_preservation() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "get.json");
        let route_params = RouteParams::new("/api", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                let expected_path = temp_dir.path().join("get.json").into_os_string();
                assert_eq!(route_basic.path, expected_path);
            }
            _ => panic!("Expected Route::Basic"),
        }
    }

    // Tests for new static route behavior (non-method files)

    #[test]
    fn test_try_parse_static_route_simple() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "users.json");
        let route_params = RouteParams::new("/api", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/users");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic for static route"),
        }
    }

    #[test]
    fn test_try_parse_static_route_with_protection() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "admin.json");
        let route_params = RouteParams::new("/api", &entry, true);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/admin");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(route_basic.is_protected); // Should inherit protection
            }
            _ => panic!("Expected Route::Basic for protected static route"),
        }
    }

    #[test]
    fn test_try_parse_static_route_different_extensions() {
        let temp_dir = TempDir::new().unwrap();
        let test_cases = vec![
            ("data.json", "data"),
            ("info.xml", "info"),
            ("config.yaml", "config"),
            ("settings.txt", "settings"),
            ("readme.md", "readme"),
        ];

        for (filename, expected_stem) in test_cases {
            let entry = create_test_file(temp_dir.path(), filename);
            let route_params = RouteParams::new("/files", &entry, false);
            let result = RouteBasic::try_parse(route_params);

            match result {
                Route::Basic(route_basic) => {
                    assert_eq!(route_basic.method, Method::GET);
                    assert_eq!(route_basic.route, format!("/files/{}", expected_stem));
                    assert_eq!(route_basic.sub_route, SubRoute::None);
                    assert!(!route_basic.is_protected);
                }
                _ => panic!("Expected Route::Basic for {}", filename),
            }
        }
    }

    #[test]
    fn test_try_parse_static_route_complex_names() {
        let temp_dir = TempDir::new().unwrap();
        let test_cases = vec![
            ("user-profile.json", "user-profile"),
            ("api_documentation.md", "api_documentation"),
            ("system.config.xml", "system"),
            ("data-2024.csv", "data-2024"),
        ];

        for (filename, expected_stem) in test_cases {
            let entry = create_test_file(temp_dir.path(), filename);
            let route_params = RouteParams::new("/resources", &entry, false);
            let result = RouteBasic::try_parse(route_params);

            match result {
                Route::Basic(route_basic) => {
                    assert_eq!(route_basic.method, Method::GET);
                    assert_eq!(route_basic.route, format!("/resources/{}", expected_stem));
                    assert_eq!(route_basic.sub_route, SubRoute::None);
                    assert!(!route_basic.is_protected);
                }
                _ => panic!("Expected Route::Basic for {}", filename),
            }
        }
    }

    #[test]
    fn test_try_parse_static_route_nested_paths() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "statistics.json");
        let route_params = RouteParams::new("/api/v1/reports", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/v1/reports/statistics");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic for nested static route"),
        }
    }

    #[test]
    fn test_try_parse_static_route_with_empty_parent() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "index.html");
        let route_params = RouteParams::new("", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/index");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic for static route with empty parent"),
        }
    }

    #[test]
    fn test_try_parse_static_route_no_extension() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "readme");
        let route_params = RouteParams::new("/docs", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/docs/readme");
                assert_eq!(route_basic.sub_route, SubRoute::None);
                assert!(!route_basic.is_protected);
            }
            _ => panic!("Expected Route::Basic for file without extension"),
        }
    }

    #[test]
    fn test_try_parse_static_route_vs_method_precedence() {
        let temp_dir = TempDir::new().unwrap();

        // Method files should still work as before
        let method_entry = create_test_file(temp_dir.path(), "get.json");
        let method_params = RouteParams::new("/api", &method_entry, false);
        let method_result = RouteBasic::try_parse(method_params);

        match method_result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api");
                assert_eq!(route_basic.sub_route, SubRoute::None);
            }
            _ => panic!("Expected Route::Basic for method file"),
        }

        // Non-method files should create static routes
        let static_entry = create_test_file(temp_dir.path(), "getconfig.json");
        let static_params = RouteParams::new("/api", &static_entry, false);
        let static_result = RouteBasic::try_parse(static_params);

        match static_result {
            Route::Basic(route_basic) => {
                assert_eq!(route_basic.method, Method::GET);
                assert_eq!(route_basic.route, "/api/getconfig");
                assert_eq!(route_basic.sub_route, SubRoute::None);
            }
            _ => panic!("Expected Route::Basic for static file"),
        }
    }

    #[test]
    fn test_try_parse_static_route_file_path_preservation() {
        let temp_dir = TempDir::new().unwrap();
        let entry = create_test_file(temp_dir.path(), "metadata.json");
        let route_params = RouteParams::new("/api", &entry, false);

        let result = RouteBasic::try_parse(route_params);

        match result {
            Route::Basic(route_basic) => {
                let expected_path = temp_dir.path().join("metadata.json").into_os_string();
                assert_eq!(route_basic.path, expected_path);
                assert_eq!(route_basic.route, "/api/metadata");
            }
            _ => panic!("Expected Route::Basic"),
        }
    }
}