more tests for content type

This commit is contained in:
Quentin 2023-07-22 12:14:37 +02:00
parent 0d4e472d41
commit d8363d70e8
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 36 additions and 97 deletions

View file

@ -1,97 +0,0 @@
use std::borrow::Cow;
use encoding_rs::Encoding;
use nom::{
branch::alt,
bytes::complete::{tag,take_while1},
character::complete as character,
combinator::{into, opt},
multi::many0,
sequence::{delimited, preceded, tuple},
IResult,
};
use crate::error::IMFError;
use crate::fragments::lazy;
use crate::fragments::whitespace::cfws;
use crate::fragments::quoted::quoted_string;
/*
impl<'a> From<&'a str> for MultipartSubtype<'a> {
fn from(csub: &'a str) -> Self {
match csub.to_lowercase().as_ref() {
"alternative" => MultipartSubtype::Alternative,
"mixed" => MultipartSubtype::Mixed,
"digest" => MultipartSubtype::Digest,
"parallel" => MultipartSubtype::Parallel,
"report" => MultipartSubtype::Report,
_ => MultipartSubtype::Other(csub),
}
}
}
impl<'a> From<&'a str> for MessageSubtype<'a> {
fn from(csub: &'a str) -> Self {
match csub.to_lowercase().as_ref() {
"rfc822" => MessageSubtype::RFC822,
"partial" => MessageSubtype::Partial,
"external" => MessageSubtype::External,
_ => MessageSubtype::Other(csub),
}
}
}
impl<'a> From<&'a str> for TextSubtype<'a> {
fn from(csub: &'a str) -> Self {
match csub.to_lowercase().as_ref() {
"html" => TextSubtype::Html,
"plain" => TextSubtype::Plain,
_ => TextSubtype::Other(csub),
}
}
}
*/
#[cfg(test)]
mod tests {
use super::*;
use crate::fragments::lazy;
#[test]
fn test_content_type_multipart() {
assert_eq!(
Type::try_from(&lazy::Type("multipart/mixed;\r\n\tboundary=\"--==_mimepart_64a3f2c69114f_2a13d020975fe\";\r\n\tcharset=UTF-8")),
Ok(Type::Multipart(MultipartDesc {
subtype: MultipartSubtype::Mixed,
boundary: "--==_mimepart_64a3f2c69114f_2a13d020975fe".into(),
unknown_parameters: vec![Parameter::Charset(EmailCharset::UTF_8)],
}))
);
}
#[test]
fn test_content_type_message() {
assert_eq!(
Type::try_from(&lazy::Type("message/rfc822")),
Ok(Type::Message(MessageDesc {
subtype: MessageSubtype::RFC822,
unknown_parameters: vec![],
}))
);
}
#[test]
fn test_parameter_ascii() {
assert_eq!(
parameter("charset=us-ascii (Plain text)"),
Ok(("", Parameter::Charset(EmailCharset::US_ASCII)))
);
}
}

View file

@ -195,4 +195,40 @@ mod tests {
}) })
); );
} }
#[test]
fn test_content_type_multipart() {
let (rest, nt) = naive_type(b"multipart/mixed;\r\n\tboundary=\"--==_mimepart_64a3f2c69114f_2a13d020975fe\";\r\n\tcharset=UTF-8").unwrap();
assert_eq!(rest, &[]);
assert_eq!(
nt.to_type(),
Type::Multipart(MultipartDesc {
subtype: MultipartSubtype::Mixed,
boundary: "--==_mimepart_64a3f2c69114f_2a13d020975fe".into(),
})
);
}
#[test]
fn test_content_type_message() {
let (rest, nt) = naive_type(b"message/rfc822").unwrap();
assert_eq!(rest, &[]);
assert_eq!(
nt.to_type(),
Type::Message(MessageSubtype::RFC822),
);
}
#[test]
fn test_parameter_ascii() {
assert_eq!(
parameter(b"charset = (simple) us-ascii (Plain text)"),
Ok((&b""[..], Parameter {
name: &b"charset"[..],
value: MIMEWord::Atom(&b"us-ascii"[..]),
}))
);
}
} }