at main 5.0 kB view raw
1"""Test cases for the URI class in atpasser.uri module.""" 2 3import pytest 4from atpasser.uri import URI 5from atpasser.uri.exceptions import InvalidURIError, ValidationError 6 7 8class TestURI: 9 """Test cases for the URI class.""" 10 11 def test_valid_uri_with_did(self): 12 """Test creating a URI with a valid DID.""" 13 uri_str = ( 14 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 15 ) 16 uri = URI(uri_str) 17 18 assert str(uri) == uri_str 19 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 20 assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] 21 assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" 22 assert uri.query is None 23 assert uri.queryAsText is None 24 assert uri.fragment is None 25 assert uri.fragmentAsText is None 26 27 def test_valid_uri_with_handle(self): 28 """Test creating a URI with a valid handle.""" 29 uri_str = "at://bnewbold.bsky.team/app.bsky.feed.post/3jwdwj2ctlk26" 30 uri = URI(uri_str) 31 32 assert str(uri) == uri_str 33 assert uri.authorityAsText == "bnewbold.bsky.team" 34 assert uri.path == ["app.bsky.feed.post", "3jwdwj2ctlk26"] 35 assert uri.pathAsText == "app.bsky.feed.post/3jwdwj2ctlk26" 36 37 def test_valid_uri_with_collection_only(self): 38 """Test creating a URI with only a collection.""" 39 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" 40 uri = URI(uri_str) 41 42 assert str(uri) == uri_str 43 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 44 assert uri.path == ["app.bsky.feed.post"] 45 assert uri.pathAsText == "app.bsky.feed.post" 46 47 def test_valid_uri_with_authority_only(self): 48 """Test creating a URI with only an authority.""" 49 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur" 50 uri = URI(uri_str) 51 52 assert str(uri) == uri_str 53 assert uri.authorityAsText == "did:plc:z72i7hdynmk6r22z27h6tvur" 54 assert uri.path == [] 55 assert uri.pathAsText == "" 56 57 def test_valid_uri_with_query(self): 58 """Test creating a URI with query parameters.""" 59 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post?param1=value1&param2=value2" 60 uri = URI(uri_str) 61 62 assert uri.query == {"param1": ["value1"], "param2": ["value2"]} 63 assert uri.queryAsText == "param1%3Dvalue1%26param2%3Dvalue2" 64 65 def test_valid_uri_with_fragment(self): 66 """Test creating a URI with a fragment.""" 67 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26#$.some.json.path" 68 uri = URI(uri_str) 69 70 assert uri.fragment is not None 71 assert uri.fragmentAsText == "%24.some.json.path" 72 73 def test_invalid_uri_non_ascii_characters(self): 74 """Test that non-ASCII characters in URI raise InvalidURIError.""" 75 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/💩" 76 77 with pytest.raises(InvalidURIError, match="contains invalid characters"): 78 URI(uri_str) 79 80 def test_invalid_uri_too_long(self): 81 """Test that a URI that is too long raises InvalidURIError.""" 82 # Create a URI that exceeds the 8000 character limit 83 long_path = "a" * 8000 84 uri_str = f"at://did:plc:z72i7hdynmk6r22z27h6tvur/{long_path}" 85 86 with pytest.raises(InvalidURIError, match="exceeds maximum length"): 87 URI(uri_str) 88 89 def test_invalid_uri_wrong_scheme(self): 90 """Test that a URI with wrong scheme raises InvalidURIError.""" 91 uri_str = ( 92 "https://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 93 ) 94 95 with pytest.raises(InvalidURIError, match="invalid format"): 96 URI(uri_str) 97 98 def test_invalid_uri_trailing_slash(self): 99 """Test that a URI with trailing slash raises InvalidURIError.""" 100 uri_str = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/" 101 102 with pytest.raises(InvalidURIError, match="cannot end with a slash"): 103 URI(uri_str) 104 105 def test_invalid_uri_with_userinfo(self): 106 """Test that a URI with userinfo raises InvalidURIError.""" 107 uri_str = "at://user:pass@did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post" 108 109 with pytest.raises(InvalidURIError, match="does not support user information"): 110 URI(uri_str) 111 112 def test_uri_equality(self): 113 """Test URI equality comparison.""" 114 uri_str = ( 115 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 116 ) 117 uri1 = URI(uri_str) 118 uri2 = URI(uri_str) 119 120 assert uri1 == uri2 121 assert uri1 != "not a uri object" 122 123 def test_uri_string_representation(self): 124 """Test URI string representation.""" 125 uri_str = ( 126 "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.post/3jwdwj2ctlk26" 127 ) 128 uri = URI(uri_str) 129 130 assert str(uri) == uri_str