__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2012-2016 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. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  23. os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
  24. os.path.dirname(__file__)), "config")
  25. class BaseTest(object):
  26. """Base class for tests."""
  27. def request(self, method, path, data=None, **args):
  28. """Send a request."""
  29. self.application._status = None
  30. self.application._headers = None
  31. self.application._answer = None
  32. for key in args:
  33. args[key.upper()] = args[key]
  34. args["REQUEST_METHOD"] = method.upper()
  35. args["PATH_INFO"] = path
  36. if data:
  37. data = data.encode("utf-8")
  38. args["wsgi.input"] = BytesIO(data)
  39. args["CONTENT_LENGTH"] = str(len(data))
  40. self.application._answer = self.application(args, self.start_response)
  41. return (
  42. int(self.application._status.split()[0]),
  43. dict(self.application._headers),
  44. self.application._answer[0].decode("utf-8")
  45. if self.application._answer else None)
  46. def start_response(self, status, headers):
  47. """Put the response values into the current application."""
  48. self.application._status = status
  49. self.application._headers = headers