//! test plumbing the backfill test modules share
use std::net::SocketAddr;
use axum::Router;
use crate::backfill::client::ThrottledHttpClient;
use crate::util::throttle::Throttler;
/// serves `app` on a free local port and says where
pub(crate) async fn serve(app: Router) -> SocketAddr {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
address
}
/// a client that reaches the mock pds at `address` through a public hostname, since the
/// backfill client refuses private addresses. returns that hostname's url and the client
pub(crate) fn public_client(
address: SocketAddr,
throttler: &Throttler,
) -> (url::Url, ThrottledHttpClient) {
let client = reqwest::Client::builder()
.resolve("pds.example", address)
.build()
.unwrap();
let http = ThrottledHttpClient::new(
crate::net::PublicHttpClient::test(client),
throttler.clone(),
);
(url::Url::parse("http://pds.example/").unwrap(), http)
}
/// a `sh.tangled.repo` record called `name`
pub(crate) fn tangled_repo(name: &str) -> Vec<u8> {
serde_ipld_dagcbor::to_vec(&serde_json::json!({
"$type": "sh.tangled.repo",
"name": name,
}))
.unwrap()
}