Skip to content

Commit 049993d

Browse files
zecakehHywan
authored andcommitted
test(sdk): Port sliding-sync tests to MatrixMockServer and MockClientBuilder
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 14366e8 commit 049993d

File tree

2 files changed

+255
-222
lines changed

2 files changed

+255
-222
lines changed

crates/matrix-sdk/src/test_utils/mocks/mod.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use ruma::{
3838
room::member::RoomMemberEvent, AnyStateEvent, AnyTimelineEvent, GlobalAccountDataEventType,
3939
MessageLikeEventType, RoomAccountDataEventType, StateEventType,
4040
},
41+
media::Method,
4142
serde::Raw,
4243
time::Duration,
4344
DeviceId, MxcUri, OwnedDeviceId, OwnedEventId, OwnedOneTimeKeyId, OwnedRoomId, OwnedUserId,
@@ -1226,6 +1227,76 @@ impl MatrixMockServer {
12261227
let mock = Mock::given(method("POST")).and(path("/_matrix/client/v3/createRoom"));
12271228
self.mock_endpoint(mock, CreateRoomEndpoint).expect_default_access_token()
12281229
}
1230+
1231+
/// Create a prebuilt mock for the endpoint used to pre-allocate a MXC URI
1232+
/// for a media file.
1233+
pub fn mock_media_allocate(&self) -> MockEndpoint<'_, MediaAllocateEndpoint> {
1234+
let mock = Mock::given(method("POST")).and(path("/_matrix/media/v1/create"));
1235+
self.mock_endpoint(mock, MediaAllocateEndpoint)
1236+
}
1237+
1238+
/// Create a prebuilt mock for the endpoint used to upload a media file with
1239+
/// a pre-allocated MXC URI.
1240+
pub fn mock_media_allocated_upload(
1241+
&self,
1242+
server_name: &str,
1243+
media_id: &str,
1244+
) -> MockEndpoint<'_, MediaAllocatedUploadEndpoint> {
1245+
let mock = Mock::given(method("PUT"))
1246+
.and(path(format!("/_matrix/media/v3/upload/{server_name}/{media_id}")));
1247+
self.mock_endpoint(mock, MediaAllocatedUploadEndpoint)
1248+
}
1249+
1250+
/// Create a prebuilt mock for the endpoint used to download a media file
1251+
/// without requiring authentication.
1252+
pub fn mock_media_download(&self) -> MockEndpoint<'_, MediaDownloadEndpoint> {
1253+
let mock = Mock::given(method("GET")).and(path_regex("^/_matrix/media/v3/download/"));
1254+
self.mock_endpoint(mock, MediaDownloadEndpoint)
1255+
}
1256+
1257+
/// Create a prebuilt mock for the endpoint used to download a thumbnail of
1258+
/// a media file without requiring authentication.
1259+
pub fn mock_media_thumbnail(
1260+
&self,
1261+
resize_method: Method,
1262+
width: u16,
1263+
height: u16,
1264+
animated: bool,
1265+
) -> MockEndpoint<'_, MediaThumbnailEndpoint> {
1266+
let mock = Mock::given(method("GET"))
1267+
.and(path_regex("^/_matrix/media/v3/thumbnail/"))
1268+
.and(query_param("method", resize_method.as_str()))
1269+
.and(query_param("width", width.to_string()))
1270+
.and(query_param("height", height.to_string()))
1271+
.and(query_param("animated", animated.to_string()));
1272+
self.mock_endpoint(mock, MediaThumbnailEndpoint)
1273+
}
1274+
1275+
/// Create a prebuilt mock for the endpoint used to download a media file
1276+
/// that requires authentication.
1277+
pub fn mock_authed_media_download(&self) -> MockEndpoint<'_, AuthedMediaDownloadEndpoint> {
1278+
let mock =
1279+
Mock::given(method("GET")).and(path_regex("^/_matrix/client/v1/media/download/"));
1280+
self.mock_endpoint(mock, AuthedMediaDownloadEndpoint).expect_default_access_token()
1281+
}
1282+
1283+
/// Create a prebuilt mock for the endpoint used to download a thumbnail of
1284+
/// a media file that requires authentication.
1285+
pub fn mock_authed_media_thumbnail(
1286+
&self,
1287+
resize_method: Method,
1288+
width: u16,
1289+
height: u16,
1290+
animated: bool,
1291+
) -> MockEndpoint<'_, AuthedMediaThumbnailEndpoint> {
1292+
let mock = Mock::given(method("GET"))
1293+
.and(path_regex("^/_matrix/client/v1/media/thumbnail/"))
1294+
.and(query_param("method", resize_method.as_str()))
1295+
.and(query_param("width", width.to_string()))
1296+
.and(query_param("height", height.to_string()))
1297+
.and(query_param("animated", animated.to_string()));
1298+
self.mock_endpoint(mock, AuthedMediaThumbnailEndpoint).expect_default_access_token()
1299+
}
12291300
}
12301301

12311302
/// Parameter to [`MatrixMockServer::sync_room`].
@@ -2666,6 +2737,19 @@ impl<'a> MockEndpoint<'a, VersionsEndpoint> {
26662737
"versions": Self::versions()
26672738
})))
26682739
}
2740+
2741+
/// Returns a successful `/_matrix/client/versions` request with the given
2742+
/// versions and unstable features.
2743+
pub fn ok_custom(
2744+
self,
2745+
versions: &[&str],
2746+
unstable_features: &BTreeMap<&str, bool>,
2747+
) -> MatrixMock<'a> {
2748+
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({
2749+
"unstable_features": unstable_features,
2750+
"versions": versions,
2751+
})))
2752+
}
26692753
}
26702754

26712755
/// A prebuilt mock for the room summary endpoint.
@@ -3176,3 +3260,84 @@ impl<'a> MockEndpoint<'a, CreateRoomEndpoint> {
31763260
)
31773261
}
31783262
}
3263+
3264+
/// A prebuilt mock for `POST /media/v1/create` requests.
3265+
pub struct MediaAllocateEndpoint;
3266+
3267+
impl<'a> MockEndpoint<'a, MediaAllocateEndpoint> {
3268+
/// Returns a successful response.
3269+
pub fn ok(self) -> MatrixMock<'a> {
3270+
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({
3271+
"content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw"
3272+
})))
3273+
}
3274+
}
3275+
3276+
/// A prebuilt mock for `PUT /media/v3/upload/{server_name}/{media_id}`
3277+
/// requests.
3278+
pub struct MediaAllocatedUploadEndpoint;
3279+
3280+
impl<'a> MockEndpoint<'a, MediaAllocatedUploadEndpoint> {
3281+
/// Returns a successful response.
3282+
pub fn ok(self) -> MatrixMock<'a> {
3283+
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
3284+
}
3285+
}
3286+
3287+
/// A prebuilt mock for `GET /media/v3/download` requests.
3288+
pub struct MediaDownloadEndpoint;
3289+
3290+
impl<'a> MockEndpoint<'a, MediaDownloadEndpoint> {
3291+
/// Returns a successful response with a plain text content.
3292+
pub fn ok_plain_text(self) -> MatrixMock<'a> {
3293+
self.respond_with(ResponseTemplate::new(200).set_body_string("Hello, World!"))
3294+
}
3295+
3296+
/// Returns a successful response with a fake image content.
3297+
pub fn ok_image(self) -> MatrixMock<'a> {
3298+
self.respond_with(
3299+
ResponseTemplate::new(200).set_body_raw(b"binaryjpegfullimagedata", "image/jpeg"),
3300+
)
3301+
}
3302+
}
3303+
3304+
/// A prebuilt mock for `GET /media/v3/thumbnail` requests.
3305+
pub struct MediaThumbnailEndpoint;
3306+
3307+
impl<'a> MockEndpoint<'a, MediaThumbnailEndpoint> {
3308+
/// Returns a successful response with a fake image content.
3309+
pub fn ok(self) -> MatrixMock<'a> {
3310+
self.respond_with(
3311+
ResponseTemplate::new(200).set_body_raw(b"binaryjpegthumbnaildata", "image/jpeg"),
3312+
)
3313+
}
3314+
}
3315+
3316+
/// A prebuilt mock for `GET /client/v1/media/download` requests.
3317+
pub struct AuthedMediaDownloadEndpoint;
3318+
3319+
impl<'a> MockEndpoint<'a, AuthedMediaDownloadEndpoint> {
3320+
/// Returns a successful response with a plain text content.
3321+
pub fn ok_plain_text(self) -> MatrixMock<'a> {
3322+
self.respond_with(ResponseTemplate::new(200).set_body_string("Hello, World!"))
3323+
}
3324+
3325+
/// Returns a successful response with a fake image content.
3326+
pub fn ok_image(self) -> MatrixMock<'a> {
3327+
self.respond_with(
3328+
ResponseTemplate::new(200).set_body_raw(b"binaryjpegfullimagedata", "image/jpeg"),
3329+
)
3330+
}
3331+
}
3332+
3333+
/// A prebuilt mock for `GET /client/v1/media/thumbnail` requests.
3334+
pub struct AuthedMediaThumbnailEndpoint;
3335+
3336+
impl<'a> MockEndpoint<'a, AuthedMediaThumbnailEndpoint> {
3337+
/// Returns a successful response with a fake image content.
3338+
pub fn ok(self) -> MatrixMock<'a> {
3339+
self.respond_with(
3340+
ResponseTemplate::new(200).set_body_raw(b"binaryjpegthumbnaildata", "image/jpeg"),
3341+
)
3342+
}
3343+
}

0 commit comments

Comments
 (0)