__init__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 sys
  22. from io import BytesIO
  23. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  24. logger = logging.getLogger("radicale_test")
  25. if not logger.hasHandlers():
  26. handler = logging.StreamHandler(sys.stderr)
  27. handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  28. logger.addHandler(handler)
  29. logger.setLevel(logging.DEBUG)
  30. class BaseTest:
  31. """Base class for tests."""
  32. logger = logger
  33. def request(self, method, path, data=None, **args):
  34. """Send a request."""
  35. self.application._status = None
  36. self.application._headers = None
  37. self.application._answer = None
  38. for key in args:
  39. args[key.upper()] = args[key]
  40. args["REQUEST_METHOD"] = method.upper()
  41. args["PATH_INFO"] = path
  42. if data:
  43. data = data.encode("utf-8")
  44. args["wsgi.input"] = BytesIO(data)
  45. args["CONTENT_LENGTH"] = str(len(data))
  46. self.application._answer = self.application(args, self.start_response)
  47. return (
  48. int(self.application._status.split()[0]),
  49. dict(self.application._headers),
  50. self.application._answer[0].decode("utf-8")
  51. if self.application._answer else None)
  52. def start_response(self, status, headers):
  53. """Put the response values into the current application."""
  54. self.application._status = status
  55. self.application._headers = headers