Something went wrong. Try again.
A 5e storytelling engine with an LLM DM
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113//! Test-support fixtures shared by srd's own tests and by anything else//! in the crate that exercises `fetch`: a loopback HTTP server that//! serves one canned response, tarball builders for that server to//! serve, and a fixture `SrdMeta`.
use std::io::{Read, Write};use std::net::TcpListener;use std::thread::JoinHandle;
use super::fetch::SrdMeta;
/// Serves `body` once to the first connection it accepts, as a complete/// HTTP/1.1 response, then returns the URL to fetch it from.pub(crate) fn serve_once(body: Vec<u8>) -> (String, JoinHandle<()>) { let listener = TcpListener::bind("127.0.0.1:0").unwrap(); let addr = listener.local_addr().unwrap(); let handle = std::thread::spawn(move || { let (mut stream, _) = listener.accept().unwrap(); let mut request = [0u8; 1024]; let _ = stream.read(&mut request).unwrap(); let header = format!( "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len() ); stream.write_all(header.as_bytes()).unwrap(); stream.write_all(&body).unwrap(); }); (format!("http://{addr}/fixture.pdf"), handle)}
/// Appends a tar entry for `path` to `builder`, as a directory when/// `contents` is `None` or as a file with that content otherwise.pub(crate) fn append_entry( builder: &mut tar::Builder<Vec<u8>>, path: &str, contents: Option<&[u8]>,) { let mut header = tar::Header::new_gnu(); header.set_path(path).unwrap(); header.set_mode(0o644); match contents { Some(bytes) => { header.set_size(bytes.len() as u64); header.set_cksum(); builder.append(&header, bytes).unwrap(); } None => { header.set_size(0); header.set_entry_type(tar::EntryType::Directory); header.set_cksum(); builder.append(&header, std::io::empty()).unwrap(); } }}
/// Builds a tiny gzipped tarball with one top-level directory (`top_dir`,/// itself stored as a bare directory entry), shaped like the real/// dnd.srd.5.2.1 repo: a numbered content directory and a top-level/// licensing file that `should_vendor` keeps, alongside `.github/`,/// `docs_original/`, and an unlisted top-level file that it drops.pub(crate) fn tiny_tar_gz(top_dir: &str) -> Vec<u8> { let mut builder = tar::Builder::new(Vec::new()); append_entry(&mut builder, &format!("{top_dir}/"), None); append_entry(&mut builder, &format!("{top_dir}/07_Spells/"), None); append_entry( &mut builder, &format!("{top_dir}/07_Spells/Fireball.md"), Some(b"# Fireball\n"), ); append_entry( &mut builder, &format!("{top_dir}/Legal.md"), Some(b"CC-BY-4.0\n"), ); append_entry(&mut builder, &format!("{top_dir}/.github/"), None); append_entry( &mut builder, &format!("{top_dir}/.github/FUNDING.yml"), Some(b"github: []\n"), ); append_entry(&mut builder, &format!("{top_dir}/docs_original/"), None); append_entry( &mut builder, &format!("{top_dir}/docs_original/dnd_srd_5.2.1_cc.pdf"), Some(b"%PDF-fixture"), ); append_entry( &mut builder, &format!("{top_dir}/SRD-reForged.png"), Some(b"\x89PNG-fixture"), ); let tar_bytes = builder.into_inner().unwrap();
let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast()); encoder.write_all(&tar_bytes).unwrap(); encoder.finish().unwrap()}
/// A fixture `SrdMeta`, standing in for the pinned SRD 5.2.1 record with/// values a test can distinguish from the real thing at a glance.pub(crate) fn fixture_meta() -> SrdMeta { SrdMeta { version: "5.2.1".to_string(), pdf_path: "sources/SRD_CC_v5.2.1.pdf".to_string(), pdf_url: "https://example.test/srd.pdf".to_string(), pdf_sha256: "a".repeat(64), text_path: "sources/dnd.srd.5.2.1/".to_string(), text_url: "https://example.test/dnd.srd.5.2.1".to_string(), text_commit: "deadbeef".to_string(), text_tarball_sha256: "b".repeat(64), }}