__init__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2017 Guillaume Ayoub
  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. Tests for Radicale.
  18. """
  19. import logging
  20. import os
  21. import radicale
  22. import sys
  23. from io import BytesIO
  24. # Allow importing of tests.custom....
  25. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  26. # Enable debug output
  27. radicale.log.logger.setLevel(logging.DEBUG)
  28. class BaseTest:
  29. """Base class for tests."""
  30. def request(self, method, path, data=None, **args):
  31. """Send a request."""
  32. self.application._status = None
  33. self.application._headers = None
  34. self.application._answer = None
  35. for key in args:
  36. args[key.upper()] = args[key]
  37. args["REQUEST_METHOD"] = method.upper()
  38. args["PATH_INFO"] = path
  39. if data:
  40. data = data.encode("utf-8")
  41. args["wsgi.input"] = BytesIO(data)
  42. args["CONTENT_LENGTH"] = str(len(data))
  43. args["wsgi.errors"] = sys.stderr
  44. status = headers = None
  45. def start_response(status_, headers_):
  46. nonlocal status, headers
  47. status = status_
  48. headers = headers_
  49. answer = self.application(args, start_response)
  50. return (int(status.split()[0]), dict(headers),
  51. answer[0].decode("utf-8") if answer else None)