test_safe_url.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. #
  4. # Copyright (c) 2012 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. """Unit tests for class SafeURL"""
  19. import unittest
  20. from gitbuildsys.safe_url import SafeURL
  21. class SafeURLTest(unittest.TestCase):
  22. '''Test SafeURL class'''
  23. def test_passwd_no_user(self):
  24. '''raise ValueError if only given password'''
  25. self.assertRaises(ValueError, SafeURL, 'http://:password@server')
  26. def test_password_no_user_by_arg(self):
  27. '''raise ValueError if only given password'''
  28. self.assertRaises(ValueError, SafeURL, 'http://server', None, 'passwd')
  29. def test_both_user_and_password(self):
  30. '''both user and passwd are given'''
  31. url = SafeURL('http://server', 'Alice', 'password')
  32. self.assertEqual('http://server', url)
  33. self.assertEqual('http://Alice:password@server', url.full)
  34. def test_only_user_no_password(self):
  35. '''only user no password'''
  36. url = SafeURL('http://Alice@server')
  37. self.assertEqual('http://server', url)
  38. self.assertEqual('http://Alice@server', url.full)
  39. def test_no_user_and_no_password(self):
  40. '''no user and no passwd'''
  41. url = SafeURL('http://server')
  42. self.assertEqual('http://server', url)
  43. self.assertEqual(url, url.full)
  44. def test_port(self):
  45. '''port given'''
  46. url = SafeURL('http://Alice:password@server:8080')
  47. self.assertEqual('http://server:8080', str(url))
  48. self.assertEqual('http://Alice:password@server:8080', url.full)
  49. def test_escape_userinfo(self):
  50. '''user and passwd should be escape'''
  51. url = SafeURL('http://server', 'Alice', 'a;/?:@&=+$,b')
  52. self.assertEqual('http://Alice:a%3B%2F%3F%3A%40%26%3D%2B%24%2Cb@server',
  53. url.full)
  54. def test_join_a_file(self):
  55. '''join a file'''
  56. self.assertEqual('http://server/path/a/file.txt',
  57. SafeURL('http://server/path').pathjoin('a/file.txt'))
  58. def test_join_with_tailing_slash(self):
  59. '''join a file to url with tailing slash'''
  60. self.assertEqual('http://server/path/a/file.txt',
  61. SafeURL('http://server/path/').pathjoin('a/file.txt'))
  62. def test_join_a_dir(self):
  63. '''join a dir'''
  64. self.assertEqual('http://server/path/a/dir',
  65. SafeURL('http://server/path').pathjoin('a/dir'))
  66. def test_reduce_doubel_dot(self):
  67. '''reduce .. and get a path(alwasy with tailing slash)'''
  68. url = SafeURL('http://server/a/b/c')
  69. self.assertEqual('http://server/a/', url.pathjoin('../../'))
  70. self.assertEqual('http://server/a/', url.pathjoin('../..'))
  71. def test_local_path(self):
  72. '''local path should not change'''
  73. url = SafeURL('/local/path')
  74. self.assertEqual('/local/path', url)
  75. self.assertEqual(url, url.full)
  76. def test_local_path_need_not_auth(self):
  77. '''local path should ignore user and password'''
  78. url = SafeURL('/local/path', 'test', 'password')
  79. self.assertEqual('/local/path', url)
  80. self.assertEqual(url, url.full)