Compare commits

..

No commits in common. "9abf1812a82e07390fdb07cc895df967bbf55a7a" and "707d6d3d543d9e51a7b03a52f5193681b366d062" have entirely different histories.

2 changed files with 41 additions and 115 deletions

View file

@ -2,43 +2,31 @@
//!
//! [RFC 6415]: https://datatracker.ietf.org/doc/html/rfc6415
use reqwest::header::HeaderValue;
use serde::Deserialize;
use serde::{Serialize, Deserialize};
/// A [host-meta document] in JRD representation.
/// A [host-meta document].
///
/// [host-meta document]: https://datatracker.ietf.org/doc/html/rfc6415
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct HostMetaDocument {
/// The resource this document refers to.
#[serde(alias = "Subject")]
pub subject: Option<String>,
/// Links established between the [`Self::subject`] and other resources.
#[serde(alias = "Link")]
pub links: Vec<HostMetaLink>,
}
/// A [host-meta Link Element], which puts the subject resource in relation with another.
///
/// [host-meta Link Element]: https://datatracker.ietf.org/doc/html/rfc6415#section-3.1.1
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostMetaLink {
/// The kind of relation established by the subject with the attached resource.
#[serde(alias = "@rel")]
pub rel: String,
/// The resource put in relation with the subject resource.
#[serde(alias = "@href")]
pub href: Option<String>,
/// Template to fill to get the resource put in relation with the subject resource.
#[serde(alias = "@template")]
pub template: Option<String>,
/// The `Content-Type` of the resource put in relation.
#[serde(alias = "@type")]
pub r#type: Option<String>,
pub href: String,
}
impl HostMetaDocument {
@ -72,21 +60,15 @@ impl HostMetaDocument {
.await
.map_err(Request)?;
log::trace!("Checking `Content-Type` of the response...");
let content_type = response
log::trace!("Checking headers of the response...");
response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.ok_or(ContentTypeMissing)?;
log::trace!("Extracting MIME type from the `Content-Type` header...");
let mime_type = extract_mime_from_content_type(content_type)
.get("Content-Type")
.ok_or(ContentTypeMissing)?
.eq("application/json")
.then_some(())
.ok_or(ContentTypeInvalid)?;
log::trace!("Ensuring MIME type of `{mime_type}` is acceptable for JRD parsing...");
if mime_type != "application/json" {
return Err(ContentTypeInvalid)
}
log::trace!("Attempting to parse response as JSON...");
let data = response.json::<Self>()
.await
@ -125,21 +107,15 @@ impl HostMetaDocument {
.await
.map_err(Request)?;
log::trace!("Checking `Content-Type` of the response...");
let content_type = response
log::trace!("Checking headers of the response...");
response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.ok_or(ContentTypeMissing)?;
log::trace!("Extracting MIME type from the `Content-Type` header...");
let mime_type = extract_mime_from_content_type(content_type)
.get("Content-Type")
.ok_or(ContentTypeMissing)?
.eq("application/xrd+json")
.then_some(())
.ok_or(ContentTypeInvalid)?;
log::trace!("Ensuring MIME type of `{mime_type}` is acceptable for JRD parsing...");
if mime_type != "application/xrd+xml" {
return Err(ContentTypeInvalid)
}
log::trace!("Attempting to parse response as text...");
let data = response.text()
.await
@ -326,13 +302,4 @@ pub enum HostMetaGetJRDError {
ContentTypeInvalid,
/// The document failed to be parsed as JSON by [`reqwest`].
Parse(reqwest::Error),
}
/// Extract the MIME type from the value of the `Content-Type` header.
fn extract_mime_from_content_type(value: &HeaderValue) -> Option<String> {
let value = value.to_str().ok()?;
match value.split_once("; ") {
None => Some(value.to_string()),
Some((mime, _)) => Some(mime.to_string()),
}
}
}

View file

@ -1,67 +1,26 @@
fn make_client() -> reqwest::Client {
let crate_name = env!("CARGO_PKG_NAME");
let crate_version = env!("CARGO_PKG_VERSION");
let crate_repository = env!("CARGO_PKG_REPOSITORY");
let user_agent = format!("{crate_name}/{crate_version} ({crate_repository})");
reqwest::Client::builder()
.user_agent(user_agent)
.build()
.expect("reqwest client to build")
#![allow(non_snake_case)]
use acrate_nodeinfo::*;
#[tokio::test]
async fn discover_hostmeta__junimo_party() {
let client = reqwest::Client::new();
let base: reqwest::Url = "https://junimo.party".parse()
.expect("a valid URL");
HostMetaDocument::discover_hostmeta(&client, base)
.await
.expect("host-meta discovery to succeed");
}
macro_rules! test {
($id:ident, $url:literal) => {
test!($id, $url,);
};
($id:ident, $url:literal, $($tag:meta),*) => {
mod $id {
use acrate_nodeinfo::*;
use super::*;
#[tokio::test]
async fn discover_nodeinfo__junimo_party() {
let client = reqwest::Client::new();
let base: reqwest::Url = "https://junimo.party".parse()
.expect("a valid URL");
#[tokio::test]
$(#[$tag])*
async fn test_hostmeta() {
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
let doc = HostMetaDocument::discover_hostmeta(&client, base)
.await
.expect("host-meta discovery to succeed");
println!("{doc:#?}");
}
#[tokio::test]
$(#[$tag])*
async fn test_nodeinfo() {
let client = make_client();
let base: reqwest::Url = $url.parse()
.expect("a valid URL");
let doc = HostMetaDocument::discover_nodeinfo(&client, base)
.await
.expect("host-meta discovery to succeed");
println!("{doc:#?}");
}
}
};
HostMetaDocument::discover_nodeinfo(&client, base)
.await
.expect("nodeinfo discovery to succeed");
}
test!(akkoma, "https://junimo.party");
test!(mastodon, "https://mastodon.social");
test!(misskey, "https://misskey.io");
test!(iceshrimpnet, "https://ice.frieren.quest");
test!(gotosocial, "https://alpha.polymaths.social");
test!(bridgyfed, "https://fed.brid.gy", ignore = "Returns application/jrd+json");
test!(threads, "https://threads.net", ignore = "Not implemented on their end");