/test/netloc_test.py

http://github.com/zacharyvoase/urlobject · Python · 110 lines · 77 code · 27 blank · 6 comment · 0 complexity · 1ce9bb274f39bbab8c7204a4cd23651f MD5 · raw file

  1. import unittest
  2. from nose.tools import assert_raises
  3. from urlobject.netloc import Netloc
  4. class NetlocTest(unittest.TestCase):
  5. def test_preserves_equality_of_the_original_string(self):
  6. netloc = 'zack:1234@github.com:443'
  7. assert Netloc(netloc) == netloc
  8. def test_preserves_hash_of_the_original_string(self):
  9. netloc = 'zack:1234@github.com:443'
  10. assert hash(Netloc(netloc)) == hash(netloc)
  11. def test_username(self):
  12. assert Netloc('github.com').username is None
  13. assert Netloc('zack@github.com').username == 'zack'
  14. assert Netloc('zack:1234@github.com').username == 'zack'
  15. def test_with_username_adds_username(self):
  16. assert Netloc('github.com').with_username('zack') == 'zack@github.com'
  17. def test_with_username_replaces_username(self):
  18. assert (Netloc('zack@github.com').with_username('alice') ==
  19. 'alice@github.com')
  20. assert (Netloc('zack:1234@github.com').with_username('alice') ==
  21. 'alice:1234@github.com')
  22. def test_without_username_removes_username(self):
  23. assert Netloc('github.com').without_username() == 'github.com'
  24. assert Netloc('zack@github.com').without_username() == 'github.com'
  25. # Removing the username will also remove the password.
  26. assert Netloc('zack:1234@github.com:443').without_username() == 'github.com:443'
  27. def test_password(self):
  28. assert Netloc('github.com').password is None
  29. assert Netloc('zack@github.com').password is None
  30. assert Netloc('zack:1234@github.com').password == '1234'
  31. def test_with_password_adds_password(self):
  32. assert (Netloc('zack@github.com').with_password('1234') ==
  33. 'zack:1234@github.com')
  34. def test_with_password_replaces_password(self):
  35. assert (Netloc('zack:1234@github.com:443').with_password('5678') ==
  36. 'zack:5678@github.com:443')
  37. def test_with_password_on_a_netloc_with_no_username_raises_ValueError(self):
  38. assert_raises(ValueError,
  39. lambda: Netloc('github.com').with_password('1234'))
  40. def test_with_auth_with_one_arg_adds_username(self):
  41. assert (Netloc('github.com').with_auth('zack') ==
  42. 'zack@github.com')
  43. def test_auth(self):
  44. assert Netloc('github.com').auth == (None, None)
  45. assert Netloc('zack@github.com').auth == ('zack', None)
  46. assert Netloc('zack:1234@github.com').auth == ('zack', '1234')
  47. def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self):
  48. assert (Netloc('alice:1234@github.com').with_auth('zack') ==
  49. 'zack@github.com')
  50. def test_with_auth_with_two_args_adds_username_and_password(self):
  51. assert (Netloc('github.com').with_auth('zack', '1234') ==
  52. 'zack:1234@github.com')
  53. def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self):
  54. # Replaces username-only auth string
  55. assert (Netloc('alice@github.com').with_auth('zack', '1234') ==
  56. 'zack:1234@github.com')
  57. # Replaces username and password.
  58. assert (Netloc('alice:4567@github.com').with_auth('zack', '1234') ==
  59. 'zack:1234@github.com')
  60. def test_without_auth_removes_entire_auth_string(self):
  61. # No username or password => no-op.
  62. netloc = Netloc('github.com')
  63. assert netloc.without_auth() == 'github.com'
  64. # Username-only.
  65. netloc = Netloc('alice@github.com')
  66. assert netloc.without_auth() == 'github.com'
  67. # Username and password.
  68. netloc = Netloc('alice:1234@github.com')
  69. assert netloc.without_auth() == 'github.com'
  70. def test_hostname(self):
  71. assert Netloc('zack:1234@github.com:443').hostname == 'github.com'
  72. def test_with_hostname_replaces_hostname(self):
  73. assert (Netloc('zack:1234@github.com:443').with_hostname('example.com') ==
  74. 'zack:1234@example.com:443')
  75. def test_port(self):
  76. assert Netloc('github.com:443').port == 443
  77. assert Netloc('github.com').port is None
  78. def test_with_port_adds_port(self):
  79. assert Netloc('github.com').with_port(443) == 'github.com:443'
  80. def test_with_port_replaces_port(self):
  81. assert Netloc('github.com:443').with_port(80) == 'github.com:80'
  82. def test_without_port_removes_port(self):
  83. assert Netloc('github.com:443').without_port() == 'github.com'