From 0ca0eefaa44fe36a8cb77acecb52c9f87bff0d88 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Tue, 6 Jun 2023 15:28:36 -0400 Subject: [PATCH] Add tests to check fetching sub/user/oauth --- src/client.rs | 4 ++-- src/oauth.rs | 11 +++++++++++ src/subreddit.rs | 5 +++++ src/user.rs | 7 +++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index fad981e..861b210 100644 --- a/src/client.rs +++ b/src/client.rs @@ -137,13 +137,13 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo let client: client::Client<_, hyper::Body> = CLIENT.clone(); let (token, vendor_id, device_id, user_agent, loid) = { - let client = OAUTH_CLIENT.blocking_read(); + let client = tokio::task::block_in_place(move || OAUTH_CLIENT.blocking_read()); ( client.token.clone(), client.headers_map.get("Client-Vendor-Id").unwrap().clone(), client.headers_map.get("X-Reddit-Device-Id").unwrap().clone(), client.headers_map.get("User-Agent").unwrap().clone(), - client.headers_map.get("x-reddit-loid").unwrap().clone(), + client.headers_map.get("x-reddit-loid").cloned().unwrap_or_default(), ) }; // Build request to Reddit. When making a GET, request gzip compression. diff --git a/src/oauth.rs b/src/oauth.rs index a600d88..7851e07 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -208,3 +208,14 @@ impl Device { } } } + +#[tokio::test] +async fn test_oauth_client() { + initialize().await; +} + +#[tokio::test] +async fn test_oauth_client_refresh() { + initialize().await; + OAUTH_CLIENT.write().await.refresh().await.unwrap(); +} diff --git a/src/subreddit.rs b/src/subreddit.rs index ee57ea5..05bbd64 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -434,3 +434,8 @@ async fn subreddit(sub: &str, quarantined: bool) -> Result { nsfw: res["data"]["over18"].as_bool().unwrap_or_default(), }) } + +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn test_fetching_subreddit() { + subreddit("rust", false).await.unwrap(); +} diff --git a/src/user.rs b/src/user.rs index efa70a9..a5b8d97 100644 --- a/src/user.rs +++ b/src/user.rs @@ -129,3 +129,10 @@ async fn user(name: &str) -> Result { } }) } + +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn test_fetching_user() { + let user = user("spez").await; + assert!(user.is_ok()); + assert!(user.unwrap().karma > 100); +}