@@ -1861,7 +1861,7 @@ pub struct MockEndpoint<'a, T> {
18611861
18621862impl < ' a , T > MockEndpoint < ' a , T > {
18631863 fn new ( server : & ' a MockServer , mock : MockBuilder , endpoint : T ) -> Self {
1864- Self { server, mock, endpoint, expected_access_token : ExpectedAccessToken :: None }
1864+ Self { server, mock, endpoint, expected_access_token : ExpectedAccessToken :: Ignore }
18651865 }
18661866
18671867 /// Expect authentication with the default access token on this endpoint.
@@ -1876,12 +1876,30 @@ impl<'a, T> MockEndpoint<'a, T> {
18761876 self
18771877 }
18781878
1879- /// Don't expect authentication with an access token on this endpoint.
1879+ /// Expect authentication with any access token on this endpoint, regardless
1880+ /// of its value.
18801881 ///
1881- /// This should be used to override the default behavior of the endpoint,
1882- /// when the access token is unknown for example.
1883- pub fn do_not_expect_access_token ( mut self ) -> Self {
1884- self . expected_access_token = ExpectedAccessToken :: None ;
1882+ /// This is useful if we don't want to track the value of the access token.
1883+ pub fn expect_any_access_token ( mut self ) -> Self {
1884+ self . expected_access_token = ExpectedAccessToken :: Any ;
1885+ self
1886+ }
1887+
1888+ /// Expect no authentication on this endpoint.
1889+ ///
1890+ /// This means that the endpoint will not match if an `AUTHENTICATION`
1891+ /// header is present.
1892+ pub fn expect_missing_access_token ( mut self ) -> Self {
1893+ self . expected_access_token = ExpectedAccessToken :: Missing ;
1894+ self
1895+ }
1896+
1897+ /// Ignore the access token on this endpoint.
1898+ ///
1899+ /// This should be used to override the default behavior of an endpoint that
1900+ /// requires access tokens.
1901+ pub fn ignore_access_token ( mut self ) -> Self {
1902+ self . expected_access_token = ExpectedAccessToken :: Ignore ;
18851903 self
18861904 }
18871905
@@ -1927,10 +1945,7 @@ impl<'a, T> MockEndpoint<'a, T> {
19271945 /// # anyhow::Ok(()) });
19281946 /// ```
19291947 pub fn respond_with < R : Respond + ' static > ( self , func : R ) -> MatrixMock < ' a > {
1930- let mock = self
1931- . expected_access_token
1932- . maybe_match_authorization_header ( self . mock )
1933- . respond_with ( func) ;
1948+ let mock = self . mock . and ( self . expected_access_token ) . respond_with ( func) ;
19341949 MatrixMock { mock, server : self . server }
19351950 }
19361951
@@ -1984,6 +1999,17 @@ impl<'a, T> MockEndpoint<'a, T> {
19841999 } ) ) )
19852000 }
19862001
2002+ /// Returns a mocked endpoint that emulates an unknown token error, i.e
2003+ /// responds with a 401 HTTP status code and an `M_UNKNOWN_TOKEN` Matrix
2004+ /// error code.
2005+ pub fn error_unknown_token ( self , soft_logout : bool ) -> MatrixMock < ' a > {
2006+ self . respond_with ( ResponseTemplate :: new ( 401 ) . set_body_json ( json ! ( {
2007+ "errcode" : "M_UNKNOWN_TOKEN" ,
2008+ "error" : "Unrecognized access token" ,
2009+ "soft_logout" : soft_logout,
2010+ } ) ) )
2011+ }
2012+
19872013 /// Internal helper to return an `{ event_id }` JSON struct along with a 200
19882014 /// ok response.
19892015 fn ok_with_event_id ( self , event_id : OwnedEventId ) -> MatrixMock < ' a > {
@@ -2036,25 +2062,44 @@ impl<'a, T> MockEndpoint<'a, T> {
20362062
20372063/// The access token to expect on an endpoint.
20382064enum ExpectedAccessToken {
2039- /// We don't expect an access token.
2040- None ,
2065+ /// Ignore any access token or lack thereof .
2066+ Ignore ,
20412067
20422068 /// We expect the default access token.
20432069 Default ,
20442070
20452071 /// We expect the given access token.
20462072 Custom ( & ' static str ) ,
2073+
2074+ /// We expect any access token.
2075+ Any ,
2076+
2077+ /// We expect that there is no access token.
2078+ Missing ,
20472079}
20482080
20492081impl ExpectedAccessToken {
2050- /// Match an `Authorization` header on the given mock if one is expected.
2051- fn maybe_match_authorization_header ( & self , mock : MockBuilder ) -> MockBuilder {
2052- let token = match self {
2053- Self :: None => return mock,
2054- Self :: Default => "1234" ,
2055- Self :: Custom ( token) => token,
2056- } ;
2057- mock. and ( header ( http:: header:: AUTHORIZATION , format ! ( "Bearer {token}" ) ) )
2082+ /// Get the access token from the given request.
2083+ fn access_token ( request : & Request ) -> Option < & str > {
2084+ request
2085+ . headers
2086+ . get ( & http:: header:: AUTHORIZATION ) ?
2087+ . to_str ( )
2088+ . ok ( ) ?
2089+ . strip_prefix ( "Bearer " )
2090+ . filter ( |token| !token. is_empty ( ) )
2091+ }
2092+ }
2093+
2094+ impl wiremock:: Match for ExpectedAccessToken {
2095+ fn matches ( & self , request : & Request ) -> bool {
2096+ match self {
2097+ Self :: Ignore => true ,
2098+ Self :: Default => Self :: access_token ( request) == Some ( "1234" ) ,
2099+ Self :: Custom ( token) => Self :: access_token ( request) == Some ( token) ,
2100+ Self :: Any => Self :: access_token ( request) . is_some ( ) ,
2101+ Self :: Missing => request. headers . get ( & http:: header:: AUTHORIZATION ) . is_none ( ) ,
2102+ }
20582103 }
20592104}
20602105
@@ -3483,14 +3528,6 @@ impl<'a> MockEndpoint<'a, WhoAmIEndpoint> {
34833528 "device_id" : device_id,
34843529 } ) ) )
34853530 }
3486-
3487- /// Returns an error response with an `M_UNKNOWN_TOKEN`.
3488- pub fn err_unknown_token ( self ) -> MatrixMock < ' a > {
3489- self . respond_with ( ResponseTemplate :: new ( 401 ) . set_body_json ( json ! ( {
3490- "errcode" : "M_UNKNOWN_TOKEN" ,
3491- "error" : "Invalid token"
3492- } ) ) )
3493- }
34943531}
34953532
34963533/// A prebuilt mock for `POST /keys/upload` request.
0 commit comments