From cdaab95ed54765cdd70c61c39f9d2d19348472a9 Mon Sep 17 00:00:00 2001 From: Ana Gelez Date: Wed, 29 Jan 2020 21:48:09 +0100 Subject: [PATCH] Update deps + asyncify fetching --- Cargo.toml | 5 +++-- src/lib.rs | 17 ++++++++++------- src/tests.rs | 10 +++++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 319f47f..110dae8 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,9 +11,10 @@ license = "GPL-3.0" edition = "2018" [dependencies] -reqwest = "0.9" +reqwest = { version = "0.10", features = [ "json" ] } serde = { version = "1.0", features = [ "derive" ] } [dev-dependencies] serde_json = "1.0" -mockito = "0.16" +mockito = "0.22" +tokio = "0.2" diff --git a/src/lib.rs b/src/lib.rs index 7a9000d..8c72f11 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,7 +119,7 @@ pub fn url_for( } /// Fetches a WebFinger resource, identified by the `acct` parameter, a Webfinger URI. -pub fn resolve_with_prefix( +pub async fn resolve_with_prefix( prefix: Prefix, acct: impl Into, with_https: bool, @@ -129,27 +129,30 @@ pub fn resolve_with_prefix( .get(&url[..]) .header(ACCEPT, "application/jrd+json, application/json") .send() - .map_err(|_| WebfingerError::HttpError) - .and_then(|mut r| r.json().map_err(|_| WebfingerError::JsonError)) + .await + .map_err(|_| WebfingerError::HttpError)? + .json() + .await + .map_err(|_| WebfingerError::JsonError) } /// Fetches a Webfinger resource. /// /// If the resource doesn't have a prefix, `acct:` will be used. -pub fn resolve(acct: impl Into, with_https: bool) -> Result { +pub async fn resolve(acct: impl Into, with_https: bool) -> Result { let acct = acct.into(); let mut parsed = acct.splitn(2, ':'); let first = parsed.next().ok_or(WebfingerError::ParseError)?; if first.contains('@') { // This : was a port number, not a prefix - resolve_with_prefix(Prefix::Acct, acct, with_https) + resolve_with_prefix(Prefix::Acct, acct, with_https).await } else { if let Some(other) = parsed.next() { - resolve_with_prefix(Prefix::from(first), other, with_https) + resolve_with_prefix(Prefix::from(first), other, with_https).await } else { // fallback to acct: - resolve_with_prefix(Prefix::Acct, first, with_https) + resolve_with_prefix(Prefix::Acct, first, with_https).await } } } diff --git a/src/tests.rs b/src/tests.rs index 7dc6427..b1305ce 100755 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,5 +1,6 @@ use super::*; use serde_json; +use tokio::runtime::Runtime; #[test] fn test_url_for() { @@ -35,6 +36,7 @@ fn test_url_for() { #[test] fn test_resolve() { + let mut r = Runtime::new().unwrap(); let m = mockito::mock("GET", mockito::Matcher::Any) .with_body( r#" @@ -66,10 +68,12 @@ fn test_resolve() { let url = format!("test@{}", mockito::server_url()).replace("http://", ""); println!("{}", url); - let res = resolve(url, false).unwrap(); - assert_eq!(res.subject, String::from("acct:test@example.org")); + r.block_on(async { + let res = resolve(url, false).await.unwrap(); + assert_eq!(res.subject, String::from("acct:test@example.org")); - m.assert(); + m.assert(); + }); } #[test]