use url::Url;
use crate::{Error, Response, Status};
pub(crate) async fn fetch(url: &Url) -> Result<Response, Error> {
let body = nex_protocol::fetch(url.as_str(), &Default::default())
.await
.map_err(map_error)?;
Ok(Response {
url: url.clone(),
status: Status::Success,
raw_status: None,
meta: String::new(),
body,
})
}
fn map_error(error: nex_protocol::ClientError) -> Error {
use nex_protocol::ClientError as Nex;
match error {
Nex::BadUrl(message) => Error::BadUrl(message),
Nex::Io(message) => Error::Io(message),
Nex::Timeout(_) => Error::Timeout,
Nex::BodyTooLarge { max } => Error::Protocol(format!("nex response exceeds {max} bytes")),
}
}
#[cfg(test)]
mod tests {
use crate::{Scheme, Status};
#[test]
fn nex_scheme_routes_to_port_1900() {
assert_eq!(Scheme::Nex.default_port(), 1900);
}
#[tokio::test]
#[ignore = "hits the live network; run with `cargo test -- --ignored`"]
async fn live_nex_smoke() {
let r = crate::fetch("nex://nightfall.city/")
.await
.expect("fetch nex root");
assert_eq!(r.status, Status::Success);
assert!(!r.body.is_empty());
}
}