use tokio::io::{AsyncWrite, AsyncWriteExt};
const BASE_CAPABILITIES: &[u8] = b"*push\n*fetch\noption\n";
const BUNDLE_URI_LINE: &[u8] = b"bundle-uri\n";
const TERMINATOR: &[u8] = b"\n";
pub(crate) async fn handle_capabilities<W>(
writer: &mut W,
advertise_bundle_uri: bool,
) -> std::io::Result<()>
where
W: AsyncWrite + Unpin,
{
writer.write_all(BASE_CAPABILITIES).await?;
if advertise_bundle_uri {
writer.write_all(BUNDLE_URI_LINE).await?;
}
writer.write_all(TERMINATOR).await?;
writer.flush().await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn writes_base_capabilities_block_when_bundle_uri_disabled() {
let mut buf: Vec<u8> = Vec::new();
handle_capabilities(&mut buf, false).await.unwrap();
assert_eq!(&buf, b"*push\n*fetch\noption\n\n");
}
#[tokio::test]
async fn appends_bundle_uri_line_when_enabled() {
let mut buf: Vec<u8> = Vec::new();
handle_capabilities(&mut buf, true).await.unwrap();
assert_eq!(&buf, b"*push\n*fetch\noption\nbundle-uri\n\n");
}
}