__init__.py 1.8 KB

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