use sitemap_xml_writer::{Lastmod, Loc, Sitemap, SitemapIndexWriter};
use std::io::Cursor;
#[test]
fn test_sitemap_index_writer_start_with_indent() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start_with_indent(Cursor::new(Vec::new()))?;
writer.write("http://www.example.com/sitemap1.xml.gz")?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = r#"
http://www.example.com/sitemap1.xml.gz
"#;
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_sitemap_index_writer_write_str() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write("http://www.example.com/sitemap1.xml.gz")?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_sitemap_index_writer_write_sitemap() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
.lastmod("2004-10-01T18:23:17+00:00")?,
)?;
writer.write(Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?.lastmod("2005-01-01")?)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
// Sample XML Sitemap Index in
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17+00:00"#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap2.xml.gz"#,
r#"2005-01-01"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[cfg(feature = "url")]
#[test]
fn test_sitemap_index_writer_write_sitemap_with_url_feature() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
.lastmod("2004-10-01T18:23:17+00:00")?,
)?;
writer.write(
// support
Sitemap::loc(::url::Url::parse("http://www.example.com/sitemap2.xml.gz")?)?
.lastmod("2005-01-01")?,
)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
// Sample XML Sitemap Index in
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17+00:00"#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap2.xml.gz"#,
r#"2005-01-01"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[cfg(feature = "chrono")]
#[test]
fn test_sitemap_index_writer_write_sitemap_with_chrono_feature() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
// `::chrono::DateTime` support
.lastmod(::chrono::DateTime::parse_from_rfc3339(
"2004-10-01T18:23:17+00:00",
)?)?,
)?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?
// `::chrono::NaiveDate` support
.lastmod(::chrono::NaiveDate::parse_from_str(
"2005-01-01",
"%Y-%m-%d",
)?)?,
)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
// Sample XML Sitemap Index in
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17+00:00"#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap2.xml.gz"#,
r#"2005-01-01"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[cfg(feature = "time")]
#[test]
fn test_sitemap_index_writer_write_sitemap_with_time_feature() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?
// `::time::OffsetDateTime` support
.lastmod(::time::macros::datetime!(2004-10-01 18:23:17 +00:00))?,
)?;
#[rustfmt::skip]
writer.write(
Sitemap::loc("http://www.example.com/sitemap2.xml.gz")?
// `::time::Date` support
.lastmod(::time::macros::date!(2005-01-01))?,
)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
// Sample XML Sitemap Index in
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17.000000000Z"#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap2.xml.gz"#,
r#"2005-01-01"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_sitemap_index_writer_write_sitemap_typed() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(
Sitemap::loc(Loc::try_from("http://www.example.com/sitemap1.xml.gz")?)?
.lastmod(Lastmod::try_from("2004-10-01T18:23:17+00:00")?)?,
)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#"2004-10-01T18:23:17+00:00"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_sitemap_index_writer_write_sitemap_loc_only() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
writer.write(Sitemap::loc("http://www.example.com/sitemap1.xml.gz")?)?;
writer.end()?;
let actual = String::from_utf8(writer.into_inner().into_inner())?;
let expected = concat!(
r#""#,
r#""#,
r#""#,
r#"http://www.example.com/sitemap1.xml.gz"#,
r#""#,
r#""#
);
assert_eq!(actual, expected);
Ok(())
}
#[test]
fn test_sitemap_index_writer_max_byte_length() -> anyhow::Result<()> {
let head_and_tail_length = concat!(
r#""#,
r#""#,
r#""#
)
.len();
let url = format!("http://www.example.com/{}", "x".repeat(1027));
let url_length = format!(r#"{}"#, url).len();
let url2 = format!("http://www.example.com/{}", "x".repeat(28));
let url2_length = format!(r#"{}"#, url2).len();
let url3 = format!("http://www.example.com/{}", "x".repeat(29));
let url3_length = format!(r#"{}"#, url3).len();
assert_eq!(head_and_tail_length, 119);
assert_eq!(url_length, 1_080);
assert_eq!(url2_length, 81);
assert_eq!(url3_length, 82);
// 119 + 1_080 * 48_545 = 52_428_719
// MAX_BYTE_LENGTH = 52_428_800
// 52_428_800 - 52_428_707 = 81
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
for _ in 0..48_545 {
writer.write(url.as_str())?;
}
writer.write(url2.as_str())?;
writer.end()?;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
for _ in 0..48_545 {
writer.write(url.as_str())?;
}
writer.write(url3.as_str())?;
assert!(writer.end().is_err());
Ok(())
}
#[test]
fn test_sitemap_index_writer_max_number_of_urls() -> anyhow::Result<()> {
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
for _ in 0..50_000 {
writer.write("http://www.example.com/sitemap1.xml.gz")?;
}
writer.end()?;
let mut writer = SitemapIndexWriter::start(Cursor::new(Vec::new()))?;
for _ in 0..50_000 {
writer.write("http://www.example.com/sitemap1.xml.gz")?;
}
assert!(writer
.write("http://www.example.com/sitemap1.xml.gz")
.is_err());
writer.end()?;
Ok(())
}