@@ -288,13 +288,12 @@ impl MatrixMockServer {
288
288
/// # anyhow::Ok(()) });
289
289
/// ```
290
290
pub fn mock_sync ( & self ) -> MockEndpoint < ' _ , SyncEndpoint > {
291
- let mock = Mock :: given ( method ( "GET" ) )
292
- . and ( path ( "/_matrix/client/v3/sync" ) )
293
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
291
+ let mock = Mock :: given ( method ( "GET" ) ) . and ( path ( "/_matrix/client/v3/sync" ) ) ;
294
292
self . mock_endpoint (
295
293
mock,
296
294
SyncEndpoint { sync_response_builder : self . sync_response_builder . clone ( ) } ,
297
295
)
296
+ . expect_default_access_token ( )
298
297
}
299
298
300
299
/// Creates a prebuilt mock for sending an event in a room.
@@ -336,9 +335,8 @@ impl MatrixMockServer {
336
335
/// ```
337
336
pub fn mock_room_send ( & self ) -> MockEndpoint < ' _ , RoomSendEndpoint > {
338
337
let mock = Mock :: given ( method ( "PUT" ) )
339
- . and ( header ( "authorization" , "Bearer 1234" ) )
340
338
. and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/send/.*" . to_owned ( ) ) ) ;
341
- self . mock_endpoint ( mock, RoomSendEndpoint )
339
+ self . mock_endpoint ( mock, RoomSendEndpoint ) . expect_default_access_token ( )
342
340
}
343
341
344
342
/// Creates a prebuilt mock for sending a state event in a room.
@@ -385,10 +383,9 @@ impl MatrixMockServer {
385
383
/// # anyhow::Ok(()) });
386
384
/// ```
387
385
pub fn mock_room_send_state ( & self ) -> MockEndpoint < ' _ , RoomSendStateEndpoint > {
388
- let mock = Mock :: given ( method ( "PUT" ) )
389
- . and ( header ( "authorization" , "Bearer 1234" ) )
390
- . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/.*/.*" ) ) ;
391
- self . mock_endpoint ( mock, RoomSendStateEndpoint :: default ( ) )
386
+ let mock =
387
+ Mock :: given ( method ( "PUT" ) ) . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/.*/.*" ) ) ;
388
+ self . mock_endpoint ( mock, RoomSendStateEndpoint :: default ( ) ) . expect_default_access_token ( )
392
389
}
393
390
394
391
/// Creates a prebuilt mock for asking whether *a* room is encrypted or not.
@@ -418,9 +415,8 @@ impl MatrixMockServer {
418
415
/// ```
419
416
pub fn mock_room_state_encryption ( & self ) -> MockEndpoint < ' _ , EncryptionStateEndpoint > {
420
417
let mock = Mock :: given ( method ( "GET" ) )
421
- . and ( header ( "authorization" , "Bearer 1234" ) )
422
418
. and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?" ) ) ;
423
- self . mock_endpoint ( mock, EncryptionStateEndpoint )
419
+ self . mock_endpoint ( mock, EncryptionStateEndpoint ) . expect_default_access_token ( )
424
420
}
425
421
426
422
/// Creates a prebuilt mock for setting the room encryption state.
@@ -458,9 +454,8 @@ impl MatrixMockServer {
458
454
/// ```
459
455
pub fn mock_set_room_state_encryption ( & self ) -> MockEndpoint < ' _ , SetEncryptionStateEndpoint > {
460
456
let mock = Mock :: given ( method ( "PUT" ) )
461
- . and ( header ( "authorization" , "Bearer 1234" ) )
462
457
. and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?" ) ) ;
463
- self . mock_endpoint ( mock, SetEncryptionStateEndpoint )
458
+ self . mock_endpoint ( mock, SetEncryptionStateEndpoint ) . expect_default_access_token ( )
464
459
}
465
460
466
461
/// Creates a prebuilt mock for the room redact endpoint.
@@ -491,32 +486,29 @@ impl MatrixMockServer {
491
486
/// ```
492
487
pub fn mock_room_redact ( & self ) -> MockEndpoint < ' _ , RoomRedactEndpoint > {
493
488
let mock = Mock :: given ( method ( "PUT" ) )
494
- . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/redact/.*?/.*?" ) )
495
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
496
- self . mock_endpoint ( mock, RoomRedactEndpoint )
489
+ . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/redact/.*?/.*?" ) ) ;
490
+ self . mock_endpoint ( mock, RoomRedactEndpoint ) . expect_default_access_token ( )
497
491
}
498
492
499
493
/// Creates a prebuilt mock for retrieving an event with /room/.../event.
500
494
pub fn mock_room_event ( & self ) -> MockEndpoint < ' _ , RoomEventEndpoint > {
501
- let mock = Mock :: given ( method ( "GET" ) ) . and ( header ( "authorization" , "Bearer 1234" ) ) ;
495
+ let mock = Mock :: given ( method ( "GET" ) ) ;
502
496
self . mock_endpoint ( mock, RoomEventEndpoint { room : None , match_event_id : false } )
497
+ . expect_default_access_token ( )
503
498
}
504
499
505
500
/// Create a prebuild mock for paginating room message with the `/messages`
506
501
/// endpoint.
507
502
pub fn mock_room_messages ( & self ) -> MockEndpoint < ' _ , RoomMessagesEndpoint > {
508
- let mock = Mock :: given ( method ( "GET" ) )
509
- . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/messages$" ) )
510
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
511
- self . mock_endpoint ( mock, RoomMessagesEndpoint )
503
+ let mock =
504
+ Mock :: given ( method ( "GET" ) ) . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/messages$" ) ) ;
505
+ self . mock_endpoint ( mock, RoomMessagesEndpoint ) . expect_default_access_token ( )
512
506
}
513
507
514
508
/// Create a prebuilt mock for uploading media.
515
509
pub fn mock_upload ( & self ) -> MockEndpoint < ' _ , UploadEndpoint > {
516
- let mock = Mock :: given ( method ( "POST" ) )
517
- . and ( path ( "/_matrix/media/v3/upload" ) )
518
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
519
- self . mock_endpoint ( mock, UploadEndpoint )
510
+ let mock = Mock :: given ( method ( "POST" ) ) . and ( path ( "/_matrix/media/v3/upload" ) ) ;
511
+ self . mock_endpoint ( mock, UploadEndpoint ) . expect_default_access_token ( )
520
512
}
521
513
522
514
/// Create a prebuilt mock for resolving room aliases.
@@ -768,26 +760,23 @@ impl MatrixMockServer {
768
760
/// # }
769
761
/// ```
770
762
pub fn mock_room_keys_version ( & self ) -> MockEndpoint < ' _ , RoomKeysVersionEndpoint > {
771
- let mock = Mock :: given ( method ( "GET" ) )
772
- . and ( path_regex ( r"_matrix/client/v3/room_keys/version" ) )
773
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
774
- self . mock_endpoint ( mock, RoomKeysVersionEndpoint )
763
+ let mock =
764
+ Mock :: given ( method ( "GET" ) ) . and ( path_regex ( r"_matrix/client/v3/room_keys/version" ) ) ;
765
+ self . mock_endpoint ( mock, RoomKeysVersionEndpoint ) . expect_default_access_token ( )
775
766
}
776
767
777
768
/// Create a prebuilt mock for adding key storage backups via POST
778
769
pub fn mock_add_room_keys_version ( & self ) -> MockEndpoint < ' _ , AddRoomKeysVersionEndpoint > {
779
- let mock = Mock :: given ( method ( "POST" ) )
780
- . and ( path_regex ( r"_matrix/client/v3/room_keys/version" ) )
781
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
782
- self . mock_endpoint ( mock, AddRoomKeysVersionEndpoint )
770
+ let mock =
771
+ Mock :: given ( method ( "POST" ) ) . and ( path_regex ( r"_matrix/client/v3/room_keys/version" ) ) ;
772
+ self . mock_endpoint ( mock, AddRoomKeysVersionEndpoint ) . expect_default_access_token ( )
783
773
}
784
774
785
775
/// Create a prebuilt mock for adding key storage backups via POST
786
776
pub fn mock_delete_room_keys_version ( & self ) -> MockEndpoint < ' _ , DeleteRoomKeysVersionEndpoint > {
787
777
let mock = Mock :: given ( method ( "DELETE" ) )
788
- . and ( path_regex ( r"_matrix/client/v3/room_keys/version/[^/]*" ) )
789
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
790
- self . mock_endpoint ( mock, DeleteRoomKeysVersionEndpoint )
778
+ . and ( path_regex ( r"_matrix/client/v3/room_keys/version/[^/]*" ) ) ;
779
+ self . mock_endpoint ( mock, DeleteRoomKeysVersionEndpoint ) . expect_default_access_token ( )
791
780
}
792
781
793
782
/// Create a prebuilt mock for getting the room members in a room.
@@ -945,9 +934,8 @@ impl MatrixMockServer {
945
934
/// events.
946
935
pub fn mock_set_room_pinned_events ( & self ) -> MockEndpoint < ' _ , SetRoomPinnedEventsEndpoint > {
947
936
let mock = Mock :: given ( method ( "PUT" ) )
948
- . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/m.room.pinned_events/.*?" ) )
949
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
950
- self . mock_endpoint ( mock, SetRoomPinnedEventsEndpoint )
937
+ . and ( path_regex ( r"^/_matrix/client/v3/rooms/.*/state/m.room.pinned_events/.*?" ) ) ;
938
+ self . mock_endpoint ( mock, SetRoomPinnedEventsEndpoint ) . expect_default_access_token ( )
951
939
}
952
940
953
941
/// Creates a prebuilt mock for the endpoint used to get information about
@@ -958,25 +946,21 @@ impl MatrixMockServer {
958
946
pub fn mock_who_am_i ( & self ) -> MockEndpoint < ' _ , WhoAmIEndpoint > {
959
947
let mock =
960
948
Mock :: given ( method ( "GET" ) ) . and ( path_regex ( r"^/_matrix/client/v3/account/whoami" ) ) ;
961
- self . mock_endpoint ( mock, WhoAmIEndpoint { expected_access_token : "1234" } )
949
+ self . mock_endpoint ( mock, WhoAmIEndpoint ) . expect_default_access_token ( )
962
950
}
963
951
964
952
/// Creates a prebuilt mock for the endpoint used to publish end-to-end
965
953
/// encryption keys.
966
954
pub fn mock_upload_keys ( & self ) -> MockEndpoint < ' _ , UploadKeysEndpoint > {
967
- let mock = Mock :: given ( method ( "POST" ) )
968
- . and ( path_regex ( r"^/_matrix/client/v3/keys/upload" ) )
969
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
970
- self . mock_endpoint ( mock, UploadKeysEndpoint )
955
+ let mock = Mock :: given ( method ( "POST" ) ) . and ( path_regex ( r"^/_matrix/client/v3/keys/upload" ) ) ;
956
+ self . mock_endpoint ( mock, UploadKeysEndpoint ) . expect_default_access_token ( )
971
957
}
972
958
973
959
/// Creates a prebuilt mock for the endpoint used to query end-to-end
974
960
/// encryption keys.
975
961
pub fn mock_query_keys ( & self ) -> MockEndpoint < ' _ , QueryKeysEndpoint > {
976
- let mock = Mock :: given ( method ( "POST" ) )
977
- . and ( path_regex ( r"^/_matrix/client/v3/keys/query" ) )
978
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
979
- self . mock_endpoint ( mock, QueryKeysEndpoint )
962
+ let mock = Mock :: given ( method ( "POST" ) ) . and ( path_regex ( r"^/_matrix/client/v3/keys/query" ) ) ;
963
+ self . mock_endpoint ( mock, QueryKeysEndpoint ) . expect_default_access_token ( )
980
964
}
981
965
982
966
/// Creates a prebuilt mock for the endpoint used to discover the URL of a
@@ -992,9 +976,8 @@ impl MatrixMockServer {
992
976
& self ,
993
977
) -> MockEndpoint < ' _ , UploadCrossSigningKeysEndpoint > {
994
978
let mock = Mock :: given ( method ( "POST" ) )
995
- . and ( path_regex ( r"^/_matrix/client/v3/keys/device_signing/upload" ) )
996
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
997
- self . mock_endpoint ( mock, UploadCrossSigningKeysEndpoint )
979
+ . and ( path_regex ( r"^/_matrix/client/v3/keys/device_signing/upload" ) ) ;
980
+ self . mock_endpoint ( mock, UploadCrossSigningKeysEndpoint ) . expect_default_access_token ( )
998
981
}
999
982
1000
983
/// Creates a prebuilt mock for the endpoint used to publish cross-signing
@@ -1003,9 +986,8 @@ impl MatrixMockServer {
1003
986
& self ,
1004
987
) -> MockEndpoint < ' _ , UploadCrossSigningSignaturesEndpoint > {
1005
988
let mock = Mock :: given ( method ( "POST" ) )
1006
- . and ( path_regex ( r"^/_matrix/client/v3/keys/signatures/upload" ) )
1007
- . and ( header ( "authorization" , "Bearer 1234" ) ) ;
1008
- self . mock_endpoint ( mock, UploadCrossSigningSignaturesEndpoint )
989
+ . and ( path_regex ( r"^/_matrix/client/v3/keys/signatures/upload" ) ) ;
990
+ self . mock_endpoint ( mock, UploadCrossSigningSignaturesEndpoint ) . expect_default_access_token ( )
1009
991
}
1010
992
}
1011
993
@@ -1162,11 +1144,24 @@ pub struct MockEndpoint<'a, T> {
1162
1144
server : & ' a MockServer ,
1163
1145
mock : MockBuilder ,
1164
1146
endpoint : T ,
1147
+ expected_access_token : ExpectedAccessToken ,
1165
1148
}
1166
1149
1167
1150
impl < ' a , T > MockEndpoint < ' a , T > {
1168
1151
fn new ( server : & ' a MockServer , mock : MockBuilder , endpoint : T ) -> Self {
1169
- Self { server, mock, endpoint }
1152
+ Self { server, mock, endpoint, expected_access_token : ExpectedAccessToken :: None }
1153
+ }
1154
+
1155
+ /// Expect authentication with the default access token on this endpoint.
1156
+ pub fn expect_default_access_token ( mut self ) -> Self {
1157
+ self . expected_access_token = ExpectedAccessToken :: Default ;
1158
+ self
1159
+ }
1160
+
1161
+ /// Expect authentication with the given access token on this endpoint.
1162
+ pub fn expect_access_token ( mut self , access_token : & ' static str ) -> Self {
1163
+ self . expected_access_token = ExpectedAccessToken :: Custom ( access_token) ;
1164
+ self
1170
1165
}
1171
1166
1172
1167
/// Specify how to respond to a query (viz., like
@@ -1211,7 +1206,11 @@ impl<'a, T> MockEndpoint<'a, T> {
1211
1206
/// # anyhow::Ok(()) });
1212
1207
/// ```
1213
1208
pub fn respond_with < R : Respond + ' static > ( self , func : R ) -> MatrixMock < ' a > {
1214
- MatrixMock { mock : self . mock . respond_with ( func) , server : self . server }
1209
+ let mock = self
1210
+ . expected_access_token
1211
+ . maybe_match_authorization_header ( self . mock )
1212
+ . respond_with ( func) ;
1213
+ MatrixMock { mock, server : self . server }
1215
1214
}
1216
1215
1217
1216
/// Returns a send endpoint that emulates a transient failure, i.e responds
@@ -1292,6 +1291,30 @@ impl<'a, T> MockEndpoint<'a, T> {
1292
1291
}
1293
1292
}
1294
1293
1294
+ /// The access token to expect on an endpoint.
1295
+ enum ExpectedAccessToken {
1296
+ /// We don't expect an access token.
1297
+ None ,
1298
+
1299
+ /// We expect the default access token.
1300
+ Default ,
1301
+
1302
+ /// We expect the given access token.
1303
+ Custom ( & ' static str ) ,
1304
+ }
1305
+
1306
+ impl ExpectedAccessToken {
1307
+ /// Match an `Authorization` header on the given mock if one is expected.
1308
+ fn maybe_match_authorization_header ( & self , mock : MockBuilder ) -> MockBuilder {
1309
+ let token = match self {
1310
+ Self :: None => return mock,
1311
+ Self :: Default => "1234" ,
1312
+ Self :: Custom ( token) => token,
1313
+ } ;
1314
+ mock. and ( header ( http:: header:: AUTHORIZATION , format ! ( "Bearer {token}" ) ) )
1315
+ }
1316
+ }
1317
+
1295
1318
/// A prebuilt mock for sending a message like event in a room.
1296
1319
pub struct RoomSendEndpoint ;
1297
1320
@@ -2377,35 +2400,19 @@ impl<'a> MockEndpoint<'a, SetRoomPinnedEventsEndpoint> {
2377
2400
}
2378
2401
2379
2402
/// A prebuilt mock for `GET /account/whoami` request.
2380
- pub struct WhoAmIEndpoint {
2381
- expected_access_token : & ' static str ,
2382
- }
2383
-
2384
- impl WhoAmIEndpoint {
2385
- fn add_access_token_matcher ( & self , mock : MockBuilder ) -> MockBuilder {
2386
- mock. and ( header ( "authorization" , format ! ( "Bearer {}" , self . expected_access_token) ) )
2387
- }
2388
- }
2403
+ pub struct WhoAmIEndpoint ;
2389
2404
2390
2405
impl < ' a > MockEndpoint < ' a , WhoAmIEndpoint > {
2391
- /// Override the access token to expect for this endpoint.
2392
- pub fn expected_access_token ( mut self , access_token : & ' static str ) -> Self {
2393
- self . endpoint . expected_access_token = access_token;
2394
- self
2395
- }
2396
-
2397
2406
/// Returns a successful response with a user ID and device ID.
2398
- pub fn ok ( mut self ) -> MatrixMock < ' a > {
2399
- self . mock = self . endpoint . add_access_token_matcher ( self . mock ) ;
2407
+ pub fn ok ( self ) -> MatrixMock < ' a > {
2400
2408
self . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
2401
2409
"user_id" : "@joe:example.org" ,
2402
2410
"device_id" : "D3V1C31D" ,
2403
2411
} ) ) )
2404
2412
}
2405
2413
2406
2414
/// Returns an error response with an `M_UNKNOWN_TOKEN`.
2407
- pub fn err_unknown_token ( mut self ) -> MatrixMock < ' a > {
2408
- self . mock = self . endpoint . add_access_token_matcher ( self . mock ) ;
2415
+ pub fn err_unknown_token ( self ) -> MatrixMock < ' a > {
2409
2416
self . respond_with ( ResponseTemplate :: new ( 401 ) . set_body_json ( json ! ( {
2410
2417
"errcode" : "M_UNKNOWN_TOKEN" ,
2411
2418
"error" : "Invalid token"
0 commit comments