__init__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. for key in args:
  47. args[key.upper()] = args[key]
  48. args["REQUEST_METHOD"] = method.upper()
  49. args["PATH_INFO"] = path
  50. if data:
  51. data = data.encode()
  52. args["wsgi.input"] = BytesIO(data)
  53. args["CONTENT_LENGTH"] = str(len(data))
  54. args["wsgi.errors"] = sys.stderr
  55. status = headers = None
  56. def start_response(status_, headers_):
  57. nonlocal status, headers
  58. status = status_
  59. headers = headers_
  60. answer = self.application(args, start_response)
  61. return (int(status.split()[0]), dict(headers),
  62. answer[0].decode() if answer else None)