Skip to main content

mas_router/
lib.rs

1// Copyright 2025, 2026 Element Creations Ltd.
2// Copyright 2024, 2025 New Vector Ltd.
3// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
4//
5// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
6// Please see LICENSE files in the repository root for full details.
7
8pub(crate) mod endpoints;
9pub(crate) mod traits;
10mod url_builder;
11
12pub use self::{endpoints::*, traits::Route, url_builder::UrlBuilder};
13
14#[cfg(test)]
15mod tests {
16    use std::borrow::Cow;
17
18    use ulid::Ulid;
19    use url::Url;
20
21    use super::*;
22
23    #[test]
24    fn test_relative_urls() {
25        assert_eq!(
26            OidcConfiguration.path_and_query(),
27            Cow::Borrowed("/.well-known/openid-configuration")
28        );
29        assert_eq!(Index.path_and_query(), Cow::Borrowed("/"));
30        assert_eq!(
31            Login::and_continue_grant(Ulid::nil()).path_and_query(),
32            Cow::Borrowed("/login?kind=continue_authorization_grant&id=00000000000000000000000000")
33        );
34        assert_eq!(
35            Register::and_then(PostAuthAction::ContinueRoomInvite { id: Ulid::nil() })
36                .path_and_query(),
37            Cow::Borrowed("/register?kind=continue_room_invite&id=00000000000000000000000000")
38        );
39        assert_eq!(
40            OpenInviteRoom(Ulid::nil()).path_and_query(),
41            Cow::Borrowed("/open-room/00000000000000000000000000")
42        );
43    }
44
45    #[test]
46    fn test_register_urls() {
47        assert_eq!(
48            Register::default().path_and_query(),
49            Cow::Borrowed("/register")
50        );
51        assert_eq!(
52            Register::default()
53                .with_token("a token&more".to_owned())
54                .path_and_query(),
55            Cow::Borrowed("/register?token=a+token%26more")
56        );
57        assert_eq!(
58            Register::and_continue_grant(Ulid::nil())
59                .with_token("invite".to_owned())
60                .path_and_query(),
61            Cow::Borrowed(
62                "/register?token=invite&kind=continue_authorization_grant&id=00000000000000000000000000"
63            )
64        );
65    }
66
67    #[test]
68    fn test_absolute_urls() {
69        let base = Url::try_from("https://example.com/").unwrap();
70        assert_eq!(Index.absolute_url(&base).as_str(), "https://example.com/");
71        assert_eq!(
72            OidcConfiguration.absolute_url(&base).as_str(),
73            "https://example.com/.well-known/openid-configuration"
74        );
75    }
76}