feedparser_rs/http/
response.rs1use std::collections::HashMap;
2
3#[derive(Debug, Clone)]
5pub struct FeedHttpResponse {
6 pub status: u16,
8 pub url: String,
10 pub headers: HashMap<String, String>,
12 pub body: Vec<u8>,
14 pub etag: Option<String>,
16 pub last_modified: Option<String>,
18 pub content_type: Option<String>,
20 pub encoding: Option<String>,
22}
23
24impl FeedHttpResponse {
25 pub fn extract_charset_from_content_type(content_type: &str) -> Option<String> {
29 content_type.split(';').find_map(|part| {
30 part.trim()
31 .strip_prefix("charset=")
32 .map(|s| s.trim_matches('"').to_string())
33 })
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
42 fn test_extract_charset_simple() {
43 let ct = "text/xml; charset=utf-8";
44 assert_eq!(
45 FeedHttpResponse::extract_charset_from_content_type(ct),
46 Some("utf-8".to_string())
47 );
48 }
49
50 #[test]
51 fn test_extract_charset_quoted() {
52 let ct = "application/xml; charset=\"ISO-8859-1\"";
53 assert_eq!(
54 FeedHttpResponse::extract_charset_from_content_type(ct),
55 Some("ISO-8859-1".to_string())
56 );
57 }
58
59 #[test]
60 fn test_extract_charset_no_charset() {
61 let ct = "application/xml";
62 assert_eq!(
63 FeedHttpResponse::extract_charset_from_content_type(ct),
64 None
65 );
66 }
67
68 #[test]
69 fn test_extract_charset_multiple_params() {
70 let ct = "text/html; boundary=something; charset=utf-8";
71 assert_eq!(
72 FeedHttpResponse::extract_charset_from_content_type(ct),
73 Some("utf-8".to_string())
74 );
75 }
76}