1
0

test_endpoint.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. ":80": ("", 80),
  26. "": ("", None),
  27. }
  28. for i, o in test_data.items():
  29. self.assertEqual(parse_server_name(i), o)
  30. def test_validate_bad_server_names(self):
  31. test_data = [
  32. "", # empty
  33. "localhost:http", # non-numeric port
  34. "1234]", # smells like ipv6 literal but isn't
  35. "[1234",
  36. "[1.2.3.4]",
  37. "underscore_.com",
  38. "percent%65.com",
  39. "newline.com\n",
  40. ".empty-label.com",
  41. "1234:5678:80", # too many colons
  42. ":80",
  43. ]
  44. for i in test_data:
  45. try:
  46. parse_and_validate_server_name(i)
  47. self.fail(
  48. "Expected parse_and_validate_server_name('%s') to throw" % (i,)
  49. )
  50. except ValueError:
  51. pass