1818
1919
2020def test_build_metadata_strips_trailing_slash_from_issuer ():
21- """Test that build_metadata strips trailing slash from issuer URL.
21+ """Test that build_metadata strips trailing slash from issuer URL when serialized .
2222
2323 Pydantic's AnyHttpUrl automatically adds trailing slashes to bare hostnames.
24- This test verifies that we strip them to comply with RFC 8414 §3.3.
24+ This test verifies that we strip them during serialization to comply with RFC 8414 §3.3.
2525 """
2626 # Use a bare hostname URL which Pydantic will add a trailing slash to
2727 issuer_url = AnyHttpUrl ("http://localhost:8000" )
@@ -33,13 +33,14 @@ def test_build_metadata_strips_trailing_slash_from_issuer():
3333 revocation_options = RevocationOptions (enabled = False ),
3434 )
3535
36- # The issuer should NOT have a trailing slash
37- assert str (metadata .issuer ) == "http://localhost:8000"
38- assert not str (metadata .issuer ).endswith ("/" )
36+ # The serialized issuer should NOT have a trailing slash
37+ serialized = metadata .model_dump (mode = "json" )
38+ assert serialized ["issuer" ] == "http://localhost:8000"
39+ assert not serialized ["issuer" ].endswith ("/" )
3940
4041
4142def test_build_metadata_strips_trailing_slash_from_issuer_with_path ():
42- """Test that build_metadata strips trailing slash from issuer URL with path."""
43+ """Test that build_metadata strips trailing slash from issuer URL with path when serialized ."""
4344 # URL with path that has trailing slash
4445 issuer_url = AnyHttpUrl ("http://localhost:8000/auth/" )
4546
@@ -50,9 +51,10 @@ def test_build_metadata_strips_trailing_slash_from_issuer_with_path():
5051 revocation_options = RevocationOptions (enabled = False ),
5152 )
5253
53- # The issuer should NOT have a trailing slash
54- assert str (metadata .issuer ) == "http://localhost:8000/auth"
55- assert not str (metadata .issuer ).endswith ("/" )
54+ # The serialized issuer should NOT have a trailing slash
55+ serialized = metadata .model_dump (mode = "json" )
56+ assert serialized ["issuer" ] == "http://localhost:8000/auth"
57+ assert not serialized ["issuer" ].endswith ("/" )
5658
5759
5860def test_build_metadata_endpoints_have_no_double_slashes ():
@@ -81,7 +83,7 @@ def test_build_metadata_endpoints_have_no_double_slashes():
8183
8284
8385def test_protected_resource_metadata_strips_trailing_slash_from_resource ():
84- """Test that protected resource metadata strips trailing slash from resource URL.
86+ """Test that protected resource metadata strips trailing slash from resource URL when serialized .
8587
8688 RFC 9728 §3 requires that the resource URL in the metadata response must be
8789 identical to the URL used for discovery.
@@ -104,13 +106,14 @@ def test_protected_resource_metadata_strips_trailing_slash_from_resource():
104106
105107 metadata = handler .__self__ .metadata # type: ignore
106108
107- # The resource URL should NOT have a trailing slash
108- assert str (metadata .resource ) == "http://localhost:8000"
109- assert not str (metadata .resource ).endswith ("/" )
109+ # The serialized resource URL should NOT have a trailing slash
110+ serialized = metadata .model_dump (mode = "json" )
111+ assert serialized ["resource" ] == "http://localhost:8000"
112+ assert not serialized ["resource" ].endswith ("/" )
110113
111114
112115def test_protected_resource_metadata_strips_trailing_slash_from_authorization_servers ():
113- """Test that protected resource metadata strips trailing slashes from authorization server URLs."""
116+ """Test that protected resource metadata strips trailing slashes from auth server URLs when serialized ."""
114117 resource_url = AnyHttpUrl ("http://localhost:8000/resource" )
115118 # Use bare hostname URLs which Pydantic will add trailing slashes to
116119 auth_servers = [
@@ -129,11 +132,12 @@ def test_protected_resource_metadata_strips_trailing_slash_from_authorization_se
129132 handler = cors_app .app .func # type: ignore
130133 metadata = handler .__self__ .metadata # type: ignore
131134
132- # All authorization server URLs should NOT have trailing slashes
133- assert str (metadata .authorization_servers [0 ]) == "http://auth1.example.com"
134- assert str (metadata .authorization_servers [1 ]) == "http://auth2.example.com"
135- assert not str (metadata .authorization_servers [0 ]).endswith ("/" )
136- assert not str (metadata .authorization_servers [1 ]).endswith ("/" )
135+ # All serialized authorization server URLs should NOT have trailing slashes
136+ serialized = metadata .model_dump (mode = "json" )
137+ assert serialized ["authorization_servers" ][0 ] == "http://auth1.example.com"
138+ assert serialized ["authorization_servers" ][1 ] == "http://auth2.example.com"
139+ assert not serialized ["authorization_servers" ][0 ].endswith ("/" )
140+ assert not serialized ["authorization_servers" ][1 ].endswith ("/" )
137141
138142
139143@pytest .fixture
0 commit comments