2012-09-15 10:00:13 +02:00
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2017-05-27 17:28:07 +02:00
|
|
|
# Copyright © 2012-2017 Guillaume Ayoub
|
2018-09-04 03:33:47 +02:00
|
|
|
# Copyright © 2017-2018 Unrud<unrud@outlook.com>
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2018-08-21 18:43:44 +02:00
|
|
|
import logging
|
2012-09-15 10:00:13 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2013-09-05 15:13:31 +02:00
|
|
|
from io import BytesIO
|
2012-09-15 10:00:13 +02:00
|
|
|
|
2018-09-09 07:28:36 +02:00
|
|
|
from pytest_cov import embed
|
2019-06-15 09:01:55 +02:00
|
|
|
|
|
|
|
import radicale
|
2018-09-09 07:28:36 +02:00
|
|
|
from radicale import server
|
|
|
|
|
|
|
|
# Measure coverage of forked processes
|
|
|
|
finish_request = server.ParallelHTTPServer.finish_request
|
|
|
|
pid = os.getpid()
|
|
|
|
|
|
|
|
|
|
|
|
def finish_request_cov(self, request, client_address):
|
|
|
|
cov = None
|
|
|
|
if pid != os.getpid():
|
|
|
|
cov = embed.init()
|
|
|
|
try:
|
|
|
|
return finish_request(self, request, client_address)
|
|
|
|
finally:
|
|
|
|
if cov:
|
|
|
|
embed.cleanup(cov)
|
|
|
|
|
|
|
|
|
|
|
|
server.ParallelHTTPServer.finish_request = finish_request_cov
|
|
|
|
|
2018-04-29 23:09:58 +02:00
|
|
|
# Allow importing of tests.custom....
|
2012-09-15 10:00:13 +02:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
2018-08-21 18:43:44 +02:00
|
|
|
# Enable debug output
|
|
|
|
radicale.log.logger.setLevel(logging.DEBUG)
|
2012-09-15 10:00:13 +02:00
|
|
|
|
2013-09-09 15:36:30 +02:00
|
|
|
|
2016-04-22 11:37:02 +09:00
|
|
|
class BaseTest:
|
2012-09-15 10:00:13 +02:00
|
|
|
"""Base class for tests."""
|
2017-05-07 08:19:25 +02:00
|
|
|
|
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))
|
2018-08-16 07:59:56 +02:00
|
|
|
args["wsgi.errors"] = sys.stderr
|
2018-04-29 23:06:18 +02:00
|
|
|
status = headers = None
|
|
|
|
|
|
|
|
def start_response(status_, headers_):
|
|
|
|
nonlocal status, headers
|
|
|
|
status = status_
|
|
|
|
headers = headers_
|
|
|
|
answer = self.application(args, start_response)
|
|
|
|
|
|
|
|
return (int(status.split()[0]), dict(headers),
|
|
|
|
answer[0].decode("utf-8") if answer else None)
|