1
0

__init__.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import radicale
  25. from pytest_cov import embed
  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. # Allow importing of tests.custom....
  41. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  42. # Enable debug output
  43. radicale.log.logger.setLevel(logging.DEBUG)
  44. class BaseTest:
  45. """Base class for tests."""
  46. def request(self, method, path, data=None, **args):
  47. """Send a request."""
  48. self.application._status = None
  49. self.application._headers = None
  50. self.application._answer = None
  51. for key in args:
  52. args[key.upper()] = args[key]
  53. args["REQUEST_METHOD"] = method.upper()
  54. args["PATH_INFO"] = path
  55. if data:
  56. data = data.encode("utf-8")
  57. args["wsgi.input"] = BytesIO(data)
  58. args["CONTENT_LENGTH"] = str(len(data))
  59. args["wsgi.errors"] = sys.stderr
  60. status = headers = None
  61. def start_response(status_, headers_):
  62. nonlocal status, headers
  63. status = status_
  64. headers = headers_
  65. answer = self.application(args, start_response)
  66. return (int(status.split()[0]), dict(headers),
  67. answer[0].decode("utf-8") if answer else None)