test_server.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2018 Unrud<unrud@outlook.com>
  3. #
  4. # This library is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Test the internal server.
  18. """
  19. import shutil
  20. import socket
  21. import ssl
  22. import tempfile
  23. import threading
  24. import time
  25. from urllib import request
  26. from urllib.error import HTTPError, URLError
  27. from radicale import config, server
  28. from .helpers import get_file_path
  29. class DisabledRedirectHandler(request.HTTPRedirectHandler):
  30. def http_error_302(self, req, fp, code, msg, headers):
  31. raise HTTPError(req.full_url, code, msg, headers, fp)
  32. http_error_301 = http_error_303 = http_error_307 = http_error_302
  33. class TestBaseServerRequests:
  34. """Test the internal server."""
  35. def setup(self):
  36. self.configuration = config.load()
  37. self.colpath = tempfile.mkdtemp()
  38. self.configuration["storage"]["filesystem_folder"] = self.colpath
  39. # Disable syncing to disk for better performance
  40. self.configuration["internal"]["filesystem_fsync"] = "False"
  41. self.shutdown_socket, shutdown_socket_out = socket.socketpair()
  42. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  43. # Find available port
  44. sock.bind(("localhost", 0))
  45. self.sockname = sock.getsockname()
  46. self.configuration["server"]["hosts"] = "[%s]:%d" % self.sockname
  47. self.thread = threading.Thread(target=server.serve, args=(
  48. self.configuration, shutdown_socket_out))
  49. ssl_context = ssl.create_default_context()
  50. ssl_context.check_hostname = False
  51. ssl_context.verify_mode = ssl.CERT_NONE
  52. self.opener = request.build_opener(
  53. request.HTTPSHandler(context=ssl_context),
  54. DisabledRedirectHandler)
  55. def teardown(self):
  56. self.shutdown_socket.sendall(b" ")
  57. self.thread.join()
  58. shutil.rmtree(self.colpath)
  59. def request(self, method, path, data=None, **headers):
  60. """Send a request."""
  61. scheme = ("https" if self.configuration.getboolean("server", "ssl")
  62. else "http")
  63. req = request.Request(
  64. "%s://[%s]:%d%s" % (scheme, *self.sockname, path),
  65. data=data, headers=headers, method=method)
  66. while True:
  67. assert self.thread.is_alive()
  68. try:
  69. with self.opener.open(req) as f:
  70. return f.getcode(), f.info(), f.read().decode()
  71. except HTTPError as e:
  72. return e.code, e.headers, e.read().decode()
  73. except URLError as e:
  74. if not isinstance(e.reason, ConnectionRefusedError):
  75. raise
  76. time.sleep(0.1)
  77. def test_root(self):
  78. self.thread.start()
  79. status, _, _ = self.request("GET", "/")
  80. assert status == 302
  81. def test_ssl(self):
  82. self.configuration["server"]["ssl"] = "True"
  83. self.configuration["server"]["certificate"] = get_file_path("cert.pem")
  84. self.configuration["server"]["key"] = get_file_path("key.pem")
  85. self.thread.start()
  86. status, _, _ = self.request("GET", "/")
  87. assert status == 302