fix tests

cleanup-request-rs
Felix Ableitner 2023-10-30 11:58:44 +01:00
parent c2a763d6fb
commit 7fbfa48590
2 changed files with 25 additions and 4 deletions

View File

@ -55,16 +55,32 @@ impl LemmyContext {
&self.rate_limit_cell &self.rate_limit_cell
} }
/// Initialize a context for use in tests, doesn't allow network requests. /// Initialize a context for use in tests, optionally blocks network requests.
/// ///
/// Do not use this in production code. /// Do not use this in production code.
pub async fn init_test_context() -> Data<LemmyContext> { pub async fn init_test_context() -> Data<LemmyContext> {
Self::build_test_context(true).await
}
/// Initialize a context for use in tests, with network requests allowed.
/// TODO: get rid of this if possible.
///
/// Do not use this in production code.
pub async fn init_test_context_with_networking() -> Data<LemmyContext> {
Self::build_test_context(false).await
}
async fn build_test_context(block_networking: bool) -> Data<LemmyContext> {
// call this to run migrations // call this to run migrations
let pool = build_db_pool_for_tests().await; let pool = build_db_pool_for_tests().await;
let client = client_builder(&SETTINGS).build().expect("build client"); let client = client_builder(&SETTINGS).build().expect("build client");
let client = ClientBuilder::new(client).with(BlockedMiddleware).build(); let mut client = ClientBuilder::new(client);
if block_networking {
client = client.with(BlockedMiddleware);
}
let client = client.build();
let secret = Secret { let secret = Secret {
id: 0, id: 0,
jwt_secret: String::new(), jwt_secret: String::new(),

View File

@ -288,12 +288,14 @@ mod tests {
context::LemmyContext, context::LemmyContext,
request::{extract_opengraph_data, fetch_link_metadata}, request::{extract_opengraph_data, fetch_link_metadata},
}; };
use serial_test::serial;
use url::Url; use url::Url;
// These helped with testing // These helped with testing
#[tokio::test] #[tokio::test]
#[serial]
async fn test_link_metadata() { async fn test_link_metadata() {
let context = LemmyContext::init_test_context().await; let context = LemmyContext::init_test_context_with_networking().await;
let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap(); let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap();
let sample_res = fetch_link_metadata(&sample_url, false, &context) let sample_res = fetch_link_metadata(&sample_url, false, &context)
.await .await
@ -315,7 +317,10 @@ mod tests {
sample_res.image sample_res.image
); );
assert_eq!(None, sample_res.embed_video_url); assert_eq!(None, sample_res.embed_video_url);
assert_eq!(None, sample_res.content_type); assert_eq!(
Some(mime::TEXT_HTML_UTF_8.to_string()),
sample_res.content_type
);
assert_eq!(None, sample_res.thumbnail); assert_eq!(None, sample_res.thumbnail);
} }