radicale/tests/__init__.py

60 lines
2.0 KiB
Python
Raw Normal View History

2012-09-15 10:00:13 +02:00
# This file is part of Radicale Server - Calendar Server
2016-03-31 19:57:40 +02:00
# Copyright © 2012-2016 Guillaume Ayoub
2012-09-15 10:00:13 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
Tests for Radicale.
"""
import os
import sys
2013-09-05 15:13:31 +02:00
from io import BytesIO
2012-09-15 10:00:13 +02:00
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
2013-09-09 17:00:04 +02:00
os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
os.path.dirname(__file__)), "config")
2012-09-15 10:00:13 +02:00
class BaseTest(object):
"""Base class for tests."""
2013-09-05 15:13:31 +02:00
def request(self, method, path, data=None, **args):
2012-09-15 10:00:13 +02:00
"""Send a request."""
self.application._status = None
self.application._headers = None
self.application._answer = None
for key in args:
args[key.upper()] = args[key]
args["REQUEST_METHOD"] = method.upper()
args["PATH_INFO"] = path
2013-09-05 15:13:31 +02:00
if data:
2016-03-31 19:57:40 +02:00
data = data.encode("utf-8")
2013-09-05 15:13:31 +02:00
args["wsgi.input"] = BytesIO(data)
args["CONTENT_LENGTH"] = str(len(data))
2012-09-15 10:00:13 +02:00
self.application._answer = self.application(args, self.start_response)
return (
int(self.application._status.split()[0]),
dict(self.application._headers),
2013-09-05 15:13:31 +02:00
self.application._answer[0].decode("utf-8")
if self.application._answer else None)
2012-09-15 10:00:13 +02:00
def start_response(self, status, headers):
"""Put the response values into the current application."""
self.application._status = status
self.application._headers = headers