__init__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. class BaseTest:
  24. """Base class for tests."""
  25. def request(self, method, path, data=None, **args):
  26. """Send a request."""
  27. self.application._status = None
  28. self.application._headers = None
  29. self.application._answer = None
  30. for key in args:
  31. args[key.upper()] = args[key]
  32. args["REQUEST_METHOD"] = method.upper()
  33. args["PATH_INFO"] = path
  34. if data:
  35. data = data.encode("utf-8")
  36. args["wsgi.input"] = BytesIO(data)
  37. args["CONTENT_LENGTH"] = str(len(data))
  38. self.application._answer = self.application(args, self.start_response)
  39. return (
  40. int(self.application._status.split()[0]),
  41. dict(self.application._headers),
  42. self.application._answer[0].decode("utf-8")
  43. if self.application._answer else None)
  44. def start_response(self, status, headers):
  45. """Put the response values into the current application."""
  46. self.application._status = status
  47. self.application._headers = headers