__init__.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  3. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Tests for Radicale.
  19. """
  20. import logging
  21. import os
  22. import sys
  23. from io import BytesIO
  24. from pytest_cov import embed
  25. import radicale
  26. from radicale import server
  27. # Measure coverage of forked processes
  28. finish_request = server.ParallelHTTPServer.finish_request
  29. pid = os.getpid()
  30. def finish_request_cov(self, request, client_address):
  31. cov = None
  32. if pid != os.getpid():
  33. cov = embed.init()
  34. try:
  35. return finish_request(self, request, client_address)
  36. finally:
  37. if cov:
  38. embed.cleanup(cov)
  39. server.ParallelHTTPServer.finish_request = finish_request_cov
  40. # Enable debug output
  41. radicale.log.logger.setLevel(logging.DEBUG)
  42. class BaseTest:
  43. """Base class for tests."""
  44. def request(self, method, path, data=None, **args):
  45. """Send a request."""
  46. self.application._status = None
  47. self.application._headers = None
  48. self.application._answer = None
  49. for key in args:
  50. args[key.upper()] = args[key]
  51. args["REQUEST_METHOD"] = method.upper()
  52. args["PATH_INFO"] = path
  53. if data:
  54. data = data.encode("utf-8")
  55. args["wsgi.input"] = BytesIO(data)
  56. args["CONTENT_LENGTH"] = str(len(data))
  57. args["wsgi.errors"] = sys.stderr
  58. status = headers = None
  59. def start_response(status_, headers_):
  60. nonlocal status, headers
  61. status = status_
  62. headers = headers_
  63. answer = self.application(args, start_response)
  64. return (int(status.split()[0]), dict(headers),
  65. answer[0].decode("utf-8") if answer else None)