mockforge-core 0.3.116

Shared logic for MockForge - routing, validation, latency, proxy
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
//! Postman collection import functionality
//!
//! This module handles parsing Postman collections and converting them
//! to MockForge routes and configurations.

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;

/// Postman collection structure
#[derive(Debug, Deserialize)]
pub struct PostmanCollection {
    /// Collection metadata
    pub info: CollectionInfo,
    /// Array of collection items (requests or folders)
    pub item: Vec<CollectionItem>,
    /// Collection-level variables
    #[serde(default)]
    pub variable: Vec<Variable>,
}

/// Collection metadata
#[derive(Debug, Deserialize)]
pub struct CollectionInfo {
    /// Postman collection ID
    #[serde(rename = "_postman_id")]
    pub postman_id: Option<String>,
    /// Collection name
    pub name: String,
    /// Optional collection description
    pub description: Option<String>,
    /// Postman schema URL
    pub schema: Option<String>,
}

/// Collection item (can be a request or a folder)
#[derive(Debug, Deserialize)]
pub struct CollectionItem {
    /// Item name
    pub name: String,
    /// Child items (for folders)
    #[serde(default)]
    pub item: Vec<CollectionItem>, // For folders
    /// Request data (None if this is a folder)
    pub request: Option<PostmanRequest>,
}

/// Postman request structure
#[derive(Debug, Deserialize)]
pub struct PostmanRequest {
    /// HTTP method (GET, POST, PUT, etc.)
    pub method: String,
    /// Request headers
    #[serde(default)]
    pub header: Vec<Header>,
    /// Request URL (can be string or structured)
    pub url: UrlOrString,
    /// Optional request body
    #[serde(default)]
    pub body: Option<RequestBody>,
    /// Optional authentication configuration
    pub auth: Option<Auth>,
}

/// URL can be a string or structured object
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum UrlOrString {
    /// Simple string URL
    String(String),
    /// Structured URL with components
    Structured(StructuredUrl),
}

/// Structured URL with host, path, query, etc.
#[derive(Debug, Deserialize)]
pub struct StructuredUrl {
    /// Raw URL string
    pub raw: Option<String>,
    /// URL protocol (http, https, etc.)
    pub protocol: Option<String>,
    /// URL host parts
    pub host: Option<Vec<String>>,
    /// URL path segments
    pub path: Option<Vec<StringOrVariable>>,
    /// Query parameters
    #[serde(default)]
    pub query: Vec<QueryParam>,
    /// URL variables
    #[serde(default)]
    pub variable: Vec<Variable>,
}

/// Variable or string in URL components
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum StringOrVariable {
    /// Plain string value
    String(String),
    /// Variable reference
    Variable(Variable),
}

/// Query parameter
#[derive(Debug, Deserialize)]
pub struct QueryParam {
    /// Parameter key
    pub key: Option<String>,
    /// Parameter value
    pub value: Option<String>,
    /// Optional parameter description
    pub description: Option<String>,
    /// Whether this parameter is disabled
    #[serde(default)]
    pub disabled: bool,
}

/// HTTP header entry
#[derive(Debug, Deserialize)]
pub struct Header {
    /// Header name
    pub key: String,
    /// Header value
    pub value: String,
    /// Whether this header is disabled
    #[serde(default)]
    pub disabled: bool,
}

/// Request body structure
#[derive(Debug, Deserialize)]
pub struct RequestBody {
    /// Body mode (raw, urlencoded, formdata, etc.)
    pub mode: String,
    /// Raw body content (for raw mode)
    pub raw: Option<String>,
    /// URL-encoded form parameters
    pub urlencoded: Option<Vec<FormParam>>,
    /// Form data parameters
    pub formdata: Option<Vec<FormParam>>,
}

/// Form parameter entry
#[derive(Debug, Deserialize)]
pub struct FormParam {
    /// Parameter key
    pub key: String,
    /// Parameter value
    pub value: String,
    /// Parameter type (text, file, etc.)
    #[serde(rename = "type")]
    pub param_type: Option<String>,
}

/// Authentication configuration
#[derive(Debug, Deserialize)]
pub struct Auth {
    /// Authentication type (bearer, basic, etc.)
    #[serde(rename = "type")]
    pub auth_type: String,
    /// Authentication configuration (type-specific)
    #[serde(flatten)]
    pub config: Value,
}

/// Postman variable definition
#[derive(Debug, Deserialize)]
pub struct Variable {
    /// Variable name
    pub key: String,
    /// Variable value
    pub value: Option<String>,
    /// Variable type (string, number, etc.)
    #[serde(rename = "type")]
    pub var_type: Option<String>,
}

/// MockForge route structure for import
#[derive(Debug, Serialize)]
pub struct MockForgeRoute {
    /// HTTP method
    pub method: String,
    /// Request path
    pub path: String,
    /// Request headers
    pub headers: HashMap<String, String>,
    /// Optional request body
    pub body: Option<String>,
    /// Mock response for this route
    pub response: MockForgeResponse,
}

/// MockForge response structure
#[derive(Debug, Serialize)]
pub struct MockForgeResponse {
    /// HTTP status code
    pub status: u16,
    /// Response headers
    pub headers: HashMap<String, String>,
    /// Response body
    pub body: Value,
}

/// Result of importing a Postman collection
pub struct ImportResult {
    /// Converted routes from the collection
    pub routes: Vec<MockForgeRoute>,
    /// Extracted variables from the collection
    pub variables: HashMap<String, String>,
    /// Warnings encountered during import
    pub warnings: Vec<String>,
}

/// Import a Postman collection
pub fn import_postman_collection(
    content: &str,
    base_url: Option<&str>,
) -> Result<ImportResult, String> {
    let collection: PostmanCollection = serde_json::from_str(content)
        .map_err(|e| format!("Failed to parse Postman collection: {}", e))?;

    let mut routes = Vec::new();
    let mut variables = HashMap::new();
    let mut warnings = Vec::new();

    // Extract global variables
    for var in &collection.variable {
        if let Some(value) = &var.value {
            variables.insert(var.key.clone(), value.clone());
        }
    }

    // Process all items (recursive for folders)
    process_items(&collection.item, &mut routes, &variables, base_url, &mut warnings);

    Ok(ImportResult {
        routes,
        variables,
        warnings,
    })
}

/// Recursively process collection items
fn process_items(
    items: &[CollectionItem],
    routes: &mut Vec<MockForgeRoute>,
    variables: &HashMap<String, String>,
    base_url: Option<&str>,
    warnings: &mut Vec<String>,
) {
    for item in items {
        // Check if this item has a request (flattened fields)
        if item.request.is_some() {
            if let Some(request) = &item.request {
                // This is a request
                match convert_request_to_route(request, &item.name, variables, base_url) {
                    Ok(route) => routes.push(route),
                    Err(e) => {
                        warnings.push(format!("Failed to convert request '{}': {}", item.name, e))
                    }
                }
            }
        } else if !item.item.is_empty() {
            // This is a folder, process recursively
            process_items(&item.item, routes, variables, base_url, warnings);
        }
    }
}

/// Convert a Postman request to a MockForge route
fn convert_request_to_route(
    request: &PostmanRequest,
    _name: &str,
    variables: &HashMap<String, String>,
    base_url: Option<&str>,
) -> Result<MockForgeRoute, String> {
    // Build URL
    let url = build_url(&request.url, variables, base_url)?;

    // Extract headers
    let mut headers = HashMap::new();
    for header in &request.header {
        if !header.disabled && !header.key.is_empty() {
            headers.insert(header.key.clone(), resolve_variables(&header.value, variables));
        }
    }

    // Extract body
    let body = match &request.body {
        Some(body) if body.mode == "raw" => {
            body.raw.as_ref().map(|raw| resolve_variables(raw, variables))
        }
        Some(body) if body.mode == "urlencoded" => {
            if let Some(form_params) = &body.urlencoded {
                let encoded_params: Vec<String> = form_params
                    .iter()
                    .map(|param| {
                        let key = resolve_variables(&param.key, variables);
                        let value = resolve_variables(&param.value, variables);
                        format!("{}={}", key, value)
                    })
                    .collect();
                if encoded_params.is_empty() {
                    None
                } else {
                    Some(encoded_params.join("&"))
                }
            } else {
                None
            }
        }
        Some(body) if body.mode == "formdata" => {
            if let Some(form_params) = &body.formdata {
                let encoded_params: Vec<String> = form_params
                    .iter()
                    .map(|param| {
                        let key = resolve_variables(&param.key, variables);
                        let value = resolve_variables(&param.value, variables);
                        format!("{}={}", key, value)
                    })
                    .collect();
                if encoded_params.is_empty() {
                    None
                } else {
                    Some(encoded_params.join("&"))
                }
            } else {
                None
            }
        }
        _ => None,
    };

    // Generate mock response
    let response = generate_mock_response(request, variables);

    Ok(MockForgeRoute {
        method: request.method.clone(),
        path: url,
        headers,
        body,
        response,
    })
}

/// Build URL from Postman URL structure
fn build_url(
    url: &UrlOrString,
    variables: &HashMap<String, String>,
    base_url: Option<&str>,
) -> Result<String, String> {
    let raw_url = match url {
        UrlOrString::String(s) => resolve_variables(s, variables),
        UrlOrString::Structured(structured) => {
            if let Some(raw) = &structured.raw {
                resolve_variables(raw, variables)
            } else {
                // Build URL from components
                let mut url_parts = Vec::new();

                // Protocol
                if let Some(protocol) = &structured.protocol {
                    url_parts.push(format!("{}://", protocol));
                }

                // Host
                if let Some(host_parts) = &structured.host {
                    let host = host_parts.join(".");
                    url_parts.push(resolve_variables(&host, variables));
                }

                // Path
                if let Some(path_parts) = &structured.path {
                    let path: Vec<String> = path_parts
                        .iter()
                        .map(|part| match part {
                            StringOrVariable::String(s) => resolve_variables(s, variables),
                            StringOrVariable::Variable(var) => {
                                if let Some(value) = variables.get(&var.key) {
                                    value.clone()
                                } else {
                                    var.key.clone()
                                }
                            }
                        })
                        .collect();
                    url_parts.push(path.join("/"));
                }

                // Query
                let query_parts: Vec<String> = structured
                    .query
                    .iter()
                    .filter(|q| !q.disabled && q.key.is_some())
                    .map(|q| {
                        let key = resolve_variables(q.key.as_ref().unwrap(), variables);
                        let value = q
                            .value
                            .as_ref()
                            .map(|v| resolve_variables(v, variables))
                            .unwrap_or_default();
                        format!("{}={}", key, value)
                    })
                    .collect();

                if !query_parts.is_empty() {
                    url_parts.push(format!("?{}", query_parts.join("&")));
                }

                url_parts.join("")
            }
        }
    };

    // If base_url is provided, make path relative
    if let Some(base) = base_url {
        if raw_url.starts_with(base) {
            let relative_path = raw_url.trim_start_matches(base).trim_start_matches('/');
            return Ok(if relative_path.is_empty() {
                "/".to_string()
            } else {
                format!("/{}", relative_path)
            });
        }
    }

    // Extract path from full URL
    if let Ok(url) = url::Url::parse(&raw_url) {
        Ok(url.path().to_string())
    } else {
        // Assume it's already a path
        Ok(raw_url)
    }
}

/// Resolve variables in a string
fn resolve_variables(input: &str, variables: &HashMap<String, String>) -> String {
    let mut result = input.to_string();
    for (key, value) in variables {
        let pattern = format!("{{{{{}}}}}", key);
        result = result.replace(&pattern, value);
    }
    result
}

/// Generate a mock response for the request
fn generate_mock_response(
    request: &PostmanRequest,
    _variables: &HashMap<String, String>,
) -> MockForgeResponse {
    let mut headers = HashMap::new();
    headers.insert("Content-Type".to_string(), "application/json".to_string());

    let body = match request.method.as_str() {
        "GET" => json!({"message": "Mock GET response", "method": "GET"}),
        "POST" => json!({"message": "Mock POST response", "method": "POST", "created": true}),
        "PUT" => json!({"message": "Mock PUT response", "method": "PUT", "updated": true}),
        "DELETE" => json!({"message": "Mock DELETE response", "method": "DELETE", "deleted": true}),
        "PATCH" => json!({"message": "Mock PATCH response", "method": "PATCH", "patched": true}),
        _ => json!({"message": "Mock response", "method": &request.method}),
    };

    MockForgeResponse {
        status: 200,
        headers,
        body,
    }
}

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

    #[test]
    fn test_parse_postman_collection() {
        let collection_json = r#"{
            "info": {
                "_postman_id": "test-id",
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Get Users",
                    "request": {
                        "method": "GET",
                        "header": [{"key": "Authorization", "value": "Bearer {{token}}"}],
                        "url": {"raw": "{{baseUrl}}/users"}
                    }
                }
            ],
            "variable": [
                {"key": "baseUrl", "value": "https://api.example.com"},
                {"key": "token", "value": "test-token"}
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/users");
        assert!(result.routes[0].headers.contains_key("Authorization"));
    }

    #[test]
    fn test_parse_postman_collection_with_multiple_requests() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Get Users",
                    "request": {
                        "method": "GET",
                        "header": [],
                        "url": "https://api.example.com/users"
                    }
                },
                {
                    "name": "Create User",
                    "request": {
                        "method": "POST",
                        "header": [{"key": "Content-Type", "value": "application/json"}],
                        "url": "https://api.example.com/users",
                        "body": {
                            "mode": "raw",
                            "raw": "{\"name\": \"John\", \"age\": 30}"
                        }
                    }
                },
                {
                    "name": "Update User",
                    "request": {
                        "method": "PUT",
                        "header": [{"key": "Content-Type", "value": "application/json"}],
                        "url": "https://api.example.com/users/123",
                        "body": {
                            "mode": "raw",
                            "raw": "{\"name\": \"Jane\"}"
                        }
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 3);

        // Check first route (GET)
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/users");

        // Check second route (POST)
        assert_eq!(result.routes[1].method, "POST");
        assert_eq!(result.routes[1].path, "/users");
        assert_eq!(result.routes[1].body, Some("{\"name\": \"John\", \"age\": 30}".to_string()));
        assert_eq!(
            result.routes[1].headers.get("Content-Type"),
            Some(&"application/json".to_string())
        );

        // Check third route (PUT)
        assert_eq!(result.routes[2].method, "PUT");
        assert_eq!(result.routes[2].path, "/users/123");
        assert_eq!(result.routes[2].body, Some("{\"name\": \"Jane\"}".to_string()));
    }

    #[test]
    fn test_parse_postman_collection_with_folders() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "User Operations",
                    "item": [
                        {
                            "name": "Get Users",
                            "request": {
                                "method": "GET",
                                "header": [],
                                "url": "https://api.example.com/users"
                            }
                        },
                        {
                            "name": "Create User",
                            "request": {
                                "method": "POST",
                                "header": [],
                                "url": "https://api.example.com/users"
                            }
                        }
                    ]
                },
                {
                    "name": "Admin Operations",
                    "item": [
                        {
                            "name": "Get Stats",
                            "request": {
                                "method": "GET",
                                "header": [],
                                "url": "https://api.example.com/admin/stats"
                            }
                        }
                    ]
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 3);

        // Check routes are created from nested items
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/users");

        assert_eq!(result.routes[1].method, "POST");
        assert_eq!(result.routes[1].path, "/users");

        assert_eq!(result.routes[2].method, "GET");
        assert_eq!(result.routes[2].path, "/admin/stats");
    }

    #[test]
    fn test_parse_postman_collection_with_query_parameters() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Search Users",
                    "request": {
                        "method": "GET",
                        "header": [],
                        "url": {
                            "raw": "https://api.example.com/search?q=test&page=1&limit=10",
                            "host": ["api", "example", "com"],
                            "path": ["search"],
                            "query": [
                                {"key": "q", "value": "test"},
                                {"key": "page", "value": "1"},
                                {"key": "limit", "value": "10"}
                            ]
                        }
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/search?q=test&page=1&limit=10");
    }

    #[test]
    fn test_parse_postman_collection_with_different_methods() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {"name": "GET Request", "request": {"method": "GET", "url": "https://api.example.com/get"}},
                {"name": "POST Request", "request": {"method": "POST", "url": "https://api.example.com/post"}},
                {"name": "PUT Request", "request": {"method": "PUT", "url": "https://api.example.com/put"}},
                {"name": "DELETE Request", "request": {"method": "DELETE", "url": "https://api.example.com/delete"}},
                {"name": "PATCH Request", "request": {"method": "PATCH", "url": "https://api.example.com/patch"}},
                {"name": "HEAD Request", "request": {"method": "HEAD", "url": "https://api.example.com/head"}},
                {"name": "OPTIONS Request", "request": {"method": "OPTIONS", "url": "https://api.example.com/options"}}
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 7);

        let expected_methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
        for (i, expected_method) in expected_methods.iter().enumerate() {
            assert_eq!(result.routes[i].method, *expected_method);
            assert_eq!(result.routes[i].path, format!("/{}", expected_method.to_lowercase()));
        }
    }

    #[test]
    fn test_parse_postman_collection_with_form_data() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Form Submit",
                    "request": {
                        "method": "POST",
                        "header": [{"key": "Content-Type", "value": "application/x-www-form-urlencoded"}],
                        "url": "https://api.example.com/form",
                        "body": {
                            "mode": "urlencoded",
                            "urlencoded": [
                                {"key": "username", "value": "john_doe"},
                                {"key": "password", "value": "secret123"},
                                {"key": "remember", "value": "true"}
                            ]
                        }
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "POST");
        assert_eq!(result.routes[0].path, "/form");
        assert_eq!(
            result.routes[0].body,
            Some("username=john_doe&password=secret123&remember=true".to_string())
        );
    }

    #[test]
    fn test_parse_postman_collection_with_raw_body() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "JSON Post",
                    "request": {
                        "method": "POST",
                        "header": [{"key": "Content-Type", "value": "application/json"}],
                        "url": "https://api.example.com/json",
                        "body": {
                            "mode": "raw",
                            "raw": "{\"message\": \"Hello World\", \"data\": {\"key\": \"value\"}}"
                        }
                    }
                },
                {
                    "name": "XML Post",
                    "request": {
                        "method": "POST",
                        "header": [{"key": "Content-Type", "value": "application/xml"}],
                        "url": "https://api.example.com/xml",
                        "body": {
                            "mode": "raw",
                            "raw": "<root><message>Hello</message><data><key>value</key></data></root>"
                        }
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 2);

        // Check JSON request
        assert_eq!(result.routes[0].method, "POST");
        assert_eq!(result.routes[0].path, "/json");
        assert_eq!(
            result.routes[0].body,
            Some("{\"message\": \"Hello World\", \"data\": {\"key\": \"value\"}}".to_string())
        );

        // Check XML request
        assert_eq!(result.routes[1].method, "POST");
        assert_eq!(result.routes[1].path, "/xml");
        assert_eq!(
            result.routes[1].body,
            Some("<root><message>Hello</message><data><key>value</key></data></root>".to_string())
        );
    }

    #[test]
    fn test_parse_postman_collection_with_auth() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Protected Request",
                    "request": {
                        "method": "GET",
                        "header": [
                            {"key": "Authorization", "value": "Bearer {{token}}"},
                            {"key": "X-API-Key", "value": "api-key-123"}
                        ],
                        "url": "https://api.example.com/protected",
                        "auth": {
                            "type": "bearer",
                            "bearer": [
                                {"key": "token", "value": "{{token}}", "type": "string"}
                            ]
                        }
                    }
                }
            ],
            "variable": [
                {"key": "token", "value": "test-token-abc"}
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/protected");
        assert_eq!(
            result.routes[0].headers.get("Authorization"),
            Some(&"Bearer test-token-abc".to_string())
        );
        assert_eq!(result.routes[0].headers.get("X-API-Key"), Some(&"api-key-123".to_string()));
    }

    #[test]
    fn test_parse_postman_collection_with_variables() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Variable Test",
                    "request": {
                        "method": "GET",
                        "header": [
                            {"key": "X-User-ID", "value": "{{userId}}"},
                            {"key": "X-Environment", "value": "{{environment}}"}
                        ],
                        "url": "{{baseUrl}}/test/{{userId}}?env={{environment}}"
                    }
                }
            ],
            "variable": [
                {"key": "baseUrl", "value": "https://api.example.com"},
                {"key": "userId", "value": "12345"},
                {"key": "environment", "value": "production"}
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/test/12345?env=production");
        assert_eq!(result.routes[0].headers.get("X-User-ID"), Some(&"12345".to_string()));
        assert_eq!(result.routes[0].headers.get("X-Environment"), Some(&"production".to_string()));
    }

    #[test]
    fn test_parse_postman_collection_with_disabled_items() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Enabled Request",
                    "request": {
                        "method": "GET",
                        "url": "https://api.example.com/enabled"
                    }
                },
                {
                    "name": "Disabled Request",
                    "request": {
                        "method": "GET",
                        "url": "https://api.example.com/disabled"
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        // All requests should be imported regardless of disabled status in Postman
        // (disabled in Postman means not executed during collection run, not that it's invalid)
        assert_eq!(result.routes.len(), 2);
    }

    #[test]
    fn test_parse_postman_collection_with_complex_headers() {
        let collection_json = r#"{
            "info": {
                "name": "Test Collection",
                "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
            },
            "item": [
                {
                    "name": "Complex Headers",
                    "request": {
                        "method": "GET",
                        "header": [
                            {"key": "Authorization", "value": "Bearer token123"},
                            {"key": "Content-Type", "value": "application/json"},
                            {"key": "Accept", "value": "application/json"},
                            {"key": "X-Custom-Header", "value": "custom-value"},
                            {"key": "X-Request-ID", "value": "req-123"},
                            {"key": "User-Agent", "value": "PostmanRuntime/7.29.0"},
                            {"key": "Cache-Control", "value": "no-cache"}
                        ],
                        "url": "https://api.example.com/complex"
                    }
                }
            ]
        }"#;

        let result =
            import_postman_collection(collection_json, Some("https://api.example.com")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/complex");

        // Check that all headers are preserved
        let headers = &result.routes[0].headers;
        assert_eq!(headers.get("Authorization"), Some(&"Bearer token123".to_string()));
        assert_eq!(headers.get("Content-Type"), Some(&"application/json".to_string()));
        assert_eq!(headers.get("Accept"), Some(&"application/json".to_string()));
        assert_eq!(headers.get("X-Custom-Header"), Some(&"custom-value".to_string()));
        assert_eq!(headers.get("X-Request-ID"), Some(&"req-123".to_string()));
        assert_eq!(headers.get("User-Agent"), Some(&"PostmanRuntime/7.29.0".to_string()));
        assert_eq!(headers.get("Cache-Control"), Some(&"no-cache".to_string()));
    }
}