@@ -34,11 +34,8 @@ use wiremock::{
3434use super :: logged_in_client;
3535use crate :: { Client , Room } ;
3636
37- /// A `wiremock` server along with a client connected to it, with useful methods
38- /// to help mocking Matrix client-server API endpoints easily.
39- ///
40- /// This is a pair of a [`MockServer`] and a [`Client]` (one can retrieve them
41- /// respectively with [`Self::server()`] and [`Self::client()`]).
37+ /// A `wiremock` [`MockServer`] along with useful methods to help mocking Matrix
38+ /// client-server API endpoints easily.
4239///
4340/// It implements mock endpoints, limiting the shared code as much as possible,
4441/// so the mocks are still flexible to use as scoped/unscoped mounts, named, and
@@ -63,7 +60,6 @@ use crate::{Client, Room};
6360/// mostly defers its implementations to [`wiremock::Mock`] under the hood.
6461pub struct MatrixMockServer {
6562 server : MockServer ,
66- client : Client ,
6763
6864 /// Make the sync response builder stateful, to keep in memory the batch
6965 /// token and avoid the client ignoring subsequent responses after the first
@@ -75,19 +71,19 @@ impl MatrixMockServer {
7571 /// Create a new `wiremock` server specialized for Matrix usage.
7672 pub async fn new ( ) -> Self {
7773 let server = MockServer :: start ( ) . await ;
78- let client = logged_in_client ( Some ( server. uri ( ) . to_string ( ) ) ) . await ;
79- Self { client, server, sync_response_builder : Default :: default ( ) }
74+ Self { server, sync_response_builder : Default :: default ( ) }
8075 }
8176
8277 /// Creates a new [`MatrixMockServer`] when both parts have been already
8378 /// created.
84- pub fn from_parts ( server : MockServer , client : Client ) -> Self {
85- Self { client , server, sync_response_builder : Default :: default ( ) }
79+ pub fn from_server ( server : MockServer ) -> Self {
80+ Self { server, sync_response_builder : Default :: default ( ) }
8681 }
8782
88- /// Return the underlying client.
89- pub fn client ( & self ) -> Client {
90- self . client . clone ( )
83+ /// Creates a new [`Client`] configured to use this server, preconfigured
84+ /// with a session expected by the server endpoints.
85+ pub async fn make_client ( & self ) -> Client {
86+ logged_in_client ( Some ( self . server . uri ( ) . to_string ( ) ) ) . await
9187 }
9288
9389 /// Return the underlying server.
@@ -98,11 +94,16 @@ impl MatrixMockServer {
9894 /// Overrides the sync/ endpoint with knowledge that the given
9995 /// invited/joined/knocked/left room exists, runs a sync and returns the
10096 /// given room.
101- pub async fn sync_room ( & self , room_id : & RoomId , room_data : impl Into < AnyRoomBuilder > ) -> Room {
97+ pub async fn sync_room (
98+ & self ,
99+ client : & Client ,
100+ room_id : & RoomId ,
101+ room_data : impl Into < AnyRoomBuilder > ,
102+ ) -> Room {
102103 let any_room = room_data. into ( ) ;
103104
104105 self . mock_sync ( )
105- . ok_and_run ( move |builder| match any_room {
106+ . ok_and_run ( client , move |builder| match any_room {
106107 AnyRoomBuilder :: Invited ( invited) => {
107108 builder. add_invited_room ( invited) ;
108109 }
@@ -118,13 +119,13 @@ impl MatrixMockServer {
118119 } )
119120 . await ;
120121
121- self . client . get_room ( room_id) . expect ( "look at me, the room is known now" )
122+ client. get_room ( room_id) . expect ( "look at me, the room is known now" )
122123 }
123124
124125 /// Overrides the sync/ endpoint with knowledge that the given room exists
125126 /// in the joined state, runs a sync and returns the given room.
126- pub async fn sync_joined_room ( & self , room_id : & RoomId ) -> Room {
127- self . sync_room ( room_id, JoinedRoomBuilder :: new ( room_id) ) . await
127+ pub async fn sync_joined_room ( & self , client : & Client , room_id : & RoomId ) -> Room {
128+ self . sync_room ( client , room_id, JoinedRoomBuilder :: new ( room_id) ) . await
128129 }
129130
130131 /// Verify that the previous mocks expected number of requests match
@@ -144,7 +145,6 @@ impl MatrixMockServer {
144145 . and ( header ( "authorization" , "Bearer 1234" ) ) ;
145146 MockSync {
146147 mock,
147- client : self . client . clone ( ) ,
148148 server : & self . server ,
149149 sync_response_builder : self . sync_response_builder . clone ( ) ,
150150 }
@@ -360,15 +360,14 @@ pub struct MockSync<'a> {
360360 mock : MockBuilder ,
361361 server : & ' a MockServer ,
362362 sync_response_builder : Arc < Mutex < SyncResponseBuilder > > ,
363- client : Client ,
364363}
365364
366365impl < ' a > MockSync < ' a > {
367366 /// Temporarily mocks the sync with the given endpoint and runs a client
368367 /// sync with it.
369368 ///
370369 /// After calling this function, the sync endpoint isn't mocked anymore.
371- pub async fn ok_and_run < F : FnOnce ( & mut SyncResponseBuilder ) > ( self , func : F ) {
370+ pub async fn ok_and_run < F : FnOnce ( & mut SyncResponseBuilder ) > ( self , client : & Client , func : F ) {
372371 let json_response = {
373372 let mut builder = self . sync_response_builder . lock ( ) . unwrap ( ) ;
374373 func ( & mut builder) ;
@@ -381,7 +380,7 @@ impl<'a> MockSync<'a> {
381380 . mount_as_scoped ( self . server )
382381 . await ;
383382
384- let _response = self . client . sync_once ( Default :: default ( ) ) . await . unwrap ( ) ;
383+ let _response = client. sync_once ( Default :: default ( ) ) . await . unwrap ( ) ;
385384 }
386385}
387386
0 commit comments