1
0

test_endpoint.py 1.8 KB

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