test_endpoint.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.util.stringutils import parse_and_validate_server_name, parse_server_name
  15. from tests import unittest
  16. class ServerNameTestCase(unittest.TestCase):
  17. def test_parse_server_name(self):
  18. test_data = {
  19. "localhost": ("localhost", None),
  20. "my-example.com:1234": ("my-example.com", 1234),
  21. "1.2.3.4": ("1.2.3.4", None),
  22. "[0abc:1def::1234]": ("[0abc:1def::1234]", None),
  23. "1.2.3.4:1": ("1.2.3.4", 1),
  24. "[0abc:1def::1234]:8080": ("[0abc:1def::1234]", 8080),
  25. }
  26. for i, o in test_data.items():
  27. self.assertEqual(parse_server_name(i), o)
  28. def test_validate_bad_server_names(self):
  29. test_data = [
  30. "", # empty
  31. "localhost:http", # non-numeric port
  32. "1234]", # smells like ipv6 literal but isn't
  33. "[1234",
  34. "[1.2.3.4]",
  35. "underscore_.com",
  36. "percent%65.com",
  37. "newline.com\n",
  38. ".empty-label.com",
  39. "1234:5678:80", # too many colons
  40. ]
  41. for i in test_data:
  42. try:
  43. parse_and_validate_server_name(i)
  44. self.fail(
  45. "Expected parse_and_validate_server_name('%s') to throw" % (i,)
  46. )
  47. except ValueError:
  48. pass