silent 2.16.1

Silent Web Framework
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
use crate::core::req_body::ReqBody;
use crate::header::{CONTENT_TYPE, HeaderMap};
use crate::multer::{Field, Multipart};
use crate::{SilentError, StatusCode};
use async_fs::File;
use futures::io::AsyncWriteExt;
use multimap::MultiMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use tempfile::Builder;
use textnonce::TextNonce;

/// The extracted text fields and uploaded files from a `multipart/form-data` request.
#[derive(Debug)]
pub struct FormData {
    /// Name-value pairs for plain text fields. Technically, these are form data parts with no
    /// filename specified in the part's `Content-Disposition`.
    pub fields: MultiMap<String, String>,
    /// Name-value pairs for temporary files. Technically, these are form data parts with a filename
    /// specified in the part's `Content-Disposition`.
    #[cfg(feature = "server")]
    pub files: MultiMap<String, FilePart>,
}

impl FormData {
    /// Create new `FormData`.
    #[inline]
    pub fn new() -> FormData {
        FormData {
            fields: MultiMap::new(),
            #[cfg(feature = "server")]
            files: MultiMap::new(),
        }
    }

    /// Parse MIME `multipart/*` information from a stream as a [`FormData`].
    pub(crate) async fn read(headers: &HeaderMap, body: ReqBody) -> Result<FormData, SilentError> {
        let mut form_data = FormData::new();
        if let Some(boundary) = headers
            .get(CONTENT_TYPE)
            .and_then(|ct| ct.to_str().ok())
            .and_then(|ct| multer::parse_boundary(ct).ok())
        {
            let mut multipart = Multipart::new(body, boundary);
            while let Some(mut field) = multipart.next_field().await? {
                if let Some(name) = field.name().map(|s| s.to_owned()) {
                    if field.headers().get(CONTENT_TYPE).is_some() {
                        form_data
                            .files
                            .insert(name, FilePart::create(&mut field).await?);
                    } else {
                        form_data.fields.insert(name, field.text().await?);
                    }
                }
            }
        }
        Ok(form_data)
    }
}

impl Default for FormData {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

// A file that is to be inserted into a `multipart/*` or alternatively an uploaded file that
/// was received as part of `multipart/*` parsing.
#[derive(Clone, Debug)]
pub struct FilePart {
    name: Option<String>,
    /// The headers of the part
    headers: HeaderMap,
    /// A temporary file containing the file content
    path: PathBuf,
    /// Optionally, the size of the file.  This is filled when multiparts are parsed, but is
    /// not necessary when they are generated.
    size: u64,
    // The temporary directory the upload was put into, saved for the Drop trait
    temp_dir: Option<PathBuf>,
}

impl FilePart {
    /// Get file name.
    #[inline]
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }
    /// Get file name mutable reference.
    #[inline]
    pub fn name_mut(&mut self) -> Option<&mut String> {
        self.name.as_mut()
    }
    /// Get headers.
    #[inline]
    pub fn headers(&self) -> &HeaderMap {
        &self.headers
    }
    /// Get headers mutable reference.
    pub fn headers_mut(&mut self) -> &mut HeaderMap {
        &mut self.headers
    }
    /// Get file path.
    #[inline]
    pub fn path(&self) -> &PathBuf {
        &self.path
    }
    /// Get file size.
    #[inline]
    pub fn size(&self) -> u64 {
        self.size
    }
    /// If you do not want the file on disk to be deleted when Self drops, call this
    /// function.  It will become your responsibility to clean up.
    #[inline]
    pub fn do_not_delete_on_drop(&mut self) {
        self.temp_dir = None;
    }
    /// Save the file to a new location.
    #[inline]
    pub fn save(&self, path: String) -> Result<u64, SilentError> {
        std::fs::copy(self.path(), Path::new(&path)).map_err(|e| SilentError::BusinessError {
            code: StatusCode::INTERNAL_SERVER_ERROR,
            msg: format!("Failed to save file: {e}"),
        })
    }

    /// Create a new temporary FilePart (when created this way, the file will be
    /// deleted once the FilePart object goes out of scope).
    #[inline]
    pub async fn create(field: &mut Field<'_>) -> Result<FilePart, SilentError> {
        // Set up a file to capture the contents.
        let mut path = Builder::new()
            .prefix("silent_http_multipart")
            .tempdir()?
            .keep();
        let temp_dir = Some(path.clone());
        let name = field.file_name().map(|s| s.to_owned());
        path.push(format!(
            "{}.{}",
            TextNonce::sized_urlsafe(32)?.into_string(),
            name.as_deref()
                .and_then(|name| { Path::new(name).extension().and_then(OsStr::to_str) })
                .unwrap_or("unknown")
        ));
        let mut file = File::create(&path).await?;
        let mut size = 0;
        while let Some(chunk) = field.chunk().await? {
            size += chunk.len() as u64;
            file.write_all(&chunk).await?;
        }
        Ok(FilePart {
            name,
            headers: field.headers().to_owned(),
            path,
            size,
            temp_dir,
        })
    }
}

impl Drop for FilePart {
    fn drop(&mut self) {
        if let Some(temp_dir) = &self.temp_dir {
            let path = self.path.clone();
            let temp_dir = temp_dir.to_owned();
            std::thread::spawn(move || {
                let _ = std::fs::remove_file(&path);
                let _ = std::fs::remove_dir(temp_dir);
            });
        }
    }
}

#[cfg(all(test, feature = "server", feature = "multipart"))]
mod tests {
    use super::*;
    use crate::header::{HeaderMap, HeaderValue};
    use bytes::Bytes;

    // FormData 构造函数测试
    #[test]
    fn test_form_data_new() {
        let form_data = FormData::new();
        assert_eq!(form_data.fields.len(), 0);
        assert_eq!(form_data.files.len(), 0);
    }

    #[test]
    fn test_form_data_default() {
        let form_data = FormData::default();
        assert_eq!(form_data.fields.len(), 0);
        assert_eq!(form_data.files.len(), 0);
    }

    // FormData::read() 边界条件测试
    #[tokio::test]
    async fn test_form_data_read_no_content_type() {
        let headers = HeaderMap::new();
        let body = ReqBody::Once(Bytes::from("test data"));
        let result = FormData::read(&headers, body).await;
        assert!(result.is_ok());
        let form_data = result.unwrap();
        assert_eq!(form_data.fields.len(), 0);
        assert_eq!(form_data.files.len(), 0);
    }

    #[tokio::test]
    async fn test_form_data_read_empty_body() {
        let mut headers = HeaderMap::new();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("multipart/form-data; boundary=----WebKitFormBoundary"),
        );
        let body = ReqBody::Empty;
        let result = FormData::read(&headers, body).await;
        // 空的 multipart body 可能成功或失败,取决于底层实现
        // 这里验证至少不会 panic
        let _ = result;
    }

    #[tokio::test]
    async fn test_form_data_read_invalid_content_type() {
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        let body = ReqBody::Once(Bytes::from("test data"));
        let result = FormData::read(&headers, body).await;
        assert!(result.is_ok());
    }

    // FilePart getter 方法测试
    #[test]
    fn test_file_part_name() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.name(), Some("test_file.txt"));
    }

    #[test]
    fn test_file_part_name_none() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: None,
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.name(), None);
    }

    #[test]
    fn test_file_part_name_mut() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut file_part = FilePart {
            name: Some("old_name.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        if let Some(name) = file_part.name_mut() {
            *name = "new_name.txt".to_string();
        }
        assert_eq!(file_part.name(), Some("new_name.txt"));
    }

    #[test]
    fn test_file_part_headers() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut headers = HeaderMap::new();
        headers.insert("content-type", HeaderValue::from_static("text/plain"));

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: headers.clone(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(
            file_part.headers().get("content-type").unwrap(),
            "text/plain"
        );
    }

    #[test]
    fn test_file_part_headers_mut() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut headers = HeaderMap::new();
        headers.insert("content-type", HeaderValue::from_static("text/plain"));

        let mut file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers,
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        file_part
            .headers_mut()
            .insert("content-type", HeaderValue::from_static("application/json"));
        assert_eq!(
            file_part.headers().get("content-type").unwrap(),
            "application/json"
        );
    }

    #[test]
    fn test_file_part_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.path(), &file_path);
    }

    #[test]
    fn test_file_part_size() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 1024,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.size(), 1024);
    }

    // FilePart::save() 方法测试
    #[test]
    fn test_file_part_save() {
        let temp_dir = tempfile::tempdir().unwrap();
        let source_dir = tempfile::tempdir().unwrap();
        let source_path = source_dir.path().join("source.txt");
        std::fs::write(&source_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("source.txt".to_string()),
            headers: HeaderMap::new(),
            path: source_path.clone(),
            size: 12,
            temp_dir: Some(source_dir.path().to_path_buf()),
        };

        let dest_path = temp_dir.path().join("dest.txt");
        let result = file_part.save(dest_path.to_str().unwrap().to_string());
        assert!(result.is_ok());
        assert!(dest_path.exists());
        assert_eq!(std::fs::read_to_string(&dest_path).unwrap(), "test content");
    }

    #[test]
    fn test_file_part_save_invalid_path() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        // 尝试保存到无效路径
        let result = file_part.save("/nonexistent/directory/file.txt".to_string());
        assert!(result.is_err());
    }

    // FilePart::do_not_delete_on_drop() 测试
    #[test]
    fn test_file_part_do_not_delete_on_drop() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        // 调用 do_not_delete_on_drop 后,temp_dir 应该被设置为 None
        file_part.do_not_delete_on_drop();
        assert!(file_part.temp_dir.is_none());
    }

    // FilePart 内存布局测试
    #[test]
    fn test_file_part_size_and_alignment() {
        let size = std::mem::size_of::<FilePart>();
        let align = std::mem::align_of::<FilePart>();
        assert!(size > 0);
        assert!(align >= std::mem::align_of::<usize>());
    }

    #[test]
    fn test_file_part_clone() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        let cloned = file_part.clone();
        assert_eq!(cloned.name(), file_part.name());
        assert_eq!(cloned.path(), file_part.path());
        assert_eq!(cloned.size(), file_part.size());
    }

    // MultiMap 集成测试
    #[test]
    fn test_form_data_fields_multimap() {
        let mut form_data = FormData::new();
        form_data
            .fields
            .insert("username".to_string(), "alice".to_string());
        form_data
            .fields
            .insert("username".to_string(), "bob".to_string());

        // MultiMap 允许重复键
        let values = form_data.fields.get_vec("username").unwrap();
        assert_eq!(values.len(), 2);
        assert!(values.contains(&"alice".to_string()));
        assert!(values.contains(&"bob".to_string()));
    }

    #[test]
    fn test_form_data_files_multimap() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path1 = temp_dir.path().join("file1.txt");
        let file_path2 = temp_dir.path().join("file2.txt");
        std::fs::write(&file_path1, b"content1").unwrap();
        std::fs::write(&file_path2, b"content2").unwrap();

        let mut form_data = FormData::new();
        form_data.files.insert(
            "files".to_string(),
            FilePart {
                name: Some("file1.txt".to_string()),
                headers: HeaderMap::new(),
                path: file_path1,
                size: 8,
                temp_dir: Some(temp_dir.path().to_path_buf()),
            },
        );
        form_data.files.insert(
            "files".to_string(),
            FilePart {
                name: Some("file2.txt".to_string()),
                headers: HeaderMap::new(),
                path: file_path2,
                size: 8,
                temp_dir: Some(temp_dir.path().to_path_buf()),
            },
        );

        let files = form_data.files.get_vec("files").unwrap();
        assert_eq!(files.len(), 2);
    }

    // 边界条件和错误处理测试
    #[tokio::test]
    async fn test_form_data_read_malformed_boundary() {
        let mut headers = HeaderMap::new();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("multipart/form-data; boundary=unterminated"),
        );
        let body = ReqBody::Once(Bytes::from("------WebKitFormBoundary\r\n"));
        let result = FormData::read(&headers, body).await;
        // 应该处理格式错误的数据
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_file_part_zero_size() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("empty_file.txt");
        std::fs::write(&file_path, b"").unwrap();

        let file_part = FilePart {
            name: Some("empty_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 0,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.size(), 0);
        assert!(file_part.path().exists());
    }

    #[test]
    fn test_file_part_large_size() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("large_file.bin");
        let large_size = u64::MAX / 2;

        let file_part = FilePart {
            name: Some("large_file.bin".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: large_size,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.size(), large_size);
    }

    // FormData 和 FilePart 类型测试
    #[test]
    fn test_form_data_debug() {
        let form_data = FormData::new();
        let debug_str = format!("{:?}", form_data);
        assert!(debug_str.contains("FormData"));
    }

    #[test]
    fn test_file_part_debug() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        let debug_str = format!("{:?}", file_part);
        assert!(debug_str.contains("FilePart"));
    }

    // HeaderMap 集成测试
    #[test]
    fn test_file_part_with_custom_headers() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut headers = HeaderMap::new();
        headers.insert("content-type", HeaderValue::from_static("image/jpeg"));
        headers.insert(
            "content-disposition",
            HeaderValue::from_static("attachment"),
        );

        let file_part = FilePart {
            name: Some("photo.jpg".to_string()),
            headers: headers.clone(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.headers().len(), 2);
        assert_eq!(
            file_part.headers().get("content-type").unwrap(),
            "image/jpeg"
        );
        assert_eq!(
            file_part.headers().get("content-disposition").unwrap(),
            "attachment"
        );
    }

    // 文件路径处理测试
    #[test]
    fn test_file_part_path_with_special_characters() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_name = "test file-@#$.txt";
        let file_path = temp_dir.path().join(file_name);
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some(file_name.to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.path(), &file_path);
        assert_eq!(file_part.name(), Some(file_name));
    }

    // 临时目录管理测试
    #[test]
    fn test_file_part_temp_dir_none() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        // temp_dir 为 None 的 FilePart 不会被自动删除
        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: None,
        };

        assert!(file_part.temp_dir.is_none());
        assert!(file_path.exists());
    }

    #[test]
    fn test_file_part_temp_dir_some() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("test_file.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("test_file.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path.clone(),
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert!(file_part.temp_dir.is_some());
        assert_eq!(file_part.temp_dir.as_ref().unwrap(), temp_dir.path());
    }

    // 重复字段测试
    #[test]
    fn test_form_data_duplicate_fields() {
        let mut form_data = FormData::new();
        form_data
            .fields
            .insert("key".to_string(), "value1".to_string());
        form_data
            .fields
            .insert("key".to_string(), "value2".to_string());
        form_data
            .fields
            .insert("key".to_string(), "value3".to_string());

        let values = form_data.fields.get_vec("key").unwrap();
        assert_eq!(values.len(), 3);
    }

    // 文件名变更测试
    #[test]
    fn test_file_part_rename() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join("old_name.txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let mut file_part = FilePart {
            name: Some("old_name.txt".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        if let Some(name) = file_part.name_mut() {
            *name = "new_name.txt".to_string();
        }

        assert_eq!(file_part.name(), Some("new_name.txt"));
    }

    // 空文件名测试
    #[test]
    fn test_file_part_empty_filename() {
        let temp_dir = tempfile::tempdir().unwrap();
        let file_path = temp_dir.path().join(".txt");
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some("".to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.name(), Some(""));
    }

    // Unicode 文件名测试
    #[test]
    fn test_file_part_unicode_filename() {
        let temp_dir = tempfile::tempdir().unwrap();
        let unicode_name = "测试文件🎉.txt";
        let file_path = temp_dir.path().join(unicode_name);
        std::fs::write(&file_path, b"test content").unwrap();

        let file_part = FilePart {
            name: Some(unicode_name.to_string()),
            headers: HeaderMap::new(),
            path: file_path,
            size: 12,
            temp_dir: Some(temp_dir.path().to_path_buf()),
        };

        assert_eq!(file_part.name(), Some(unicode_name));
    }

    // 多字段组合测试
    #[test]
    fn test_form_data_multiple_fields_and_files() {
        let mut form_data = FormData::new();

        // 添加多个文本字段
        form_data
            .fields
            .insert("username".to_string(), "alice".to_string());
        form_data
            .fields
            .insert("email".to_string(), "alice@example.com".to_string());

        // 添加多个文件
        let temp_dir = tempfile::tempdir().unwrap();
        for i in 1..=3 {
            let file_path = temp_dir.path().join(format!("file{}.txt", i));
            std::fs::write(&file_path, format!("content{}", i)).unwrap();
            form_data.files.insert(
                format!("file{}", i),
                FilePart {
                    name: Some(format!("file{}.txt", i)),
                    headers: HeaderMap::new(),
                    path: file_path,
                    size: 8,
                    temp_dir: Some(temp_dir.path().to_path_buf()),
                },
            );
        }

        assert_eq!(form_data.fields.len(), 2);
        assert_eq!(form_data.files.len(), 3);
    }
}