refactor(sdk): Take an Option<&str> in MockClientBuilder::new()

Usually tests that don't construct it via MockMatrixServer don't care
about the homeserver URL, so this allows to provide a default URL for
them.

Also we don't force to allocate a string when the inner API actually
uses a borrowed string.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2025-07-04 16:14:21 +02:00
committed by Ivan Enderlin
parent 07808b4301
commit 7fb3d216f6
7 changed files with 21 additions and 22 deletions

View File

@@ -1177,7 +1177,7 @@ mod tests {
#[async_test]
async fn test_dont_ignore_oneself() {
let client = MockClientBuilder::new("https://example.org".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
// It's forbidden to ignore the logged-in user.
assert_matches!(

View File

@@ -269,11 +269,7 @@ mod tests {
// Create a client that will use sqlite databases.
let tmp_dir = tempfile::tempdir()?;
let client = MockClientBuilder::new("https://example.org".to_owned())
.sqlite_store(&tmp_dir)
.unlogged()
.build()
.await;
let client = MockClientBuilder::new(None).sqlite_store(&tmp_dir).unlogged().build().await;
let tokens = mock_session_tokens_with_refresh();

View File

@@ -483,7 +483,7 @@ async fn test_finish_login() -> anyhow::Result<()> {
#[async_test]
async fn test_oauth_session() -> anyhow::Result<()> {
let client = MockClientBuilder::new("https://example.org".to_owned()).unlogged().build().await;
let client = MockClientBuilder::new(None).unlogged().build().await;
let oauth = client.oauth();
let tokens = mock_session_tokens_with_refresh();

View File

@@ -2010,7 +2010,7 @@ mod timed_tests {
let event_cache_store = Arc::new(MemoryStore::new());
let client = MockClientBuilder::new("http://localhost".to_owned())
let client = MockClientBuilder::new(None)
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
@@ -2084,7 +2084,7 @@ mod timed_tests {
let event_cache_store = Arc::new(MemoryStore::new());
let client = MockClientBuilder::new("http://localhost".to_owned())
let client = MockClientBuilder::new(None)
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
@@ -2223,7 +2223,7 @@ mod timed_tests {
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
let client = MockClientBuilder::new(None)
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
@@ -2361,7 +2361,7 @@ mod timed_tests {
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
let client = MockClientBuilder::new(None)
.store_config(
StoreConfig::new("hodlor".to_owned()).event_cache_store(event_cache_store.clone()),
)
@@ -2482,7 +2482,7 @@ mod timed_tests {
.await
.unwrap();
let client = MockClientBuilder::new("http://localhost".to_owned())
let client = MockClientBuilder::new(None)
.store_config(
StoreConfig::new("holder".to_owned()).event_cache_store(event_cache_store.clone()),
)
@@ -2516,7 +2516,7 @@ mod timed_tests {
async fn test_no_useless_gaps() {
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
let event_cache = client.event_cache();
event_cache.subscribe().unwrap();
@@ -2638,7 +2638,7 @@ mod timed_tests {
async fn test_shrink_to_last_chunk() {
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
let f = EventFactory::new().room(room_id);
@@ -2747,7 +2747,7 @@ mod timed_tests {
async fn test_room_ordering() {
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
let f = EventFactory::new().room(room_id).sender(*ALICE);
@@ -2882,7 +2882,7 @@ mod timed_tests {
async fn test_auto_shrink_after_all_subscribers_are_gone() {
let room_id = room_id!("!galette:saucisse.bzh");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
let f = EventFactory::new().room(room_id);
@@ -2993,7 +2993,7 @@ mod timed_tests {
async fn test_rfind_event_in_memory_by() {
let user_id = user_id!("@mnt_io:matrix.org");
let room_id = room_id!("!raclette:patate.ch");
let client = MockClientBuilder::new("http://localhost".to_owned()).build().await;
let client = MockClientBuilder::new(None).build().await;
let event_factory = EventFactory::new().room(room_id);

View File

@@ -36,10 +36,13 @@ impl MockClientBuilder {
/// Create a new [`MockClientBuilder`] connected to the given homeserver,
/// using Matrix V1.12, and which will not attempt any network retry (by
/// default).
pub(crate) fn new(homeserver: String) -> Self {
///
/// If no homeserver is provided, `http://localhost` is used as a homeserver.
pub(crate) fn new(homeserver: Option<&str>) -> Self {
let homeserver = homeserver.unwrap_or("http://localhost");
let default_builder = Client::builder()
.homeserver_url(&homeserver)
.server_versions([MatrixVersion::V1_12])
.homeserver_url(homeserver)
.request_config(RequestConfig::new().disable_retry());
Self {

View File

@@ -96,7 +96,7 @@ impl MatrixMockServer {
mappings.insert(auth_string, user_id.to_owned());
}
MockClientBuilder::new(self.server.uri()).logged_in_with_token(
MockClientBuilder::new(Some(&self.server.uri())).logged_in_with_token(
access_token,
user_id.to_owned(),
device_id.to_owned(),

View File

@@ -180,7 +180,7 @@ impl MatrixMockServer {
/// Creates a new [`MockClientBuilder`] configured to use this server,
/// preconfigured with a session expected by the server endpoints.
pub fn client_builder(&self) -> MockClientBuilder {
MockClientBuilder::new(self.server.uri())
MockClientBuilder::new(Some(&self.server.uri()))
}
/// Return the underlying [`wiremock`] server.