Skip to content

Commit f032d16

Browse files
committed
task(tests): mock upload too
1 parent 57137cd commit f032d16

File tree

3 files changed

+169
-188
lines changed

3 files changed

+169
-188
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use matrix_sdk_test::{
2424
test_json, InvitedRoomBuilder, JoinedRoomBuilder, KnockedRoomBuilder, LeftRoomBuilder,
2525
SyncResponseBuilder,
2626
};
27-
use ruma::{OwnedEventId, OwnedRoomId, RoomId};
27+
use ruma::{MxcUri, OwnedEventId, OwnedRoomId, RoomId};
2828
use serde_json::json;
2929
use wiremock::{
30-
matchers::{header, method, path, path_regex},
30+
matchers::{body_partial_json, header, method, path, path_regex},
3131
Mock, MockBuilder, MockGuard, MockServer, Respond, ResponseTemplate, Times,
3232
};
3333

@@ -193,6 +193,14 @@ impl MatrixMockServer {
193193
let mock = Mock::given(method("GET")).and(header("authorization", "Bearer 1234"));
194194
MockRoomEvent { mock, server: &self.server, room: None, match_event_id: false }
195195
}
196+
197+
/// Create a prebuilt mock for uploading media.
198+
pub fn mock_upload(&self) -> MockUpload<'_> {
199+
let mock = Mock::given(method("POST"))
200+
.and(path("/_matrix/media/r0/upload"))
201+
.and(header("authorization", "Bearer 1234"));
202+
MockUpload { mock, server: &self.server }
203+
}
196204
}
197205

198206
/// Parameter to [`MatrixMockServer::sync_room`].
@@ -303,6 +311,12 @@ pub struct MockRoomSend<'a> {
303311
}
304312

305313
impl<'a> MockRoomSend<'a> {
314+
/// Ensures that the body of the request is a superset of the provided
315+
/// `body` parameter.
316+
pub fn body_matches_partial_json(self, body: serde_json::Value) -> Self {
317+
Self { mock: self.mock.and(body_partial_json(body)), ..self }
318+
}
319+
306320
/// Returns a send endpoint that emulates success, i.e. the event has been
307321
/// sent with the given event id.
308322
pub fn ok(self, returned_event_id: impl Into<OwnedEventId>) -> MatrixMock<'a> {
@@ -469,3 +483,32 @@ impl<'a> MockRoomEvent<'a> {
469483
MatrixMock { server: self.server, mock }
470484
}
471485
}
486+
487+
/// A prebuilt mock for uploading media.
488+
pub struct MockUpload<'a> {
489+
server: &'a MockServer,
490+
mock: MockBuilder,
491+
}
492+
493+
impl<'a> MockUpload<'a> {
494+
/// Expect that the content type matches what's given here.
495+
pub fn expect_mime_type(self, content_type: &str) -> Self {
496+
Self { mock: self.mock.and(header("content-type", content_type)), ..self }
497+
}
498+
499+
/// Returns a redact endpoint that emulates success, i.e. the redaction
500+
/// event has been sent with the given event id.
501+
pub fn ok(self, mxc_id: &MxcUri) -> MatrixMock<'a> {
502+
let mock = self.mock.respond_with(ResponseTemplate::new(200).set_body_json(json!({
503+
"content_uri": mxc_id
504+
})));
505+
MatrixMock { server: self.server, mock }
506+
}
507+
508+
/// Specify how to respond to a query (viz., like
509+
/// [`MockBuilder::respond_with`] does), when other predefined responses
510+
/// aren't sufficient.
511+
pub fn respond_with<R: Respond + 'static>(self, func: R) -> MatrixMock<'a> {
512+
MatrixMock { mock: self.mock.respond_with(func), server: self.server }
513+
}
514+
}

0 commit comments

Comments
 (0)