__init__.py 2.0 KB

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