test_processors.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for processor functions.
  4. """
  5. import unittest
  6. from urlparse import urlparse
  7. from txclib.processors import hostname_tld_migration, hostname_ssl_migration
  8. class TestHostname(unittest.TestCase):
  9. """Test for hostname processors."""
  10. def test_tld_migration_needed(self):
  11. """
  12. Test the tld migration of Transifex, when needed.
  13. """
  14. hostnames = [
  15. 'http://transifex.net', 'http://www.transifex.net',
  16. 'https://fedora.transifex.net',
  17. ]
  18. for h in hostnames:
  19. hostname = hostname_tld_migration(h)
  20. self.assertTrue(hostname.endswith('com'))
  21. orig_hostname = 'http://www.transifex.net/path/'
  22. hostname = hostname_tld_migration(orig_hostname)
  23. self.assertEqual(hostname, orig_hostname.replace('net', 'com', 1))
  24. def test_tld_migration_needed(self):
  25. """
  26. Test that unneeded tld migrations are detected correctly.
  27. """
  28. hostnames = [
  29. 'https://www.transifex.com', 'http://fedora.transifex.com',
  30. 'http://www.example.net/path/'
  31. ]
  32. for h in hostnames:
  33. hostname = hostname_tld_migration(h)
  34. self.assertEqual(hostname, h)
  35. def test_no_scheme_specified(self):
  36. """
  37. Test that, if no scheme has been specified, the https one will be used.
  38. """
  39. hostname = '//transifex.net'
  40. hostname = hostname_ssl_migration(hostname)
  41. self.assertTrue(hostname.startswith('https://'))
  42. def test_http_replacement(self):
  43. """Test the replacement of http with https."""
  44. hostnames = [
  45. 'http://transifex.com', 'http://transifex.net/http/',
  46. 'http://www.transifex.com/path/'
  47. ]
  48. for h in hostnames:
  49. hostname = hostname_ssl_migration(h)
  50. self.assertEqual(hostname[:8], 'https://')
  51. self.assertEqual(hostname[7:], h[6:])
  52. def test_no_http_replacement_needed(self):
  53. """Test that http will not be replaces with https, when not needed."""
  54. for h in ['http://example.com', 'http://example.com/http/']:
  55. hostname = hostname_ssl_migration(h)
  56. self.assertEqual(hostname, hostname)