__init__.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012-2013 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Tests for Radicale.
  20. """
  21. import os
  22. import sys
  23. from io import BytesIO
  24. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  25. os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
  26. os.path.dirname(__file__)), "config")
  27. from .helpers import get_file_content
  28. class BaseTest(object):
  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. if sys.version_info[0] >= 3:
  41. data = data.encode("utf-8")
  42. args["wsgi.input"] = BytesIO(data)
  43. args["CONTENT_LENGTH"] = str(len(data))
  44. self.application._answer = self.application(args, self.start_response)
  45. return (
  46. int(self.application._status.split()[0]),
  47. dict(self.application._headers),
  48. self.application._answer[0].decode("utf-8")
  49. if self.application._answer else None)
  50. def start_response(self, status, headers):
  51. """Put the response values into the current application."""
  52. self.application._status = status
  53. self.application._headers = headers