|
18 | 18 | build_oauth_authorization_server_metadata_discovery_urls, |
19 | 19 | build_protected_resource_metadata_discovery_urls, |
20 | 20 | create_client_info_from_metadata_url, |
| 21 | + create_client_registration_request, |
21 | 22 | create_oauth_metadata_request, |
22 | 23 | extract_field_from_www_auth, |
23 | 24 | extract_resource_metadata_from_www_auth, |
@@ -948,6 +949,49 @@ def text(self): |
948 | 949 | assert "Registration failed: 400" in str(exc_info.value) |
949 | 950 |
|
950 | 951 |
|
| 952 | +class TestCreateClientRegistrationRequest: |
| 953 | + """Test client registration request creation.""" |
| 954 | + |
| 955 | + def test_uses_registration_endpoint_from_metadata(self): |
| 956 | + """Test that registration URL comes from metadata when available.""" |
| 957 | + oauth_metadata = OAuthMetadata( |
| 958 | + issuer=AnyHttpUrl("https://auth.example.com"), |
| 959 | + authorization_endpoint=AnyHttpUrl("https://auth.example.com/authorize"), |
| 960 | + token_endpoint=AnyHttpUrl("https://auth.example.com/token"), |
| 961 | + registration_endpoint=AnyHttpUrl("https://auth.example.com/register"), |
| 962 | + ) |
| 963 | + client_metadata = OAuthClientMetadata(redirect_uris=[AnyHttpUrl("http://localhost:3000/callback")]) |
| 964 | + |
| 965 | + request = create_client_registration_request(oauth_metadata, client_metadata, "https://auth.example.com") |
| 966 | + |
| 967 | + assert str(request.url) == "https://auth.example.com/register" |
| 968 | + assert request.method == "POST" |
| 969 | + |
| 970 | + def test_falls_back_to_default_register_endpoint_when_no_metadata(self): |
| 971 | + """Test that registration uses fallback URL when auth_server_metadata is None.""" |
| 972 | + client_metadata = OAuthClientMetadata(redirect_uris=[AnyHttpUrl("http://localhost:3000/callback")]) |
| 973 | + |
| 974 | + request = create_client_registration_request(None, client_metadata, "https://auth.example.com") |
| 975 | + |
| 976 | + assert str(request.url) == "https://auth.example.com/register" |
| 977 | + assert request.method == "POST" |
| 978 | + |
| 979 | + def test_falls_back_when_metadata_has_no_registration_endpoint(self): |
| 980 | + """Test fallback when metadata exists but lacks registration_endpoint.""" |
| 981 | + oauth_metadata = OAuthMetadata( |
| 982 | + issuer=AnyHttpUrl("https://auth.example.com"), |
| 983 | + authorization_endpoint=AnyHttpUrl("https://auth.example.com/authorize"), |
| 984 | + token_endpoint=AnyHttpUrl("https://auth.example.com/token"), |
| 985 | + # No registration_endpoint |
| 986 | + ) |
| 987 | + client_metadata = OAuthClientMetadata(redirect_uris=[AnyHttpUrl("http://localhost:3000/callback")]) |
| 988 | + |
| 989 | + request = create_client_registration_request(oauth_metadata, client_metadata, "https://auth.example.com") |
| 990 | + |
| 991 | + assert str(request.url) == "https://auth.example.com/register" |
| 992 | + assert request.method == "POST" |
| 993 | + |
| 994 | + |
951 | 995 | class TestAuthFlow: |
952 | 996 | """Test the auth flow in httpx.""" |
953 | 997 |
|
|
0 commit comments