use crate::xml;
use std::io::Write;
#[derive(Debug, Clone)]
pub struct PostResponse<'a> {
pub location: &'a str,
pub bucket: &'a str,
pub key: &'a str,
pub etag: &'a str,
}
impl xml::Serialize for PostResponse<'_> {
fn serialize<W: Write>(&self, s: &mut xml::Serializer<W>) -> xml::SerResult {
s.content("PostResponse", self)
}
}
impl xml::SerializeContent for PostResponse<'_> {
fn serialize_content<W: Write>(&self, s: &mut xml::Serializer<W>) -> xml::SerResult {
s.content("Location", self.location)?;
s.content("Bucket", self.bucket)?;
s.content("Key", self.key)?;
s.content("ETag", self.etag)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::xml::Serialize;
#[test]
fn test_post_response_xml() {
let response = PostResponse {
location: "/my-bucket/my-key",
bucket: "my-bucket",
key: "my-key",
etag: "\"abc123\"",
};
let mut buf = Vec::new();
let mut ser = xml::Serializer::new(&mut buf);
ser.decl().unwrap();
response.serialize(&mut ser).unwrap();
let xml_str = String::from_utf8(buf).unwrap();
assert!(xml_str.contains("<PostResponse>"));
assert!(xml_str.contains("<Location>/my-bucket/my-key</Location>"));
assert!(xml_str.contains("<Bucket>my-bucket</Bucket>"));
assert!(xml_str.contains("<Key>my-key</Key>"));
assert!(xml_str.contains("<ETag>"abc123"</ETag>"));
assert!(xml_str.contains("</PostResponse>"));
}
#[test]
fn test_post_response_xml_escaping() {
let response = PostResponse {
location: "/bucket/<test>",
bucket: "bucket",
key: "<test>&value",
etag: "\"etag\"",
};
let mut buf = Vec::new();
let mut ser = xml::Serializer::new(&mut buf);
response.serialize(&mut ser).unwrap();
let xml_str = String::from_utf8(buf).unwrap();
assert!(xml_str.contains("<test>"));
assert!(xml_str.contains("&value"));
}
}