From cdb3449ce25090c194a68fc3167f40c80073207c Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 14 Dec 2023 12:11:24 +0100 Subject: [PATCH] test(sdk): Test that `Media::get_media_content` with caching works. --- crates/matrix-sdk/tests/integration/client.rs | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index 6b527c381..71ad6cce0 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -279,18 +279,68 @@ async fn left_rooms() { async fn get_media_content() { let (client, server) = logged_in_client().await; + let media = client.media(); + let request = MediaRequest { source: MediaSource::Plain(mxc_uri!("mxc://localhost/textfile").to_owned()), format: MediaFormat::File, }; - Mock::given(method("GET")) - .and(path("/_matrix/media/r0/download/localhost/textfile")) - .respond_with(ResponseTemplate::new(200).set_body_string("Some very interesting text.")) - .mount(&server) - .await; + // First time, without the cache. + { + let expected_content = "Hello, World!"; + let _mock_guard = Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/localhost/textfile")) + .respond_with(ResponseTemplate::new(200).set_body_string(expected_content)) + .mount_as_scoped(&server) + .await; - client.media().get_media_content(&request, false).await.unwrap(); + assert_eq!( + media.get_media_content(&request, false).await.unwrap(), + expected_content.as_bytes() + ); + } + + // Second time, without the cache, error from the HTTP server. + { + let _mock_guard = Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/localhost/textfile")) + .respond_with(ResponseTemplate::new(500)) + .mount_as_scoped(&server) + .await; + + assert!(media.get_media_content(&request, false).await.is_err()); + } + + let expected_content = "Hello, World (2)!"; + + // Third time, with the cache. + { + let _mock_guard = Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/localhost/textfile")) + .respond_with(ResponseTemplate::new(200).set_body_string(expected_content)) + .mount_as_scoped(&server) + .await; + + assert_eq!( + media.get_media_content(&request, true).await.unwrap(), + expected_content.as_bytes() + ); + } + + // Third time, with the cache, the HTTP server isn't reached. + { + let _mock_guard = Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/localhost/textfile")) + .respond_with(ResponseTemplate::new(500)) + .mount_as_scoped(&server) + .await; + + assert_eq!( + client.media().get_media_content(&request, true).await.unwrap(), + expected_content.as_bytes() + ); + } } #[async_test]