/test/netloc_test.py
Python | 110 lines | 77 code | 27 blank | 6 comment | 0 complexity | 1ce9bb274f39bbab8c7204a4cd23651f MD5 | raw file
Possible License(s): Unlicense
1import unittest 2 3from nose.tools import assert_raises 4 5from urlobject.netloc import Netloc 6 7 8class NetlocTest(unittest.TestCase): 9 10 def test_preserves_equality_of_the_original_string(self): 11 netloc = 'zack:1234@github.com:443' 12 assert Netloc(netloc) == netloc 13 14 def test_preserves_hash_of_the_original_string(self): 15 netloc = 'zack:1234@github.com:443' 16 assert hash(Netloc(netloc)) == hash(netloc) 17 18 def test_username(self): 19 assert Netloc('github.com').username is None 20 assert Netloc('zack@github.com').username == 'zack' 21 assert Netloc('zack:1234@github.com').username == 'zack' 22 23 def test_with_username_adds_username(self): 24 assert Netloc('github.com').with_username('zack') == 'zack@github.com' 25 26 def test_with_username_replaces_username(self): 27 assert (Netloc('zack@github.com').with_username('alice') == 28 'alice@github.com') 29 assert (Netloc('zack:1234@github.com').with_username('alice') == 30 'alice:1234@github.com') 31 32 def test_without_username_removes_username(self): 33 assert Netloc('github.com').without_username() == 'github.com' 34 assert Netloc('zack@github.com').without_username() == 'github.com' 35 # Removing the username will also remove the password. 36 assert Netloc('zack:1234@github.com:443').without_username() == 'github.com:443' 37 38 def test_password(self): 39 assert Netloc('github.com').password is None 40 assert Netloc('zack@github.com').password is None 41 assert Netloc('zack:1234@github.com').password == '1234' 42 43 def test_with_password_adds_password(self): 44 assert (Netloc('zack@github.com').with_password('1234') == 45 'zack:1234@github.com') 46 47 def test_with_password_replaces_password(self): 48 assert (Netloc('zack:1234@github.com:443').with_password('5678') == 49 'zack:5678@github.com:443') 50 51 def test_with_password_on_a_netloc_with_no_username_raises_ValueError(self): 52 assert_raises(ValueError, 53 lambda: Netloc('github.com').with_password('1234')) 54 55 def test_with_auth_with_one_arg_adds_username(self): 56 assert (Netloc('github.com').with_auth('zack') == 57 'zack@github.com') 58 59 def test_auth(self): 60 assert Netloc('github.com').auth == (None, None) 61 assert Netloc('zack@github.com').auth == ('zack', None) 62 assert Netloc('zack:1234@github.com').auth == ('zack', '1234') 63 64 def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self): 65 assert (Netloc('alice:1234@github.com').with_auth('zack') == 66 'zack@github.com') 67 68 def test_with_auth_with_two_args_adds_username_and_password(self): 69 assert (Netloc('github.com').with_auth('zack', '1234') == 70 'zack:1234@github.com') 71 72 def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self): 73 # Replaces username-only auth string 74 assert (Netloc('alice@github.com').with_auth('zack', '1234') == 75 'zack:1234@github.com') 76 77 # Replaces username and password. 78 assert (Netloc('alice:4567@github.com').with_auth('zack', '1234') == 79 'zack:1234@github.com') 80 81 def test_without_auth_removes_entire_auth_string(self): 82 # No username or password => no-op. 83 netloc = Netloc('github.com') 84 assert netloc.without_auth() == 'github.com' 85 # Username-only. 86 netloc = Netloc('alice@github.com') 87 assert netloc.without_auth() == 'github.com' 88 # Username and password. 89 netloc = Netloc('alice:1234@github.com') 90 assert netloc.without_auth() == 'github.com' 91 92 def test_hostname(self): 93 assert Netloc('zack:1234@github.com:443').hostname == 'github.com' 94 95 def test_with_hostname_replaces_hostname(self): 96 assert (Netloc('zack:1234@github.com:443').with_hostname('example.com') == 97 'zack:1234@example.com:443') 98 99 def test_port(self): 100 assert Netloc('github.com:443').port == 443 101 assert Netloc('github.com').port is None 102 103 def test_with_port_adds_port(self): 104 assert Netloc('github.com').with_port(443) == 'github.com:443' 105 106 def test_with_port_replaces_port(self): 107 assert Netloc('github.com:443').with_port(80) == 'github.com:80' 108 109 def test_without_port_removes_port(self): 110 assert Netloc('github.com:443').without_port() == 'github.com'