diff --git a/Headphones.py b/Headphones.py
index b6591d75..999c8d1f 100644
--- a/Headphones.py
+++ b/Headphones.py
@@ -122,6 +122,7 @@ def main():
'http_port': http_port,
'http_host': headphones.HTTP_HOST,
'http_root': headphones.HTTP_ROOT,
+ 'http_proxy': headphones.HTTP_PROXY,
'http_username': headphones.HTTP_USERNAME,
'http_password': headphones.HTTP_PASSWORD,
})
diff --git a/TODO b/TODO
new file mode 100644
index 00000000..f9066c0e
--- /dev/null
+++ b/TODO
@@ -0,0 +1,8 @@
+##############################
+### TODO LIST (18/08/12) ###
+##############################
+
+1. Add ability to fetch a specific extra, rather than pull down the whole category
+ - This requires extra calls to MB right now, so modify 1st mb artist call to pull down the
+ whole album list, then only send the desried ones to importer.py
+2. Update API with newstyle getExtras function (lacking entirely right now)
diff --git a/cherrypy/__init__.py b/cherrypy/__init__.py
index eb7cabf6..41e3898b 100644
--- a/cherrypy/__init__.py
+++ b/cherrypy/__init__.py
@@ -57,10 +57,10 @@ These API's are described in the CherryPy specification:
http://www.cherrypy.org/wiki/CherryPySpec
"""
-__version__ = "3.2.0"
+__version__ = "3.2.2"
from cherrypy._cpcompat import urljoin as _urljoin, urlencode as _urlencode
-from cherrypy._cpcompat import basestring, unicodestr
+from cherrypy._cpcompat import basestring, unicodestr, set
from cherrypy._cperror import HTTPError, HTTPRedirect, InternalRedirect
from cherrypy._cperror import NotFound, CherryPyException, TimeoutError
@@ -89,17 +89,21 @@ except ImportError:
engine = process.bus
-# Timeout monitor
+# Timeout monitor. We add two channels to the engine
+# to which cherrypy.Application will publish.
+engine.listeners['before_request'] = set()
+engine.listeners['after_request'] = set()
+
class _TimeoutMonitor(process.plugins.Monitor):
def __init__(self, bus):
self.servings = []
process.plugins.Monitor.__init__(self, bus, self.run)
- def acquire(self):
+ def before_request(self):
self.servings.append((serving.request, serving.response))
- def release(self):
+ def after_request(self):
try:
self.servings.remove((serving.request, serving.response))
except ValueError:
@@ -585,7 +589,7 @@ def url(path="", qs="", script_name=None, base=None, relative=None):
elif relative:
# "A relative reference that does not begin with a scheme name
# or a slash character is termed a relative-path reference."
- old = url().split('/')[:-1]
+ old = url(relative=False).split('/')[:-1]
new = newurl.split('/')
while old and new:
a, b = old[0], new[0]
diff --git a/cherrypy/_cpcompat.py b/cherrypy/_cpcompat.py
index 216ddddc..ed24c1ab 100644
--- a/cherrypy/_cpcompat.py
+++ b/cherrypy/_cpcompat.py
@@ -16,9 +16,11 @@ It also provides a 'base64_decode' function with native strings as input and
output.
"""
import os
+import re
import sys
if sys.version_info >= (3, 0):
+ py3k = True
bytestr = bytes
unicodestr = str
nativestr = unicodestr
@@ -31,12 +33,19 @@ if sys.version_info >= (3, 0):
"""Return the given native string as a unicode string with the given encoding."""
# In Python 3, the native string type is unicode
return n
+ def tonative(n, encoding='ISO-8859-1'):
+ """Return the given string as a native string in the given encoding."""
+ # In Python 3, the native string type is unicode
+ if isinstance(n, bytes):
+ return n.decode(encoding)
+ return n
# type("")
from io import StringIO
# bytes:
from io import BytesIO as BytesIO
else:
# Python 2
+ py3k = False
bytestr = str
unicodestr = unicode
nativestr = bytestr
@@ -49,10 +58,25 @@ else:
return n
def ntou(n, encoding='ISO-8859-1'):
"""Return the given native string as a unicode string with the given encoding."""
- # In Python 2, the native string type is bytes. Assume it's already
- # in the given encoding, which for ISO-8859-1 is almost always what
- # was intended.
+ # In Python 2, the native string type is bytes.
+ # First, check for the special encoding 'escape'. The test suite uses this
+ # to signal that it wants to pass a string with embedded \uXXXX escapes,
+ # but without having to prefix it with u'' for Python 2, but no prefix
+ # for Python 3.
+ if encoding == 'escape':
+ return unicode(
+ re.sub(r'\\u([0-9a-zA-Z]{4})',
+ lambda m: unichr(int(m.group(1), 16)),
+ n.decode('ISO-8859-1')))
+ # Assume it's already in the given encoding, which for ISO-8859-1 is almost
+ # always what was intended.
return n.decode(encoding)
+ def tonative(n, encoding='ISO-8859-1'):
+ """Return the given string as a native string in the given encoding."""
+ # In Python 2, the native string type is bytes.
+ if isinstance(n, unicode):
+ return n.encode(encoding)
+ return n
try:
# type("")
from cStringIO import StringIO
@@ -185,6 +209,18 @@ except ImportError:
from http.client import BadStatusLine, HTTPConnection, HTTPSConnection, IncompleteRead, NotConnected
from http.server import BaseHTTPRequestHandler
+try:
+ # Python 2. We have to do it in this order so Python 2 builds
+ # don't try to import the 'http' module from cherrypy.lib
+ from httplib import HTTPSConnection
+except ImportError:
+ try:
+ # Python 3
+ from http.client import HTTPSConnection
+ except ImportError:
+ # Some platforms which don't have SSL don't expose HTTPSConnection
+ HTTPSConnection = None
+
try:
# Python 2
xrange = xrange
@@ -229,7 +265,7 @@ try:
json_decode = json.JSONDecoder().decode
json_encode = json.JSONEncoder().iterencode
except ImportError:
- if sys.version_info >= (3, 0):
+ if py3k:
# Python 3.0: json is part of the standard library,
# but outputs unicode. We need bytes.
import json
@@ -280,4 +316,3 @@ except NameError:
# Python 2
def next(i):
return i.next()
-
diff --git a/cherrypy/_cpdispatch.py b/cherrypy/_cpdispatch.py
index 7250ac92..d614e086 100644
--- a/cherrypy/_cpdispatch.py
+++ b/cherrypy/_cpdispatch.py
@@ -12,8 +12,13 @@ to a hierarchical arrangement of objects, starting at request.app.root.
import string
import sys
import types
+try:
+ classtype = (type, types.ClassType)
+except AttributeError:
+ classtype = type
import cherrypy
+from cherrypy._cpcompat import set
class PageHandler(object):
@@ -197,8 +202,18 @@ class LateParamPageHandler(PageHandler):
'cherrypy.request.params copied in)')
-punctuation_to_underscores = string.maketrans(
- string.punctuation, '_' * len(string.punctuation))
+if sys.version_info < (3, 0):
+ punctuation_to_underscores = string.maketrans(
+ string.punctuation, '_' * len(string.punctuation))
+ def validate_translator(t):
+ if not isinstance(t, str) or len(t) != 256:
+ raise ValueError("The translate argument must be a str of len 256.")
+else:
+ punctuation_to_underscores = str.maketrans(
+ string.punctuation, '_' * len(string.punctuation))
+ def validate_translator(t):
+ if not isinstance(t, dict):
+ raise ValueError("The translate argument must be a dict.")
class Dispatcher(object):
"""CherryPy Dispatcher which walks a tree of objects to find a handler.
@@ -222,8 +237,7 @@ class Dispatcher(object):
def __init__(self, dispatch_method_name=None,
translate=punctuation_to_underscores):
- if not isinstance(translate, str) or len(translate) != 256:
- raise ValueError("The translate argument must be a str of len 256.")
+ validate_translator(translate)
self.translate = translate
if dispatch_method_name:
self.dispatch_method_name = dispatch_method_name
@@ -524,7 +538,7 @@ class RoutesDispatcher(object):
controller = result.get('controller')
controller = self.controllers.get(controller, controller)
if controller:
- if isinstance(controller, (type, types.ClassType)):
+ if isinstance(controller, classtype):
controller = controller()
# Get config from the controller.
if hasattr(controller, "_cp_config"):
@@ -550,9 +564,9 @@ class RoutesDispatcher(object):
def XMLRPCDispatcher(next_dispatcher=Dispatcher()):
- from cherrypy.lib import xmlrpc
+ from cherrypy.lib import xmlrpcutil
def xmlrpc_dispatch(path_info):
- path_info = xmlrpc.patched_path(path_info)
+ path_info = xmlrpcutil.patched_path(path_info)
return next_dispatcher(path_info)
return xmlrpc_dispatch
diff --git a/cherrypy/_cperror.py b/cherrypy/_cperror.py
index 00e5b532..76a409ff 100644
--- a/cherrypy/_cperror.py
+++ b/cherrypy/_cperror.py
@@ -107,7 +107,7 @@ and not simply return an error message as a result.
from cgi import escape as _escape
from sys import exc_info as _exc_info
from traceback import format_exception as _format_exception
-from cherrypy._cpcompat import basestring, iteritems, urljoin as _urljoin
+from cherrypy._cpcompat import basestring, bytestr, iteritems, ntob, tonative, urljoin as _urljoin
from cherrypy.lib import httputil as _httputil
@@ -183,7 +183,7 @@ class HTTPRedirect(CherryPyException):
"""The list of URL's to emit."""
encoding = 'utf-8'
- """The encoding when passed urls are unicode objects"""
+ """The encoding when passed urls are not native strings"""
def __init__(self, urls, status=None, encoding=None):
import cherrypy
@@ -194,8 +194,7 @@ class HTTPRedirect(CherryPyException):
abs_urls = []
for url in urls:
- if isinstance(url, unicode):
- url = url.encode(encoding or self.encoding)
+ url = tonative(url, encoding or self.encoding)
# Note that urljoin will "do the right thing" whether url is:
# 1. a complete URL with host (e.g. "http://www.example.com/test")
@@ -248,7 +247,7 @@ class HTTPRedirect(CherryPyException):
307: "This resource has moved temporarily to %s.",
}[status]
msgs = [msg % (u, u) for u in self.urls]
- response.body = "
\n".join(msgs)
+ response.body = ntob("
\n".join(msgs), 'utf-8')
# Previous code may have set C-L, so we have to reset it
# (allow finalize to set it).
response.headers.pop('Content-Length', None)
@@ -341,8 +340,8 @@ class HTTPError(CherryPyException):
self.status = status
try:
self.code, self.reason, defaultmsg = _httputil.valid_status(status)
- except ValueError, x:
- raise self.__class__(500, x.args[0])
+ except ValueError:
+ raise self.__class__(500, _exc_info()[1].args[0])
if self.code < 400 or self.code > 599:
raise ValueError("status must be between 400 and 599.")
@@ -373,8 +372,8 @@ class HTTPError(CherryPyException):
response.headers['Content-Type'] = "text/html;charset=utf-8"
response.headers.pop('Content-Length', None)
- content = self.get_error_page(self.status, traceback=tb,
- message=self._message)
+ content = ntob(self.get_error_page(self.status, traceback=tb,
+ message=self._message), 'utf-8')
response.body = content
_be_ie_unfriendly(self.code)
@@ -442,8 +441,8 @@ def get_error_page(status, **kwargs):
try:
code, reason, message = _httputil.valid_status(status)
- except ValueError, x:
- raise cherrypy.HTTPError(500, x.args[0])
+ except ValueError:
+ raise cherrypy.HTTPError(500, _exc_info()[1].args[0])
# We can't use setdefault here, because some
# callers send None for kwarg values.
@@ -470,7 +469,8 @@ def get_error_page(status, **kwargs):
if hasattr(error_page, '__call__'):
return error_page(**kwargs)
else:
- return open(error_page, 'rb').read() % kwargs
+ data = open(error_page, 'rb').read()
+ return tonative(data) % kwargs
except:
e = _format_exception(*_exc_info())[-1]
m = kwargs['message']
@@ -508,19 +508,22 @@ def _be_ie_unfriendly(status):
if l and l < s:
# IN ADDITION: the response must be written to IE
# in one chunk or it will still get replaced! Bah.
- content = content + (" " * (s - l))
+ content = content + (ntob(" ") * (s - l))
response.body = content
response.headers['Content-Length'] = str(len(content))
def format_exc(exc=None):
"""Return exc (or sys.exc_info if None), formatted."""
- if exc is None:
- exc = _exc_info()
- if exc == (None, None, None):
- return ""
- import traceback
- return "".join(traceback.format_exception(*exc))
+ try:
+ if exc is None:
+ exc = _exc_info()
+ if exc == (None, None, None):
+ return ""
+ import traceback
+ return "".join(traceback.format_exception(*exc))
+ finally:
+ del exc
def bare_error(extrabody=None):
"""Produce status, headers, body for a critical error.
@@ -539,15 +542,15 @@ def bare_error(extrabody=None):
# it cannot be allowed to fail. Therefore, don't add to it!
# In particular, don't call any other CP functions.
- body = "Unrecoverable error in the server."
+ body = ntob("Unrecoverable error in the server.")
if extrabody is not None:
- if not isinstance(extrabody, str):
+ if not isinstance(extrabody, bytestr):
extrabody = extrabody.encode('utf-8')
- body += "\n" + extrabody
+ body += ntob("\n") + extrabody
- return ("500 Internal Server Error",
- [('Content-Type', 'text/plain'),
- ('Content-Length', str(len(body)))],
+ return (ntob("500 Internal Server Error"),
+ [(ntob('Content-Type'), ntob('text/plain')),
+ (ntob('Content-Length'), ntob(str(len(body)),'ISO-8859-1'))],
[body])
diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py
index d6ca979e..e10c9420 100644
--- a/cherrypy/_cplogging.py
+++ b/cherrypy/_cplogging.py
@@ -109,6 +109,20 @@ import sys
import cherrypy
from cherrypy import _cperror
+from cherrypy._cpcompat import ntob, py3k
+
+
+class NullHandler(logging.Handler):
+ """A no-op logging handler to silence the logging.lastResort handler."""
+
+ def handle(self, record):
+ pass
+
+ def emit(self, record):
+ pass
+
+ def createLock(self):
+ self.lock = None
class LogManager(object):
@@ -127,8 +141,12 @@ class LogManager(object):
access_log = None
"""The actual :class:`logging.Logger` instance for access messages."""
- access_log_format = \
- '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
+ if py3k:
+ access_log_format = \
+ '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}"'
+ else:
+ access_log_format = \
+ '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
logger_root = None
"""The "top-level" logger name.
@@ -152,8 +170,13 @@ class LogManager(object):
self.access_log = logging.getLogger("%s.access.%s" % (logger_root, appid))
self.error_log.setLevel(logging.INFO)
self.access_log.setLevel(logging.INFO)
+
+ # Silence the no-handlers "warning" (stderr write!) in stdlib logging
+ self.error_log.addHandler(NullHandler())
+ self.access_log.addHandler(NullHandler())
+
cherrypy.engine.subscribe('graceful', self.reopen_files)
-
+
def reopen_files(self):
"""Close and reopen all file handlers."""
for log in (self.error_log, self.access_log):
@@ -206,7 +229,9 @@ class LogManager(object):
if response.output_status is None:
status = "-"
else:
- status = response.output_status.split(" ", 1)[0]
+ status = response.output_status.split(ntob(" "), 1)[0]
+ if py3k:
+ status = status.decode('ISO-8859-1')
atoms = {'h': remote.name or remote.ip,
'l': '-',
@@ -218,21 +243,43 @@ class LogManager(object):
'f': dict.get(inheaders, 'Referer', ''),
'a': dict.get(inheaders, 'User-Agent', ''),
}
- for k, v in atoms.items():
- if isinstance(v, unicode):
- v = v.encode('utf8')
- elif not isinstance(v, str):
- v = str(v)
- # Fortunately, repr(str) escapes unprintable chars, \n, \t, etc
- # and backslash for us. All we have to do is strip the quotes.
- v = repr(v)[1:-1]
- # Escape double-quote.
- atoms[k] = v.replace('"', '\\"')
-
- try:
- self.access_log.log(logging.INFO, self.access_log_format % atoms)
- except:
- self(traceback=True)
+ if py3k:
+ for k, v in atoms.items():
+ if not isinstance(v, str):
+ v = str(v)
+ v = v.replace('"', '\\"').encode('utf8')
+ # Fortunately, repr(str) escapes unprintable chars, \n, \t, etc
+ # and backslash for us. All we have to do is strip the quotes.
+ v = repr(v)[2:-1]
+
+ # in python 3.0 the repr of bytes (as returned by encode)
+ # uses double \'s. But then the logger escapes them yet, again
+ # resulting in quadruple slashes. Remove the extra one here.
+ v = v.replace('\\\\', '\\')
+
+ # Escape double-quote.
+ atoms[k] = v
+
+ try:
+ self.access_log.log(logging.INFO, self.access_log_format.format(**atoms))
+ except:
+ self(traceback=True)
+ else:
+ for k, v in atoms.items():
+ if isinstance(v, unicode):
+ v = v.encode('utf8')
+ elif not isinstance(v, str):
+ v = str(v)
+ # Fortunately, repr(str) escapes unprintable chars, \n, \t, etc
+ # and backslash for us. All we have to do is strip the quotes.
+ v = repr(v)[1:-1]
+ # Escape double-quote.
+ atoms[k] = v.replace('"', '\\"')
+
+ try:
+ self.access_log.log(logging.INFO, self.access_log_format % atoms)
+ except:
+ self(traceback=True)
def time(self):
"""Return now() in Apache Common Log Format (no timezone)."""
diff --git a/cherrypy/_cpmodpy.py b/cherrypy/_cpmodpy.py
index ba2ab22f..76ef6ead 100644
--- a/cherrypy/_cpmodpy.py
+++ b/cherrypy/_cpmodpy.py
@@ -224,7 +224,7 @@ def handler(req):
qs = ir.query_string
rfile = BytesIO()
- send_response(req, response.status, response.header_list,
+ send_response(req, response.output_status, response.header_list,
response.body, response.stream)
finally:
app.release_serving()
@@ -266,11 +266,22 @@ def send_response(req, status, headers, body, stream=False):
import os
import re
+try:
+ import subprocess
+ def popen(fullcmd):
+ p = subprocess.Popen(fullcmd, shell=True,
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+ close_fds=True)
+ return p.stdout
+except ImportError:
+ def popen(fullcmd):
+ pipein, pipeout = os.popen4(fullcmd)
+ return pipeout
def read_process(cmd, args=""):
fullcmd = "%s %s" % (cmd, args)
- pipein, pipeout = os.popen4(fullcmd)
+ pipeout = popen(fullcmd)
try:
firstline = pipeout.readline()
if (re.search(ntob("(not recognized|No such file|not found)"), firstline,
diff --git a/cherrypy/_cpreqbody.py b/cherrypy/_cpreqbody.py
index 1b0496e3..5d72c854 100644
--- a/cherrypy/_cpreqbody.py
+++ b/cherrypy/_cpreqbody.py
@@ -101,10 +101,28 @@ If we were defining a custom processor, we can do so without making a ``Tool``.
Note that you can only replace the ``processors`` dict wholesale this way, not update the existing one.
"""
+try:
+ from io import DEFAULT_BUFFER_SIZE
+except ImportError:
+ DEFAULT_BUFFER_SIZE = 8192
import re
import sys
import tempfile
-from urllib import unquote_plus
+try:
+ from urllib import unquote_plus
+except ImportError:
+ def unquote_plus(bs):
+ """Bytes version of urllib.parse.unquote_plus."""
+ bs = bs.replace(ntob('+'), ntob(' '))
+ atoms = bs.split(ntob('%'))
+ for i in range(1, len(atoms)):
+ item = atoms[i]
+ try:
+ pct = int(item[:2], 16)
+ atoms[i] = bytes([pct]) + item[2:]
+ except ValueError:
+ pass
+ return ntob('').join(atoms)
import cherrypy
from cherrypy._cpcompat import basestring, ntob, ntou
@@ -399,7 +417,6 @@ class Entity(object):
# Copy the class 'attempt_charsets', prepending any Content-Type charset
dec = self.content_type.params.get("charset", None)
if dec:
- #dec = dec.decode('ISO-8859-1')
self.attempt_charsets = [dec] + [c for c in self.attempt_charsets
if c != dec]
else:
@@ -446,11 +463,14 @@ class Entity(object):
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
line = self.readline()
if not line:
raise StopIteration
return line
+
+ def next(self):
+ return self.__next__()
def read_into_file(self, fp_out=None):
"""Read the request body into fp_out (or make_file() if None). Return fp_out."""
@@ -671,13 +691,16 @@ class Part(Entity):
Entity.part_class = Part
-
-class Infinity(object):
- def __cmp__(self, other):
- return 1
- def __sub__(self, other):
- return self
-inf = Infinity()
+try:
+ inf = float('inf')
+except ValueError:
+ # Python 2.4 and lower
+ class Infinity(object):
+ def __cmp__(self, other):
+ return 1
+ def __sub__(self, other):
+ return self
+ inf = Infinity()
comma_separated_headers = ['Accept', 'Accept-Charset', 'Accept-Encoding',
@@ -689,7 +712,7 @@ comma_separated_headers = ['Accept', 'Accept-Charset', 'Accept-Encoding',
class SizedReader:
- def __init__(self, fp, length, maxbytes, bufsize=8192, has_trailers=False):
+ def __init__(self, fp, length, maxbytes, bufsize=DEFAULT_BUFFER_SIZE, has_trailers=False):
# Wrap our fp in a buffer so peek() works
self.fp = fp
self.length = length
@@ -930,8 +953,9 @@ class RequestBody(Entity):
request_params = self.request_params
for key, value in self.params.items():
# Python 2 only: keyword arguments must be byte strings (type 'str').
- if isinstance(key, unicode):
- key = key.encode('ISO-8859-1')
+ if sys.version_info < (3, 0):
+ if isinstance(key, unicode):
+ key = key.encode('ISO-8859-1')
if key in request_params:
if not isinstance(request_params[key], list):
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py
index ae5e8971..5890c728 100644
--- a/cherrypy/_cprequest.py
+++ b/cherrypy/_cprequest.py
@@ -6,7 +6,7 @@ import warnings
import cherrypy
from cherrypy._cpcompat import basestring, copykeys, ntob, unicodestr
-from cherrypy._cpcompat import SimpleCookie, CookieError
+from cherrypy._cpcompat import SimpleCookie, CookieError, py3k
from cherrypy import _cpreqbody, _cpconfig
from cherrypy._cperror import format_exc, bare_error
from cherrypy.lib import httputil, file_generator
@@ -49,7 +49,12 @@ class Hook(object):
self.kwargs = kwargs
+ def __lt__(self, other):
+ # Python 3
+ return self.priority < other.priority
+
def __cmp__(self, other):
+ # Python 2
return cmp(self.priority, other.priority)
def __call__(self):
@@ -104,7 +109,7 @@ class HookMap(dict):
exc = sys.exc_info()[1]
cherrypy.log(traceback=True, severity=40)
if exc:
- raise
+ raise exc
def __copy__(self):
newmap = self.__class__()
@@ -488,14 +493,20 @@ class Request(object):
self.stage = 'close'
def run(self, method, path, query_string, req_protocol, headers, rfile):
- """Process the Request. (Core)
+ r"""Process the Request. (Core)
method, path, query_string, and req_protocol should be pulled directly
from the Request-Line (e.g. "GET /path?key=val HTTP/1.0").
path
This should be %XX-unquoted, but query_string should not be.
- They both MUST be byte strings, not unicode strings.
+
+ When using Python 2, they both MUST be byte strings,
+ not unicode strings.
+
+ When using Python 3, they both MUST be unicode strings,
+ not byte strings, and preferably not bytes \x00-\xFF
+ disguised as unicode.
headers
A list of (name, value) tuples.
@@ -676,10 +687,11 @@ class Request(object):
self.query_string_encoding)
# Python 2 only: keyword arguments must be byte strings (type 'str').
- for key, value in p.items():
- if isinstance(key, unicode):
- del p[key]
- p[key.encode(self.query_string_encoding)] = value
+ if not py3k:
+ for key, value in p.items():
+ if isinstance(key, unicode):
+ del p[key]
+ p[key.encode(self.query_string_encoding)] = value
self.params.update(p)
def process_headers(self):
@@ -770,6 +782,10 @@ class Request(object):
class ResponseBody(object):
"""The body of the HTTP response (the response entity)."""
+ if py3k:
+ unicode_err = ("Page handlers MUST return bytes. Use tools.encode "
+ "if you wish to return unicode.")
+
def __get__(self, obj, objclass=None):
if obj is None:
# When calling on the class instead of an instance...
@@ -779,6 +795,9 @@ class ResponseBody(object):
def __set__(self, obj, value):
# Convert the given value to an iterable object.
+ if py3k and isinstance(value, str):
+ raise ValueError(self.unicode_err)
+
if isinstance(value, basestring):
# strings get wrapped in a list because iterating over a single
# item list is much faster than iterating over every character
@@ -788,6 +807,11 @@ class ResponseBody(object):
else:
# [''] doesn't evaluate to False, so replace it with [].
value = []
+ elif py3k and isinstance(value, list):
+ # every item in a list must be bytes...
+ for i, item in enumerate(value):
+ if isinstance(item, str):
+ raise ValueError(self.unicode_err)
# Don't use isinstance here; io.IOBase which has an ABC takes
# 1000 times as long as, say, isinstance(value, str)
elif hasattr(value, 'read'):
@@ -862,7 +886,12 @@ class Response(object):
if isinstance(self.body, basestring):
return self.body
- newbody = ''.join([chunk for chunk in self.body])
+ newbody = []
+ for chunk in self.body:
+ if py3k and not isinstance(chunk, bytes):
+ raise TypeError("Chunk %s is not of type 'bytes'." % repr(chunk))
+ newbody.append(chunk)
+ newbody = ntob('').join(newbody)
self.body = newbody
return newbody
@@ -876,6 +905,7 @@ class Response(object):
headers = self.headers
+ self.status = "%s %s" % (code, reason)
self.output_status = ntob(str(code), 'ascii') + ntob(" ") + headers.encode(reason)
if self.stream:
diff --git a/cherrypy/_cpserver.py b/cherrypy/_cpserver.py
index c1695a66..2eecd6ec 100644
--- a/cherrypy/_cpserver.py
+++ b/cherrypy/_cpserver.py
@@ -4,7 +4,7 @@ import warnings
import cherrypy
from cherrypy.lib import attributes
-from cherrypy._cpcompat import basestring
+from cherrypy._cpcompat import basestring, py3k
# We import * because we want to export check_port
# et al as attributes of this module.
@@ -98,12 +98,22 @@ class Server(ServerAdapter):
ssl_private_key = None
"""The filename of the private key to use with SSL."""
- ssl_module = 'pyopenssl'
- """The name of a registered SSL adaptation module to use with the builtin
- WSGI server. Builtin options are 'builtin' (to use the SSL library built
- into recent versions of Python) and 'pyopenssl' (to use the PyOpenSSL
- project, which you must install separately). You may also register your
- own classes in the wsgiserver.ssl_adapters dict."""
+ if py3k:
+ ssl_module = 'builtin'
+ """The name of a registered SSL adaptation module to use with the builtin
+ WSGI server. Builtin options are: 'builtin' (to use the SSL library built
+ into recent versions of Python). You may also register your
+ own classes in the wsgiserver.ssl_adapters dict."""
+ else:
+ ssl_module = 'pyopenssl'
+ """The name of a registered SSL adaptation module to use with the builtin
+ WSGI server. Builtin options are 'builtin' (to use the SSL library built
+ into recent versions of Python) and 'pyopenssl' (to use the PyOpenSSL
+ project, which you must install separately). You may also register your
+ own classes in the wsgiserver.ssl_adapters dict."""
+
+ statistics = False
+ """Turns statistics-gathering on or off for aware HTTP servers."""
nodelay = True
"""If True (the default since 3.1), sets the TCP_NODELAY socket option."""
diff --git a/cherrypy/_cptools.py b/cherrypy/_cptools.py
index d3eab059..22316b31 100644
--- a/cherrypy/_cptools.py
+++ b/cherrypy/_cptools.py
@@ -243,7 +243,7 @@ class ErrorTool(Tool):
# Builtin tools #
from cherrypy.lib import cptools, encoding, auth, static, jsontools
-from cherrypy.lib import sessions as _sessions, xmlrpc as _xmlrpc
+from cherrypy.lib import sessions as _sessions, xmlrpcutil as _xmlrpc
from cherrypy.lib import caching as _caching
from cherrypy.lib import auth_basic, auth_digest
@@ -367,7 +367,7 @@ class XMLRPCController(object):
# http://www.cherrypy.org/ticket/533
# if a method is not found, an xmlrpclib.Fault should be returned
# raising an exception here will do that; see
- # cherrypy.lib.xmlrpc.on_error
+ # cherrypy.lib.xmlrpcutil.on_error
raise Exception('method "%s" is not supported' % attr)
conf = cherrypy.serving.request.toolmaps['tools'].get("xmlrpc", {})
diff --git a/cherrypy/_cptree.py b/cherrypy/_cptree.py
index 67ce5465..3aa4b9e1 100644
--- a/cherrypy/_cptree.py
+++ b/cherrypy/_cptree.py
@@ -1,8 +1,10 @@
"""CherryPy Application and Tree objects."""
import os
+import sys
+
import cherrypy
-from cherrypy._cpcompat import ntou
+from cherrypy._cpcompat import ntou, py3k
from cherrypy import _cpconfig, _cplogging, _cprequest, _cpwsgi, tools
from cherrypy.lib import httputil
@@ -123,8 +125,8 @@ class Application(object):
resp = self.response_class()
cherrypy.serving.load(req, resp)
- cherrypy.engine.timeout_monitor.acquire()
cherrypy.engine.publish('acquire_thread')
+ cherrypy.engine.publish('before_request')
return req, resp
@@ -132,7 +134,7 @@ class Application(object):
"""Release the current serving (request and response)."""
req = cherrypy.serving.request
- cherrypy.engine.timeout_monitor.release()
+ cherrypy.engine.publish('after_request')
try:
req.close()
@@ -266,14 +268,23 @@ class Tree(object):
# Correct the SCRIPT_NAME and PATH_INFO environ entries.
environ = environ.copy()
- if environ.get(u'wsgi.version') == (u'u', 0):
- # Python 2/WSGI u.0: all strings MUST be of type unicode
- enc = environ[u'wsgi.url_encoding']
- environ[u'SCRIPT_NAME'] = sn.decode(enc)
- environ[u'PATH_INFO'] = path[len(sn.rstrip("/")):].decode(enc)
+ if not py3k:
+ if environ.get(ntou('wsgi.version')) == (ntou('u'), 0):
+ # Python 2/WSGI u.0: all strings MUST be of type unicode
+ enc = environ[ntou('wsgi.url_encoding')]
+ environ[ntou('SCRIPT_NAME')] = sn.decode(enc)
+ environ[ntou('PATH_INFO')] = path[len(sn.rstrip("/")):].decode(enc)
+ else:
+ # Python 2/WSGI 1.x: all strings MUST be of type str
+ environ['SCRIPT_NAME'] = sn
+ environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
else:
- # Python 2/WSGI 1.x: all strings MUST be of type str
- environ['SCRIPT_NAME'] = sn
- environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
+ if environ.get(ntou('wsgi.version')) == (ntou('u'), 0):
+ # Python 3/WSGI u.0: all strings MUST be full unicode
+ environ['SCRIPT_NAME'] = sn
+ environ['PATH_INFO'] = path[len(sn.rstrip("/")):]
+ else:
+ # Python 3/WSGI 1.x: all strings MUST be ISO-8859-1 str
+ environ['SCRIPT_NAME'] = sn.encode('utf-8').decode('ISO-8859-1')
+ environ['PATH_INFO'] = path[len(sn.rstrip("/")):].encode('utf-8').decode('ISO-8859-1')
return app(environ, start_response)
-
diff --git a/cherrypy/_cpwsgi.py b/cherrypy/_cpwsgi.py
index aa4b7631..91cd044e 100644
--- a/cherrypy/_cpwsgi.py
+++ b/cherrypy/_cpwsgi.py
@@ -10,7 +10,7 @@ still be translatable to bytes via the Latin-1 encoding!"
import sys as _sys
import cherrypy as _cherrypy
-from cherrypy._cpcompat import BytesIO
+from cherrypy._cpcompat import BytesIO, bytestr, ntob, ntou, py3k, unicodestr
from cherrypy import _cperror
from cherrypy.lib import httputil
@@ -19,11 +19,11 @@ def downgrade_wsgi_ux_to_1x(environ):
"""Return a new environ dict for WSGI 1.x from the given WSGI u.x environ."""
env1x = {}
- url_encoding = environ[u'wsgi.url_encoding']
- for k, v in environ.items():
- if k in [u'PATH_INFO', u'SCRIPT_NAME', u'QUERY_STRING']:
+ url_encoding = environ[ntou('wsgi.url_encoding')]
+ for k, v in list(environ.items()):
+ if k in [ntou('PATH_INFO'), ntou('SCRIPT_NAME'), ntou('QUERY_STRING')]:
v = v.encode(url_encoding)
- elif isinstance(v, unicode):
+ elif isinstance(v, unicodestr):
v = v.encode('ISO-8859-1')
env1x[k.encode('ISO-8859-1')] = v
@@ -94,7 +94,8 @@ class InternalRedirector(object):
environ = environ.copy()
try:
return self.nextapp(environ, start_response)
- except _cherrypy.InternalRedirect, ir:
+ except _cherrypy.InternalRedirect:
+ ir = _sys.exc_info()[1]
sn = environ.get('SCRIPT_NAME', '')
path = environ.get('PATH_INFO', '')
qs = environ.get('QUERY_STRING', '')
@@ -152,8 +153,12 @@ class _TrappedResponse(object):
self.started_response = True
return self
- def next(self):
- return self.trap(self.iter_response.next)
+ if py3k:
+ def __next__(self):
+ return self.trap(next, self.iter_response)
+ else:
+ def next(self):
+ return self.trap(self.iter_response.next)
def close(self):
if hasattr(self.response, 'close'):
@@ -173,6 +178,11 @@ class _TrappedResponse(object):
if not _cherrypy.request.show_tracebacks:
tb = ""
s, h, b = _cperror.bare_error(tb)
+ if py3k:
+ # What fun.
+ s = s.decode('ISO-8859-1')
+ h = [(k.decode('ISO-8859-1'), v.decode('ISO-8859-1'))
+ for k, v in h]
if self.started_response:
# Empty our iterable (so future calls raise StopIteration)
self.iter_response = iter([])
@@ -191,7 +201,7 @@ class _TrappedResponse(object):
raise
if self.started_response:
- return "".join(b)
+ return ntob("").join(b)
else:
return b
@@ -203,24 +213,52 @@ class AppResponse(object):
"""WSGI response iterable for CherryPy applications."""
def __init__(self, environ, start_response, cpapp):
- if environ.get(u'wsgi.version') == (u'u', 0):
- environ = downgrade_wsgi_ux_to_1x(environ)
- self.environ = environ
self.cpapp = cpapp
try:
+ if not py3k:
+ if environ.get(ntou('wsgi.version')) == (ntou('u'), 0):
+ environ = downgrade_wsgi_ux_to_1x(environ)
+ self.environ = environ
self.run()
+
+ r = _cherrypy.serving.response
+
+ outstatus = r.output_status
+ if not isinstance(outstatus, bytestr):
+ raise TypeError("response.output_status is not a byte string.")
+
+ outheaders = []
+ for k, v in r.header_list:
+ if not isinstance(k, bytestr):
+ raise TypeError("response.header_list key %r is not a byte string." % k)
+ if not isinstance(v, bytestr):
+ raise TypeError("response.header_list value %r is not a byte string." % v)
+ outheaders.append((k, v))
+
+ if py3k:
+ # According to PEP 3333, when using Python 3, the response status
+ # and headers must be bytes masquerading as unicode; that is, they
+ # must be of type "str" but are restricted to code points in the
+ # "latin-1" set.
+ outstatus = outstatus.decode('ISO-8859-1')
+ outheaders = [(k.decode('ISO-8859-1'), v.decode('ISO-8859-1'))
+ for k, v in outheaders]
+
+ self.iter_response = iter(r.body)
+ self.write = start_response(outstatus, outheaders)
except:
self.close()
raise
- r = _cherrypy.serving.response
- self.iter_response = iter(r.body)
- self.write = start_response(r.output_status, r.header_list)
def __iter__(self):
return self
- def next(self):
- return self.iter_response.next()
+ if py3k:
+ def __next__(self):
+ return next(self.iter_response)
+ else:
+ def next(self):
+ return self.iter_response.next()
def close(self):
"""Close and de-reference the current request and response. (Core)"""
@@ -253,6 +291,29 @@ class AppResponse(object):
path = httputil.urljoin(self.environ.get('SCRIPT_NAME', ''),
self.environ.get('PATH_INFO', ''))
qs = self.environ.get('QUERY_STRING', '')
+
+ if py3k:
+ # This isn't perfect; if the given PATH_INFO is in the wrong encoding,
+ # it may fail to match the appropriate config section URI. But meh.
+ old_enc = self.environ.get('wsgi.url_encoding', 'ISO-8859-1')
+ new_enc = self.cpapp.find_config(self.environ.get('PATH_INFO', ''),
+ "request.uri_encoding", 'utf-8')
+ if new_enc.lower() != old_enc.lower():
+ # Even though the path and qs are unicode, the WSGI server is
+ # required by PEP 3333 to coerce them to ISO-8859-1 masquerading
+ # as unicode. So we have to encode back to bytes and then decode
+ # again using the "correct" encoding.
+ try:
+ u_path = path.encode(old_enc).decode(new_enc)
+ u_qs = qs.encode(old_enc).decode(new_enc)
+ except (UnicodeEncodeError, UnicodeDecodeError):
+ # Just pass them through without transcoding and hope.
+ pass
+ else:
+ # Only set transcoded values if they both succeed.
+ path = u_path
+ qs = u_qs
+
rproto = self.environ.get('SERVER_PROTOCOL')
headers = self.translate_headers(self.environ)
rfile = self.environ['wsgi.input']
diff --git a/cherrypy/_cpwsgi_server.py b/cherrypy/_cpwsgi_server.py
index 49fd5a19..21af5134 100644
--- a/cherrypy/_cpwsgi_server.py
+++ b/cherrypy/_cpwsgi_server.py
@@ -37,8 +37,11 @@ class CPWSGIServer(wsgiserver.CherryPyWSGIServer):
)
self.protocol = self.server_adapter.protocol_version
self.nodelay = self.server_adapter.nodelay
-
- ssl_module = self.server_adapter.ssl_module or 'pyopenssl'
+
+ if sys.version_info >= (3, 0):
+ ssl_module = self.server_adapter.ssl_module or 'builtin'
+ else:
+ ssl_module = self.server_adapter.ssl_module or 'pyopenssl'
if self.server_adapter.ssl_context:
adapter_class = wsgiserver.get_ssl_adapter_class(ssl_module)
self.ssl_adapter = adapter_class(
@@ -52,3 +55,9 @@ class CPWSGIServer(wsgiserver.CherryPyWSGIServer):
self.server_adapter.ssl_certificate,
self.server_adapter.ssl_private_key,
self.server_adapter.ssl_certificate_chain)
+
+ self.stats['Enabled'] = getattr(self.server_adapter, 'statistics', False)
+
+ def error_log(self, msg="", level=20, traceback=False):
+ cherrypy.engine.log(msg, level, traceback)
+
diff --git a/cherrypy/lib/__init__.py b/cherrypy/lib/__init__.py
index 611350c9..3fc0ec58 100644
--- a/cherrypy/lib/__init__.py
+++ b/cherrypy/lib/__init__.py
@@ -1,7 +1,7 @@
"""CherryPy Library"""
# Deprecated in CherryPy 3.2 -- remove in CherryPy 3.3
-from cherrypy.lib.reprconf import _Builder, unrepr, modules, attributes
+from cherrypy.lib.reprconf import unrepr, modules, attributes
class file_generator(object):
"""Yield the given input (a file object) in chunks (default 64k). (Core)"""
diff --git a/cherrypy/lib/cpstats.py b/cherrypy/lib/cpstats.py
index 79d5c3a9..9be947f2 100644
--- a/cherrypy/lib/cpstats.py
+++ b/cherrypy/lib/cpstats.py
@@ -320,20 +320,21 @@ class StatsTool(cherrypy.Tool):
def record_stop(self, uriset=None, slow_queries=1.0, slow_queries_count=100,
debug=False, **kwargs):
"""Record the end of a request."""
+ resp = cherrypy.serving.response
w = appstats['Requests'][threading._get_ident()]
r = cherrypy.request.rfile.bytes_read
w['Bytes Read'] = r
appstats['Total Bytes Read'] += r
- if cherrypy.response.stream:
+ if resp.stream:
w['Bytes Written'] = 'chunked'
else:
- cl = int(cherrypy.response.headers.get('Content-Length', 0))
+ cl = int(resp.headers.get('Content-Length', 0))
w['Bytes Written'] = cl
appstats['Total Bytes Written'] += cl
- w['Response Status'] = cherrypy.response.status
+ w['Response Status'] = getattr(resp, 'output_status', None) or resp.status
w['End Time'] = time.time()
p = w['End Time'] - w['Start Time']
diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py
index 3eedf97a..b426a3e7 100644
--- a/cherrypy/lib/cptools.py
+++ b/cherrypy/lib/cptools.py
@@ -116,7 +116,7 @@ def validate_since():
# Tool code #
def allow(methods=None, debug=False):
- """Raise 405 if request.method not in methods (default GET/HEAD).
+ """Raise 405 if request.method not in methods (default ['GET', 'HEAD']).
The given methods are case-insensitive, and may be in any order.
If only one method is allowed, you may supply a single string;
@@ -151,6 +151,10 @@ def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For',
For running a CP server behind Apache, lighttpd, or other HTTP server.
+ For Apache and lighttpd, you should leave the 'local' argument at the
+ default value of 'X-Forwarded-Host'. For Squid, you probably want to set
+ tools.proxy.local = 'Origin'.
+
If you want the new request.base to include path info (not just the host),
you must explicitly set base to the full base path, and ALSO set 'local'
to '', so that the X-Forwarded-Host request header (which never includes
@@ -581,9 +585,11 @@ class MonitoredHeaderMap(_httputil.HeaderMap):
self.accessed_headers.add(key)
return _httputil.HeaderMap.get(self, key, default=default)
- def has_key(self, key):
- self.accessed_headers.add(key)
- return _httputil.HeaderMap.has_key(self, key)
+ if hasattr({}, 'has_key'):
+ # Python 2
+ def has_key(self, key):
+ self.accessed_headers.add(key)
+ return _httputil.HeaderMap.has_key(self, key)
def autovary(ignore=None, debug=False):
diff --git a/cherrypy/lib/gctools.py b/cherrypy/lib/gctools.py
new file mode 100644
index 00000000..183148b2
--- /dev/null
+++ b/cherrypy/lib/gctools.py
@@ -0,0 +1,214 @@
+import gc
+import inspect
+import os
+import sys
+import time
+
+try:
+ import objgraph
+except ImportError:
+ objgraph = None
+
+import cherrypy
+from cherrypy import _cprequest, _cpwsgi
+from cherrypy.process.plugins import SimplePlugin
+
+
+class ReferrerTree(object):
+ """An object which gathers all referrers of an object to a given depth."""
+
+ peek_length = 40
+
+ def __init__(self, ignore=None, maxdepth=2, maxparents=10):
+ self.ignore = ignore or []
+ self.ignore.append(inspect.currentframe().f_back)
+ self.maxdepth = maxdepth
+ self.maxparents = maxparents
+
+ def ascend(self, obj, depth=1):
+ """Return a nested list containing referrers of the given object."""
+ depth += 1
+ parents = []
+
+ # Gather all referrers in one step to minimize
+ # cascading references due to repr() logic.
+ refs = gc.get_referrers(obj)
+ self.ignore.append(refs)
+ if len(refs) > self.maxparents:
+ return [("[%s referrers]" % len(refs), [])]
+
+ try:
+ ascendcode = self.ascend.__code__
+ except AttributeError:
+ ascendcode = self.ascend.im_func.func_code
+ for parent in refs:
+ if inspect.isframe(parent) and parent.f_code is ascendcode:
+ continue
+ if parent in self.ignore:
+ continue
+ if depth <= self.maxdepth:
+ parents.append((parent, self.ascend(parent, depth)))
+ else:
+ parents.append((parent, []))
+
+ return parents
+
+ def peek(self, s):
+ """Return s, restricted to a sane length."""
+ if len(s) > (self.peek_length + 3):
+ half = self.peek_length // 2
+ return s[:half] + '...' + s[-half:]
+ else:
+ return s
+
+ def _format(self, obj, descend=True):
+ """Return a string representation of a single object."""
+ if inspect.isframe(obj):
+ filename, lineno, func, context, index = inspect.getframeinfo(obj)
+ return "" % func
+
+ if not descend:
+ return self.peek(repr(obj))
+
+ if isinstance(obj, dict):
+ return "{" + ", ".join(["%s: %s" % (self._format(k, descend=False),
+ self._format(v, descend=False))
+ for k, v in obj.items()]) + "}"
+ elif isinstance(obj, list):
+ return "[" + ", ".join([self._format(item, descend=False)
+ for item in obj]) + "]"
+ elif isinstance(obj, tuple):
+ return "(" + ", ".join([self._format(item, descend=False)
+ for item in obj]) + ")"
+
+ r = self.peek(repr(obj))
+ if isinstance(obj, (str, int, float)):
+ return r
+ return "%s: %s" % (type(obj), r)
+
+ def format(self, tree):
+ """Return a list of string reprs from a nested list of referrers."""
+ output = []
+ def ascend(branch, depth=1):
+ for parent, grandparents in branch:
+ output.append((" " * depth) + self._format(parent))
+ if grandparents:
+ ascend(grandparents, depth + 1)
+ ascend(tree)
+ return output
+
+
+def get_instances(cls):
+ return [x for x in gc.get_objects() if isinstance(x, cls)]
+
+
+class RequestCounter(SimplePlugin):
+
+ def start(self):
+ self.count = 0
+
+ def before_request(self):
+ self.count += 1
+
+ def after_request(self):
+ self.count -=1
+request_counter = RequestCounter(cherrypy.engine)
+request_counter.subscribe()
+
+
+def get_context(obj):
+ if isinstance(obj, _cprequest.Request):
+ return "path=%s;stage=%s" % (obj.path_info, obj.stage)
+ elif isinstance(obj, _cprequest.Response):
+ return "status=%s" % obj.status
+ elif isinstance(obj, _cpwsgi.AppResponse):
+ return "PATH_INFO=%s" % obj.environ.get('PATH_INFO', '')
+ elif hasattr(obj, "tb_lineno"):
+ return "tb_lineno=%s" % obj.tb_lineno
+ return ""
+
+
+class GCRoot(object):
+ """A CherryPy page handler for testing reference leaks."""
+
+ classes = [(_cprequest.Request, 2, 2,
+ "Should be 1 in this request thread and 1 in the main thread."),
+ (_cprequest.Response, 2, 2,
+ "Should be 1 in this request thread and 1 in the main thread."),
+ (_cpwsgi.AppResponse, 1, 1,
+ "Should be 1 in this request thread only."),
+ ]
+
+ def index(self):
+ return "Hello, world!"
+ index.exposed = True
+
+ def stats(self):
+ output = ["Statistics:"]
+
+ for trial in range(10):
+ if request_counter.count > 0:
+ break
+ time.sleep(0.5)
+ else:
+ output.append("\nNot all requests closed properly.")
+
+ # gc_collect isn't perfectly synchronous, because it may
+ # break reference cycles that then take time to fully
+ # finalize. Call it thrice and hope for the best.
+ gc.collect()
+ gc.collect()
+ unreachable = gc.collect()
+ if unreachable:
+ if objgraph is not None:
+ final = objgraph.by_type('Nondestructible')
+ if final:
+ objgraph.show_backrefs(final, filename='finalizers.png')
+
+ trash = {}
+ for x in gc.garbage:
+ trash[type(x)] = trash.get(type(x), 0) + 1
+ if trash:
+ output.insert(0, "\n%s unreachable objects:" % unreachable)
+ trash = [(v, k) for k, v in trash.items()]
+ trash.sort()
+ for pair in trash:
+ output.append(" " + repr(pair))
+
+ # Check declared classes to verify uncollected instances.
+ # These don't have to be part of a cycle; they can be
+ # any objects that have unanticipated referrers that keep
+ # them from being collected.
+ allobjs = {}
+ for cls, minobj, maxobj, msg in self.classes:
+ allobjs[cls] = get_instances(cls)
+
+ for cls, minobj, maxobj, msg in self.classes:
+ objs = allobjs[cls]
+ lenobj = len(objs)
+ if lenobj < minobj or lenobj > maxobj:
+ if minobj == maxobj:
+ output.append(
+ "\nExpected %s %r references, got %s." %
+ (minobj, cls, lenobj))
+ else:
+ output.append(
+ "\nExpected %s to %s %r references, got %s." %
+ (minobj, maxobj, cls, lenobj))
+
+ for obj in objs:
+ if objgraph is not None:
+ ig = [id(objs), id(inspect.currentframe())]
+ fname = "graph_%s_%s.png" % (cls.__name__, id(obj))
+ objgraph.show_backrefs(
+ obj, extra_ignore=ig, max_depth=4, too_many=20,
+ filename=fname, extra_info=get_context)
+ output.append("\nReferrers for %s (refcount=%s):" %
+ (repr(obj), sys.getrefcount(obj)))
+ t = ReferrerTree(ignore=[objs], maxdepth=3)
+ tree = t.ascend(obj)
+ output.extend(t.format(tree))
+
+ return "\n".join(output)
+ stats.exposed = True
+
diff --git a/cherrypy/lib/httputil.py b/cherrypy/lib/httputil.py
index e0058751..5f77d547 100644
--- a/cherrypy/lib/httputil.py
+++ b/cherrypy/lib/httputil.py
@@ -9,7 +9,7 @@ to a public caning.
from binascii import b2a_base64
from cherrypy._cpcompat import BaseHTTPRequestHandler, HTTPDate, ntob, ntou, reversed, sorted
-from cherrypy._cpcompat import basestring, iteritems, unicodestr, unquote_qs
+from cherrypy._cpcompat import basestring, bytestr, iteritems, nativestr, unicodestr, unquote_qs
response_codes = BaseHTTPRequestHandler.responses.copy()
# From http://www.cherrypy.org/ticket/361
@@ -38,6 +38,18 @@ def urljoin(*atoms):
# Special-case the final url of "", and return "/" instead.
return url or "/"
+def urljoin_bytes(*atoms):
+ """Return the given path *atoms, joined into a single URL.
+
+ This will correctly join a SCRIPT_NAME and PATH_INFO into the
+ original URL, even if either atom is blank.
+ """
+ url = ntob("/").join([x for x in atoms if x])
+ while ntob("//") in url:
+ url = url.replace(ntob("//"), ntob("/"))
+ # Special-case the final url of "", and return "/" instead.
+ return url or ntob("/")
+
def protocol_from_http(protocol_str):
"""Return a protocol tuple from the given 'HTTP/x.y' string."""
return int(protocol_str[5]), int(protocol_str[7])
@@ -105,9 +117,15 @@ class HeaderElement(object):
def __cmp__(self, other):
return cmp(self.value, other.value)
+ def __lt__(self, other):
+ return self.value < other.value
+
def __str__(self):
p = [";%s=%s" % (k, v) for k, v in iteritems(self.params)]
return "%s%s" % (self.value, "".join(p))
+
+ def __bytes__(self):
+ return ntob(self.__str__())
def __unicode__(self):
return ntou(self.__str__())
@@ -181,6 +199,12 @@ class AcceptElement(HeaderElement):
if diff == 0:
diff = cmp(str(self), str(other))
return diff
+
+ def __lt__(self, other):
+ if self.qvalue == other.qvalue:
+ return str(self) < str(other)
+ else:
+ return self.qvalue < other.qvalue
def header_elements(fieldname, fieldvalue):
@@ -199,8 +223,12 @@ def header_elements(fieldname, fieldvalue):
return list(reversed(sorted(result)))
def decode_TEXT(value):
- r"""Decode :rfc:`2047` TEXT (e.g. "=?utf-8?q?f=C3=BCr?=" -> u"f\xfcr")."""
- from email.Header import decode_header
+ r"""Decode :rfc:`2047` TEXT (e.g. "=?utf-8?q?f=C3=BCr?=" -> "f\xfcr")."""
+ try:
+ # Python 3
+ from email.header import decode_header
+ except ImportError:
+ from email.Header import decode_header
atoms = decode_header(value)
decodedvalue = ""
for atom, charset in atoms:
@@ -253,6 +281,10 @@ def valid_status(status):
return code, reason, message
+# NOTE: the parse_qs functions that follow are modified version of those
+# in the python3.0 source - we need to pass through an encoding to the unquote
+# method, but the default parse_qs function doesn't allow us to. These do.
+
def _parse_qs(qs, keep_blank_values=0, strict_parsing=0, encoding='utf-8'):
"""Parse a query given as a string argument.
@@ -338,8 +370,9 @@ class CaseInsensitiveDict(dict):
def get(self, key, default=None):
return dict.get(self, str(key).title(), default)
- def has_key(self, key):
- return dict.has_key(self, str(key).title())
+ if hasattr({}, 'has_key'):
+ def has_key(self, key):
+ return dict.has_key(self, str(key).title())
def update(self, E):
for k in E.keys():
@@ -369,8 +402,12 @@ class CaseInsensitiveDict(dict):
# A CRLF is allowed in the definition of TEXT only as part of a header
# field continuation. It is expected that the folding LWS will be
# replaced with a single SP before interpretation of the TEXT value."
-header_translate_table = ''.join([chr(i) for i in xrange(256)])
-header_translate_deletechars = ''.join([chr(i) for i in xrange(32)]) + chr(127)
+if nativestr == bytestr:
+ header_translate_table = ''.join([chr(i) for i in xrange(256)])
+ header_translate_deletechars = ''.join([chr(i) for i in xrange(32)]) + chr(127)
+else:
+ header_translate_table = None
+ header_translate_deletechars = bytes(range(32)) + bytes([127])
class HeaderMap(CaseInsensitiveDict):
diff --git a/cherrypy/lib/jsontools.py b/cherrypy/lib/jsontools.py
index 09042e45..20925791 100644
--- a/cherrypy/lib/jsontools.py
+++ b/cherrypy/lib/jsontools.py
@@ -82,6 +82,6 @@ def json_out(content_type='application/json', debug=False, handler=json_handler)
request.handler = handler
if content_type is not None:
if debug:
- cherrypy.log('Setting Content-Type to %s' % ct, 'TOOLS.JSON_OUT')
+ cherrypy.log('Setting Content-Type to %s' % content_type, 'TOOLS.JSON_OUT')
cherrypy.serving.response.headers['Content-Type'] = content_type
diff --git a/cherrypy/lib/reprconf.py b/cherrypy/lib/reprconf.py
index e18949ee..ba8ff51e 100644
--- a/cherrypy/lib/reprconf.py
+++ b/cherrypy/lib/reprconf.py
@@ -28,6 +28,20 @@ try:
set
except NameError:
from sets import Set as set
+
+try:
+ basestring
+except NameError:
+ basestring = str
+
+try:
+ # Python 3
+ import builtins
+except ImportError:
+ # Python 2
+ import __builtin__ as builtins
+
+import operator as _operator
import sys
def as_dict(config):
@@ -195,10 +209,11 @@ class Parser(ConfigParser):
if section not in result:
result[section] = {}
for option in self.options(section):
- value = self.get(section, option, raw, vars)
+ value = self.get(section, option, raw=raw, vars=vars)
try:
value = unrepr(value)
- except Exception, x:
+ except Exception:
+ x = sys.exc_info()[1]
msg = ("Config error in section: %r, option: %r, "
"value: %r. Config values must be valid Python." %
(section, option, value))
@@ -216,7 +231,8 @@ class Parser(ConfigParser):
# public domain "unrepr" implementation, found on the web and then improved.
-class _Builder:
+
+class _Builder2:
def build(self, o):
m = getattr(self, 'build_' + o.__class__.__name__, None)
@@ -225,6 +241,18 @@ class _Builder:
repr(o.__class__.__name__))
return m(o)
+ def astnode(self, s):
+ """Return a Python2 ast Node compiled from a string."""
+ try:
+ import compiler
+ except ImportError:
+ # Fallback to eval when compiler package is not available,
+ # e.g. IronPython 1.0.
+ return eval(s)
+
+ p = compiler.parse("__tempvalue__ = " + s)
+ return p.getChildren()[1].getChildren()[0].getChildren()[1]
+
def build_Subscript(self, o):
expr, flags, subs = o.getChildren()
expr = self.build(expr)
@@ -272,8 +300,7 @@ class _Builder:
# See if the Name is in builtins.
try:
- import __builtin__
- return getattr(__builtin__, name)
+ return getattr(builtins, name)
except AttributeError:
pass
@@ -282,6 +309,10 @@ class _Builder:
def build_Add(self, o):
left, right = map(self.build, o.getChildren())
return left + right
+
+ def build_Mul(self, o):
+ left, right = map(self.build, o.getChildren())
+ return left * right
def build_Getattr(self, o):
parent = self.build(o.expr)
@@ -297,25 +328,128 @@ class _Builder:
return self.build(o.getChildren()[0])
-def _astnode(s):
- """Return a Python ast Node compiled from a string."""
- try:
- import compiler
- except ImportError:
- # Fallback to eval when compiler package is not available,
- # e.g. IronPython 1.0.
- return eval(s)
+class _Builder3:
- p = compiler.parse("__tempvalue__ = " + s)
- return p.getChildren()[1].getChildren()[0].getChildren()[1]
+ def build(self, o):
+ m = getattr(self, 'build_' + o.__class__.__name__, None)
+ if m is None:
+ raise TypeError("unrepr does not recognize %s" %
+ repr(o.__class__.__name__))
+ return m(o)
+ def astnode(self, s):
+ """Return a Python3 ast Node compiled from a string."""
+ try:
+ import ast
+ except ImportError:
+ # Fallback to eval when ast package is not available,
+ # e.g. IronPython 1.0.
+ return eval(s)
+
+ p = ast.parse("__tempvalue__ = " + s)
+ return p.body[0].value
+
+ def build_Subscript(self, o):
+ return self.build(o.value)[self.build(o.slice)]
+
+ def build_Index(self, o):
+ return self.build(o.value)
+
+ def build_Call(self, o):
+ callee = self.build(o.func)
+
+ if o.args is None:
+ args = ()
+ else:
+ args = tuple([self.build(a) for a in o.args])
+
+ if o.starargs is None:
+ starargs = ()
+ else:
+ starargs = self.build(o.starargs)
+
+ if o.kwargs is None:
+ kwargs = {}
+ else:
+ kwargs = self.build(o.kwargs)
+
+ return callee(*(args + starargs), **kwargs)
+
+ def build_List(self, o):
+ return list(map(self.build, o.elts))
+
+ def build_Str(self, o):
+ return o.s
+
+ def build_Num(self, o):
+ return o.n
+
+ def build_Dict(self, o):
+ return dict([(self.build(k), self.build(v))
+ for k, v in zip(o.keys, o.values)])
+
+ def build_Tuple(self, o):
+ return tuple(self.build_List(o))
+
+ def build_Name(self, o):
+ name = o.id
+ if name == 'None':
+ return None
+ if name == 'True':
+ return True
+ if name == 'False':
+ return False
+
+ # See if the Name is a package or module. If it is, import it.
+ try:
+ return modules(name)
+ except ImportError:
+ pass
+
+ # See if the Name is in builtins.
+ try:
+ import builtins
+ return getattr(builtins, name)
+ except AttributeError:
+ pass
+
+ raise TypeError("unrepr could not resolve the name %s" % repr(name))
+
+ def build_UnaryOp(self, o):
+ op, operand = map(self.build, [o.op, o.operand])
+ return op(operand)
+
+ def build_BinOp(self, o):
+ left, op, right = map(self.build, [o.left, o.op, o.right])
+ return op(left, right)
+
+ def build_Add(self, o):
+ return _operator.add
+
+ def build_Mult(self, o):
+ return _operator.mul
+
+ def build_USub(self, o):
+ return _operator.neg
+
+ def build_Attribute(self, o):
+ parent = self.build(o.value)
+ return getattr(parent, o.attr)
+
+ def build_NoneType(self, o):
+ return None
+
def unrepr(s):
"""Return a Python object compiled from a string."""
if not s:
return s
- obj = _astnode(s)
- return _Builder().build(obj)
+ if sys.version_info < (3, 0):
+ b = _Builder2()
+ else:
+ b = _Builder3()
+ obj = b.astnode(s)
+ return b.build(obj)
def modules(modulePath):
diff --git a/cherrypy/lib/sessions.py b/cherrypy/lib/sessions.py
index 42c28009..9763f120 100644
--- a/cherrypy/lib/sessions.py
+++ b/cherrypy/lib/sessions.py
@@ -93,7 +93,7 @@ import types
from warnings import warn
import cherrypy
-from cherrypy._cpcompat import copyitems, pickle, random20
+from cherrypy._cpcompat import copyitems, pickle, random20, unicodestr
from cherrypy.lib import httputil
@@ -171,7 +171,15 @@ class Session(object):
self.id = None
self.missing = True
self._regenerate()
-
+
+ def now(self):
+ """Generate the session specific concept of 'now'.
+
+ Other session providers can override this to use alternative,
+ possibly timezone aware, versions of 'now'.
+ """
+ return datetime.datetime.now()
+
def regenerate(self):
"""Replace the current session (with a new id)."""
self.regenerated = True
@@ -210,7 +218,7 @@ class Session(object):
# accessed: no need to save it
if self.loaded:
t = datetime.timedelta(seconds = self.timeout * 60)
- expiration_time = datetime.datetime.now() + t
+ expiration_time = self.now() + t
if self.debug:
cherrypy.log('Saving with expiry %s' % expiration_time,
'TOOLS.SESSIONS')
@@ -225,7 +233,7 @@ class Session(object):
"""Copy stored session data into this session instance."""
data = self._load()
# data is either None or a tuple (session_data, expiration_time)
- if data is None or data[1] < datetime.datetime.now():
+ if data is None or data[1] < self.now():
if self.debug:
cherrypy.log('Expired session, flushing data', 'TOOLS.SESSIONS')
self._data = {}
@@ -277,10 +285,11 @@ class Session(object):
if not self.loaded: self.load()
return key in self._data
- def has_key(self, key):
- """D.has_key(k) -> True if D has a key k, else False."""
- if not self.loaded: self.load()
- return key in self._data
+ if hasattr({}, 'has_key'):
+ def has_key(self, key):
+ """D.has_key(k) -> True if D has a key k, else False."""
+ if not self.loaded: self.load()
+ return key in self._data
def get(self, key, default=None):
"""D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."""
@@ -326,7 +335,7 @@ class RamSession(Session):
def clean_up(self):
"""Clean up expired sessions."""
- now = datetime.datetime.now()
+ now = self.now()
for id, (data, expiration_time) in copyitems(self.cache):
if expiration_time <= now:
try:
@@ -337,6 +346,11 @@ class RamSession(Session):
del self.locks[id]
except KeyError:
pass
+
+ # added to remove obsolete lock objects
+ for id in list(self.locks):
+ if id not in self.cache:
+ self.locks.pop(id, None)
def _exists(self):
return self.id in self.cache
@@ -467,7 +481,7 @@ class FileSession(Session):
def clean_up(self):
"""Clean up expired sessions."""
- now = datetime.datetime.now()
+ now = self.now()
# Iterate over all session files in self.storage_path
for fname in os.listdir(self.storage_path):
if (fname.startswith(self.SESSION_PREFIX)
@@ -575,7 +589,7 @@ class PostgresqlSession(Session):
def clean_up(self):
"""Clean up expired sessions."""
self.cursor.execute('delete from session where expiration_time < %s',
- (datetime.datetime.now(),))
+ (self.now(),))
class MemcachedSession(Session):
@@ -602,6 +616,19 @@ class MemcachedSession(Session):
cls.cache = memcache.Client(cls.servers)
setup = classmethod(setup)
+ def _get_id(self):
+ return self._id
+ def _set_id(self, value):
+ # This encode() call is where we differ from the superclass.
+ # Memcache keys MUST be byte strings, not unicode.
+ if isinstance(value, unicodestr):
+ value = value.encode('utf-8')
+
+ self._id = value
+ for o in self.id_observers:
+ o(value)
+ id = property(_get_id, _set_id, doc="The current session ID.")
+
def _exists(self):
self.mc_lock.acquire()
try:
@@ -683,12 +710,12 @@ close.priority = 90
def init(storage_type='ram', path=None, path_header=None, name='session_id',
timeout=60, domain=None, secure=False, clean_freq=5,
- persistent=True, debug=False, **kwargs):
+ persistent=True, httponly=False, debug=False, **kwargs):
"""Initialize session object (using cookies).
storage_type
- One of 'ram', 'file', 'postgresql'. This will be used
- to look up the corresponding class in cherrypy.lib.sessions
+ One of 'ram', 'file', 'postgresql', 'memcached'. This will be
+ used to look up the corresponding class in cherrypy.lib.sessions
globals. For example, 'file' will use the FileSession class.
path
@@ -722,6 +749,10 @@ def init(storage_type='ram', path=None, path_header=None, name='session_id',
and the cookie will be a "session cookie" which expires when the
browser is closed.
+ httponly
+ If False (the default) the cookie 'httponly' value will not be set.
+ If True, the cookie 'httponly' value will be set (to 1).
+
Any additional kwargs will be bound to the new Session instance,
and may be specific to the storage type. See the subclass of Session
you're using for more information.
@@ -772,11 +803,12 @@ def init(storage_type='ram', path=None, path_header=None, name='session_id',
# and http://support.mozilla.com/en-US/kb/Cookies
cookie_timeout = None
set_response_cookie(path=path, path_header=path_header, name=name,
- timeout=cookie_timeout, domain=domain, secure=secure)
+ timeout=cookie_timeout, domain=domain, secure=secure,
+ httponly=httponly)
def set_response_cookie(path=None, path_header=None, name='session_id',
- timeout=60, domain=None, secure=False):
+ timeout=60, domain=None, secure=False, httponly=False):
"""Set a response cookie for the client.
path
@@ -801,6 +833,10 @@ def set_response_cookie(path=None, path_header=None, name='session_id',
if False (the default) the cookie 'secure' value will not
be set. If True, the cookie 'secure' value will be set (to 1).
+ httponly
+ If False (the default) the cookie 'httponly' value will not be set.
+ If True, the cookie 'httponly' value will be set (to 1).
+
"""
# Set response cookie
cookie = cherrypy.serving.response.cookie
@@ -820,7 +856,10 @@ def set_response_cookie(path=None, path_header=None, name='session_id',
cookie[name]['domain'] = domain
if secure:
cookie[name]['secure'] = 1
-
+ if httponly:
+ if not cookie[name].isReservedKey('httponly'):
+ raise ValueError("The httponly cookie token is not supported.")
+ cookie[name]['httponly'] = 1
def expire():
"""Expire the current session cookie."""
diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py
index cb9a68cb..2d142307 100644
--- a/cherrypy/lib/static.py
+++ b/cherrypy/lib/static.py
@@ -1,3 +1,7 @@
+try:
+ from io import UnsupportedOperation
+except ImportError:
+ UnsupportedOperation = object()
import logging
import mimetypes
mimetypes.init()
@@ -115,6 +119,8 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None,
if debug:
cherrypy.log('os has no fstat attribute', 'TOOLS.STATIC')
content_length = None
+ except UnsupportedOperation:
+ content_length = None
else:
# Set the Last-Modified response header, so that
# modified-since validation code can work.
@@ -174,7 +180,12 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False):
else:
# Return a multipart/byteranges response.
response.status = "206 Partial Content"
- from mimetools import choose_boundary
+ try:
+ # Python 3
+ from email.generator import _make_boundary as choose_boundary
+ except ImportError:
+ # Python 2
+ from mimetools import choose_boundary
boundary = choose_boundary()
ct = "multipart/byteranges; boundary=%s" % boundary
response.headers['Content-Type'] = ct
diff --git a/cherrypy/lib/xmlrpc.py b/cherrypy/lib/xmlrpcutil.py
similarity index 61%
rename from cherrypy/lib/xmlrpc.py
rename to cherrypy/lib/xmlrpcutil.py
index 8a5ef546..9a44464b 100644
--- a/cherrypy/lib/xmlrpc.py
+++ b/cherrypy/lib/xmlrpcutil.py
@@ -1,13 +1,19 @@
import sys
import cherrypy
+from cherrypy._cpcompat import ntob
+def get_xmlrpclib():
+ try:
+ import xmlrpc.client as x
+ except ImportError:
+ import xmlrpclib as x
+ return x
def process_body():
"""Return (params, method) from request body."""
try:
- import xmlrpclib
- return xmlrpclib.loads(cherrypy.request.body.read())
+ return get_xmlrpclib().loads(cherrypy.request.body.read())
except Exception:
return ('ERROR PARAMS', ), 'ERRORMETHOD'
@@ -29,21 +35,21 @@ def _set_response(body):
# as a "Protocol Error", we'll just return 200 every time.
response = cherrypy.response
response.status = '200 OK'
- response.body = body
+ response.body = ntob(body, 'utf-8')
response.headers['Content-Type'] = 'text/xml'
response.headers['Content-Length'] = len(body)
def respond(body, encoding='utf-8', allow_none=0):
- from xmlrpclib import Fault, dumps
- if not isinstance(body, Fault):
+ xmlrpclib = get_xmlrpclib()
+ if not isinstance(body, xmlrpclib.Fault):
body = (body,)
- _set_response(dumps(body, methodresponse=1,
- encoding=encoding,
- allow_none=allow_none))
+ _set_response(xmlrpclib.dumps(body, methodresponse=1,
+ encoding=encoding,
+ allow_none=allow_none))
def on_error(*args, **kwargs):
body = str(sys.exc_info()[1])
- from xmlrpclib import Fault, dumps
- _set_response(dumps(Fault(1, body)))
+ xmlrpclib = get_xmlrpclib()
+ _set_response(xmlrpclib.dumps(xmlrpclib.Fault(1, body)))
diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py
index 488958eb..ba618a0b 100644
--- a/cherrypy/process/plugins.py
+++ b/cherrypy/process/plugins.py
@@ -453,13 +453,14 @@ class BackgroundTask(threading.Thread):
it won't delay stopping the whole process.
"""
- def __init__(self, interval, function, args=[], kwargs={}):
+ def __init__(self, interval, function, args=[], kwargs={}, bus=None):
threading.Thread.__init__(self)
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.running = False
+ self.bus = bus
def cancel(self):
self.running = False
@@ -473,8 +474,9 @@ class BackgroundTask(threading.Thread):
try:
self.function(*self.args, **self.kwargs)
except Exception:
- self.bus.log("Error in background task thread function %r." %
- self.function, level=40, traceback=True)
+ if self.bus:
+ self.bus.log("Error in background task thread function %r."
+ % self.function, level=40, traceback=True)
# Quit on first error to avoid massive logs.
raise
@@ -506,8 +508,8 @@ class Monitor(SimplePlugin):
if self.frequency > 0:
threadname = self.name or self.__class__.__name__
if self.thread is None:
- self.thread = BackgroundTask(self.frequency, self.callback)
- self.thread.bus = self.bus
+ self.thread = BackgroundTask(self.frequency, self.callback,
+ bus = self.bus)
self.thread.setName(threadname)
self.thread.start()
self.bus.log("Started monitor thread %r." % threadname)
diff --git a/cherrypy/process/servers.py b/cherrypy/process/servers.py
index 272e8436..fa714d65 100644
--- a/cherrypy/process/servers.py
+++ b/cherrypy/process/servers.py
@@ -385,34 +385,43 @@ def check_port(host, port, timeout=1.0):
if s:
s.close()
-def wait_for_free_port(host, port):
+
+# Feel free to increase these defaults on slow systems:
+free_port_timeout = 0.1
+occupied_port_timeout = 1.0
+
+def wait_for_free_port(host, port, timeout=None):
"""Wait for the specified port to become free (drop requests)."""
if not host:
raise ValueError("Host values of '' or None are not allowed.")
+ if timeout is None:
+ timeout = free_port_timeout
for trial in range(50):
try:
# we are expecting a free port, so reduce the timeout
- check_port(host, port, timeout=0.1)
+ check_port(host, port, timeout=timeout)
except IOError:
# Give the old server thread time to free the port.
- time.sleep(0.1)
+ time.sleep(timeout)
else:
return
raise IOError("Port %r not free on %r" % (port, host))
-def wait_for_occupied_port(host, port):
+def wait_for_occupied_port(host, port, timeout=None):
"""Wait for the specified port to become active (receive requests)."""
if not host:
raise ValueError("Host values of '' or None are not allowed.")
+ if timeout is None:
+ timeout = occupied_port_timeout
for trial in range(50):
try:
- check_port(host, port)
+ check_port(host, port, timeout=timeout)
except IOError:
return
else:
- time.sleep(.1)
+ time.sleep(timeout)
raise IOError("Port %r not bound on %r" % (port, host))
diff --git a/cherrypy/process/wspbus.py b/cherrypy/process/wspbus.py
index 46cd75a2..6ef768dc 100644
--- a/cherrypy/process/wspbus.py
+++ b/cherrypy/process/wspbus.py
@@ -90,11 +90,11 @@ class ChannelFailures(Exception):
def handle_exception(self):
"""Append the current exception to self."""
- self._exceptions.append(sys.exc_info())
+ self._exceptions.append(sys.exc_info()[1])
def get_instances(self):
"""Return a list of seen exception instances."""
- return [instance for cls, instance, traceback in self._exceptions]
+ return self._exceptions[:]
def __str__(self):
exception_strings = map(repr, self.get_instances())
@@ -102,8 +102,9 @@ class ChannelFailures(Exception):
__repr__ = __str__
- def __nonzero__(self):
+ def __bool__(self):
return bool(self._exceptions)
+ __nonzero__ = __bool__
# Use a flag to indicate the state of the bus.
class _StateEnum(object):
@@ -124,6 +125,17 @@ states.STOPPING = states.State()
states.EXITING = states.State()
+try:
+ import fcntl
+except ImportError:
+ max_files = 0
+else:
+ try:
+ max_files = os.sysconf('SC_OPEN_MAX')
+ except AttributeError:
+ max_files = 1024
+
+
class Bus(object):
"""Process state-machine and messenger for HTTP site deployment.
@@ -137,6 +149,7 @@ class Bus(object):
states = states
state = states.STOPPED
execv = False
+ max_cloexec_files = max_files
def __init__(self):
self.execv = False
@@ -173,13 +186,19 @@ class Bus(object):
items = [(self._priorities[(channel, listener)], listener)
for listener in self.listeners[channel]]
- items.sort()
+ try:
+ items.sort(key=lambda item: item[0])
+ except TypeError:
+ # Python 2.3 had no 'key' arg, but that doesn't matter
+ # since it could sort dissimilar types just fine.
+ items.sort()
for priority, listener in items:
try:
output.append(listener(*args, **kwargs))
except KeyboardInterrupt:
raise
- except SystemExit, e:
+ except SystemExit:
+ e = sys.exc_info()[1]
# If we have previous errors ensure the exit code is non-zero
if exc and e.code == 0:
e.code = 1
@@ -221,13 +240,14 @@ class Bus(object):
except:
self.log("Shutting down due to error in start listener:",
level=40, traceback=True)
- e_info = sys.exc_info()
+ e_info = sys.exc_info()[1]
try:
self.exit()
except:
# Any stop/exit errors will be logged inside publish().
pass
- raise e_info[0], e_info[1], e_info[2]
+ # Re-raise the original error
+ raise e_info
def exit(self):
"""Stop all services and prepare to exit the process."""
@@ -354,8 +374,28 @@ class Bus(object):
args = ['"%s"' % arg for arg in args]
os.chdir(_startup_cwd)
+ if self.max_cloexec_files:
+ self._set_cloexec()
os.execv(sys.executable, args)
+ def _set_cloexec(self):
+ """Set the CLOEXEC flag on all open files (except stdin/out/err).
+
+ If self.max_cloexec_files is an integer (the default), then on
+ platforms which support it, it represents the max open files setting
+ for the operating system. This function will be called just before
+ the process is restarted via os.execv() to prevent open files
+ from persisting into the new process.
+
+ Set self.max_cloexec_files to 0 to disable this behavior.
+ """
+ for fd in range(3, self.max_cloexec_files): # skip stdin/out/err
+ try:
+ flags = fcntl.fcntl(fd, fcntl.F_GETFD)
+ except IOError:
+ continue
+ fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
+
def stop(self):
"""Stop all services."""
self.state = states.STOPPING
@@ -386,8 +426,7 @@ class Bus(object):
def log(self, msg="", level=20, traceback=False):
"""Log the given message. Append the last traceback if requested."""
if traceback:
- exc = sys.exc_info()
- msg += "\n" + "".join(_traceback.format_exception(*exc))
+ msg += "\n" + "".join(_traceback.format_exception(*sys.exc_info()))
self.publish('log', msg, level)
bus = Bus()
diff --git a/cherrypy/test/__init__.py b/cherrypy/test/__init__.py
deleted file mode 100644
index e4c400d6..00000000
--- a/cherrypy/test/__init__.py
+++ /dev/null
@@ -1,25 +0,0 @@
-"""Regression test suite for CherryPy.
-
-Run 'nosetests -s test/' to exercise all tests.
-
-The '-s' flag instructs nose to output stdout messages, wihch is crucial to
-the 'interactive' mode of webtest.py. If you run these tests without the '-s'
-flag, don't be surprised if the test seems to hang: it's waiting for your
-interactive input.
-"""
-
-import sys
-def newexit():
- raise SystemExit('Exit called')
-
-def setup():
- # We want to monkey patch sys.exit so that we can get some
- # information about where exit is being called.
- newexit._old = sys.exit
- sys.exit = newexit
-
-def teardown():
- try:
- sys.exit = sys.exit._old
- except AttributeError:
- sys.exit = sys._exit
diff --git a/cherrypy/test/_test_decorators.py b/cherrypy/test/_test_decorators.py
deleted file mode 100644
index 5bcbc1e6..00000000
--- a/cherrypy/test/_test_decorators.py
+++ /dev/null
@@ -1,41 +0,0 @@
-"""Test module for the @-decorator syntax, which is version-specific"""
-
-from cherrypy import expose, tools
-from cherrypy._cpcompat import ntob
-
-
-class ExposeExamples(object):
-
- @expose
- def no_call(self):
- return "Mr E. R. Bradshaw"
-
- @expose()
- def call_empty(self):
- return "Mrs. B.J. Smegma"
-
- @expose("call_alias")
- def nesbitt(self):
- return "Mr Nesbitt"
-
- @expose(["alias1", "alias2"])
- def andrews(self):
- return "Mr Ken Andrews"
-
- @expose(alias="alias3")
- def watson(self):
- return "Mr. and Mrs. Watson"
-
-
-class ToolExamples(object):
-
- @expose
- @tools.response_headers(headers=[('Content-Type', 'application/data')])
- def blah(self):
- yield ntob("blah")
- # This is here to demonstrate that _cp_config = {...} overwrites
- # the _cp_config attribute added by the Tool decorator. You have
- # to write _cp_config[k] = v or _cp_config.update(...) instead.
- blah._cp_config['response.stream'] = True
-
-
diff --git a/cherrypy/test/_test_states_demo.py b/cherrypy/test/_test_states_demo.py
deleted file mode 100644
index 3f8f196c..00000000
--- a/cherrypy/test/_test_states_demo.py
+++ /dev/null
@@ -1,66 +0,0 @@
-import os
-import sys
-import time
-starttime = time.time()
-
-import cherrypy
-
-
-class Root:
-
- def index(self):
- return "Hello World"
- index.exposed = True
-
- def mtimes(self):
- return repr(cherrypy.engine.publish("Autoreloader", "mtimes"))
- mtimes.exposed = True
-
- def pid(self):
- return str(os.getpid())
- pid.exposed = True
-
- def start(self):
- return repr(starttime)
- start.exposed = True
-
- def exit(self):
- # This handler might be called before the engine is STARTED if an
- # HTTP worker thread handles it before the HTTP server returns
- # control to engine.start. We avoid that race condition here
- # by waiting for the Bus to be STARTED.
- cherrypy.engine.wait(state=cherrypy.engine.states.STARTED)
- cherrypy.engine.exit()
- exit.exposed = True
-
-
-def unsub_sig():
- cherrypy.log("unsubsig: %s" % cherrypy.config.get('unsubsig', False))
- if cherrypy.config.get('unsubsig', False):
- cherrypy.log("Unsubscribing the default cherrypy signal handler")
- cherrypy.engine.signal_handler.unsubscribe()
- try:
- from signal import signal, SIGTERM
- except ImportError:
- pass
- else:
- def old_term_handler(signum=None, frame=None):
- cherrypy.log("I am an old SIGTERM handler.")
- sys.exit(0)
- cherrypy.log("Subscribing the new one.")
- signal(SIGTERM, old_term_handler)
-cherrypy.engine.subscribe('start', unsub_sig, priority=100)
-
-
-def starterror():
- if cherrypy.config.get('starterror', False):
- zerodiv = 1 / 0
-cherrypy.engine.subscribe('start', starterror, priority=6)
-
-def log_test_case_name():
- if cherrypy.config.get('test_case_name', False):
- cherrypy.log("STARTED FROM: %s" % cherrypy.config.get('test_case_name'))
-cherrypy.engine.subscribe('start', log_test_case_name, priority=6)
-
-
-cherrypy.tree.mount(Root(), '/', {'/': {}})
diff --git a/cherrypy/test/benchmark.py b/cherrypy/test/benchmark.py
deleted file mode 100644
index 90536a56..00000000
--- a/cherrypy/test/benchmark.py
+++ /dev/null
@@ -1,409 +0,0 @@
-"""CherryPy Benchmark Tool
-
- Usage:
- benchmark.py --null --notests --help --cpmodpy --modpython --ab=path --apache=path
-
- --null: use a null Request object (to bench the HTTP server only)
- --notests: start the server but do not run the tests; this allows
- you to check the tested pages with a browser
- --help: show this help message
- --cpmodpy: run tests via apache on 8080 (with the builtin _cpmodpy)
- --modpython: run tests via apache on 8080 (with modpython_gateway)
- --ab=path: Use the ab script/executable at 'path' (see below)
- --apache=path: Use the apache script/exe at 'path' (see below)
-
- To run the benchmarks, the Apache Benchmark tool "ab" must either be on
- your system path, or specified via the --ab=path option.
-
- To run the modpython tests, the "apache" executable or script must be
- on your system path, or provided via the --apache=path option. On some
- platforms, "apache" may be called "apachectl" or "apache2ctl"--create
- a symlink to them if needed.
-"""
-
-import getopt
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-
-import re
-import sys
-import time
-import traceback
-
-import cherrypy
-from cherrypy._cpcompat import ntob
-from cherrypy import _cperror, _cpmodpy
-from cherrypy.lib import httputil
-
-
-AB_PATH = ""
-APACHE_PATH = "apache"
-SCRIPT_NAME = "/cpbench/users/rdelon/apps/blog"
-
-__all__ = ['ABSession', 'Root', 'print_report',
- 'run_standard_benchmarks', 'safe_threads',
- 'size_report', 'startup', 'thread_report',
- ]
-
-size_cache = {}
-
-class Root:
-
- def index(self):
- return """
-
- CherryPy Benchmark
-
-
-
-
-"""
- index.exposed = True
-
- def hello(self):
- return "Hello, world\r\n"
- hello.exposed = True
-
- def sizer(self, size):
- resp = size_cache.get(size, None)
- if resp is None:
- size_cache[size] = resp = "X" * int(size)
- return resp
- sizer.exposed = True
-
-
-cherrypy.config.update({
- 'log.error.file': '',
- 'environment': 'production',
- 'server.socket_host': '127.0.0.1',
- 'server.socket_port': 8080,
- 'server.max_request_header_size': 0,
- 'server.max_request_body_size': 0,
- 'engine.deadlock_poll_freq': 0,
- })
-
-# Cheat mode on ;)
-del cherrypy.config['tools.log_tracebacks.on']
-del cherrypy.config['tools.log_headers.on']
-del cherrypy.config['tools.trailing_slash.on']
-
-appconf = {
- '/static': {
- 'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'static',
- 'tools.staticdir.root': curdir,
- },
- }
-app = cherrypy.tree.mount(Root(), SCRIPT_NAME, appconf)
-
-
-class NullRequest:
- """A null HTTP request class, returning 200 and an empty body."""
-
- def __init__(self, local, remote, scheme="http"):
- pass
-
- def close(self):
- pass
-
- def run(self, method, path, query_string, protocol, headers, rfile):
- cherrypy.response.status = "200 OK"
- cherrypy.response.header_list = [("Content-Type", 'text/html'),
- ("Server", "Null CherryPy"),
- ("Date", httputil.HTTPDate()),
- ("Content-Length", "0"),
- ]
- cherrypy.response.body = [""]
- return cherrypy.response
-
-
-class NullResponse:
- pass
-
-
-class ABSession:
- """A session of 'ab', the Apache HTTP server benchmarking tool.
-
-Example output from ab:
-
-This is ApacheBench, Version 2.0.40-dev <$Revision: 1.121.2.1 $> apache-2.0
-Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
-Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/
-
-Benchmarking 127.0.0.1 (be patient)
-Completed 100 requests
-Completed 200 requests
-Completed 300 requests
-Completed 400 requests
-Completed 500 requests
-Completed 600 requests
-Completed 700 requests
-Completed 800 requests
-Completed 900 requests
-
-
-Server Software: CherryPy/3.1beta
-Server Hostname: 127.0.0.1
-Server Port: 8080
-
-Document Path: /static/index.html
-Document Length: 14 bytes
-
-Concurrency Level: 10
-Time taken for tests: 9.643867 seconds
-Complete requests: 1000
-Failed requests: 0
-Write errors: 0
-Total transferred: 189000 bytes
-HTML transferred: 14000 bytes
-Requests per second: 103.69 [#/sec] (mean)
-Time per request: 96.439 [ms] (mean)
-Time per request: 9.644 [ms] (mean, across all concurrent requests)
-Transfer rate: 19.08 [Kbytes/sec] received
-
-Connection Times (ms)
- min mean[+/-sd] median max
-Connect: 0 0 2.9 0 10
-Processing: 20 94 7.3 90 130
-Waiting: 0 43 28.1 40 100
-Total: 20 95 7.3 100 130
-
-Percentage of the requests served within a certain time (ms)
- 50% 100
- 66% 100
- 75% 100
- 80% 100
- 90% 100
- 95% 100
- 98% 100
- 99% 110
- 100% 130 (longest request)
-Finished 1000 requests
-"""
-
- parse_patterns = [('complete_requests', 'Completed',
- ntob(r'^Complete requests:\s*(\d+)')),
- ('failed_requests', 'Failed',
- ntob(r'^Failed requests:\s*(\d+)')),
- ('requests_per_second', 'req/sec',
- ntob(r'^Requests per second:\s*([0-9.]+)')),
- ('time_per_request_concurrent', 'msec/req',
- ntob(r'^Time per request:\s*([0-9.]+).*concurrent requests\)$')),
- ('transfer_rate', 'KB/sec',
- ntob(r'^Transfer rate:\s*([0-9.]+)')),
- ]
-
- def __init__(self, path=SCRIPT_NAME + "/hello", requests=1000, concurrency=10):
- self.path = path
- self.requests = requests
- self.concurrency = concurrency
-
- def args(self):
- port = cherrypy.server.socket_port
- assert self.concurrency > 0
- assert self.requests > 0
- # Don't use "localhost".
- # Cf http://mail.python.org/pipermail/python-win32/2008-March/007050.html
- return ("-k -n %s -c %s http://127.0.0.1:%s%s" %
- (self.requests, self.concurrency, port, self.path))
-
- def run(self):
- # Parse output of ab, setting attributes on self
- try:
- self.output = _cpmodpy.read_process(AB_PATH or "ab", self.args())
- except:
- print(_cperror.format_exc())
- raise
-
- for attr, name, pattern in self.parse_patterns:
- val = re.search(pattern, self.output, re.MULTILINE)
- if val:
- val = val.group(1)
- setattr(self, attr, val)
- else:
- setattr(self, attr, None)
-
-
-safe_threads = (25, 50, 100, 200, 400)
-if sys.platform in ("win32",):
- # For some reason, ab crashes with > 50 threads on my Win2k laptop.
- safe_threads = (10, 20, 30, 40, 50)
-
-
-def thread_report(path=SCRIPT_NAME + "/hello", concurrency=safe_threads):
- sess = ABSession(path)
- attrs, names, patterns = list(zip(*sess.parse_patterns))
- avg = dict.fromkeys(attrs, 0.0)
-
- yield ('threads',) + names
- for c in concurrency:
- sess.concurrency = c
- sess.run()
- row = [c]
- for attr in attrs:
- val = getattr(sess, attr)
- if val is None:
- print(sess.output)
- row = None
- break
- val = float(val)
- avg[attr] += float(val)
- row.append(val)
- if row:
- yield row
-
- # Add a row of averages.
- yield ["Average"] + [str(avg[attr] / len(concurrency)) for attr in attrs]
-
-def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000),
- concurrency=50):
- sess = ABSession(concurrency=concurrency)
- attrs, names, patterns = list(zip(*sess.parse_patterns))
- yield ('bytes',) + names
- for sz in sizes:
- sess.path = "%s/sizer?size=%s" % (SCRIPT_NAME, sz)
- sess.run()
- yield [sz] + [getattr(sess, attr) for attr in attrs]
-
-def print_report(rows):
- for row in rows:
- print("")
- for i, val in enumerate(row):
- sys.stdout.write(str(val).rjust(10) + " | ")
- print("")
-
-
-def run_standard_benchmarks():
- print("")
- print("Client Thread Report (1000 requests, 14 byte response body, "
- "%s server threads):" % cherrypy.server.thread_pool)
- print_report(thread_report())
-
- print("")
- print("Client Thread Report (1000 requests, 14 bytes via staticdir, "
- "%s server threads):" % cherrypy.server.thread_pool)
- print_report(thread_report("%s/static/index.html" % SCRIPT_NAME))
-
- print("")
- print("Size Report (1000 requests, 50 client threads, "
- "%s server threads):" % cherrypy.server.thread_pool)
- print_report(size_report())
-
-
-# modpython and other WSGI #
-
-def startup_modpython(req=None):
- """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI)."""
- if cherrypy.engine.state == cherrypy._cpengine.STOPPED:
- if req:
- if "nullreq" in req.get_options():
- cherrypy.engine.request_class = NullRequest
- cherrypy.engine.response_class = NullResponse
- ab_opt = req.get_options().get("ab", "")
- if ab_opt:
- global AB_PATH
- AB_PATH = ab_opt
- cherrypy.engine.start()
- if cherrypy.engine.state == cherrypy._cpengine.STARTING:
- cherrypy.engine.wait()
- return 0 # apache.OK
-
-
-def run_modpython(use_wsgi=False):
- print("Starting mod_python...")
- pyopts = []
-
- # Pass the null and ab=path options through Apache
- if "--null" in opts:
- pyopts.append(("nullreq", ""))
-
- if "--ab" in opts:
- pyopts.append(("ab", opts["--ab"]))
-
- s = _cpmodpy.ModPythonServer
- if use_wsgi:
- pyopts.append(("wsgi.application", "cherrypy::tree"))
- pyopts.append(("wsgi.startup", "cherrypy.test.benchmark::startup_modpython"))
- handler = "modpython_gateway::handler"
- s = s(port=8080, opts=pyopts, apache_path=APACHE_PATH, handler=handler)
- else:
- pyopts.append(("cherrypy.setup", "cherrypy.test.benchmark::startup_modpython"))
- s = s(port=8080, opts=pyopts, apache_path=APACHE_PATH)
-
- try:
- s.start()
- run()
- finally:
- s.stop()
-
-
-
-if __name__ == '__main__':
- longopts = ['cpmodpy', 'modpython', 'null', 'notests',
- 'help', 'ab=', 'apache=']
- try:
- switches, args = getopt.getopt(sys.argv[1:], "", longopts)
- opts = dict(switches)
- except getopt.GetoptError:
- print(__doc__)
- sys.exit(2)
-
- if "--help" in opts:
- print(__doc__)
- sys.exit(0)
-
- if "--ab" in opts:
- AB_PATH = opts['--ab']
-
- if "--notests" in opts:
- # Return without stopping the server, so that the pages
- # can be tested from a standard web browser.
- def run():
- port = cherrypy.server.socket_port
- print("You may now open http://127.0.0.1:%s%s/" %
- (port, SCRIPT_NAME))
-
- if "--null" in opts:
- print("Using null Request object")
- else:
- def run():
- end = time.time() - start
- print("Started in %s seconds" % end)
- if "--null" in opts:
- print("\nUsing null Request object")
- try:
- try:
- run_standard_benchmarks()
- except:
- print(_cperror.format_exc())
- raise
- finally:
- cherrypy.engine.exit()
-
- print("Starting CherryPy app server...")
-
- class NullWriter(object):
- """Suppresses the printing of socket errors."""
- def write(self, data):
- pass
- sys.stderr = NullWriter()
-
- start = time.time()
-
- if "--cpmodpy" in opts:
- run_modpython()
- elif "--modpython" in opts:
- run_modpython(use_wsgi=True)
- else:
- if "--null" in opts:
- cherrypy.server.request_class = NullRequest
- cherrypy.server.response_class = NullResponse
-
- cherrypy.engine.start_with_callback(run)
- cherrypy.engine.block()
diff --git a/cherrypy/test/checkerdemo.py b/cherrypy/test/checkerdemo.py
deleted file mode 100644
index 32a7dee2..00000000
--- a/cherrypy/test/checkerdemo.py
+++ /dev/null
@@ -1,47 +0,0 @@
-"""Demonstration app for cherrypy.checker.
-
-This application is intentionally broken and badly designed.
-To demonstrate the output of the CherryPy Checker, simply execute
-this module.
-"""
-
-import os
-import cherrypy
-thisdir = os.path.dirname(os.path.abspath(__file__))
-
-class Root:
- pass
-
-if __name__ == '__main__':
- conf = {'/base': {'tools.staticdir.root': thisdir,
- # Obsolete key.
- 'throw_errors': True,
- },
- # This entry should be OK.
- '/base/static': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'static'},
- # Warn on missing folder.
- '/base/js': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'js'},
- # Warn on dir with an abs path even though we provide root.
- '/base/static2': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': '/static'},
- # Warn on dir with a relative path with no root.
- '/static3': {'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'static'},
- # Warn on unknown namespace
- '/unknown': {'toobles.gzip.on': True},
- # Warn special on cherrypy..*
- '/cpknown': {'cherrypy.tools.encode.on': True},
- # Warn on mismatched types
- '/conftype': {'request.show_tracebacks': 14},
- # Warn on unknown tool.
- '/web': {'tools.unknown.on': True},
- # Warn on server.* in app config.
- '/app1': {'server.socket_host': '0.0.0.0'},
- # Warn on 'localhost'
- 'global': {'server.socket_host': 'localhost'},
- # Warn on '[name]'
- '[/extra_brackets]': {},
- }
- cherrypy.quickstart(Root(), config=conf)
diff --git a/cherrypy/test/fastcgi.conf b/cherrypy/test/fastcgi.conf
deleted file mode 100644
index e5c5163c..00000000
--- a/cherrypy/test/fastcgi.conf
+++ /dev/null
@@ -1,18 +0,0 @@
-
-# Apache2 server conf file for testing CherryPy with mod_fastcgi.
-# fumanchu: I had to hard-code paths due to crazy Debian layouts :(
-ServerRoot /usr/lib/apache2
-User #1000
-ErrorLog /usr/lib/python2.5/site-packages/cproot/trunk/cherrypy/test/mod_fastcgi.error.log
-
-DocumentRoot "/usr/lib/python2.5/site-packages/cproot/trunk/cherrypy/test"
-ServerName 127.0.0.1
-Listen 8080
-LoadModule fastcgi_module modules/mod_fastcgi.so
-LoadModule rewrite_module modules/mod_rewrite.so
-
-Options +ExecCGI
-SetHandler fastcgi-script
-RewriteEngine On
-RewriteRule ^(.*)$ /fastcgi.pyc [L]
-FastCgiExternalServer "/usr/lib/python2.5/site-packages/cproot/trunk/cherrypy/test/fastcgi.pyc" -host 127.0.0.1:4000
diff --git a/cherrypy/test/fcgi.conf b/cherrypy/test/fcgi.conf
deleted file mode 100644
index 8cf24b64..00000000
--- a/cherrypy/test/fcgi.conf
+++ /dev/null
@@ -1,14 +0,0 @@
-
-# Apache2 server conf file for testing CherryPy with mod_fcgid.
-
-DocumentRoot "C:\Python25\Lib\site-packages\cherrypy\test"
-ServerName 127.0.0.1
-Listen 8080
-LoadModule fastcgi_module modules/mod_fastcgi.dll
-LoadModule rewrite_module modules/mod_rewrite.so
-
-Options ExecCGI
-SetHandler fastcgi-script
-RewriteEngine On
-RewriteRule ^(.*)$ /fastcgi.pyc [L]
-FastCgiExternalServer "C:\\Python25\\Lib\\site-packages\\cherrypy\\test\\fastcgi.pyc" -host 127.0.0.1:4000
diff --git a/cherrypy/test/helper.py b/cherrypy/test/helper.py
deleted file mode 100644
index ff9e06cf..00000000
--- a/cherrypy/test/helper.py
+++ /dev/null
@@ -1,476 +0,0 @@
-"""A library of helper functions for the CherryPy test suite."""
-
-import datetime
-import logging
-log = logging.getLogger(__name__)
-import os
-thisdir = os.path.abspath(os.path.dirname(__file__))
-serverpem = os.path.join(os.getcwd(), thisdir, 'test.pem')
-
-import re
-import sys
-import time
-import warnings
-
-import cherrypy
-from cherrypy._cpcompat import basestring, copyitems, HTTPSConnection, ntob
-from cherrypy.lib import httputil
-from cherrypy.lib.reprconf import unrepr
-from cherrypy.test import webtest
-
-import nose
-
-_testconfig = None
-
-def get_tst_config(overconf = {}):
- global _testconfig
- if _testconfig is None:
- conf = {
- 'scheme': 'http',
- 'protocol': "HTTP/1.1",
- 'port': 8080,
- 'host': '127.0.0.1',
- 'validate': False,
- 'conquer': False,
- 'server': 'wsgi',
- }
- try:
- import testconfig
- _conf = testconfig.config.get('supervisor', None)
- if _conf is not None:
- for k, v in _conf.items():
- if isinstance(v, basestring):
- _conf[k] = unrepr(v)
- conf.update(_conf)
- except ImportError:
- pass
- _testconfig = conf
- conf = _testconfig.copy()
- conf.update(overconf)
-
- return conf
-
-class Supervisor(object):
- """Base class for modeling and controlling servers during testing."""
-
- def __init__(self, **kwargs):
- for k, v in kwargs.items():
- if k == 'port':
- setattr(self, k, int(v))
- setattr(self, k, v)
-
-
-log_to_stderr = lambda msg, level: sys.stderr.write(msg + os.linesep)
-
-class LocalSupervisor(Supervisor):
- """Base class for modeling/controlling servers which run in the same process.
-
- When the server side runs in a different process, start/stop can dump all
- state between each test module easily. When the server side runs in the
- same process as the client, however, we have to do a bit more work to ensure
- config and mounted apps are reset between tests.
- """
-
- using_apache = False
- using_wsgi = False
-
- def __init__(self, **kwargs):
- for k, v in kwargs.items():
- setattr(self, k, v)
-
- cherrypy.server.httpserver = self.httpserver_class
-
- engine = cherrypy.engine
- if hasattr(engine, "signal_handler"):
- engine.signal_handler.subscribe()
- if hasattr(engine, "console_control_handler"):
- engine.console_control_handler.subscribe()
- #engine.subscribe('log', log_to_stderr)
-
- def start(self, modulename=None):
- """Load and start the HTTP server."""
- if modulename:
- # Unhook httpserver so cherrypy.server.start() creates a new
- # one (with config from setup_server, if declared).
- cherrypy.server.httpserver = None
-
- cherrypy.engine.start()
-
- self.sync_apps()
-
- def sync_apps(self):
- """Tell the server about any apps which the setup functions mounted."""
- pass
-
- def stop(self):
- td = getattr(self, 'teardown', None)
- if td:
- td()
-
- cherrypy.engine.exit()
-
- for name, server in copyitems(getattr(cherrypy, 'servers', {})):
- server.unsubscribe()
- del cherrypy.servers[name]
-
-
-class NativeServerSupervisor(LocalSupervisor):
- """Server supervisor for the builtin HTTP server."""
-
- httpserver_class = "cherrypy._cpnative_server.CPHTTPServer"
- using_apache = False
- using_wsgi = False
-
- def __str__(self):
- return "Builtin HTTP Server on %s:%s" % (self.host, self.port)
-
-
-class LocalWSGISupervisor(LocalSupervisor):
- """Server supervisor for the builtin WSGI server."""
-
- httpserver_class = "cherrypy._cpwsgi_server.CPWSGIServer"
- using_apache = False
- using_wsgi = True
-
- def __str__(self):
- return "Builtin WSGI Server on %s:%s" % (self.host, self.port)
-
- def sync_apps(self):
- """Hook a new WSGI app into the origin server."""
- cherrypy.server.httpserver.wsgi_app = self.get_app()
-
- def get_app(self, app=None):
- """Obtain a new (decorated) WSGI app to hook into the origin server."""
- if app is None:
- app = cherrypy.tree
-
- if self.conquer:
- try:
- import wsgiconq
- except ImportError:
- warnings.warn("Error importing wsgiconq. pyconquer will not run.")
- else:
- app = wsgiconq.WSGILogger(app, c_calls=True)
-
- if self.validate:
- try:
- from wsgiref import validate
- except ImportError:
- warnings.warn("Error importing wsgiref. The validator will not run.")
- else:
- #wraps the app in the validator
- app = validate.validator(app)
-
- return app
-
-
-def get_cpmodpy_supervisor(**options):
- from cherrypy.test import modpy
- sup = modpy.ModPythonSupervisor(**options)
- sup.template = modpy.conf_cpmodpy
- return sup
-
-def get_modpygw_supervisor(**options):
- from cherrypy.test import modpy
- sup = modpy.ModPythonSupervisor(**options)
- sup.template = modpy.conf_modpython_gateway
- sup.using_wsgi = True
- return sup
-
-def get_modwsgi_supervisor(**options):
- from cherrypy.test import modwsgi
- return modwsgi.ModWSGISupervisor(**options)
-
-def get_modfcgid_supervisor(**options):
- from cherrypy.test import modfcgid
- return modfcgid.ModFCGISupervisor(**options)
-
-def get_modfastcgi_supervisor(**options):
- from cherrypy.test import modfastcgi
- return modfastcgi.ModFCGISupervisor(**options)
-
-def get_wsgi_u_supervisor(**options):
- cherrypy.server.wsgi_version = ('u', 0)
- return LocalWSGISupervisor(**options)
-
-
-class CPWebCase(webtest.WebCase):
-
- script_name = ""
- scheme = "http"
-
- available_servers = {'wsgi': LocalWSGISupervisor,
- 'wsgi_u': get_wsgi_u_supervisor,
- 'native': NativeServerSupervisor,
- 'cpmodpy': get_cpmodpy_supervisor,
- 'modpygw': get_modpygw_supervisor,
- 'modwsgi': get_modwsgi_supervisor,
- 'modfcgid': get_modfcgid_supervisor,
- 'modfastcgi': get_modfastcgi_supervisor,
- }
- default_server = "wsgi"
-
- def _setup_server(cls, supervisor, conf):
- v = sys.version.split()[0]
- log.info("Python version used to run this test script: %s" % v)
- log.info("CherryPy version: %s" % cherrypy.__version__)
- if supervisor.scheme == "https":
- ssl = " (ssl)"
- else:
- ssl = ""
- log.info("HTTP server version: %s%s" % (supervisor.protocol, ssl))
- log.info("PID: %s" % os.getpid())
-
- cherrypy.server.using_apache = supervisor.using_apache
- cherrypy.server.using_wsgi = supervisor.using_wsgi
-
- if sys.platform[:4] == 'java':
- cherrypy.config.update({'server.nodelay': False})
-
- if isinstance(conf, basestring):
- parser = cherrypy.lib.reprconf.Parser()
- conf = parser.dict_from_file(conf).get('global', {})
- else:
- conf = conf or {}
- baseconf = conf.copy()
- baseconf.update({'server.socket_host': supervisor.host,
- 'server.socket_port': supervisor.port,
- 'server.protocol_version': supervisor.protocol,
- 'environment': "test_suite",
- })
- if supervisor.scheme == "https":
- #baseconf['server.ssl_module'] = 'builtin'
- baseconf['server.ssl_certificate'] = serverpem
- baseconf['server.ssl_private_key'] = serverpem
-
- # helper must be imported lazily so the coverage tool
- # can run against module-level statements within cherrypy.
- # Also, we have to do "from cherrypy.test import helper",
- # exactly like each test module does, because a relative import
- # would stick a second instance of webtest in sys.modules,
- # and we wouldn't be able to globally override the port anymore.
- if supervisor.scheme == "https":
- webtest.WebCase.HTTP_CONN = HTTPSConnection
- return baseconf
- _setup_server = classmethod(_setup_server)
-
- def setup_class(cls):
- ''
- #Creates a server
- conf = get_tst_config()
- supervisor_factory = cls.available_servers.get(conf.get('server', 'wsgi'))
- if supervisor_factory is None:
- raise RuntimeError('Unknown server in config: %s' % conf['server'])
- supervisor = supervisor_factory(**conf)
-
- #Copied from "run_test_suite"
- cherrypy.config.reset()
- baseconf = cls._setup_server(supervisor, conf)
- cherrypy.config.update(baseconf)
- setup_client()
-
- if hasattr(cls, 'setup_server'):
- # Clear the cherrypy tree and clear the wsgi server so that
- # it can be updated with the new root
- cherrypy.tree = cherrypy._cptree.Tree()
- cherrypy.server.httpserver = None
- cls.setup_server()
- supervisor.start(cls.__module__)
-
- cls.supervisor = supervisor
- setup_class = classmethod(setup_class)
-
- def teardown_class(cls):
- ''
- if hasattr(cls, 'setup_server'):
- cls.supervisor.stop()
- teardown_class = classmethod(teardown_class)
-
- def prefix(self):
- return self.script_name.rstrip("/")
-
- def base(self):
- if ((self.scheme == "http" and self.PORT == 80) or
- (self.scheme == "https" and self.PORT == 443)):
- port = ""
- else:
- port = ":%s" % self.PORT
-
- return "%s://%s%s%s" % (self.scheme, self.HOST, port,
- self.script_name.rstrip("/"))
-
- def exit(self):
- sys.exit()
-
- def getPage(self, url, headers=None, method="GET", body=None, protocol=None):
- """Open the url. Return status, headers, body."""
- if self.script_name:
- url = httputil.urljoin(self.script_name, url)
- return webtest.WebCase.getPage(self, url, headers, method, body, protocol)
-
- def skip(self, msg='skipped '):
- raise nose.SkipTest(msg)
-
- def assertErrorPage(self, status, message=None, pattern=''):
- """Compare the response body with a built in error page.
-
- The function will optionally look for the regexp pattern,
- within the exception embedded in the error page."""
-
- # This will never contain a traceback
- page = cherrypy._cperror.get_error_page(status, message=message)
-
- # First, test the response body without checking the traceback.
- # Stick a match-all group (.*) in to grab the traceback.
- esc = re.escape
- epage = esc(page)
- epage = epage.replace(esc(''),
- esc('') + '(.*)' + esc(''))
- m = re.match(ntob(epage, self.encoding), self.body, re.DOTALL)
- if not m:
- self._handlewebError('Error page does not match; expected:\n' + page)
- return
-
- # Now test the pattern against the traceback
- if pattern is None:
- # Special-case None to mean that there should be *no* traceback.
- if m and m.group(1):
- self._handlewebError('Error page contains traceback')
- else:
- if (m is None) or (
- not re.search(ntob(re.escape(pattern), self.encoding),
- m.group(1))):
- msg = 'Error page does not contain %s in traceback'
- self._handlewebError(msg % repr(pattern))
-
- date_tolerance = 2
-
- def assertEqualDates(self, dt1, dt2, seconds=None):
- """Assert abs(dt1 - dt2) is within Y seconds."""
- if seconds is None:
- seconds = self.date_tolerance
-
- if dt1 > dt2:
- diff = dt1 - dt2
- else:
- diff = dt2 - dt1
- if not diff < datetime.timedelta(seconds=seconds):
- raise AssertionError('%r and %r are not within %r seconds.' %
- (dt1, dt2, seconds))
-
-
-def setup_client():
- """Set up the WebCase classes to match the server's socket settings."""
- webtest.WebCase.PORT = cherrypy.server.socket_port
- webtest.WebCase.HOST = cherrypy.server.socket_host
- if cherrypy.server.ssl_certificate:
- CPWebCase.scheme = 'https'
-
-# --------------------------- Spawning helpers --------------------------- #
-
-
-class CPProcess(object):
-
- pid_file = os.path.join(thisdir, 'test.pid')
- config_file = os.path.join(thisdir, 'test.conf')
- config_template = """[global]
-server.socket_host: '%(host)s'
-server.socket_port: %(port)s
-checker.on: False
-log.screen: False
-log.error_file: r'%(error_log)s'
-log.access_file: r'%(access_log)s'
-%(ssl)s
-%(extra)s
-"""
- error_log = os.path.join(thisdir, 'test.error.log')
- access_log = os.path.join(thisdir, 'test.access.log')
-
- def __init__(self, wait=False, daemonize=False, ssl=False, socket_host=None, socket_port=None):
- self.wait = wait
- self.daemonize = daemonize
- self.ssl = ssl
- self.host = socket_host or cherrypy.server.socket_host
- self.port = socket_port or cherrypy.server.socket_port
-
- def write_conf(self, extra=""):
- if self.ssl:
- serverpem = os.path.join(thisdir, 'test.pem')
- ssl = """
-server.ssl_certificate: r'%s'
-server.ssl_private_key: r'%s'
-""" % (serverpem, serverpem)
- else:
- ssl = ""
-
- conf = self.config_template % {
- 'host': self.host,
- 'port': self.port,
- 'error_log': self.error_log,
- 'access_log': self.access_log,
- 'ssl': ssl,
- 'extra': extra,
- }
- f = open(self.config_file, 'wb')
- f.write(ntob(conf, 'utf-8'))
- f.close()
-
- def start(self, imports=None):
- """Start cherryd in a subprocess."""
- cherrypy._cpserver.wait_for_free_port(self.host, self.port)
-
- args = [sys.executable, os.path.join(thisdir, '..', 'cherryd'),
- '-c', self.config_file, '-p', self.pid_file]
-
- if not isinstance(imports, (list, tuple)):
- imports = [imports]
- for i in imports:
- if i:
- args.append('-i')
- args.append(i)
-
- if self.daemonize:
- args.append('-d')
-
- env = os.environ.copy()
- # Make sure we import the cherrypy package in which this module is defined.
- grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
- if env.get('PYTHONPATH', ''):
- env['PYTHONPATH'] = os.pathsep.join((grandparentdir, env['PYTHONPATH']))
- else:
- env['PYTHONPATH'] = grandparentdir
- if self.wait:
- self.exit_code = os.spawnve(os.P_WAIT, sys.executable, args, env)
- else:
- os.spawnve(os.P_NOWAIT, sys.executable, args, env)
- cherrypy._cpserver.wait_for_occupied_port(self.host, self.port)
-
- # Give the engine a wee bit more time to finish STARTING
- if self.daemonize:
- time.sleep(2)
- else:
- time.sleep(1)
-
- def get_pid(self):
- return int(open(self.pid_file, 'rb').read())
-
- def join(self):
- """Wait for the process to exit."""
- try:
- try:
- # Mac, UNIX
- os.wait()
- except AttributeError:
- # Windows
- try:
- pid = self.get_pid()
- except IOError:
- # Assume the subprocess deleted the pidfile on shutdown.
- pass
- else:
- os.waitpid(pid, 0)
- except OSError:
- x = sys.exc_info()[1]
- if x.args != (10, 'No child processes'):
- raise
-
diff --git a/cherrypy/test/logtest.py b/cherrypy/test/logtest.py
deleted file mode 100644
index c093da2c..00000000
--- a/cherrypy/test/logtest.py
+++ /dev/null
@@ -1,181 +0,0 @@
-"""logtest, a unittest.TestCase helper for testing log output."""
-
-import sys
-import time
-
-import cherrypy
-
-
-try:
- # On Windows, msvcrt.getch reads a single char without output.
- import msvcrt
- def getchar():
- return msvcrt.getch()
-except ImportError:
- # Unix getchr
- import tty, termios
- def getchar():
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(sys.stdin.fileno())
- ch = sys.stdin.read(1)
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
-
-
-class LogCase(object):
- """unittest.TestCase mixin for testing log messages.
-
- logfile: a filename for the desired log. Yes, I know modes are evil,
- but it makes the test functions so much cleaner to set this once.
-
- lastmarker: the last marker in the log. This can be used to search for
- messages since the last marker.
-
- markerPrefix: a string with which to prefix log markers. This should be
- unique enough from normal log output to use for marker identification.
- """
-
- logfile = None
- lastmarker = None
- markerPrefix = "test suite marker: "
-
- def _handleLogError(self, msg, data, marker, pattern):
- print("")
- print(" ERROR: %s" % msg)
-
- if not self.interactive:
- raise self.failureException(msg)
-
- p = " Show: [L]og [M]arker [P]attern; [I]gnore, [R]aise, or sys.e[X]it >> "
- print p,
- # ARGH
- sys.stdout.flush()
- while True:
- i = getchar().upper()
- if i not in "MPLIRX":
- continue
- print(i.upper()) # Also prints new line
- if i == "L":
- for x, line in enumerate(data):
- if (x + 1) % self.console_height == 0:
- # The \r and comma should make the next line overwrite
- print "<-- More -->\r",
- m = getchar().lower()
- # Erase our "More" prompt
- print " \r",
- if m == "q":
- break
- print(line.rstrip())
- elif i == "M":
- print(repr(marker or self.lastmarker))
- elif i == "P":
- print(repr(pattern))
- elif i == "I":
- # return without raising the normal exception
- return
- elif i == "R":
- raise self.failureException(msg)
- elif i == "X":
- self.exit()
- print p,
-
- def exit(self):
- sys.exit()
-
- def emptyLog(self):
- """Overwrite self.logfile with 0 bytes."""
- open(self.logfile, 'wb').write("")
-
- def markLog(self, key=None):
- """Insert a marker line into the log and set self.lastmarker."""
- if key is None:
- key = str(time.time())
- self.lastmarker = key
-
- open(self.logfile, 'ab+').write("%s%s\n" % (self.markerPrefix, key))
-
- def _read_marked_region(self, marker=None):
- """Return lines from self.logfile in the marked region.
-
- If marker is None, self.lastmarker is used. If the log hasn't
- been marked (using self.markLog), the entire log will be returned.
- """
-## # Give the logger time to finish writing?
-## time.sleep(0.5)
-
- logfile = self.logfile
- marker = marker or self.lastmarker
- if marker is None:
- return open(logfile, 'rb').readlines()
-
- data = []
- in_region = False
- for line in open(logfile, 'rb'):
- if in_region:
- if (line.startswith(self.markerPrefix) and not marker in line):
- break
- else:
- data.append(line)
- elif marker in line:
- in_region = True
- return data
-
- def assertInLog(self, line, marker=None):
- """Fail if the given (partial) line is not in the log.
-
- The log will be searched from the given marker to the next marker.
- If marker is None, self.lastmarker is used. If the log hasn't
- been marked (using self.markLog), the entire log will be searched.
- """
- data = self._read_marked_region(marker)
- for logline in data:
- if line in logline:
- return
- msg = "%r not found in log" % line
- self._handleLogError(msg, data, marker, line)
-
- def assertNotInLog(self, line, marker=None):
- """Fail if the given (partial) line is in the log.
-
- The log will be searched from the given marker to the next marker.
- If marker is None, self.lastmarker is used. If the log hasn't
- been marked (using self.markLog), the entire log will be searched.
- """
- data = self._read_marked_region(marker)
- for logline in data:
- if line in logline:
- msg = "%r found in log" % line
- self._handleLogError(msg, data, marker, line)
-
- def assertLog(self, sliceargs, lines, marker=None):
- """Fail if log.readlines()[sliceargs] is not contained in 'lines'.
-
- The log will be searched from the given marker to the next marker.
- If marker is None, self.lastmarker is used. If the log hasn't
- been marked (using self.markLog), the entire log will be searched.
- """
- data = self._read_marked_region(marker)
- if isinstance(sliceargs, int):
- # Single arg. Use __getitem__ and allow lines to be str or list.
- if isinstance(lines, (tuple, list)):
- lines = lines[0]
- if lines not in data[sliceargs]:
- msg = "%r not found on log line %r" % (lines, sliceargs)
- self._handleLogError(msg, [data[sliceargs]], marker, lines)
- else:
- # Multiple args. Use __getslice__ and require lines to be list.
- if isinstance(lines, tuple):
- lines = list(lines)
- elif isinstance(lines, basestring):
- raise TypeError("The 'lines' arg must be a list when "
- "'sliceargs' is a tuple.")
-
- start, stop = sliceargs
- for line, logline in zip(lines, data[start:stop]):
- if line not in logline:
- msg = "%r not found in log" % line
- self._handleLogError(msg, data[start:stop], marker, line)
-
diff --git a/cherrypy/test/modfastcgi.py b/cherrypy/test/modfastcgi.py
deleted file mode 100644
index 95acf141..00000000
--- a/cherrypy/test/modfastcgi.py
+++ /dev/null
@@ -1,135 +0,0 @@
-"""Wrapper for mod_fastcgi, for use as a CherryPy HTTP server when testing.
-
-To autostart fastcgi, the "apache" executable or script must be
-on your system path, or you must override the global APACHE_PATH.
-On some platforms, "apache" may be called "apachectl", "apache2ctl",
-or "httpd"--create a symlink to them if needed.
-
-You'll also need the WSGIServer from flup.servers.
-See http://projects.amor.org/misc/wiki/ModPythonGateway
-
-
-KNOWN BUGS
-==========
-
-1. Apache processes Range headers automatically; CherryPy's truncated
- output is then truncated again by Apache. See test_core.testRanges.
- This was worked around in http://www.cherrypy.org/changeset/1319.
-2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
- See test_core.testHTTPMethods.
-3. Max request header and body settings do not work with Apache.
-4. Apache replaces status "reason phrases" automatically. For example,
- CherryPy may set "304 Not modified" but Apache will write out
- "304 Not Modified" (capital "M").
-5. Apache does not allow custom error codes as per the spec.
-6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
- Request-URI too early.
-7. mod_python will not read request bodies which use the "chunked"
- transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
- instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
- mod_python's requestobject.c).
-8. Apache will output a "Content-Length: 0" response header even if there's
- no response entity body. This isn't really a bug; it just differs from
- the CherryPy default.
-"""
-
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-import re
-import sys
-import time
-
-import cherrypy
-from cherrypy.process import plugins, servers
-from cherrypy.test import helper
-
-
-def read_process(cmd, args=""):
- pipein, pipeout = os.popen4("%s %s" % (cmd, args))
- try:
- firstline = pipeout.readline()
- if (re.search(r"(not recognized|No such file|not found)", firstline,
- re.IGNORECASE)):
- raise IOError('%s must be on your system path.' % cmd)
- output = firstline + pipeout.read()
- finally:
- pipeout.close()
- return output
-
-
-APACHE_PATH = "apache2ctl"
-CONF_PATH = "fastcgi.conf"
-
-conf_fastcgi = """
-# Apache2 server conf file for testing CherryPy with mod_fastcgi.
-# fumanchu: I had to hard-code paths due to crazy Debian layouts :(
-ServerRoot /usr/lib/apache2
-User #1000
-ErrorLog %(root)s/mod_fastcgi.error.log
-
-DocumentRoot "%(root)s"
-ServerName 127.0.0.1
-Listen %(port)s
-LoadModule fastcgi_module modules/mod_fastcgi.so
-LoadModule rewrite_module modules/mod_rewrite.so
-
-Options +ExecCGI
-SetHandler fastcgi-script
-RewriteEngine On
-RewriteRule ^(.*)$ /fastcgi.pyc [L]
-FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
-"""
-
-def erase_script_name(environ, start_response):
- environ['SCRIPT_NAME'] = ''
- return cherrypy.tree(environ, start_response)
-
-class ModFCGISupervisor(helper.LocalWSGISupervisor):
-
- httpserver_class = "cherrypy.process.servers.FlupFCGIServer"
- using_apache = True
- using_wsgi = True
- template = conf_fastcgi
-
- def __str__(self):
- return "FCGI Server on %s:%s" % (self.host, self.port)
-
- def start(self, modulename):
- cherrypy.server.httpserver = servers.FlupFCGIServer(
- application=erase_script_name, bindAddress=('127.0.0.1', 4000))
- cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000)
- cherrypy.server.socket_port = 4000
- # For FCGI, we both start apache...
- self.start_apache()
- # ...and our local server
- cherrypy.engine.start()
- self.sync_apps()
-
- def start_apache(self):
- fcgiconf = CONF_PATH
- if not os.path.isabs(fcgiconf):
- fcgiconf = os.path.join(curdir, fcgiconf)
-
- # Write the Apache conf file.
- f = open(fcgiconf, 'wb')
- try:
- server = repr(os.path.join(curdir, 'fastcgi.pyc'))[1:-1]
- output = self.template % {'port': self.port, 'root': curdir,
- 'server': server}
- output = output.replace('\r\n', '\n')
- f.write(output)
- finally:
- f.close()
-
- result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf)
- if result:
- print(result)
-
- def stop(self):
- """Gracefully shutdown a server that is serving forever."""
- read_process(APACHE_PATH, "-k stop")
- helper.LocalWSGISupervisor.stop(self)
-
- def sync_apps(self):
- cherrypy.server.httpserver.fcgiserver.application = self.get_app(erase_script_name)
-
diff --git a/cherrypy/test/modfcgid.py b/cherrypy/test/modfcgid.py
deleted file mode 100644
index 736aa4c8..00000000
--- a/cherrypy/test/modfcgid.py
+++ /dev/null
@@ -1,125 +0,0 @@
-"""Wrapper for mod_fcgid, for use as a CherryPy HTTP server when testing.
-
-To autostart fcgid, the "apache" executable or script must be
-on your system path, or you must override the global APACHE_PATH.
-On some platforms, "apache" may be called "apachectl", "apache2ctl",
-or "httpd"--create a symlink to them if needed.
-
-You'll also need the WSGIServer from flup.servers.
-See http://projects.amor.org/misc/wiki/ModPythonGateway
-
-
-KNOWN BUGS
-==========
-
-1. Apache processes Range headers automatically; CherryPy's truncated
- output is then truncated again by Apache. See test_core.testRanges.
- This was worked around in http://www.cherrypy.org/changeset/1319.
-2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
- See test_core.testHTTPMethods.
-3. Max request header and body settings do not work with Apache.
-4. Apache replaces status "reason phrases" automatically. For example,
- CherryPy may set "304 Not modified" but Apache will write out
- "304 Not Modified" (capital "M").
-5. Apache does not allow custom error codes as per the spec.
-6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
- Request-URI too early.
-7. mod_python will not read request bodies which use the "chunked"
- transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
- instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
- mod_python's requestobject.c).
-8. Apache will output a "Content-Length: 0" response header even if there's
- no response entity body. This isn't really a bug; it just differs from
- the CherryPy default.
-"""
-
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-import re
-import sys
-import time
-
-import cherrypy
-from cherrypy._cpcompat import ntob
-from cherrypy.process import plugins, servers
-from cherrypy.test import helper
-
-
-def read_process(cmd, args=""):
- pipein, pipeout = os.popen4("%s %s" % (cmd, args))
- try:
- firstline = pipeout.readline()
- if (re.search(r"(not recognized|No such file|not found)", firstline,
- re.IGNORECASE)):
- raise IOError('%s must be on your system path.' % cmd)
- output = firstline + pipeout.read()
- finally:
- pipeout.close()
- return output
-
-
-APACHE_PATH = "httpd"
-CONF_PATH = "fcgi.conf"
-
-conf_fcgid = """
-# Apache2 server conf file for testing CherryPy with mod_fcgid.
-
-DocumentRoot "%(root)s"
-ServerName 127.0.0.1
-Listen %(port)s
-LoadModule fastcgi_module modules/mod_fastcgi.dll
-LoadModule rewrite_module modules/mod_rewrite.so
-
-Options ExecCGI
-SetHandler fastcgi-script
-RewriteEngine On
-RewriteRule ^(.*)$ /fastcgi.pyc [L]
-FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
-"""
-
-class ModFCGISupervisor(helper.LocalSupervisor):
-
- using_apache = True
- using_wsgi = True
- template = conf_fcgid
-
- def __str__(self):
- return "FCGI Server on %s:%s" % (self.host, self.port)
-
- def start(self, modulename):
- cherrypy.server.httpserver = servers.FlupFCGIServer(
- application=cherrypy.tree, bindAddress=('127.0.0.1', 4000))
- cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000)
- # For FCGI, we both start apache...
- self.start_apache()
- # ...and our local server
- helper.LocalServer.start(self, modulename)
-
- def start_apache(self):
- fcgiconf = CONF_PATH
- if not os.path.isabs(fcgiconf):
- fcgiconf = os.path.join(curdir, fcgiconf)
-
- # Write the Apache conf file.
- f = open(fcgiconf, 'wb')
- try:
- server = repr(os.path.join(curdir, 'fastcgi.pyc'))[1:-1]
- output = self.template % {'port': self.port, 'root': curdir,
- 'server': server}
- output = ntob(output.replace('\r\n', '\n'))
- f.write(output)
- finally:
- f.close()
-
- result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf)
- if result:
- print(result)
-
- def stop(self):
- """Gracefully shutdown a server that is serving forever."""
- read_process(APACHE_PATH, "-k stop")
- helper.LocalServer.stop(self)
-
- def sync_apps(self):
- cherrypy.server.httpserver.fcgiserver.application = self.get_app()
-
diff --git a/cherrypy/test/modpy.py b/cherrypy/test/modpy.py
deleted file mode 100644
index 519571fc..00000000
--- a/cherrypy/test/modpy.py
+++ /dev/null
@@ -1,163 +0,0 @@
-"""Wrapper for mod_python, for use as a CherryPy HTTP server when testing.
-
-To autostart modpython, the "apache" executable or script must be
-on your system path, or you must override the global APACHE_PATH.
-On some platforms, "apache" may be called "apachectl" or "apache2ctl"--
-create a symlink to them if needed.
-
-If you wish to test the WSGI interface instead of our _cpmodpy interface,
-you also need the 'modpython_gateway' module at:
-http://projects.amor.org/misc/wiki/ModPythonGateway
-
-
-KNOWN BUGS
-==========
-
-1. Apache processes Range headers automatically; CherryPy's truncated
- output is then truncated again by Apache. See test_core.testRanges.
- This was worked around in http://www.cherrypy.org/changeset/1319.
-2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
- See test_core.testHTTPMethods.
-3. Max request header and body settings do not work with Apache.
-4. Apache replaces status "reason phrases" automatically. For example,
- CherryPy may set "304 Not modified" but Apache will write out
- "304 Not Modified" (capital "M").
-5. Apache does not allow custom error codes as per the spec.
-6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
- Request-URI too early.
-7. mod_python will not read request bodies which use the "chunked"
- transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
- instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
- mod_python's requestobject.c).
-8. Apache will output a "Content-Length: 0" response header even if there's
- no response entity body. This isn't really a bug; it just differs from
- the CherryPy default.
-"""
-
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-import re
-import time
-
-from cherrypy.test import helper
-
-
-def read_process(cmd, args=""):
- pipein, pipeout = os.popen4("%s %s" % (cmd, args))
- try:
- firstline = pipeout.readline()
- if (re.search(r"(not recognized|No such file|not found)", firstline,
- re.IGNORECASE)):
- raise IOError('%s must be on your system path.' % cmd)
- output = firstline + pipeout.read()
- finally:
- pipeout.close()
- return output
-
-
-APACHE_PATH = "httpd"
-CONF_PATH = "test_mp.conf"
-
-conf_modpython_gateway = """
-# Apache2 server conf file for testing CherryPy with modpython_gateway.
-
-ServerName 127.0.0.1
-DocumentRoot "/"
-Listen %(port)s
-LoadModule python_module modules/mod_python.so
-
-SetHandler python-program
-PythonFixupHandler cherrypy.test.modpy::wsgisetup
-PythonOption testmod %(modulename)s
-PythonHandler modpython_gateway::handler
-PythonOption wsgi.application cherrypy::tree
-PythonOption socket_host %(host)s
-PythonDebug On
-"""
-
-conf_cpmodpy = """
-# Apache2 server conf file for testing CherryPy with _cpmodpy.
-
-ServerName 127.0.0.1
-DocumentRoot "/"
-Listen %(port)s
-LoadModule python_module modules/mod_python.so
-
-SetHandler python-program
-PythonFixupHandler cherrypy.test.modpy::cpmodpysetup
-PythonHandler cherrypy._cpmodpy::handler
-PythonOption cherrypy.setup cherrypy.test.%(modulename)s::setup_server
-PythonOption socket_host %(host)s
-PythonDebug On
-"""
-
-class ModPythonSupervisor(helper.Supervisor):
-
- using_apache = True
- using_wsgi = False
- template = None
-
- def __str__(self):
- return "ModPython Server on %s:%s" % (self.host, self.port)
-
- def start(self, modulename):
- mpconf = CONF_PATH
- if not os.path.isabs(mpconf):
- mpconf = os.path.join(curdir, mpconf)
-
- f = open(mpconf, 'wb')
- try:
- f.write(self.template %
- {'port': self.port, 'modulename': modulename,
- 'host': self.host})
- finally:
- f.close()
-
- result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
- if result:
- print(result)
-
- def stop(self):
- """Gracefully shutdown a server that is serving forever."""
- read_process(APACHE_PATH, "-k stop")
-
-
-loaded = False
-def wsgisetup(req):
- global loaded
- if not loaded:
- loaded = True
- options = req.get_options()
-
- import cherrypy
- cherrypy.config.update({
- "log.error_file": os.path.join(curdir, "test.log"),
- "environment": "test_suite",
- "server.socket_host": options['socket_host'],
- })
-
- modname = options['testmod']
- mod = __import__(modname, globals(), locals(), [''])
- mod.setup_server()
-
- cherrypy.server.unsubscribe()
- cherrypy.engine.start()
- from mod_python import apache
- return apache.OK
-
-
-def cpmodpysetup(req):
- global loaded
- if not loaded:
- loaded = True
- options = req.get_options()
-
- import cherrypy
- cherrypy.config.update({
- "log.error_file": os.path.join(curdir, "test.log"),
- "environment": "test_suite",
- "server.socket_host": options['socket_host'],
- })
- from mod_python import apache
- return apache.OK
-
diff --git a/cherrypy/test/modwsgi.py b/cherrypy/test/modwsgi.py
deleted file mode 100644
index 309a541c..00000000
--- a/cherrypy/test/modwsgi.py
+++ /dev/null
@@ -1,148 +0,0 @@
-"""Wrapper for mod_wsgi, for use as a CherryPy HTTP server.
-
-To autostart modwsgi, the "apache" executable or script must be
-on your system path, or you must override the global APACHE_PATH.
-On some platforms, "apache" may be called "apachectl" or "apache2ctl"--
-create a symlink to them if needed.
-
-
-KNOWN BUGS
-==========
-
-##1. Apache processes Range headers automatically; CherryPy's truncated
-## output is then truncated again by Apache. See test_core.testRanges.
-## This was worked around in http://www.cherrypy.org/changeset/1319.
-2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
- See test_core.testHTTPMethods.
-3. Max request header and body settings do not work with Apache.
-##4. Apache replaces status "reason phrases" automatically. For example,
-## CherryPy may set "304 Not modified" but Apache will write out
-## "304 Not Modified" (capital "M").
-##5. Apache does not allow custom error codes as per the spec.
-##6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
-## Request-URI too early.
-7. mod_wsgi will not read request bodies which use the "chunked"
- transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
- instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
- mod_python's requestobject.c).
-8. When responding with 204 No Content, mod_wsgi adds a Content-Length
- header for you.
-9. When an error is raised, mod_wsgi has no facility for printing a
- traceback as the response content (it's sent to the Apache log instead).
-10. Startup and shutdown of Apache when running mod_wsgi seems slow.
-"""
-
-import os
-curdir = os.path.abspath(os.path.dirname(__file__))
-import re
-import sys
-import time
-
-import cherrypy
-from cherrypy.test import helper, webtest
-
-
-def read_process(cmd, args=""):
- pipein, pipeout = os.popen4("%s %s" % (cmd, args))
- try:
- firstline = pipeout.readline()
- if (re.search(r"(not recognized|No such file|not found)", firstline,
- re.IGNORECASE)):
- raise IOError('%s must be on your system path.' % cmd)
- output = firstline + pipeout.read()
- finally:
- pipeout.close()
- return output
-
-
-if sys.platform == 'win32':
- APACHE_PATH = "httpd"
-else:
- APACHE_PATH = "apache"
-
-CONF_PATH = "test_mw.conf"
-
-conf_modwsgi = r"""
-# Apache2 server conf file for testing CherryPy with modpython_gateway.
-
-ServerName 127.0.0.1
-DocumentRoot "/"
-Listen %(port)s
-
-AllowEncodedSlashes On
-LoadModule rewrite_module modules/mod_rewrite.so
-RewriteEngine on
-RewriteMap escaping int:escape
-
-LoadModule log_config_module modules/mod_log_config.so
-LogFormat "%%h %%l %%u %%t \"%%r\" %%>s %%b \"%%{Referer}i\" \"%%{User-agent}i\"" combined
-CustomLog "%(curdir)s/apache.access.log" combined
-ErrorLog "%(curdir)s/apache.error.log"
-LogLevel debug
-
-LoadModule wsgi_module modules/mod_wsgi.so
-LoadModule env_module modules/mod_env.so
-
-WSGIScriptAlias / "%(curdir)s/modwsgi.py"
-SetEnv testmod %(testmod)s
-"""
-
-
-class ModWSGISupervisor(helper.Supervisor):
- """Server Controller for ModWSGI and CherryPy."""
-
- using_apache = True
- using_wsgi = True
- template=conf_modwsgi
-
- def __str__(self):
- return "ModWSGI Server on %s:%s" % (self.host, self.port)
-
- def start(self, modulename):
- mpconf = CONF_PATH
- if not os.path.isabs(mpconf):
- mpconf = os.path.join(curdir, mpconf)
-
- f = open(mpconf, 'wb')
- try:
- output = (self.template %
- {'port': self.port, 'testmod': modulename,
- 'curdir': curdir})
- f.write(output)
- finally:
- f.close()
-
- result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
- if result:
- print(result)
-
- # Make a request so mod_wsgi starts up our app.
- # If we don't, concurrent initial requests will 404.
- cherrypy._cpserver.wait_for_occupied_port("127.0.0.1", self.port)
- webtest.openURL('/ihopetheresnodefault', port=self.port)
- time.sleep(1)
-
- def stop(self):
- """Gracefully shutdown a server that is serving forever."""
- read_process(APACHE_PATH, "-k stop")
-
-
-loaded = False
-def application(environ, start_response):
- import cherrypy
- global loaded
- if not loaded:
- loaded = True
- modname = "cherrypy.test." + environ['testmod']
- mod = __import__(modname, globals(), locals(), [''])
- mod.setup_server()
-
- cherrypy.config.update({
- "log.error_file": os.path.join(curdir, "test.error.log"),
- "log.access_file": os.path.join(curdir, "test.access.log"),
- "environment": "test_suite",
- "engine.SIGHUP": None,
- "engine.SIGTERM": None,
- })
- return cherrypy.tree(environ, start_response)
-
diff --git a/cherrypy/test/native-server.ini b/cherrypy/test/native-server.ini
deleted file mode 100644
index b32d98dd..00000000
--- a/cherrypy/test/native-server.ini
+++ /dev/null
@@ -1,9 +0,0 @@
-[supervisor]
-scheme="http"
-protocol="HTTP/1.1"
-port= 8080
-host= "127.0.0.1"
-profile= False
-validate= False
-conquer= False
-server="wsgi"
diff --git a/cherrypy/test/sessiondemo.py b/cherrypy/test/sessiondemo.py
deleted file mode 100755
index 342e5b59..00000000
--- a/cherrypy/test/sessiondemo.py
+++ /dev/null
@@ -1,153 +0,0 @@
-#!/usr/bin/python
-"""A session demonstration app."""
-
-import calendar
-from datetime import datetime
-import sys
-import cherrypy
-from cherrypy.lib import sessions
-from cherrypy._cpcompat import copyitems
-
-
-page = """
-
-
-
-
-
-
-
-Session Demo
-Reload this page. The session ID should not change from one reload to the next
-Index | Expire | Regenerate
-
- | Session ID: | %(sessionid)s %(changemsg)s |
- | Request Cookie | %(reqcookie)s |
- | Response Cookie | %(respcookie)s |
- | Session Data | %(sessiondata)s |
- | Server Time | %(servertime)s (Unix time: %(serverunixtime)s) |
- | Browser Time | |
- | Cherrypy Version: | %(cpversion)s |
- | Python Version: | %(pyversion)s |
-
-
-"""
-
-class Root(object):
-
- def page(self):
- changemsg = []
- if cherrypy.session.id != cherrypy.session.originalid:
- if cherrypy.session.originalid is None:
- changemsg.append('Created new session because no session id was given.')
- if cherrypy.session.missing:
- changemsg.append('Created new session due to missing (expired or malicious) session.')
- if cherrypy.session.regenerated:
- changemsg.append('Application generated a new session.')
-
- try:
- expires = cherrypy.response.cookie['session_id']['expires']
- except KeyError:
- expires = ''
-
- return page % {
- 'sessionid': cherrypy.session.id,
- 'changemsg': '
'.join(changemsg),
- 'respcookie': cherrypy.response.cookie.output(),
- 'reqcookie': cherrypy.request.cookie.output(),
- 'sessiondata': copyitems(cherrypy.session),
- 'servertime': datetime.utcnow().strftime("%Y/%m/%d %H:%M") + " UTC",
- 'serverunixtime': calendar.timegm(datetime.utcnow().timetuple()),
- 'cpversion': cherrypy.__version__,
- 'pyversion': sys.version,
- 'expires': expires,
- }
-
- def index(self):
- # Must modify data or the session will not be saved.
- cherrypy.session['color'] = 'green'
- return self.page()
- index.exposed = True
-
- def expire(self):
- sessions.expire()
- return self.page()
- expire.exposed = True
-
- def regen(self):
- cherrypy.session.regenerate()
- # Must modify data or the session will not be saved.
- cherrypy.session['color'] = 'yellow'
- return self.page()
- regen.exposed = True
-
-if __name__ == '__main__':
- cherrypy.config.update({
- #'environment': 'production',
- 'log.screen': True,
- 'tools.sessions.on': True,
- })
- cherrypy.quickstart(Root())
-
diff --git a/cherrypy/test/static/dirback.jpg b/cherrypy/test/static/dirback.jpg
deleted file mode 100644
index 530e6d6a..00000000
Binary files a/cherrypy/test/static/dirback.jpg and /dev/null differ
diff --git a/cherrypy/test/static/index.html b/cherrypy/test/static/index.html
deleted file mode 100644
index b9f5f097..00000000
--- a/cherrypy/test/static/index.html
+++ /dev/null
@@ -1 +0,0 @@
-Hello, world
diff --git a/cherrypy/test/style.css b/cherrypy/test/style.css
deleted file mode 100644
index b266e93d..00000000
--- a/cherrypy/test/style.css
+++ /dev/null
@@ -1 +0,0 @@
-Dummy stylesheet
diff --git a/cherrypy/test/test.pem b/cherrypy/test/test.pem
deleted file mode 100644
index 47a47042..00000000
--- a/cherrypy/test/test.pem
+++ /dev/null
@@ -1,38 +0,0 @@
------BEGIN RSA PRIVATE KEY-----
-MIICXAIBAAKBgQDBKo554mzIMY+AByUNpaUOP9bJnQ7ZLQe9XgHwoLJR4VzpyZZZ
-R9L4WtImEew05FY3Izerfm3MN3+MC0tJ6yQU9sOiU3vBW6RrLIMlfKsnRwBRZ0Kn
-da+O6xldVSosu8Ev3z9VZ94iC/ZgKzrH7Mjj/U8/MQO7RBS/LAqee8bFNQIDAQAB
-AoGAWOCF0ZrWxn3XMucWq2LNwPKqlvVGwbIwX3cDmX22zmnM4Fy6arXbYh4XlyCj
-9+ofqRrxIFz5k/7tFriTmZ0xag5+Jdx+Kwg0/twiP7XCNKipFogwe1Hznw8OFAoT
-enKBdj2+/n2o0Bvo/tDB59m9L/538d46JGQUmJlzMyqYikECQQDyoq+8CtMNvE18
-8VgHcR/KtApxWAjj4HpaHYL637ATjThetUZkW92mgDgowyplthusxdNqhHWyv7E8
-tWNdYErZAkEAy85ShTR0M5aWmrE7o0r0SpWInAkNBH9aXQRRARFYsdBtNfRu6I0i
-0lvU9wiu3eF57FMEC86yViZ5UBnQfTu7vQJAVesj/Zt7pwaCDfdMa740OsxMUlyR
-MVhhGx4OLpYdPJ8qUecxGQKq13XZ7R1HGyNEY4bd2X80Smq08UFuATfC6QJAH8UB
-yBHtKz2GLIcELOg6PIYizW/7v3+6rlVF60yw7sb2vzpjL40QqIn4IKoR2DSVtOkb
-8FtAIX3N21aq0VrGYQJBAIPiaEc2AZ8Bq2GC4F3wOz/BxJ/izvnkiotR12QK4fh5
-yjZMhTjWCas5zwHR5PDjlD88AWGDMsZ1PicD4348xJQ=
------END RSA PRIVATE KEY-----
------BEGIN CERTIFICATE-----
-MIIDxTCCAy6gAwIBAgIJAI18BD7eQxlGMA0GCSqGSIb3DQEBBAUAMIGeMQswCQYD
-VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTESMBAGA1UEBxMJU2FuIERpZWdv
-MRkwFwYDVQQKExBDaGVycnlQeSBQcm9qZWN0MREwDwYDVQQLEwhkZXYtdGVzdDEW
-MBQGA1UEAxMNQ2hlcnJ5UHkgVGVhbTEgMB4GCSqGSIb3DQEJARYRcmVtaUBjaGVy
-cnlweS5vcmcwHhcNMDYwOTA5MTkyMDIwWhcNMzQwMTI0MTkyMDIwWjCBnjELMAkG
-A1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExEjAQBgNVBAcTCVNhbiBEaWVn
-bzEZMBcGA1UEChMQQ2hlcnJ5UHkgUHJvamVjdDERMA8GA1UECxMIZGV2LXRlc3Qx
-FjAUBgNVBAMTDUNoZXJyeVB5IFRlYW0xIDAeBgkqhkiG9w0BCQEWEXJlbWlAY2hl
-cnJ5cHkub3JnMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBKo554mzIMY+A
-ByUNpaUOP9bJnQ7ZLQe9XgHwoLJR4VzpyZZZR9L4WtImEew05FY3Izerfm3MN3+M
-C0tJ6yQU9sOiU3vBW6RrLIMlfKsnRwBRZ0Knda+O6xldVSosu8Ev3z9VZ94iC/Zg
-KzrH7Mjj/U8/MQO7RBS/LAqee8bFNQIDAQABo4IBBzCCAQMwHQYDVR0OBBYEFDIQ
-2feb71tVZCWpU0qJ/Tw+wdtoMIHTBgNVHSMEgcswgciAFDIQ2feb71tVZCWpU0qJ
-/Tw+wdtooYGkpIGhMIGeMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5p
-YTESMBAGA1UEBxMJU2FuIERpZWdvMRkwFwYDVQQKExBDaGVycnlQeSBQcm9qZWN0
-MREwDwYDVQQLEwhkZXYtdGVzdDEWMBQGA1UEAxMNQ2hlcnJ5UHkgVGVhbTEgMB4G
-CSqGSIb3DQEJARYRcmVtaUBjaGVycnlweS5vcmeCCQCNfAQ+3kMZRjAMBgNVHRME
-BTADAQH/MA0GCSqGSIb3DQEBBAUAA4GBAL7AAQz7IePV48ZTAFHKr88ntPALsL5S
-8vHCZPNMevNkLTj3DYUw2BcnENxMjm1kou2F2BkvheBPNZKIhc6z4hAml3ed1xa2
-D7w6e6OTcstdK/+KrPDDHeOP1dhMWNs2JE1bNlfF1LiXzYKSXpe88eCKjCXsCT/T
-NluCaWQys3MS
------END CERTIFICATE-----
diff --git a/cherrypy/test/test_auth_basic.py b/cherrypy/test/test_auth_basic.py
deleted file mode 100644
index 3a9781d8..00000000
--- a/cherrypy/test/test_auth_basic.py
+++ /dev/null
@@ -1,79 +0,0 @@
-# This file is part of CherryPy
-# -*- coding: utf-8 -*-
-# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
-
-import cherrypy
-from cherrypy._cpcompat import md5, ntob
-from cherrypy.lib import auth_basic
-from cherrypy.test import helper
-
-
-class BasicAuthTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self):
- return "This is public."
- index.exposed = True
-
- class BasicProtected:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- class BasicProtected2:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- userpassdict = {'xuser' : 'xpassword'}
- userhashdict = {'xuser' : md5(ntob('xpassword')).hexdigest()}
-
- def checkpasshash(realm, user, password):
- p = userhashdict.get(user)
- return p and p == md5(ntob(password)).hexdigest() or False
-
- conf = {'/basic': {'tools.auth_basic.on': True,
- 'tools.auth_basic.realm': 'wonderland',
- 'tools.auth_basic.checkpassword': auth_basic.checkpassword_dict(userpassdict)},
- '/basic2': {'tools.auth_basic.on': True,
- 'tools.auth_basic.realm': 'wonderland',
- 'tools.auth_basic.checkpassword': checkpasshash},
- }
-
- root = Root()
- root.basic = BasicProtected()
- root.basic2 = BasicProtected2()
- cherrypy.tree.mount(root, config=conf)
- setup_server = staticmethod(setup_server)
-
- def testPublic(self):
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertHeader('Content-Type', 'text/html;charset=utf-8')
- self.assertBody('This is public.')
-
- def testBasic(self):
- self.getPage("/basic/")
- self.assertStatus(401)
- self.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
-
- self.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
- self.assertStatus(401)
-
- self.getPage('/basic/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
- self.assertStatus('200 OK')
- self.assertBody("Hello xuser, you've been authorized.")
-
- def testBasic2(self):
- self.getPage("/basic2/")
- self.assertStatus(401)
- self.assertHeader('WWW-Authenticate', 'Basic realm="wonderland"')
-
- self.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3JX')])
- self.assertStatus(401)
-
- self.getPage('/basic2/', [('Authorization', 'Basic eHVzZXI6eHBhc3N3b3Jk')])
- self.assertStatus('200 OK')
- self.assertBody("Hello xuser, you've been authorized.")
-
diff --git a/cherrypy/test/test_auth_digest.py b/cherrypy/test/test_auth_digest.py
deleted file mode 100644
index 1960fa81..00000000
--- a/cherrypy/test/test_auth_digest.py
+++ /dev/null
@@ -1,115 +0,0 @@
-# This file is part of CherryPy
-# -*- coding: utf-8 -*-
-# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
-
-
-import cherrypy
-from cherrypy.lib import auth_digest
-
-from cherrypy.test import helper
-
-class DigestAuthTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self):
- return "This is public."
- index.exposed = True
-
- class DigestProtected:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- def fetch_users():
- return {'test': 'test'}
-
-
- get_ha1 = cherrypy.lib.auth_digest.get_ha1_dict_plain(fetch_users())
- conf = {'/digest': {'tools.auth_digest.on': True,
- 'tools.auth_digest.realm': 'localhost',
- 'tools.auth_digest.get_ha1': get_ha1,
- 'tools.auth_digest.key': 'a565c27146791cfb',
- 'tools.auth_digest.debug': 'True'}}
-
- root = Root()
- root.digest = DigestProtected()
- cherrypy.tree.mount(root, config=conf)
- setup_server = staticmethod(setup_server)
-
- def testPublic(self):
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertHeader('Content-Type', 'text/html;charset=utf-8')
- self.assertBody('This is public.')
-
- def testDigest(self):
- self.getPage("/digest/")
- self.assertStatus(401)
-
- value = None
- for k, v in self.headers:
- if k.lower() == "www-authenticate":
- if v.startswith("Digest"):
- value = v
- break
-
- if value is None:
- self._handlewebError("Digest authentification scheme was not found")
-
- value = value[7:]
- items = value.split(', ')
- tokens = {}
- for item in items:
- key, value = item.split('=')
- tokens[key.lower()] = value
-
- missing_msg = "%s is missing"
- bad_value_msg = "'%s' was expecting '%s' but found '%s'"
- nonce = None
- if 'realm' not in tokens:
- self._handlewebError(missing_msg % 'realm')
- elif tokens['realm'] != '"localhost"':
- self._handlewebError(bad_value_msg % ('realm', '"localhost"', tokens['realm']))
- if 'nonce' not in tokens:
- self._handlewebError(missing_msg % 'nonce')
- else:
- nonce = tokens['nonce'].strip('"')
- if 'algorithm' not in tokens:
- self._handlewebError(missing_msg % 'algorithm')
- elif tokens['algorithm'] != '"MD5"':
- self._handlewebError(bad_value_msg % ('algorithm', '"MD5"', tokens['algorithm']))
- if 'qop' not in tokens:
- self._handlewebError(missing_msg % 'qop')
- elif tokens['qop'] != '"auth"':
- self._handlewebError(bad_value_msg % ('qop', '"auth"', tokens['qop']))
-
- get_ha1 = auth_digest.get_ha1_dict_plain({'test' : 'test'})
-
- # Test user agent response with a wrong value for 'realm'
- base_auth = 'Digest username="test", realm="wrong realm", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001')
- auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET')
- # calculate the response digest
- ha1 = get_ha1(auth.realm, 'test')
- response = auth.request_digest(ha1)
- # send response with correct response digest, but wrong realm
- auth_header = base_auth % (nonce, response, '00000001')
- self.getPage('/digest/', [('Authorization', auth_header)])
- self.assertStatus(401)
-
- # Test that must pass
- base_auth = 'Digest username="test", realm="localhost", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth_header = base_auth % (nonce, '11111111111111111111111111111111', '00000001')
- auth = auth_digest.HttpDigestAuthorization(auth_header, 'GET')
- # calculate the response digest
- ha1 = get_ha1('localhost', 'test')
- response = auth.request_digest(ha1)
- # send response with correct response digest
- auth_header = base_auth % (nonce, response, '00000001')
- self.getPage('/digest/', [('Authorization', auth_header)])
- self.assertStatus('200 OK')
- self.assertBody("Hello test, you've been authorized.")
-
diff --git a/cherrypy/test/test_bus.py b/cherrypy/test/test_bus.py
deleted file mode 100644
index 51c10220..00000000
--- a/cherrypy/test/test_bus.py
+++ /dev/null
@@ -1,263 +0,0 @@
-import threading
-import time
-import unittest
-
-import cherrypy
-from cherrypy._cpcompat import get_daemon, set
-from cherrypy.process import wspbus
-
-
-msg = "Listener %d on channel %s: %s."
-
-
-class PublishSubscribeTests(unittest.TestCase):
-
- def get_listener(self, channel, index):
- def listener(arg=None):
- self.responses.append(msg % (index, channel, arg))
- return listener
-
- def test_builtin_channels(self):
- b = wspbus.Bus()
-
- self.responses, expected = [], []
-
- for channel in b.listeners:
- for index, priority in enumerate([100, 50, 0, 51]):
- b.subscribe(channel, self.get_listener(channel, index), priority)
-
- for channel in b.listeners:
- b.publish(channel)
- expected.extend([msg % (i, channel, None) for i in (2, 1, 3, 0)])
- b.publish(channel, arg=79347)
- expected.extend([msg % (i, channel, 79347) for i in (2, 1, 3, 0)])
-
- self.assertEqual(self.responses, expected)
-
- def test_custom_channels(self):
- b = wspbus.Bus()
-
- self.responses, expected = [], []
-
- custom_listeners = ('hugh', 'louis', 'dewey')
- for channel in custom_listeners:
- for index, priority in enumerate([None, 10, 60, 40]):
- b.subscribe(channel, self.get_listener(channel, index), priority)
-
- for channel in custom_listeners:
- b.publish(channel, 'ah so')
- expected.extend([msg % (i, channel, 'ah so') for i in (1, 3, 0, 2)])
- b.publish(channel)
- expected.extend([msg % (i, channel, None) for i in (1, 3, 0, 2)])
-
- self.assertEqual(self.responses, expected)
-
- def test_listener_errors(self):
- b = wspbus.Bus()
-
- self.responses, expected = [], []
- channels = [c for c in b.listeners if c != 'log']
-
- for channel in channels:
- b.subscribe(channel, self.get_listener(channel, 1))
- # This will break since the lambda takes no args.
- b.subscribe(channel, lambda: None, priority=20)
-
- for channel in channels:
- self.assertRaises(wspbus.ChannelFailures, b.publish, channel, 123)
- expected.append(msg % (1, channel, 123))
-
- self.assertEqual(self.responses, expected)
-
-
-class BusMethodTests(unittest.TestCase):
-
- def log(self, bus):
- self._log_entries = []
- def logit(msg, level):
- self._log_entries.append(msg)
- bus.subscribe('log', logit)
-
- def assertLog(self, entries):
- self.assertEqual(self._log_entries, entries)
-
- def get_listener(self, channel, index):
- def listener(arg=None):
- self.responses.append(msg % (index, channel, arg))
- return listener
-
- def test_start(self):
- b = wspbus.Bus()
- self.log(b)
-
- self.responses = []
- num = 3
- for index in range(num):
- b.subscribe('start', self.get_listener('start', index))
-
- b.start()
- try:
- # The start method MUST call all 'start' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'start', None) for i in range(num)]))
- # The start method MUST move the state to STARTED
- # (or EXITING, if errors occur)
- self.assertEqual(b.state, b.states.STARTED)
- # The start method MUST log its states.
- self.assertLog(['Bus STARTING', 'Bus STARTED'])
- finally:
- # Exit so the atexit handler doesn't complain.
- b.exit()
-
- def test_stop(self):
- b = wspbus.Bus()
- self.log(b)
-
- self.responses = []
- num = 3
- for index in range(num):
- b.subscribe('stop', self.get_listener('stop', index))
-
- b.stop()
-
- # The stop method MUST call all 'stop' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'stop', None) for i in range(num)]))
- # The stop method MUST move the state to STOPPED
- self.assertEqual(b.state, b.states.STOPPED)
- # The stop method MUST log its states.
- self.assertLog(['Bus STOPPING', 'Bus STOPPED'])
-
- def test_graceful(self):
- b = wspbus.Bus()
- self.log(b)
-
- self.responses = []
- num = 3
- for index in range(num):
- b.subscribe('graceful', self.get_listener('graceful', index))
-
- b.graceful()
-
- # The graceful method MUST call all 'graceful' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'graceful', None) for i in range(num)]))
- # The graceful method MUST log its states.
- self.assertLog(['Bus graceful'])
-
- def test_exit(self):
- b = wspbus.Bus()
- self.log(b)
-
- self.responses = []
- num = 3
- for index in range(num):
- b.subscribe('stop', self.get_listener('stop', index))
- b.subscribe('exit', self.get_listener('exit', index))
-
- b.exit()
-
- # The exit method MUST call all 'stop' listeners,
- # and then all 'exit' listeners.
- self.assertEqual(set(self.responses),
- set([msg % (i, 'stop', None) for i in range(num)] +
- [msg % (i, 'exit', None) for i in range(num)]))
- # The exit method MUST move the state to EXITING
- self.assertEqual(b.state, b.states.EXITING)
- # The exit method MUST log its states.
- self.assertLog(['Bus STOPPING', 'Bus STOPPED', 'Bus EXITING', 'Bus EXITED'])
-
- def test_wait(self):
- b = wspbus.Bus()
-
- def f(method):
- time.sleep(0.2)
- getattr(b, method)()
-
- for method, states in [('start', [b.states.STARTED]),
- ('stop', [b.states.STOPPED]),
- ('start', [b.states.STARTING, b.states.STARTED]),
- ('exit', [b.states.EXITING]),
- ]:
- threading.Thread(target=f, args=(method,)).start()
- b.wait(states)
-
- # The wait method MUST wait for the given state(s).
- if b.state not in states:
- self.fail("State %r not in %r" % (b.state, states))
-
- def test_block(self):
- b = wspbus.Bus()
- self.log(b)
-
- def f():
- time.sleep(0.2)
- b.exit()
- def g():
- time.sleep(0.4)
- threading.Thread(target=f).start()
- threading.Thread(target=g).start()
- threads = [t for t in threading.enumerate() if not get_daemon(t)]
- self.assertEqual(len(threads), 3)
-
- b.block()
-
- # The block method MUST wait for the EXITING state.
- self.assertEqual(b.state, b.states.EXITING)
- # The block method MUST wait for ALL non-main, non-daemon threads to finish.
- threads = [t for t in threading.enumerate() if not get_daemon(t)]
- self.assertEqual(len(threads), 1)
- # The last message will mention an indeterminable thread name; ignore it
- self.assertEqual(self._log_entries[:-1],
- ['Bus STOPPING', 'Bus STOPPED',
- 'Bus EXITING', 'Bus EXITED',
- 'Waiting for child threads to terminate...'])
-
- def test_start_with_callback(self):
- b = wspbus.Bus()
- self.log(b)
- try:
- events = []
- def f(*args, **kwargs):
- events.append(("f", args, kwargs))
- def g():
- events.append("g")
- b.subscribe("start", g)
- b.start_with_callback(f, (1, 3, 5), {"foo": "bar"})
- # Give wait() time to run f()
- time.sleep(0.2)
-
- # The callback method MUST wait for the STARTED state.
- self.assertEqual(b.state, b.states.STARTED)
- # The callback method MUST run after all start methods.
- self.assertEqual(events, ["g", ("f", (1, 3, 5), {"foo": "bar"})])
- finally:
- b.exit()
-
- def test_log(self):
- b = wspbus.Bus()
- self.log(b)
- self.assertLog([])
-
- # Try a normal message.
- expected = []
- for msg in ["O mah darlin'"] * 3 + ["Clementiiiiiiiine"]:
- b.log(msg)
- expected.append(msg)
- self.assertLog(expected)
-
- # Try an error message
- try:
- foo
- except NameError:
- b.log("You are lost and gone forever", traceback=True)
- lastmsg = self._log_entries[-1]
- if "Traceback" not in lastmsg or "NameError" not in lastmsg:
- self.fail("Last log message %r did not contain "
- "the expected traceback." % lastmsg)
- else:
- self.fail("NameError was not raised as expected.")
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/cherrypy/test/test_caching.py b/cherrypy/test/test_caching.py
deleted file mode 100644
index 720a933a..00000000
--- a/cherrypy/test/test_caching.py
+++ /dev/null
@@ -1,329 +0,0 @@
-import datetime
-import gzip
-from itertools import count
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-import sys
-import threading
-import time
-import urllib
-
-import cherrypy
-from cherrypy._cpcompat import next, ntob, quote, xrange
-from cherrypy.lib import httputil
-
-gif_bytes = ntob('GIF89a\x01\x00\x01\x00\x82\x00\x01\x99"\x1e\x00\x00\x00\x00\x00'
- '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
- '\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x02\x03\x02\x08\t\x00;')
-
-
-
-from cherrypy.test import helper
-
-class CacheTest(helper.CPWebCase):
-
- def setup_server():
-
- class Root:
-
- _cp_config = {'tools.caching.on': True}
-
- def __init__(self):
- self.counter = 0
- self.control_counter = 0
- self.longlock = threading.Lock()
-
- def index(self):
- self.counter += 1
- msg = "visit #%s" % self.counter
- return msg
- index.exposed = True
-
- def control(self):
- self.control_counter += 1
- return "visit #%s" % self.control_counter
- control.exposed = True
-
- def a_gif(self):
- cherrypy.response.headers['Last-Modified'] = httputil.HTTPDate()
- return gif_bytes
- a_gif.exposed = True
-
- def long_process(self, seconds='1'):
- try:
- self.longlock.acquire()
- time.sleep(float(seconds))
- finally:
- self.longlock.release()
- return 'success!'
- long_process.exposed = True
-
- def clear_cache(self, path):
- cherrypy._cache.store[cherrypy.request.base + path].clear()
- clear_cache.exposed = True
-
- class VaryHeaderCachingServer(object):
-
- _cp_config = {'tools.caching.on': True,
- 'tools.response_headers.on': True,
- 'tools.response_headers.headers': [('Vary', 'Our-Varying-Header')],
- }
-
- def __init__(self):
- self.counter = count(1)
-
- def index(self):
- return "visit #%s" % next(self.counter)
- index.exposed = True
-
- class UnCached(object):
- _cp_config = {'tools.expires.on': True,
- 'tools.expires.secs': 60,
- 'tools.staticdir.on': True,
- 'tools.staticdir.dir': 'static',
- 'tools.staticdir.root': curdir,
- }
-
- def force(self):
- cherrypy.response.headers['Etag'] = 'bibbitybobbityboo'
- self._cp_config['tools.expires.force'] = True
- self._cp_config['tools.expires.secs'] = 0
- return "being forceful"
- force.exposed = True
- force._cp_config = {'tools.expires.secs': 0}
-
- def dynamic(self):
- cherrypy.response.headers['Etag'] = 'bibbitybobbityboo'
- cherrypy.response.headers['Cache-Control'] = 'private'
- return "D-d-d-dynamic!"
- dynamic.exposed = True
-
- def cacheable(self):
- cherrypy.response.headers['Etag'] = 'bibbitybobbityboo'
- return "Hi, I'm cacheable."
- cacheable.exposed = True
-
- def specific(self):
- cherrypy.response.headers['Etag'] = 'need_this_to_make_me_cacheable'
- return "I am being specific"
- specific.exposed = True
- specific._cp_config = {'tools.expires.secs': 86400}
-
- class Foo(object):pass
-
- def wrongtype(self):
- cherrypy.response.headers['Etag'] = 'need_this_to_make_me_cacheable'
- return "Woops"
- wrongtype.exposed = True
- wrongtype._cp_config = {'tools.expires.secs': Foo()}
-
- cherrypy.tree.mount(Root())
- cherrypy.tree.mount(UnCached(), "/expires")
- cherrypy.tree.mount(VaryHeaderCachingServer(), "/varying_headers")
- cherrypy.config.update({'tools.gzip.on': True})
- setup_server = staticmethod(setup_server)
-
- def testCaching(self):
- elapsed = 0.0
- for trial in range(10):
- self.getPage("/")
- # The response should be the same every time,
- # except for the Age response header.
- self.assertBody('visit #1')
- if trial != 0:
- age = int(self.assertHeader("Age"))
- self.assert_(age >= elapsed)
- elapsed = age
-
- # POST, PUT, DELETE should not be cached.
- self.getPage("/", method="POST")
- self.assertBody('visit #2')
- # Because gzip is turned on, the Vary header should always Vary for content-encoding
- self.assertHeader('Vary', 'Accept-Encoding')
- # The previous request should have invalidated the cache,
- # so this request will recalc the response.
- self.getPage("/", method="GET")
- self.assertBody('visit #3')
- # ...but this request should get the cached copy.
- self.getPage("/", method="GET")
- self.assertBody('visit #3')
- self.getPage("/", method="DELETE")
- self.assertBody('visit #4')
-
- # The previous request should have invalidated the cache,
- # so this request will recalc the response.
- self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')])
- self.assertHeader('Content-Encoding', 'gzip')
- self.assertHeader('Vary')
- self.assertEqual(cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
-
- # Now check that a second request gets the gzip header and gzipped body
- # This also tests a bug in 3.0 to 3.0.2 whereby the cached, gzipped
- # response body was being gzipped a second time.
- self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')])
- self.assertHeader('Content-Encoding', 'gzip')
- self.assertEqual(cherrypy.lib.encoding.decompress(self.body), ntob("visit #5"))
-
- # Now check that a third request that doesn't accept gzip
- # skips the cache (because the 'Vary' header denies it).
- self.getPage("/", method="GET")
- self.assertNoHeader('Content-Encoding')
- self.assertBody('visit #6')
-
- def testVaryHeader(self):
- self.getPage("/varying_headers/")
- self.assertStatus("200 OK")
- self.assertHeaderItemValue('Vary', 'Our-Varying-Header')
- self.assertBody('visit #1')
-
- # Now check that different 'Vary'-fields don't evict each other.
- # This test creates 2 requests with different 'Our-Varying-Header'
- # and then tests if the first one still exists.
- self.getPage("/varying_headers/", headers=[('Our-Varying-Header', 'request 2')])
- self.assertStatus("200 OK")
- self.assertBody('visit #2')
-
- self.getPage("/varying_headers/", headers=[('Our-Varying-Header', 'request 2')])
- self.assertStatus("200 OK")
- self.assertBody('visit #2')
-
- self.getPage("/varying_headers/")
- self.assertStatus("200 OK")
- self.assertBody('visit #1')
-
- def testExpiresTool(self):
- # test setting an expires header
- self.getPage("/expires/specific")
- self.assertStatus("200 OK")
- self.assertHeader("Expires")
-
- # test exceptions for bad time values
- self.getPage("/expires/wrongtype")
- self.assertStatus(500)
- self.assertInBody("TypeError")
-
- # static content should not have "cache prevention" headers
- self.getPage("/expires/index.html")
- self.assertStatus("200 OK")
- self.assertNoHeader("Pragma")
- self.assertNoHeader("Cache-Control")
- self.assertHeader("Expires")
-
- # dynamic content that sets indicators should not have
- # "cache prevention" headers
- self.getPage("/expires/cacheable")
- self.assertStatus("200 OK")
- self.assertNoHeader("Pragma")
- self.assertNoHeader("Cache-Control")
- self.assertHeader("Expires")
-
- self.getPage('/expires/dynamic')
- self.assertBody("D-d-d-dynamic!")
- # the Cache-Control header should be untouched
- self.assertHeader("Cache-Control", "private")
- self.assertHeader("Expires")
-
- # configure the tool to ignore indicators and replace existing headers
- self.getPage("/expires/force")
- self.assertStatus("200 OK")
- # This also gives us a chance to test 0 expiry with no other headers
- self.assertHeader("Pragma", "no-cache")
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.assertHeader("Cache-Control", "no-cache, must-revalidate")
- self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
- # static content should now have "cache prevention" headers
- self.getPage("/expires/index.html")
- self.assertStatus("200 OK")
- self.assertHeader("Pragma", "no-cache")
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.assertHeader("Cache-Control", "no-cache, must-revalidate")
- self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
- # the cacheable handler should now have "cache prevention" headers
- self.getPage("/expires/cacheable")
- self.assertStatus("200 OK")
- self.assertHeader("Pragma", "no-cache")
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.assertHeader("Cache-Control", "no-cache, must-revalidate")
- self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
- self.getPage('/expires/dynamic')
- self.assertBody("D-d-d-dynamic!")
- # dynamic sets Cache-Control to private but it should be
- # overwritten here ...
- self.assertHeader("Pragma", "no-cache")
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.assertHeader("Cache-Control", "no-cache, must-revalidate")
- self.assertHeader("Expires", "Sun, 28 Jan 2007 00:00:00 GMT")
-
- def testLastModified(self):
- self.getPage("/a.gif")
- self.assertStatus(200)
- self.assertBody(gif_bytes)
- lm1 = self.assertHeader("Last-Modified")
-
- # this request should get the cached copy.
- self.getPage("/a.gif")
- self.assertStatus(200)
- self.assertBody(gif_bytes)
- self.assertHeader("Age")
- lm2 = self.assertHeader("Last-Modified")
- self.assertEqual(lm1, lm2)
-
- # this request should match the cached copy, but raise 304.
- self.getPage("/a.gif", [('If-Modified-Since', lm1)])
- self.assertStatus(304)
- self.assertNoHeader("Last-Modified")
- if not getattr(cherrypy.server, "using_apache", False):
- self.assertHeader("Age")
-
- def test_antistampede(self):
- SECONDS = 4
- # We MUST make an initial synchronous request in order to create the
- # AntiStampedeCache object, and populate its selecting_headers,
- # before the actual stampede.
- self.getPage("/long_process?seconds=%d" % SECONDS)
- self.assertBody('success!')
- self.getPage("/clear_cache?path=" +
- quote('/long_process?seconds=%d' % SECONDS, safe=''))
- self.assertStatus(200)
- sys.stdout.write("prepped... ")
- sys.stdout.flush()
-
- start = datetime.datetime.now()
- def run():
- self.getPage("/long_process?seconds=%d" % SECONDS)
- # The response should be the same every time
- self.assertBody('success!')
- ts = [threading.Thread(target=run) for i in xrange(100)]
- for t in ts:
- t.start()
- for t in ts:
- t.join()
- self.assertEqualDates(start, datetime.datetime.now(),
- # Allow a second for our thread/TCP overhead etc.
- seconds=SECONDS + 1.1)
-
- def test_cache_control(self):
- self.getPage("/control")
- self.assertBody('visit #1')
- self.getPage("/control")
- self.assertBody('visit #1')
-
- self.getPage("/control", headers=[('Cache-Control', 'no-cache')])
- self.assertBody('visit #2')
- self.getPage("/control")
- self.assertBody('visit #2')
-
- self.getPage("/control", headers=[('Pragma', 'no-cache')])
- self.assertBody('visit #3')
- self.getPage("/control")
- self.assertBody('visit #3')
-
- time.sleep(1)
- self.getPage("/control", headers=[('Cache-Control', 'max-age=0')])
- self.assertBody('visit #4')
- self.getPage("/control")
- self.assertBody('visit #4')
-
diff --git a/cherrypy/test/test_config.py b/cherrypy/test/test_config.py
deleted file mode 100644
index a0bd8ab9..00000000
--- a/cherrypy/test/test_config.py
+++ /dev/null
@@ -1,249 +0,0 @@
-"""Tests for the CherryPy configuration system."""
-
-import os, sys
-localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-
-from cherrypy._cpcompat import ntob, StringIO
-import unittest
-
-import cherrypy
-
-def setup_server():
-
- class Root:
-
- _cp_config = {'foo': 'this',
- 'bar': 'that'}
-
- def __init__(self):
- cherrypy.config.namespaces['db'] = self.db_namespace
-
- def db_namespace(self, k, v):
- if k == "scheme":
- self.db = v
-
- # @cherrypy.expose(alias=('global_', 'xyz'))
- def index(self, key):
- return cherrypy.request.config.get(key, "None")
- index = cherrypy.expose(index, alias=('global_', 'xyz'))
-
- def repr(self, key):
- return repr(cherrypy.request.config.get(key, None))
- repr.exposed = True
-
- def dbscheme(self):
- return self.db
- dbscheme.exposed = True
-
- def plain(self, x):
- return x
- plain.exposed = True
- plain._cp_config = {'request.body.attempt_charsets': ['utf-16']}
-
- favicon_ico = cherrypy.tools.staticfile.handler(
- filename=os.path.join(localDir, '../favicon.ico'))
-
- class Foo:
-
- _cp_config = {'foo': 'this2',
- 'baz': 'that2'}
-
- def index(self, key):
- return cherrypy.request.config.get(key, "None")
- index.exposed = True
- nex = index
-
- def silly(self):
- return 'Hello world'
- silly.exposed = True
- silly._cp_config = {'response.headers.X-silly': 'sillyval'}
-
- def bar(self, key):
- return repr(cherrypy.request.config.get(key, None))
- bar.exposed = True
- bar._cp_config = {'foo': 'this3', 'bax': 'this4'}
-
- class Another:
-
- def index(self, key):
- return str(cherrypy.request.config.get(key, "None"))
- index.exposed = True
-
-
- def raw_namespace(key, value):
- if key == 'input.map':
- handler = cherrypy.request.handler
- def wrapper():
- params = cherrypy.request.params
- for name, coercer in list(value.items()):
- try:
- params[name] = coercer(params[name])
- except KeyError:
- pass
- return handler()
- cherrypy.request.handler = wrapper
- elif key == 'output':
- handler = cherrypy.request.handler
- def wrapper():
- # 'value' is a type (like int or str).
- return value(handler())
- cherrypy.request.handler = wrapper
-
- class Raw:
-
- _cp_config = {'raw.output': repr}
-
- def incr(self, num):
- return num + 1
- incr.exposed = True
- incr._cp_config = {'raw.input.map': {'num': int}}
-
- ioconf = StringIO("""
-[/]
-neg: -1234
-filename: os.path.join(sys.prefix, "hello.py")
-thing1: cherrypy.lib.httputil.response_codes[404]
-thing2: __import__('cherrypy.tutorial', globals(), locals(), ['']).thing2
-complex: 3+2j
-ones: "11"
-twos: "22"
-stradd: %%(ones)s + %%(twos)s + "33"
-
-[/favicon.ico]
-tools.staticfile.filename = %r
-""" % os.path.join(localDir, 'static/dirback.jpg'))
-
- root = Root()
- root.foo = Foo()
- root.raw = Raw()
- app = cherrypy.tree.mount(root, config=ioconf)
- app.request_class.namespaces['raw'] = raw_namespace
-
- cherrypy.tree.mount(Another(), "/another")
- cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove',
- 'db.scheme': r"sqlite///memory",
- })
-
-
-# Client-side code #
-
-from cherrypy.test import helper
-
-class ConfigTests(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def testConfig(self):
- tests = [
- ('/', 'nex', 'None'),
- ('/', 'foo', 'this'),
- ('/', 'bar', 'that'),
- ('/xyz', 'foo', 'this'),
- ('/foo/', 'foo', 'this2'),
- ('/foo/', 'bar', 'that'),
- ('/foo/', 'bax', 'None'),
- ('/foo/bar', 'baz', "'that2'"),
- ('/foo/nex', 'baz', 'that2'),
- # If 'foo' == 'this', then the mount point '/another' leaks into '/'.
- ('/another/','foo', 'None'),
- ]
- for path, key, expected in tests:
- self.getPage(path + "?key=" + key)
- self.assertBody(expected)
-
- expectedconf = {
- # From CP defaults
- 'tools.log_headers.on': False,
- 'tools.log_tracebacks.on': True,
- 'request.show_tracebacks': True,
- 'log.screen': False,
- 'environment': 'test_suite',
- 'engine.autoreload_on': False,
- # From global config
- 'luxuryyacht': 'throatwobblermangrove',
- # From Root._cp_config
- 'bar': 'that',
- # From Foo._cp_config
- 'baz': 'that2',
- # From Foo.bar._cp_config
- 'foo': 'this3',
- 'bax': 'this4',
- }
- for key, expected in expectedconf.items():
- self.getPage("/foo/bar?key=" + key)
- self.assertBody(repr(expected))
-
- def testUnrepr(self):
- self.getPage("/repr?key=neg")
- self.assertBody("-1234")
-
- self.getPage("/repr?key=filename")
- self.assertBody(repr(os.path.join(sys.prefix, "hello.py")))
-
- self.getPage("/repr?key=thing1")
- self.assertBody(repr(cherrypy.lib.httputil.response_codes[404]))
-
- if not getattr(cherrypy.server, "using_apache", False):
- # The object ID's won't match up when using Apache, since the
- # server and client are running in different processes.
- self.getPage("/repr?key=thing2")
- from cherrypy.tutorial import thing2
- self.assertBody(repr(thing2))
-
- self.getPage("/repr?key=complex")
- self.assertBody("(3+2j)")
-
- self.getPage("/repr?key=stradd")
- self.assertBody(repr("112233"))
-
- def testRespNamespaces(self):
- self.getPage("/foo/silly")
- self.assertHeader('X-silly', 'sillyval')
- self.assertBody('Hello world')
-
- def testCustomNamespaces(self):
- self.getPage("/raw/incr?num=12")
- self.assertBody("13")
-
- self.getPage("/dbscheme")
- self.assertBody(r"sqlite///memory")
-
- def testHandlerToolConfigOverride(self):
- # Assert that config overrides tool constructor args. Above, we set
- # the favicon in the page handler to be '../favicon.ico',
- # but then overrode it in config to be './static/dirback.jpg'.
- self.getPage("/favicon.ico")
- self.assertBody(open(os.path.join(localDir, "static/dirback.jpg"),
- "rb").read())
-
- def test_request_body_namespace(self):
- self.getPage("/plain", method='POST', headers=[
- ('Content-Type', 'application/x-www-form-urlencoded'),
- ('Content-Length', '13')],
- body=ntob('\xff\xfex\x00=\xff\xfea\x00b\x00c\x00'))
- self.assertBody("abc")
-
-
-class VariableSubstitutionTests(unittest.TestCase):
- setup_server = staticmethod(setup_server)
-
- def test_config(self):
- from textwrap import dedent
-
- # variable substitution with [DEFAULT]
- conf = dedent("""
- [DEFAULT]
- dir = "/some/dir"
- my.dir = %(dir)s + "/sub"
-
- [my]
- my.dir = %(dir)s + "/my/dir"
- my.dir2 = %(my.dir)s + '/dir2'
-
- """)
-
- fp = StringIO(conf)
-
- cherrypy.config.update(fp)
- self.assertEqual(cherrypy.config["my"]["my.dir"], "/some/dir/my/dir")
- self.assertEqual(cherrypy.config["my"]["my.dir2"], "/some/dir/my/dir/dir2")
-
diff --git a/cherrypy/test/test_config_server.py b/cherrypy/test/test_config_server.py
deleted file mode 100644
index 0b9718da..00000000
--- a/cherrypy/test/test_config_server.py
+++ /dev/null
@@ -1,121 +0,0 @@
-"""Tests for the CherryPy configuration system."""
-
-import os, sys
-localDir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-import socket
-import time
-
-import cherrypy
-
-
-# Client-side code #
-
-from cherrypy.test import helper
-
-class ServerConfigTests(helper.CPWebCase):
-
- def setup_server():
-
- class Root:
- def index(self):
- return cherrypy.request.wsgi_environ['SERVER_PORT']
- index.exposed = True
-
- def upload(self, file):
- return "Size: %s" % len(file.file.read())
- upload.exposed = True
-
- def tinyupload(self):
- return cherrypy.request.body.read()
- tinyupload.exposed = True
- tinyupload._cp_config = {'request.body.maxbytes': 100}
-
- cherrypy.tree.mount(Root())
-
- cherrypy.config.update({
- 'server.socket_host': '0.0.0.0',
- 'server.socket_port': 9876,
- 'server.max_request_body_size': 200,
- 'server.max_request_header_size': 500,
- 'server.socket_timeout': 0.5,
-
- # Test explicit server.instance
- 'server.2.instance': 'cherrypy._cpwsgi_server.CPWSGIServer',
- 'server.2.socket_port': 9877,
-
- # Test non-numeric
- # Also test default server.instance = builtin server
- 'server.yetanother.socket_port': 9878,
- })
- setup_server = staticmethod(setup_server)
-
- PORT = 9876
-
- def testBasicConfig(self):
- self.getPage("/")
- self.assertBody(str(self.PORT))
-
- def testAdditionalServers(self):
- if self.scheme == 'https':
- return self.skip("not available under ssl")
- self.PORT = 9877
- self.getPage("/")
- self.assertBody(str(self.PORT))
- self.PORT = 9878
- self.getPage("/")
- self.assertBody(str(self.PORT))
-
- def testMaxRequestSizePerHandler(self):
- if getattr(cherrypy.server, "using_apache", False):
- return self.skip("skipped due to known Apache differences... ")
-
- self.getPage('/tinyupload', method="POST",
- headers=[('Content-Type', 'text/plain'),
- ('Content-Length', '100')],
- body="x" * 100)
- self.assertStatus(200)
- self.assertBody("x" * 100)
-
- self.getPage('/tinyupload', method="POST",
- headers=[('Content-Type', 'text/plain'),
- ('Content-Length', '101')],
- body="x" * 101)
- self.assertStatus(413)
-
- def testMaxRequestSize(self):
- if getattr(cherrypy.server, "using_apache", False):
- return self.skip("skipped due to known Apache differences... ")
-
- for size in (500, 5000, 50000):
- self.getPage("/", headers=[('From', "x" * 500)])
- self.assertStatus(413)
-
- # Test for http://www.cherrypy.org/ticket/421
- # (Incorrect border condition in readline of SizeCheckWrapper).
- # This hangs in rev 891 and earlier.
- lines256 = "x" * 248
- self.getPage("/",
- headers=[('Host', '%s:%s' % (self.HOST, self.PORT)),
- ('From', lines256)])
-
- # Test upload
- body = '\r\n'.join([
- '--x',
- 'Content-Disposition: form-data; name="file"; filename="hello.txt"',
- 'Content-Type: text/plain',
- '',
- '%s',
- '--x--'])
- partlen = 200 - len(body)
- b = body % ("x" * partlen)
- h = [("Content-type", "multipart/form-data; boundary=x"),
- ("Content-Length", "%s" % len(b))]
- self.getPage('/upload', h, "POST", b)
- self.assertBody('Size: %d' % partlen)
-
- b = body % ("x" * 200)
- h = [("Content-type", "multipart/form-data; boundary=x"),
- ("Content-Length", "%s" % len(b))]
- self.getPage('/upload', h, "POST", b)
- self.assertStatus(413)
-
diff --git a/cherrypy/test/test_conn.py b/cherrypy/test/test_conn.py
deleted file mode 100644
index 1346f593..00000000
--- a/cherrypy/test/test_conn.py
+++ /dev/null
@@ -1,734 +0,0 @@
-"""Tests for TCP connection handling, including proper and timely close."""
-
-import socket
-import sys
-import time
-timeout = 1
-
-
-import cherrypy
-from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, NotConnected, BadStatusLine
-from cherrypy._cpcompat import ntob, urlopen, unicodestr
-from cherrypy.test import webtest
-from cherrypy import _cperror
-
-
-pov = 'pPeErRsSiIsStTeEnNcCeE oOfF vViIsSiIoOnN'
-
-def setup_server():
-
- def raise500():
- raise cherrypy.HTTPError(500)
-
- class Root:
-
- def index(self):
- return pov
- index.exposed = True
- page1 = index
- page2 = index
- page3 = index
-
- def hello(self):
- return "Hello, world!"
- hello.exposed = True
-
- def timeout(self, t):
- return str(cherrypy.server.httpserver.timeout)
- timeout.exposed = True
-
- def stream(self, set_cl=False):
- if set_cl:
- cherrypy.response.headers['Content-Length'] = 10
-
- def content():
- for x in range(10):
- yield str(x)
-
- return content()
- stream.exposed = True
- stream._cp_config = {'response.stream': True}
-
- def error(self, code=500):
- raise cherrypy.HTTPError(code)
- error.exposed = True
-
- def upload(self):
- if not cherrypy.request.method == 'POST':
- raise AssertionError("'POST' != request.method %r" %
- cherrypy.request.method)
- return "thanks for '%s'" % cherrypy.request.body.read()
- upload.exposed = True
-
- def custom(self, response_code):
- cherrypy.response.status = response_code
- return "Code = %s" % response_code
- custom.exposed = True
-
- def err_before_read(self):
- return "ok"
- err_before_read.exposed = True
- err_before_read._cp_config = {'hooks.on_start_resource': raise500}
-
- def one_megabyte_of_a(self):
- return ["a" * 1024] * 1024
- one_megabyte_of_a.exposed = True
-
- def custom_cl(self, body, cl):
- cherrypy.response.headers['Content-Length'] = cl
- if not isinstance(body, list):
- body = [body]
- newbody = []
- for chunk in body:
- if isinstance(chunk, unicodestr):
- chunk = chunk.encode('ISO-8859-1')
- newbody.append(chunk)
- return newbody
- custom_cl.exposed = True
- # Turn off the encoding tool so it doens't collapse
- # our response body and reclaculate the Content-Length.
- custom_cl._cp_config = {'tools.encode.on': False}
-
- cherrypy.tree.mount(Root())
- cherrypy.config.update({
- 'server.max_request_body_size': 1001,
- 'server.socket_timeout': timeout,
- })
-
-
-from cherrypy.test import helper
-
-class ConnectionCloseTests(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_HTTP11(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- self.persistent = True
-
- # Make the first request and assert there's no "Connection: close".
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertNoHeader("Connection")
-
- # Make another request on the same connection.
- self.getPage("/page1")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertNoHeader("Connection")
-
- # Test client-side close.
- self.getPage("/page2", headers=[("Connection", "close")])
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertHeader("Connection", "close")
-
- # Make another request on the same connection, which should error.
- self.assertRaises(NotConnected, self.getPage, "/")
-
- def test_Streaming_no_len(self):
- self._streaming(set_cl=False)
-
- def test_Streaming_with_len(self):
- self._streaming(set_cl=True)
-
- def _streaming(self, set_cl):
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.PROTOCOL = "HTTP/1.1"
-
- self.persistent = True
-
- # Make the first request and assert there's no "Connection: close".
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertNoHeader("Connection")
-
- # Make another, streamed request on the same connection.
- if set_cl:
- # When a Content-Length is provided, the content should stream
- # without closing the connection.
- self.getPage("/stream?set_cl=Yes")
- self.assertHeader("Content-Length")
- self.assertNoHeader("Connection", "close")
- self.assertNoHeader("Transfer-Encoding")
-
- self.assertStatus('200 OK')
- self.assertBody('0123456789')
- else:
- # When no Content-Length response header is provided,
- # streamed output will either close the connection, or use
- # chunked encoding, to determine transfer-length.
- self.getPage("/stream")
- self.assertNoHeader("Content-Length")
- self.assertStatus('200 OK')
- self.assertBody('0123456789')
-
- chunked_response = False
- for k, v in self.headers:
- if k.lower() == "transfer-encoding":
- if str(v) == "chunked":
- chunked_response = True
-
- if chunked_response:
- self.assertNoHeader("Connection", "close")
- else:
- self.assertHeader("Connection", "close")
-
- # Make another request on the same connection, which should error.
- self.assertRaises(NotConnected, self.getPage, "/")
-
- # Try HEAD. See http://www.cherrypy.org/ticket/864.
- self.getPage("/stream", method='HEAD')
- self.assertStatus('200 OK')
- self.assertBody('')
- self.assertNoHeader("Transfer-Encoding")
- else:
- self.PROTOCOL = "HTTP/1.0"
-
- self.persistent = True
-
- # Make the first request and assert Keep-Alive.
- self.getPage("/", headers=[("Connection", "Keep-Alive")])
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertHeader("Connection", "Keep-Alive")
-
- # Make another, streamed request on the same connection.
- if set_cl:
- # When a Content-Length is provided, the content should
- # stream without closing the connection.
- self.getPage("/stream?set_cl=Yes",
- headers=[("Connection", "Keep-Alive")])
- self.assertHeader("Content-Length")
- self.assertHeader("Connection", "Keep-Alive")
- self.assertNoHeader("Transfer-Encoding")
- self.assertStatus('200 OK')
- self.assertBody('0123456789')
- else:
- # When a Content-Length is not provided,
- # the server should close the connection.
- self.getPage("/stream", headers=[("Connection", "Keep-Alive")])
- self.assertStatus('200 OK')
- self.assertBody('0123456789')
-
- self.assertNoHeader("Content-Length")
- self.assertNoHeader("Connection", "Keep-Alive")
- self.assertNoHeader("Transfer-Encoding")
-
- # Make another request on the same connection, which should error.
- self.assertRaises(NotConnected, self.getPage, "/")
-
- def test_HTTP10_KeepAlive(self):
- self.PROTOCOL = "HTTP/1.0"
- if self.scheme == "https":
- self.HTTP_CONN = HTTPSConnection
- else:
- self.HTTP_CONN = HTTPConnection
-
- # Test a normal HTTP/1.0 request.
- self.getPage("/page2")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- # Apache, for example, may emit a Connection header even for HTTP/1.0
-## self.assertNoHeader("Connection")
-
- # Test a keep-alive HTTP/1.0 request.
- self.persistent = True
-
- self.getPage("/page3", headers=[("Connection", "Keep-Alive")])
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertHeader("Connection", "Keep-Alive")
-
- # Remove the keep-alive header again.
- self.getPage("/page3")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- # Apache, for example, may emit a Connection header even for HTTP/1.0
-## self.assertNoHeader("Connection")
-
-
-class PipelineTests(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_HTTP11_Timeout(self):
- # If we timeout without sending any data,
- # the server will close the conn with a 408.
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- # Connect but send nothing.
- self.persistent = True
- conn = self.HTTP_CONN
- conn.auto_open = False
- conn.connect()
-
- # Wait for our socket timeout
- time.sleep(timeout * 2)
-
- # The request should have returned 408 already.
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 408)
- conn.close()
-
- # Connect but send half the headers only.
- self.persistent = True
- conn = self.HTTP_CONN
- conn.auto_open = False
- conn.connect()
- conn.send(ntob('GET /hello HTTP/1.1'))
- conn.send(("Host: %s" % self.HOST).encode('ascii'))
-
- # Wait for our socket timeout
- time.sleep(timeout * 2)
-
- # The conn should have already sent 408.
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 408)
- conn.close()
-
- def test_HTTP11_Timeout_after_request(self):
- # If we timeout after at least one request has succeeded,
- # the server will close the conn without 408.
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- # Make an initial request
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("GET", "/timeout?t=%s" % timeout, skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 200)
- self.body = response.read()
- self.assertBody(str(timeout))
-
- # Make a second request on the same socket
- conn._output(ntob('GET /hello HTTP/1.1'))
- conn._output(ntob("Host: %s" % self.HOST, 'ascii'))
- conn._send_output()
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 200)
- self.body = response.read()
- self.assertBody("Hello, world!")
-
- # Wait for our socket timeout
- time.sleep(timeout * 2)
-
- # Make another request on the same socket, which should error
- conn._output(ntob('GET /hello HTTP/1.1'))
- conn._output(ntob("Host: %s" % self.HOST, 'ascii'))
- conn._send_output()
- response = conn.response_class(conn.sock, method="GET")
- try:
- response.begin()
- except:
- if not isinstance(sys.exc_info()[1],
- (socket.error, BadStatusLine)):
- self.fail("Writing to timed out socket didn't fail"
- " as it should have: %s" % sys.exc_info()[1])
- else:
- if response.status != 408:
- self.fail("Writing to timed out socket didn't fail"
- " as it should have: %s" %
- response.read())
-
- conn.close()
-
- # Make another request on a new socket, which should work
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("GET", "/", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 200)
- self.body = response.read()
- self.assertBody(pov)
-
-
- # Make another request on the same socket,
- # but timeout on the headers
- conn.send(ntob('GET /hello HTTP/1.1'))
- # Wait for our socket timeout
- time.sleep(timeout * 2)
- response = conn.response_class(conn.sock, method="GET")
- try:
- response.begin()
- except:
- if not isinstance(sys.exc_info()[1],
- (socket.error, BadStatusLine)):
- self.fail("Writing to timed out socket didn't fail"
- " as it should have: %s" % sys.exc_info()[1])
- else:
- self.fail("Writing to timed out socket didn't fail"
- " as it should have: %s" %
- response.read())
-
- conn.close()
-
- # Retry the request on a new connection, which should work
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("GET", "/", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.assertEqual(response.status, 200)
- self.body = response.read()
- self.assertBody(pov)
- conn.close()
-
- def test_HTTP11_pipelining(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- # Test pipelining. httplib doesn't support this directly.
- self.persistent = True
- conn = self.HTTP_CONN
-
- # Put request 1
- conn.putrequest("GET", "/hello", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
-
- for trial in range(5):
- # Put next request
- conn._output(ntob('GET /hello HTTP/1.1'))
- conn._output(ntob("Host: %s" % self.HOST, 'ascii'))
- conn._send_output()
-
- # Retrieve previous response
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- body = response.read(13)
- self.assertEqual(response.status, 200)
- self.assertEqual(body, ntob("Hello, world!"))
-
- # Retrieve final response
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- body = response.read()
- self.assertEqual(response.status, 200)
- self.assertEqual(body, ntob("Hello, world!"))
-
- conn.close()
-
- def test_100_Continue(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- self.persistent = True
- conn = self.HTTP_CONN
-
- # Try a page without an Expect request header first.
- # Note that httplib's response.begin automatically ignores
- # 100 Continue responses, so we must manually check for it.
- conn.putrequest("POST", "/upload", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Content-Type", "text/plain")
- conn.putheader("Content-Length", "4")
- conn.endheaders()
- conn.send(ntob("d'oh"))
- response = conn.response_class(conn.sock, method="POST")
- version, status, reason = response._read_status()
- self.assertNotEqual(status, 100)
- conn.close()
-
- # Now try a page with an Expect header...
- conn.connect()
- conn.putrequest("POST", "/upload", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Content-Type", "text/plain")
- conn.putheader("Content-Length", "17")
- conn.putheader("Expect", "100-continue")
- conn.endheaders()
- response = conn.response_class(conn.sock, method="POST")
-
- # ...assert and then skip the 100 response
- version, status, reason = response._read_status()
- self.assertEqual(status, 100)
- while True:
- line = response.fp.readline().strip()
- if line:
- self.fail("100 Continue should not output any headers. Got %r" % line)
- else:
- break
-
- # ...send the body
- body = ntob("I am a small file")
- conn.send(body)
-
- # ...get the final response
- response.begin()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(200)
- self.assertBody("thanks for '%s'" % body)
- conn.close()
-
-
-class ConnectionTests(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_readall_or_close(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- if self.scheme == "https":
- self.HTTP_CONN = HTTPSConnection
- else:
- self.HTTP_CONN = HTTPConnection
-
- # Test a max of 0 (the default) and then reset to what it was above.
- old_max = cherrypy.server.max_request_body_size
- for new_max in (0, old_max):
- cherrypy.server.max_request_body_size = new_max
-
- self.persistent = True
- conn = self.HTTP_CONN
-
- # Get a POST page with an error
- conn.putrequest("POST", "/err_before_read", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Content-Type", "text/plain")
- conn.putheader("Content-Length", "1000")
- conn.putheader("Expect", "100-continue")
- conn.endheaders()
- response = conn.response_class(conn.sock, method="POST")
-
- # ...assert and then skip the 100 response
- version, status, reason = response._read_status()
- self.assertEqual(status, 100)
- while True:
- skip = response.fp.readline().strip()
- if not skip:
- break
-
- # ...send the body
- conn.send(ntob("x" * 1000))
-
- # ...get the final response
- response.begin()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(500)
-
- # Now try a working page with an Expect header...
- conn._output(ntob('POST /upload HTTP/1.1'))
- conn._output(ntob("Host: %s" % self.HOST, 'ascii'))
- conn._output(ntob("Content-Type: text/plain"))
- conn._output(ntob("Content-Length: 17"))
- conn._output(ntob("Expect: 100-continue"))
- conn._send_output()
- response = conn.response_class(conn.sock, method="POST")
-
- # ...assert and then skip the 100 response
- version, status, reason = response._read_status()
- self.assertEqual(status, 100)
- while True:
- skip = response.fp.readline().strip()
- if not skip:
- break
-
- # ...send the body
- body = ntob("I am a small file")
- conn.send(body)
-
- # ...get the final response
- response.begin()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(200)
- self.assertBody("thanks for '%s'" % body)
- conn.close()
-
- def test_No_Message_Body(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- # Set our HTTP_CONN to an instance so it persists between requests.
- self.persistent = True
-
- # Make the first request and assert there's no "Connection: close".
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertBody(pov)
- self.assertNoHeader("Connection")
-
- # Make a 204 request on the same connection.
- self.getPage("/custom/204")
- self.assertStatus(204)
- self.assertNoHeader("Content-Length")
- self.assertBody("")
- self.assertNoHeader("Connection")
-
- # Make a 304 request on the same connection.
- self.getPage("/custom/304")
- self.assertStatus(304)
- self.assertNoHeader("Content-Length")
- self.assertBody("")
- self.assertNoHeader("Connection")
-
- def test_Chunked_Encoding(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
- return self.skip()
-
- if (hasattr(self, 'harness') and
- "modpython" in self.harness.__class__.__name__.lower()):
- # mod_python forbids chunked encoding
- return self.skip()
-
- self.PROTOCOL = "HTTP/1.1"
-
- # Set our HTTP_CONN to an instance so it persists between requests.
- self.persistent = True
- conn = self.HTTP_CONN
-
- # Try a normal chunked request (with extensions)
- body = ntob("8;key=value\r\nxx\r\nxxxx\r\n5\r\nyyyyy\r\n0\r\n"
- "Content-Type: application/json\r\n"
- "\r\n")
- conn.putrequest("POST", "/upload", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Transfer-Encoding", "chunked")
- conn.putheader("Trailer", "Content-Type")
- # Note that this is somewhat malformed:
- # we shouldn't be sending Content-Length.
- # RFC 2616 says the server should ignore it.
- conn.putheader("Content-Length", "3")
- conn.endheaders()
- conn.send(body)
- response = conn.getresponse()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus('200 OK')
- self.assertBody("thanks for '%s'" % ntob('xx\r\nxxxxyyyyy'))
-
- # Try a chunked request that exceeds server.max_request_body_size.
- # Note that the delimiters and trailer are included.
- body = ntob("3e3\r\n" + ("x" * 995) + "\r\n0\r\n\r\n")
- conn.putrequest("POST", "/upload", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Transfer-Encoding", "chunked")
- conn.putheader("Content-Type", "text/plain")
- # Chunked requests don't need a content-length
-## conn.putheader("Content-Length", len(body))
- conn.endheaders()
- conn.send(body)
- response = conn.getresponse()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(413)
- conn.close()
-
- def test_Content_Length_in(self):
- # Try a non-chunked request where Content-Length exceeds
- # server.max_request_body_size. Assert error before body send.
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("POST", "/upload", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader("Content-Type", "text/plain")
- conn.putheader("Content-Length", "9999")
- conn.endheaders()
- response = conn.getresponse()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(413)
- self.assertBody("The entity sent with the request exceeds "
- "the maximum allowed bytes.")
- conn.close()
-
- def test_Content_Length_out_preheaders(self):
- # Try a non-chunked response where Content-Length is less than
- # the actual bytes in the response body.
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("GET", "/custom_cl?body=I+have+too+many+bytes&cl=5",
- skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
- response = conn.getresponse()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(500)
- self.assertBody(
- "The requested resource returned more bytes than the "
- "declared Content-Length.")
- conn.close()
-
- def test_Content_Length_out_postheaders(self):
- # Try a non-chunked response where Content-Length is less than
- # the actual bytes in the response body.
- self.persistent = True
- conn = self.HTTP_CONN
- conn.putrequest("GET", "/custom_cl?body=I+too&body=+have+too+many&cl=5",
- skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.endheaders()
- response = conn.getresponse()
- self.status, self.headers, self.body = webtest.shb(response)
- self.assertStatus(200)
- self.assertBody("I too")
- conn.close()
-
- def test_598(self):
- remote_data_conn = urlopen('%s://%s:%s/one_megabyte_of_a/' %
- (self.scheme, self.HOST, self.PORT,))
- buf = remote_data_conn.read(512)
- time.sleep(timeout * 0.6)
- remaining = (1024 * 1024) - 512
- while remaining:
- data = remote_data_conn.read(remaining)
- if not data:
- break
- else:
- buf += data
- remaining -= len(data)
-
- self.assertEqual(len(buf), 1024 * 1024)
- self.assertEqual(buf, ntob("a" * 1024 * 1024))
- self.assertEqual(remaining, 0)
- remote_data_conn.close()
-
-
-class BadRequestTests(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_No_CRLF(self):
- self.persistent = True
-
- conn = self.HTTP_CONN
- conn.send(ntob('GET /hello HTTP/1.1\n\n'))
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.body = response.read()
- self.assertBody("HTTP requires CRLF terminators")
- conn.close()
-
- conn.connect()
- conn.send(ntob('GET /hello HTTP/1.1\r\n\n'))
- response = conn.response_class(conn.sock, method="GET")
- response.begin()
- self.body = response.read()
- self.assertBody("HTTP requires CRLF terminators")
- conn.close()
-
diff --git a/cherrypy/test/test_core.py b/cherrypy/test/test_core.py
deleted file mode 100644
index 09544e34..00000000
--- a/cherrypy/test/test_core.py
+++ /dev/null
@@ -1,617 +0,0 @@
-"""Basic tests for the CherryPy core: request handling."""
-
-import os
-localDir = os.path.dirname(__file__)
-import sys
-import types
-
-import cherrypy
-from cherrypy._cpcompat import IncompleteRead, itervalues, ntob
-from cherrypy import _cptools, tools
-from cherrypy.lib import httputil, static
-
-
-favicon_path = os.path.join(os.getcwd(), localDir, "../favicon.ico")
-
-# Client-side code #
-
-from cherrypy.test import helper
-
-class CoreRequestHandlingTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
-
- def index(self):
- return "hello"
- index.exposed = True
-
- favicon_ico = tools.staticfile.handler(filename=favicon_path)
-
- def defct(self, newct):
- newct = "text/%s" % newct
- cherrypy.config.update({'tools.response_headers.on': True,
- 'tools.response_headers.headers':
- [('Content-Type', newct)]})
- defct.exposed = True
-
- def baseurl(self, path_info, relative=None):
- return cherrypy.url(path_info, relative=bool(relative))
- baseurl.exposed = True
-
- root = Root()
-
- if sys.version_info >= (2, 5):
- from cherrypy.test._test_decorators import ExposeExamples
- root.expose_dec = ExposeExamples()
-
-
- class TestType(type):
- """Metaclass which automatically exposes all functions in each subclass,
- and adds an instance of the subclass as an attribute of root.
- """
- def __init__(cls, name, bases, dct):
- type.__init__(cls, name, bases, dct)
- for value in itervalues(dct):
- if isinstance(value, types.FunctionType):
- value.exposed = True
- setattr(root, name.lower(), cls())
- class Test(object):
- __metaclass__ = TestType
-
-
- class URL(Test):
-
- _cp_config = {'tools.trailing_slash.on': False}
-
- def index(self, path_info, relative=None):
- if relative != 'server':
- relative = bool(relative)
- return cherrypy.url(path_info, relative=relative)
-
- def leaf(self, path_info, relative=None):
- if relative != 'server':
- relative = bool(relative)
- return cherrypy.url(path_info, relative=relative)
-
-
- class Status(Test):
-
- def index(self):
- return "normal"
-
- def blank(self):
- cherrypy.response.status = ""
-
- # According to RFC 2616, new status codes are OK as long as they
- # are between 100 and 599.
-
- # Here is an illegal code...
- def illegal(self):
- cherrypy.response.status = 781
- return "oops"
-
- # ...and here is an unknown but legal code.
- def unknown(self):
- cherrypy.response.status = "431 My custom error"
- return "funky"
-
- # Non-numeric code
- def bad(self):
- cherrypy.response.status = "error"
- return "bad news"
-
-
- class Redirect(Test):
-
- class Error:
- _cp_config = {"tools.err_redirect.on": True,
- "tools.err_redirect.url": "/errpage",
- "tools.err_redirect.internal": False,
- }
-
- def index(self):
- raise NameError("redirect_test")
- index.exposed = True
- error = Error()
-
- def index(self):
- return "child"
-
- def custom(self, url, code):
- raise cherrypy.HTTPRedirect(url, code)
-
- def by_code(self, code):
- raise cherrypy.HTTPRedirect("somewhere%20else", code)
- by_code._cp_config = {'tools.trailing_slash.extra': True}
-
- def nomodify(self):
- raise cherrypy.HTTPRedirect("", 304)
-
- def proxy(self):
- raise cherrypy.HTTPRedirect("proxy", 305)
-
- def stringify(self):
- return str(cherrypy.HTTPRedirect("/"))
-
- def fragment(self, frag):
- raise cherrypy.HTTPRedirect("/some/url#%s" % frag)
-
- def login_redir():
- if not getattr(cherrypy.request, "login", None):
- raise cherrypy.InternalRedirect("/internalredirect/login")
- tools.login_redir = _cptools.Tool('before_handler', login_redir)
-
- def redir_custom():
- raise cherrypy.InternalRedirect("/internalredirect/custom_err")
-
- class InternalRedirect(Test):
-
- def index(self):
- raise cherrypy.InternalRedirect("/")
-
- def choke(self):
- return 3 / 0
- choke.exposed = True
- choke._cp_config = {'hooks.before_error_response': redir_custom}
-
- def relative(self, a, b):
- raise cherrypy.InternalRedirect("cousin?t=6")
-
- def cousin(self, t):
- assert cherrypy.request.prev.closed
- return cherrypy.request.prev.query_string
-
- def petshop(self, user_id):
- if user_id == "parrot":
- # Trade it for a slug when redirecting
- raise cherrypy.InternalRedirect('/image/getImagesByUser?user_id=slug')
- elif user_id == "terrier":
- # Trade it for a fish when redirecting
- raise cherrypy.InternalRedirect('/image/getImagesByUser?user_id=fish')
- else:
- # This should pass the user_id through to getImagesByUser
- raise cherrypy.InternalRedirect(
- '/image/getImagesByUser?user_id=%s' % str(user_id))
-
- # We support Python 2.3, but the @-deco syntax would look like this:
- # @tools.login_redir()
- def secure(self):
- return "Welcome!"
- secure = tools.login_redir()(secure)
- # Since calling the tool returns the same function you pass in,
- # you could skip binding the return value, and just write:
- # tools.login_redir()(secure)
-
- def login(self):
- return "Please log in"
-
- def custom_err(self):
- return "Something went horribly wrong."
-
- def early_ir(self, arg):
- return "whatever"
- early_ir._cp_config = {'hooks.before_request_body': redir_custom}
-
-
- class Image(Test):
-
- def getImagesByUser(self, user_id):
- return "0 images for %s" % user_id
-
-
- class Flatten(Test):
-
- def as_string(self):
- return "content"
-
- def as_list(self):
- return ["con", "tent"]
-
- def as_yield(self):
- yield ntob("content")
-
- def as_dblyield(self):
- yield self.as_yield()
- as_dblyield._cp_config = {'tools.flatten.on': True}
-
- def as_refyield(self):
- for chunk in self.as_yield():
- yield chunk
-
-
- class Ranges(Test):
-
- def get_ranges(self, bytes):
- return repr(httputil.get_ranges('bytes=%s' % bytes, 8))
-
- def slice_file(self):
- path = os.path.join(os.getcwd(), os.path.dirname(__file__))
- return static.serve_file(os.path.join(path, "static/index.html"))
-
-
- class Cookies(Test):
-
- def single(self, name):
- cookie = cherrypy.request.cookie[name]
- # Python2's SimpleCookie.__setitem__ won't take unicode keys.
- cherrypy.response.cookie[str(name)] = cookie.value
-
- def multiple(self, names):
- for name in names:
- cookie = cherrypy.request.cookie[name]
- # Python2's SimpleCookie.__setitem__ won't take unicode keys.
- cherrypy.response.cookie[str(name)] = cookie.value
-
-
- cherrypy.tree.mount(root)
- setup_server = staticmethod(setup_server)
-
-
- def testStatus(self):
- self.getPage("/status/")
- self.assertBody('normal')
- self.assertStatus(200)
-
- self.getPage("/status/blank")
- self.assertBody('')
- self.assertStatus(200)
-
- self.getPage("/status/illegal")
- self.assertStatus(500)
- msg = "Illegal response status from server (781 is out of range)."
- self.assertErrorPage(500, msg)
-
- if not getattr(cherrypy.server, 'using_apache', False):
- self.getPage("/status/unknown")
- self.assertBody('funky')
- self.assertStatus(431)
-
- self.getPage("/status/bad")
- self.assertStatus(500)
- msg = "Illegal response status from server ('error' is non-numeric)."
- self.assertErrorPage(500, msg)
-
- def testSlashes(self):
- # Test that requests for index methods without a trailing slash
- # get redirected to the same URI path with a trailing slash.
- # Make sure GET params are preserved.
- self.getPage("/redirect?id=3")
- self.assertStatus(301)
- self.assertInBody(""
- "%s/redirect/?id=3" % (self.base(), self.base()))
-
- if self.prefix():
- # Corner case: the "trailing slash" redirect could be tricky if
- # we're using a virtual root and the URI is "/vroot" (no slash).
- self.getPage("")
- self.assertStatus(301)
- self.assertInBody("%s/" %
- (self.base(), self.base()))
-
- # Test that requests for NON-index methods WITH a trailing slash
- # get redirected to the same URI path WITHOUT a trailing slash.
- # Make sure GET params are preserved.
- self.getPage("/redirect/by_code/?code=307")
- self.assertStatus(301)
- self.assertInBody(""
- "%s/redirect/by_code?code=307"
- % (self.base(), self.base()))
-
- # If the trailing_slash tool is off, CP should just continue
- # as if the slashes were correct. But it needs some help
- # inside cherrypy.url to form correct output.
- self.getPage('/url?path_info=page1')
- self.assertBody('%s/url/page1' % self.base())
- self.getPage('/url/leaf/?path_info=page1')
- self.assertBody('%s/url/page1' % self.base())
-
- def testRedirect(self):
- self.getPage("/redirect/")
- self.assertBody('child')
- self.assertStatus(200)
-
- self.getPage("/redirect/by_code?code=300")
- self.assertMatchesBody(r"\1somewhere%20else")
- self.assertStatus(300)
-
- self.getPage("/redirect/by_code?code=301")
- self.assertMatchesBody(r"\1somewhere%20else")
- self.assertStatus(301)
-
- self.getPage("/redirect/by_code?code=302")
- self.assertMatchesBody(r"\1somewhere%20else")
- self.assertStatus(302)
-
- self.getPage("/redirect/by_code?code=303")
- self.assertMatchesBody(r"\1somewhere%20else")
- self.assertStatus(303)
-
- self.getPage("/redirect/by_code?code=307")
- self.assertMatchesBody(r"\1somewhere%20else")
- self.assertStatus(307)
-
- self.getPage("/redirect/nomodify")
- self.assertBody('')
- self.assertStatus(304)
-
- self.getPage("/redirect/proxy")
- self.assertBody('')
- self.assertStatus(305)
-
- # HTTPRedirect on error
- self.getPage("/redirect/error/")
- self.assertStatus(('302 Found', '303 See Other'))
- self.assertInBody('/errpage')
-
- # Make sure str(HTTPRedirect()) works.
- self.getPage("/redirect/stringify", protocol="HTTP/1.0")
- self.assertStatus(200)
- self.assertBody("(['%s/'], 302)" % self.base())
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.getPage("/redirect/stringify", protocol="HTTP/1.1")
- self.assertStatus(200)
- self.assertBody("(['%s/'], 303)" % self.base())
-
- # check that #fragments are handled properly
- # http://skrb.org/ietf/http_errata.html#location-fragments
- frag = "foo"
- self.getPage("/redirect/fragment/%s" % frag)
- self.assertMatchesBody(r"\1\/some\/url\#%s" % (frag, frag))
- loc = self.assertHeader('Location')
- assert loc.endswith("#%s" % frag)
- self.assertStatus(('302 Found', '303 See Other'))
-
- # check injection protection
- # See http://www.cherrypy.org/ticket/1003
- self.getPage("/redirect/custom?code=303&url=/foobar/%0d%0aSet-Cookie:%20somecookie=someval")
- self.assertStatus(303)
- loc = self.assertHeader('Location')
- assert 'Set-Cookie' in loc
- self.assertNoHeader('Set-Cookie')
-
- def test_InternalRedirect(self):
- # InternalRedirect
- self.getPage("/internalredirect/")
- self.assertBody('hello')
- self.assertStatus(200)
-
- # Test passthrough
- self.getPage("/internalredirect/petshop?user_id=Sir-not-appearing-in-this-film")
- self.assertBody('0 images for Sir-not-appearing-in-this-film')
- self.assertStatus(200)
-
- # Test args
- self.getPage("/internalredirect/petshop?user_id=parrot")
- self.assertBody('0 images for slug')
- self.assertStatus(200)
-
- # Test POST
- self.getPage("/internalredirect/petshop", method="POST",
- body="user_id=terrier")
- self.assertBody('0 images for fish')
- self.assertStatus(200)
-
- # Test ir before body read
- self.getPage("/internalredirect/early_ir", method="POST",
- body="arg=aha!")
- self.assertBody("Something went horribly wrong.")
- self.assertStatus(200)
-
- self.getPage("/internalredirect/secure")
- self.assertBody('Please log in')
- self.assertStatus(200)
-
- # Relative path in InternalRedirect.
- # Also tests request.prev.
- self.getPage("/internalredirect/relative?a=3&b=5")
- self.assertBody("a=3&b=5")
- self.assertStatus(200)
-
- # InternalRedirect on error
- self.getPage("/internalredirect/choke")
- self.assertStatus(200)
- self.assertBody("Something went horribly wrong.")
-
- def testFlatten(self):
- for url in ["/flatten/as_string", "/flatten/as_list",
- "/flatten/as_yield", "/flatten/as_dblyield",
- "/flatten/as_refyield"]:
- self.getPage(url)
- self.assertBody('content')
-
- def testRanges(self):
- self.getPage("/ranges/get_ranges?bytes=3-6")
- self.assertBody("[(3, 7)]")
-
- # Test multiple ranges and a suffix-byte-range-spec, for good measure.
- self.getPage("/ranges/get_ranges?bytes=2-4,-1")
- self.assertBody("[(2, 5), (7, 8)]")
-
- # Get a partial file.
- if cherrypy.server.protocol_version == "HTTP/1.1":
- self.getPage("/ranges/slice_file", [('Range', 'bytes=2-5')])
- self.assertStatus(206)
- self.assertHeader("Content-Type", "text/html;charset=utf-8")
- self.assertHeader("Content-Range", "bytes 2-5/14")
- self.assertBody("llo,")
-
- # What happens with overlapping ranges (and out of order, too)?
- self.getPage("/ranges/slice_file", [('Range', 'bytes=4-6,2-5')])
- self.assertStatus(206)
- ct = self.assertHeader("Content-Type")
- expected_type = "multipart/byteranges; boundary="
- self.assert_(ct.startswith(expected_type))
- boundary = ct[len(expected_type):]
- expected_body = ("\r\n--%s\r\n"
- "Content-type: text/html\r\n"
- "Content-range: bytes 4-6/14\r\n"
- "\r\n"
- "o, \r\n"
- "--%s\r\n"
- "Content-type: text/html\r\n"
- "Content-range: bytes 2-5/14\r\n"
- "\r\n"
- "llo,\r\n"
- "--%s--\r\n" % (boundary, boundary, boundary))
- self.assertBody(expected_body)
- self.assertHeader("Content-Length")
-
- # Test "416 Requested Range Not Satisfiable"
- self.getPage("/ranges/slice_file", [('Range', 'bytes=2300-2900')])
- self.assertStatus(416)
- # "When this status code is returned for a byte-range request,
- # the response SHOULD include a Content-Range entity-header
- # field specifying the current length of the selected resource"
- self.assertHeader("Content-Range", "bytes */14")
- elif cherrypy.server.protocol_version == "HTTP/1.0":
- # Test Range behavior with HTTP/1.0 request
- self.getPage("/ranges/slice_file", [('Range', 'bytes=2-5')])
- self.assertStatus(200)
- self.assertBody("Hello, world\r\n")
-
- def testFavicon(self):
- # favicon.ico is served by staticfile.
- icofilename = os.path.join(localDir, "../favicon.ico")
- icofile = open(icofilename, "rb")
- data = icofile.read()
- icofile.close()
-
- self.getPage("/favicon.ico")
- self.assertBody(data)
-
- def testCookies(self):
- if sys.version_info >= (2, 5):
- header_value = lambda x: x
- else:
- header_value = lambda x: x+';'
-
- self.getPage("/cookies/single?name=First",
- [('Cookie', 'First=Dinsdale;')])
- self.assertHeader('Set-Cookie', header_value('First=Dinsdale'))
-
- self.getPage("/cookies/multiple?names=First&names=Last",
- [('Cookie', 'First=Dinsdale; Last=Piranha;'),
- ])
- self.assertHeader('Set-Cookie', header_value('First=Dinsdale'))
- self.assertHeader('Set-Cookie', header_value('Last=Piranha'))
-
- self.getPage("/cookies/single?name=Something-With:Colon",
- [('Cookie', 'Something-With:Colon=some-value')])
- self.assertStatus(400)
-
- def testDefaultContentType(self):
- self.getPage('/')
- self.assertHeader('Content-Type', 'text/html;charset=utf-8')
- self.getPage('/defct/plain')
- self.getPage('/')
- self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
- self.getPage('/defct/html')
-
- def test_cherrypy_url(self):
- # Input relative to current
- self.getPage('/url/leaf?path_info=page1')
- self.assertBody('%s/url/page1' % self.base())
- self.getPage('/url/?path_info=page1')
- self.assertBody('%s/url/page1' % self.base())
- # Other host header
- host = 'www.mydomain.example'
- self.getPage('/url/leaf?path_info=page1',
- headers=[('Host', host)])
- self.assertBody('%s://%s/url/page1' % (self.scheme, host))
-
- # Input is 'absolute'; that is, relative to script_name
- self.getPage('/url/leaf?path_info=/page1')
- self.assertBody('%s/page1' % self.base())
- self.getPage('/url/?path_info=/page1')
- self.assertBody('%s/page1' % self.base())
-
- # Single dots
- self.getPage('/url/leaf?path_info=./page1')
- self.assertBody('%s/url/page1' % self.base())
- self.getPage('/url/leaf?path_info=other/./page1')
- self.assertBody('%s/url/other/page1' % self.base())
- self.getPage('/url/?path_info=/other/./page1')
- self.assertBody('%s/other/page1' % self.base())
-
- # Double dots
- self.getPage('/url/leaf?path_info=../page1')
- self.assertBody('%s/page1' % self.base())
- self.getPage('/url/leaf?path_info=other/../page1')
- self.assertBody('%s/url/page1' % self.base())
- self.getPage('/url/leaf?path_info=/other/../page1')
- self.assertBody('%s/page1' % self.base())
-
- # Output relative to current path or script_name
- self.getPage('/url/?path_info=page1&relative=True')
- self.assertBody('page1')
- self.getPage('/url/leaf?path_info=/page1&relative=True')
- self.assertBody('../page1')
- self.getPage('/url/leaf?path_info=page1&relative=True')
- self.assertBody('page1')
- self.getPage('/url/leaf?path_info=leaf/page1&relative=True')
- self.assertBody('leaf/page1')
- self.getPage('/url/leaf?path_info=../page1&relative=True')
- self.assertBody('../page1')
- self.getPage('/url/?path_info=other/../page1&relative=True')
- self.assertBody('page1')
-
- # Output relative to /
- self.getPage('/baseurl?path_info=ab&relative=True')
- self.assertBody('ab')
- # Output relative to /
- self.getPage('/baseurl?path_info=/ab&relative=True')
- self.assertBody('ab')
-
- # absolute-path references ("server-relative")
- # Input relative to current
- self.getPage('/url/leaf?path_info=page1&relative=server')
- self.assertBody('/url/page1')
- self.getPage('/url/?path_info=page1&relative=server')
- self.assertBody('/url/page1')
- # Input is 'absolute'; that is, relative to script_name
- self.getPage('/url/leaf?path_info=/page1&relative=server')
- self.assertBody('/page1')
- self.getPage('/url/?path_info=/page1&relative=server')
- self.assertBody('/page1')
-
- def test_expose_decorator(self):
- if not sys.version_info >= (2, 5):
- return self.skip("skipped (Python 2.5+ only) ")
-
- # Test @expose
- self.getPage("/expose_dec/no_call")
- self.assertStatus(200)
- self.assertBody("Mr E. R. Bradshaw")
-
- # Test @expose()
- self.getPage("/expose_dec/call_empty")
- self.assertStatus(200)
- self.assertBody("Mrs. B.J. Smegma")
-
- # Test @expose("alias")
- self.getPage("/expose_dec/call_alias")
- self.assertStatus(200)
- self.assertBody("Mr Nesbitt")
- # Does the original name work?
- self.getPage("/expose_dec/nesbitt")
- self.assertStatus(200)
- self.assertBody("Mr Nesbitt")
-
- # Test @expose(["alias1", "alias2"])
- self.getPage("/expose_dec/alias1")
- self.assertStatus(200)
- self.assertBody("Mr Ken Andrews")
- self.getPage("/expose_dec/alias2")
- self.assertStatus(200)
- self.assertBody("Mr Ken Andrews")
- # Does the original name work?
- self.getPage("/expose_dec/andrews")
- self.assertStatus(200)
- self.assertBody("Mr Ken Andrews")
-
- # Test @expose(alias="alias")
- self.getPage("/expose_dec/alias3")
- self.assertStatus(200)
- self.assertBody("Mr. and Mrs. Watson")
-
diff --git a/cherrypy/test/test_dynamicobjectmapping.py b/cherrypy/test/test_dynamicobjectmapping.py
deleted file mode 100644
index 1e04d089..00000000
--- a/cherrypy/test/test_dynamicobjectmapping.py
+++ /dev/null
@@ -1,403 +0,0 @@
-import cherrypy
-from cherrypy._cptree import Application
-from cherrypy.test import helper
-
-script_names = ["", "/foo", "/users/fred/blog", "/corp/blog"]
-
-
-
-def setup_server():
- class SubSubRoot:
- def index(self):
- return "SubSubRoot index"
- index.exposed = True
-
- def default(self, *args):
- return "SubSubRoot default"
- default.exposed = True
-
- def handler(self):
- return "SubSubRoot handler"
- handler.exposed = True
-
- def dispatch(self):
- return "SubSubRoot dispatch"
- dispatch.exposed = True
-
- subsubnodes = {
- '1': SubSubRoot(),
- '2': SubSubRoot(),
- }
-
- class SubRoot:
- def index(self):
- return "SubRoot index"
- index.exposed = True
-
- def default(self, *args):
- return "SubRoot %s" % (args,)
- default.exposed = True
-
- def handler(self):
- return "SubRoot handler"
- handler.exposed = True
-
- def _cp_dispatch(self, vpath):
- return subsubnodes.get(vpath[0], None)
-
- subnodes = {
- '1': SubRoot(),
- '2': SubRoot(),
- }
- class Root:
- def index(self):
- return "index"
- index.exposed = True
-
- def default(self, *args):
- return "default %s" % (args,)
- default.exposed = True
-
- def handler(self):
- return "handler"
- handler.exposed = True
-
- def _cp_dispatch(self, vpath):
- return subnodes.get(vpath[0])
-
- #--------------------------------------------------------------------------
- # DynamicNodeAndMethodDispatcher example.
- # This example exposes a fairly naive HTTP api
- class User(object):
- def __init__(self, id, name):
- self.id = id
- self.name = name
-
- def __unicode__(self):
- return unicode(self.name)
-
- user_lookup = {
- 1: User(1, 'foo'),
- 2: User(2, 'bar'),
- }
-
- def make_user(name, id=None):
- if not id:
- id = max(*user_lookup.keys()) + 1
- user_lookup[id] = User(id, name)
- return id
-
- class UserContainerNode(object):
- exposed = True
-
- def POST(self, name):
- """
- Allow the creation of a new Object
- """
- return "POST %d" % make_user(name)
-
- def GET(self):
- keys = user_lookup.keys()
- keys.sort()
- return unicode(keys)
-
- def dynamic_dispatch(self, vpath):
- try:
- id = int(vpath[0])
- except (ValueError, IndexError):
- return None
- return UserInstanceNode(id)
-
- class UserInstanceNode(object):
- exposed = True
- def __init__(self, id):
- self.id = id
- self.user = user_lookup.get(id, None)
-
- # For all but PUT methods there MUST be a valid user identified
- # by self.id
- if not self.user and cherrypy.request.method != 'PUT':
- raise cherrypy.HTTPError(404)
-
- def GET(self, *args, **kwargs):
- """
- Return the appropriate representation of the instance.
- """
- return unicode(self.user)
-
- def POST(self, name):
- """
- Update the fields of the user instance.
- """
- self.user.name = name
- return "POST %d" % self.user.id
-
- def PUT(self, name):
- """
- Create a new user with the specified id, or edit it if it already exists
- """
- if self.user:
- # Edit the current user
- self.user.name = name
- return "PUT %d" % self.user.id
- else:
- # Make a new user with said attributes.
- return "PUT %d" % make_user(name, self.id)
-
- def DELETE(self):
- """
- Delete the user specified at the id.
- """
- id = self.user.id
- del user_lookup[self.user.id]
- del self.user
- return "DELETE %d" % id
-
-
- class ABHandler:
- class CustomDispatch:
- def index(self, a, b):
- return "custom"
- index.exposed = True
-
- def _cp_dispatch(self, vpath):
- """Make sure that if we don't pop anything from vpath,
- processing still works.
- """
- return self.CustomDispatch()
-
- def index(self, a, b=None):
- body = [ 'a:' + str(a) ]
- if b is not None:
- body.append(',b:' + str(b))
- return ''.join(body)
- index.exposed = True
-
- def delete(self, a, b):
- return 'deleting ' + str(a) + ' and ' + str(b)
- delete.exposed = True
-
- class IndexOnly:
- def _cp_dispatch(self, vpath):
- """Make sure that popping ALL of vpath still shows the index
- handler.
- """
- while vpath:
- vpath.pop()
- return self
-
- def index(self):
- return "IndexOnly index"
- index.exposed = True
-
- class DecoratedPopArgs:
- """Test _cp_dispatch with @cherrypy.popargs."""
- def index(self):
- return "no params"
- index.exposed = True
-
- def hi(self):
- return "hi was not interpreted as 'a' param"
- hi.exposed = True
- DecoratedPopArgs = cherrypy.popargs('a', 'b', handler=ABHandler())(DecoratedPopArgs)
-
- class NonDecoratedPopArgs:
- """Test _cp_dispatch = cherrypy.popargs()"""
-
- _cp_dispatch = cherrypy.popargs('a')
-
- def index(self, a):
- return "index: " + str(a)
- index.exposed = True
-
- class ParameterizedHandler:
- """Special handler created for each request"""
-
- def __init__(self, a):
- self.a = a
-
- def index(self):
- if 'a' in cherrypy.request.params:
- raise Exception("Parameterized handler argument ended up in request.params")
- return self.a
- index.exposed = True
-
- class ParameterizedPopArgs:
- """Test cherrypy.popargs() with a function call handler"""
- ParameterizedPopArgs = cherrypy.popargs('a', handler=ParameterizedHandler)(ParameterizedPopArgs)
-
- Root.decorated = DecoratedPopArgs()
- Root.undecorated = NonDecoratedPopArgs()
- Root.index_only = IndexOnly()
- Root.parameter_test = ParameterizedPopArgs()
-
- Root.users = UserContainerNode()
-
- md = cherrypy.dispatch.MethodDispatcher('dynamic_dispatch')
- for url in script_names:
- conf = {'/': {
- 'user': (url or "/").split("/")[-2],
- },
- '/users': {
- 'request.dispatch': md
- },
- }
- cherrypy.tree.mount(Root(), url, conf)
-
-class DynamicObjectMappingTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def testObjectMapping(self):
- for url in script_names:
- prefix = self.script_name = url
-
- self.getPage('/')
- self.assertBody('index')
-
- self.getPage('/handler')
- self.assertBody('handler')
-
- # Dynamic dispatch will succeed here for the subnodes
- # so the subroot gets called
- self.getPage('/1/')
- self.assertBody('SubRoot index')
-
- self.getPage('/2/')
- self.assertBody('SubRoot index')
-
- self.getPage('/1/handler')
- self.assertBody('SubRoot handler')
-
- self.getPage('/2/handler')
- self.assertBody('SubRoot handler')
-
- # Dynamic dispatch will fail here for the subnodes
- # so the default gets called
- self.getPage('/asdf/')
- self.assertBody("default ('asdf',)")
-
- self.getPage('/asdf/asdf')
- self.assertBody("default ('asdf', 'asdf')")
-
- self.getPage('/asdf/handler')
- self.assertBody("default ('asdf', 'handler')")
-
- # Dynamic dispatch will succeed here for the subsubnodes
- # so the subsubroot gets called
- self.getPage('/1/1/')
- self.assertBody('SubSubRoot index')
-
- self.getPage('/2/2/')
- self.assertBody('SubSubRoot index')
-
- self.getPage('/1/1/handler')
- self.assertBody('SubSubRoot handler')
-
- self.getPage('/2/2/handler')
- self.assertBody('SubSubRoot handler')
-
- self.getPage('/2/2/dispatch')
- self.assertBody('SubSubRoot dispatch')
-
- # The exposed dispatch will not be called as a dispatch
- # method.
- self.getPage('/2/2/foo/foo')
- self.assertBody("SubSubRoot default")
-
- # Dynamic dispatch will fail here for the subsubnodes
- # so the SubRoot gets called
- self.getPage('/1/asdf/')
- self.assertBody("SubRoot ('asdf',)")
-
- self.getPage('/1/asdf/asdf')
- self.assertBody("SubRoot ('asdf', 'asdf')")
-
- self.getPage('/1/asdf/handler')
- self.assertBody("SubRoot ('asdf', 'handler')")
-
- def testMethodDispatch(self):
- # GET acts like a container
- self.getPage("/users")
- self.assertBody("[1, 2]")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- # POST to the container URI allows creation
- self.getPage("/users", method="POST", body="name=baz")
- self.assertBody("POST 3")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- # POST to a specific instanct URI results in a 404
- # as the resource does not exit.
- self.getPage("/users/5", method="POST", body="name=baz")
- self.assertStatus(404)
-
- # PUT to a specific instanct URI results in creation
- self.getPage("/users/5", method="PUT", body="name=boris")
- self.assertBody("PUT 5")
- self.assertHeader('Allow', 'DELETE, GET, HEAD, POST, PUT')
-
- # GET acts like a container
- self.getPage("/users")
- self.assertBody("[1, 2, 3, 5]")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- test_cases = (
- (1, 'foo', 'fooupdated', 'DELETE, GET, HEAD, POST, PUT'),
- (2, 'bar', 'barupdated', 'DELETE, GET, HEAD, POST, PUT'),
- (3, 'baz', 'bazupdated', 'DELETE, GET, HEAD, POST, PUT'),
- (5, 'boris', 'borisupdated', 'DELETE, GET, HEAD, POST, PUT'),
- )
- for id, name, updatedname, headers in test_cases:
- self.getPage("/users/%d" % id)
- self.assertBody(name)
- self.assertHeader('Allow', headers)
-
- # Make sure POSTs update already existings resources
- self.getPage("/users/%d" % id, method='POST', body="name=%s" % updatedname)
- self.assertBody("POST %d" % id)
- self.assertHeader('Allow', headers)
-
- # Make sure PUTs Update already existing resources.
- self.getPage("/users/%d" % id, method='PUT', body="name=%s" % updatedname)
- self.assertBody("PUT %d" % id)
- self.assertHeader('Allow', headers)
-
- # Make sure DELETES Remove already existing resources.
- self.getPage("/users/%d" % id, method='DELETE')
- self.assertBody("DELETE %d" % id)
- self.assertHeader('Allow', headers)
-
-
- # GET acts like a container
- self.getPage("/users")
- self.assertBody("[]")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- def testVpathDispatch(self):
- self.getPage("/decorated/")
- self.assertBody("no params")
-
- self.getPage("/decorated/hi")
- self.assertBody("hi was not interpreted as 'a' param")
-
- self.getPage("/decorated/yo/")
- self.assertBody("a:yo")
-
- self.getPage("/decorated/yo/there/")
- self.assertBody("a:yo,b:there")
-
- self.getPage("/decorated/yo/there/delete")
- self.assertBody("deleting yo and there")
-
- self.getPage("/decorated/yo/there/handled_by_dispatch/")
- self.assertBody("custom")
-
- self.getPage("/undecorated/blah/")
- self.assertBody("index: blah")
-
- self.getPage("/index_only/a/b/c/d/e/f/g/")
- self.assertBody("IndexOnly index")
-
- self.getPage("/parameter_test/argument2/")
- self.assertBody("argument2")
-
diff --git a/cherrypy/test/test_encoding.py b/cherrypy/test/test_encoding.py
deleted file mode 100644
index 67b28ede..00000000
--- a/cherrypy/test/test_encoding.py
+++ /dev/null
@@ -1,363 +0,0 @@
-
-import gzip
-import sys
-
-import cherrypy
-from cherrypy._cpcompat import BytesIO, IncompleteRead, ntob, ntou
-
-europoundUnicode = ntou('\x80\xa3')
-sing = u"\u6bdb\u6cfd\u4e1c: Sing, Little Birdie?"
-sing8 = sing.encode('utf-8')
-sing16 = sing.encode('utf-16')
-
-
-from cherrypy.test import helper
-
-
-class EncodingTests(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self, param):
- assert param == europoundUnicode, "%r != %r" % (param, europoundUnicode)
- yield europoundUnicode
- index.exposed = True
-
- def mao_zedong(self):
- return sing
- mao_zedong.exposed = True
-
- def utf8(self):
- return sing8
- utf8.exposed = True
- utf8._cp_config = {'tools.encode.encoding': 'utf-8'}
-
- def cookies_and_headers(self):
- # if the headers have non-ascii characters and a cookie has
- # any part which is unicode (even ascii), the response
- # should not fail.
- cherrypy.response.cookie['candy'] = 'bar'
- cherrypy.response.cookie['candy']['domain'] = 'cherrypy.org'
- cherrypy.response.headers['Some-Header'] = 'My d\xc3\xb6g has fleas'
- return 'Any content'
- cookies_and_headers.exposed = True
-
- def reqparams(self, *args, **kwargs):
- return ntob(', ').join([": ".join((k, v)).encode('utf8')
- for k, v in cherrypy.request.params.items()])
- reqparams.exposed = True
-
- def nontext(self, *args, **kwargs):
- cherrypy.response.headers['Content-Type'] = 'application/binary'
- return '\x00\x01\x02\x03'
- nontext.exposed = True
- nontext._cp_config = {'tools.encode.text_only': False,
- 'tools.encode.add_charset': True,
- }
-
- class GZIP:
- def index(self):
- yield "Hello, world"
- index.exposed = True
-
- def noshow(self):
- # Test for ticket #147, where yield showed no exceptions (content-
- # encoding was still gzip even though traceback wasn't zipped).
- raise IndexError()
- yield "Here be dragons"
- noshow.exposed = True
- # Turn encoding off so the gzip tool is the one doing the collapse.
- noshow._cp_config = {'tools.encode.on': False}
-
- def noshow_stream(self):
- # Test for ticket #147, where yield showed no exceptions (content-
- # encoding was still gzip even though traceback wasn't zipped).
- raise IndexError()
- yield "Here be dragons"
- noshow_stream.exposed = True
- noshow_stream._cp_config = {'response.stream': True}
-
- class Decode:
- def extra_charset(self, *args, **kwargs):
- return ', '.join([": ".join((k, v))
- for k, v in cherrypy.request.params.items()])
- extra_charset.exposed = True
- extra_charset._cp_config = {
- 'tools.decode.on': True,
- 'tools.decode.default_encoding': ['utf-16'],
- }
-
- def force_charset(self, *args, **kwargs):
- return ', '.join([": ".join((k, v))
- for k, v in cherrypy.request.params.items()])
- force_charset.exposed = True
- force_charset._cp_config = {
- 'tools.decode.on': True,
- 'tools.decode.encoding': 'utf-16',
- }
-
- root = Root()
- root.gzip = GZIP()
- root.decode = Decode()
- cherrypy.tree.mount(root, config={'/gzip': {'tools.gzip.on': True}})
- setup_server = staticmethod(setup_server)
-
- def test_query_string_decoding(self):
- europoundUtf8 = europoundUnicode.encode('utf-8')
- self.getPage(ntob('/?param=') + europoundUtf8)
- self.assertBody(europoundUtf8)
-
- # Encoded utf8 query strings MUST be parsed correctly.
- # Here, q is the POUND SIGN U+00A3 encoded in utf8 and then %HEX
- self.getPage("/reqparams?q=%C2%A3")
- # The return value will be encoded as utf8.
- self.assertBody(ntob("q: \xc2\xa3"))
-
- # Query strings that are incorrectly encoded MUST raise 404.
- # Here, q is the POUND SIGN U+00A3 encoded in latin1 and then %HEX
- self.getPage("/reqparams?q=%A3")
- self.assertStatus(404)
- self.assertErrorPage(404,
- "The given query string could not be processed. Query "
- "strings for this resource must be encoded with 'utf8'.")
-
- def test_urlencoded_decoding(self):
- # Test the decoding of an application/x-www-form-urlencoded entity.
- europoundUtf8 = europoundUnicode.encode('utf-8')
- body=ntob("param=") + europoundUtf8
- self.getPage('/', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(europoundUtf8)
-
- # Encoded utf8 entities MUST be parsed and decoded correctly.
- # Here, q is the POUND SIGN U+00A3 encoded in utf8
- body = ntob("q=\xc2\xa3")
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("q: \xc2\xa3"))
-
- # ...and in utf16, which is not in the default attempt_charsets list:
- body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded;charset=utf-16"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("q: \xc2\xa3"))
-
- # Entities that are incorrectly encoded MUST raise 400.
- # Here, q is the POUND SIGN U+00A3 encoded in utf16, but
- # the Content-Type incorrectly labels it utf-8.
- body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertStatus(400)
- self.assertErrorPage(400,
- "The request entity could not be decoded. The following charsets "
- "were attempted: ['utf-8']")
-
- def test_decode_tool(self):
- # An extra charset should be tried first, and succeed if it matches.
- # Here, we add utf-16 as a charset and pass a utf-16 body.
- body = ntob("\xff\xfeq\x00=\xff\xfe\xa3\x00")
- self.getPage('/decode/extra_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("q: \xc2\xa3"))
-
- # An extra charset should be tried first, and continue to other default
- # charsets if it doesn't match.
- # Here, we add utf-16 as a charset but still pass a utf-8 body.
- body = ntob("q=\xc2\xa3")
- self.getPage('/decode/extra_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("q: \xc2\xa3"))
-
- # An extra charset should error if force is True and it doesn't match.
- # Here, we force utf-16 as a charset but still pass a utf-8 body.
- body = ntob("q=\xc2\xa3")
- self.getPage('/decode/force_charset', method='POST',
- headers=[("Content-Type", "application/x-www-form-urlencoded"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertErrorPage(400,
- "The request entity could not be decoded. The following charsets "
- "were attempted: ['utf-16']")
-
- def test_multipart_decoding(self):
- # Test the decoding of a multipart entity when the charset (utf16) is
- # explicitly given.
- body=ntob('\r\n'.join(['--X',
- 'Content-Type: text/plain;charset=utf-16',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xff\xfea\x00b\x00\x1c c\x00',
- '--X',
- 'Content-Type: text/plain;charset=utf-16',
- 'Content-Disposition: form-data; name="submit"',
- '',
- '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
- '--X--']))
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("text: ab\xe2\x80\x9cc, submit: Create"))
-
- def test_multipart_decoding_no_charset(self):
- # Test the decoding of a multipart entity when the charset (utf8) is
- # NOT explicitly given, but is in the list of charsets to attempt.
- body=ntob('\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xe2\x80\x9c',
- '--X',
- 'Content-Disposition: form-data; name="submit"',
- '',
- 'Create',
- '--X--']))
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(ntob("text: \xe2\x80\x9c, submit: Create"))
-
- def test_multipart_decoding_no_successful_charset(self):
- # Test the decoding of a multipart entity when the charset (utf16) is
- # NOT explicitly given, and is NOT in the list of charsets to attempt.
- body=ntob('\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="text"',
- '',
- '\xff\xfea\x00b\x00\x1c c\x00',
- '--X',
- 'Content-Disposition: form-data; name="submit"',
- '',
- '\xff\xfeC\x00r\x00e\x00a\x00t\x00e\x00',
- '--X--']))
- self.getPage('/reqparams', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertStatus(400)
- self.assertErrorPage(400,
- "The request entity could not be decoded. The following charsets "
- "were attempted: ['us-ascii', 'utf-8']")
-
- def test_nontext(self):
- self.getPage('/nontext')
- self.assertHeader('Content-Type', 'application/binary;charset=utf-8')
- self.assertBody('\x00\x01\x02\x03')
-
- def testEncoding(self):
- # Default encoding should be utf-8
- self.getPage('/mao_zedong')
- self.assertBody(sing8)
-
- # Ask for utf-16.
- self.getPage('/mao_zedong', [('Accept-Charset', 'utf-16')])
- self.assertHeader('Content-Type', 'text/html;charset=utf-16')
- self.assertBody(sing16)
-
- # Ask for multiple encodings. ISO-8859-1 should fail, and utf-16
- # should be produced.
- self.getPage('/mao_zedong', [('Accept-Charset',
- 'iso-8859-1;q=1, utf-16;q=0.5')])
- self.assertBody(sing16)
-
- # The "*" value should default to our default_encoding, utf-8
- self.getPage('/mao_zedong', [('Accept-Charset', '*;q=1, utf-7;q=.2')])
- self.assertBody(sing8)
-
- # Only allow iso-8859-1, which should fail and raise 406.
- self.getPage('/mao_zedong', [('Accept-Charset', 'iso-8859-1, *;q=0')])
- self.assertStatus("406 Not Acceptable")
- self.assertInBody("Your client sent this Accept-Charset header: "
- "iso-8859-1, *;q=0. We tried these charsets: "
- "iso-8859-1.")
-
- # Ask for x-mac-ce, which should be unknown. See ticket #569.
- self.getPage('/mao_zedong', [('Accept-Charset',
- 'us-ascii, ISO-8859-1, x-mac-ce')])
- self.assertStatus("406 Not Acceptable")
- self.assertInBody("Your client sent this Accept-Charset header: "
- "us-ascii, ISO-8859-1, x-mac-ce. We tried these "
- "charsets: ISO-8859-1, us-ascii, x-mac-ce.")
-
- # Test the 'encoding' arg to encode.
- self.getPage('/utf8')
- self.assertBody(sing8)
- self.getPage('/utf8', [('Accept-Charset', 'us-ascii, ISO-8859-1')])
- self.assertStatus("406 Not Acceptable")
-
- def testGzip(self):
- zbuf = BytesIO()
- zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
- zfile.write(ntob("Hello, world"))
- zfile.close()
-
- self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")])
- self.assertInBody(zbuf.getvalue()[:3])
- self.assertHeader("Vary", "Accept-Encoding")
- self.assertHeader("Content-Encoding", "gzip")
-
- # Test when gzip is denied.
- self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")])
- self.assertHeader("Vary", "Accept-Encoding")
- self.assertNoHeader("Content-Encoding")
- self.assertBody("Hello, world")
-
- self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")])
- self.assertHeader("Vary", "Accept-Encoding")
- self.assertNoHeader("Content-Encoding")
- self.assertBody("Hello, world")
-
- self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")])
- self.assertStatus(406)
- self.assertNoHeader("Content-Encoding")
- self.assertErrorPage(406, "identity, gzip")
-
- # Test for ticket #147
- self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")])
- self.assertNoHeader('Content-Encoding')
- self.assertStatus(500)
- self.assertErrorPage(500, pattern="IndexError\n")
-
- # In this case, there's nothing we can do to deliver a
- # readable page, since 1) the gzip header is already set,
- # and 2) we may have already written some of the body.
- # The fix is to never stream yields when using gzip.
- if (cherrypy.server.protocol_version == "HTTP/1.0" or
- getattr(cherrypy.server, "using_apache", False)):
- self.getPage('/gzip/noshow_stream',
- headers=[("Accept-Encoding", "gzip")])
- self.assertHeader('Content-Encoding', 'gzip')
- self.assertInBody('\x1f\x8b\x08\x00')
- else:
- # The wsgiserver will simply stop sending data, and the HTTP client
- # will error due to an incomplete chunk-encoded stream.
- self.assertRaises((ValueError, IncompleteRead), self.getPage,
- '/gzip/noshow_stream',
- headers=[("Accept-Encoding", "gzip")])
-
- def test_UnicodeHeaders(self):
- self.getPage('/cookies_and_headers')
- self.assertBody('Any content')
-
diff --git a/cherrypy/test/test_etags.py b/cherrypy/test/test_etags.py
deleted file mode 100644
index 026f9d65..00000000
--- a/cherrypy/test/test_etags.py
+++ /dev/null
@@ -1,81 +0,0 @@
-import cherrypy
-from cherrypy.test import helper
-
-
-class ETagTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def resource(self):
- return "Oh wah ta goo Siam."
- resource.exposed = True
-
- def fail(self, code):
- code = int(code)
- if 300 <= code <= 399:
- raise cherrypy.HTTPRedirect([], code)
- else:
- raise cherrypy.HTTPError(code)
- fail.exposed = True
-
- def unicoded(self):
- return u'I am a \u1ee4nicode string.'
- unicoded.exposed = True
- unicoded._cp_config = {'tools.encode.on': True}
-
- conf = {'/': {'tools.etags.on': True,
- 'tools.etags.autotags': True,
- }}
- cherrypy.tree.mount(Root(), config=conf)
- setup_server = staticmethod(setup_server)
-
- def test_etags(self):
- self.getPage("/resource")
- self.assertStatus('200 OK')
- self.assertHeader('Content-Type', 'text/html;charset=utf-8')
- self.assertBody('Oh wah ta goo Siam.')
- etag = self.assertHeader('ETag')
-
- # Test If-Match (both valid and invalid)
- self.getPage("/resource", headers=[('If-Match', etag)])
- self.assertStatus("200 OK")
- self.getPage("/resource", headers=[('If-Match', "*")])
- self.assertStatus("200 OK")
- self.getPage("/resource", headers=[('If-Match', "*")], method="POST")
- self.assertStatus("200 OK")
- self.getPage("/resource", headers=[('If-Match', "a bogus tag")])
- self.assertStatus("412 Precondition Failed")
-
- # Test If-None-Match (both valid and invalid)
- self.getPage("/resource", headers=[('If-None-Match', etag)])
- self.assertStatus(304)
- self.getPage("/resource", method='POST', headers=[('If-None-Match', etag)])
- self.assertStatus("412 Precondition Failed")
- self.getPage("/resource", headers=[('If-None-Match', "*")])
- self.assertStatus(304)
- self.getPage("/resource", headers=[('If-None-Match', "a bogus tag")])
- self.assertStatus("200 OK")
-
- def test_errors(self):
- self.getPage("/resource")
- self.assertStatus(200)
- etag = self.assertHeader('ETag')
-
- # Test raising errors in page handler
- self.getPage("/fail/412", headers=[('If-Match', etag)])
- self.assertStatus(412)
- self.getPage("/fail/304", headers=[('If-Match', etag)])
- self.assertStatus(304)
- self.getPage("/fail/412", headers=[('If-None-Match', "*")])
- self.assertStatus(412)
- self.getPage("/fail/304", headers=[('If-None-Match', "*")])
- self.assertStatus(304)
-
- def test_unicode_body(self):
- self.getPage("/unicoded")
- self.assertStatus(200)
- etag1 = self.assertHeader('ETag')
- self.getPage("/unicoded", headers=[('If-Match', etag1)])
- self.assertStatus(200)
- self.assertHeader('ETag', etag1)
-
diff --git a/cherrypy/test/test_http.py b/cherrypy/test/test_http.py
deleted file mode 100644
index eb72b5bf..00000000
--- a/cherrypy/test/test_http.py
+++ /dev/null
@@ -1,168 +0,0 @@
-"""Tests for managing HTTP issues (malformed requests, etc)."""
-
-import mimetypes
-
-import cherrypy
-from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, ntob
-
-
-def encode_multipart_formdata(files):
- """Return (content_type, body) ready for httplib.HTTP instance.
-
- files: a sequence of (name, filename, value) tuples for multipart uploads.
- """
- BOUNDARY = '________ThIs_Is_tHe_bouNdaRY_$'
- L = []
- for key, filename, value in files:
- L.append('--' + BOUNDARY)
- L.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
- (key, filename))
- ct = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
- L.append('Content-Type: %s' % ct)
- L.append('')
- L.append(value)
- L.append('--' + BOUNDARY + '--')
- L.append('')
- body = '\r\n'.join(L)
- content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
- return content_type, body
-
-
-
-
-from cherrypy.test import helper
-
-class HTTPTests(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self, *args, **kwargs):
- return "Hello world!"
- index.exposed = True
-
- def no_body(self, *args, **kwargs):
- return "Hello world!"
- no_body.exposed = True
- no_body._cp_config = {'request.process_request_body': False}
-
- def post_multipart(self, file):
- """Return a summary ("a * 65536\nb * 65536") of the uploaded file."""
- contents = file.file.read()
- summary = []
- curchar = ""
- count = 0
- for c in contents:
- if c == curchar:
- count += 1
- else:
- if count:
- summary.append("%s * %d" % (curchar, count))
- count = 1
- curchar = c
- if count:
- summary.append("%s * %d" % (curchar, count))
- return ", ".join(summary)
- post_multipart.exposed = True
-
- cherrypy.tree.mount(Root())
- cherrypy.config.update({'server.max_request_body_size': 30000000})
- setup_server = staticmethod(setup_server)
-
- def test_no_content_length(self):
- # "The presence of a message-body in a request is signaled by the
- # inclusion of a Content-Length or Transfer-Encoding header field in
- # the request's message-headers."
- #
- # Send a message with neither header and no body. Even though
- # the request is of method POST, this should be OK because we set
- # request.process_request_body to False for our handler.
- if self.scheme == "https":
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- c.request("POST", "/no_body")
- response = c.getresponse()
- self.body = response.fp.read()
- self.status = str(response.status)
- self.assertStatus(200)
- self.assertBody(ntob('Hello world!'))
-
- # Now send a message that has no Content-Length, but does send a body.
- # Verify that CP times out the socket and responds
- # with 411 Length Required.
- if self.scheme == "https":
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- c.request("POST", "/")
- response = c.getresponse()
- self.body = response.fp.read()
- self.status = str(response.status)
- self.assertStatus(411)
-
- def test_post_multipart(self):
- alphabet = "abcdefghijklmnopqrstuvwxyz"
- # generate file contents for a large post
- contents = "".join([c * 65536 for c in alphabet])
-
- # encode as multipart form data
- files=[('file', 'file.txt', contents)]
- content_type, body = encode_multipart_formdata(files)
- body = body.encode('Latin-1')
-
- # post file
- if self.scheme == 'https':
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- c.putrequest('POST', '/post_multipart')
- c.putheader('Content-Type', content_type)
- c.putheader('Content-Length', str(len(body)))
- c.endheaders()
- c.send(body)
-
- response = c.getresponse()
- self.body = response.fp.read()
- self.status = str(response.status)
- self.assertStatus(200)
- self.assertBody(", ".join(["%s * 65536" % c for c in alphabet]))
-
- def test_malformed_request_line(self):
- if getattr(cherrypy.server, "using_apache", False):
- return self.skip("skipped due to known Apache differences...")
-
- # Test missing version in Request-Line
- if self.scheme == 'https':
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- c._output(ntob('GET /'))
- c._send_output()
- if hasattr(c, 'strict'):
- response = c.response_class(c.sock, strict=c.strict, method='GET')
- else:
- # Python 3.2 removed the 'strict' feature, saying:
- # "http.client now always assumes HTTP/1.x compliant servers."
- response = c.response_class(c.sock, method='GET')
- response.begin()
- self.assertEqual(response.status, 400)
- self.assertEqual(response.fp.read(22), ntob("Malformed Request-Line"))
- c.close()
-
- def test_malformed_header(self):
- if self.scheme == 'https':
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- c.putrequest('GET', '/')
- c.putheader('Content-Type', 'text/plain')
- # See http://www.cherrypy.org/ticket/941
- c._output(ntob('Re, 1.2.3.4#015#012'))
- c.endheaders()
-
- response = c.getresponse()
- self.status = str(response.status)
- self.assertStatus(400)
- self.body = response.fp.read(20)
- self.assertBody("Illegal header line.")
-
diff --git a/cherrypy/test/test_httpauth.py b/cherrypy/test/test_httpauth.py
deleted file mode 100644
index 9d0eecb2..00000000
--- a/cherrypy/test/test_httpauth.py
+++ /dev/null
@@ -1,151 +0,0 @@
-import cherrypy
-from cherrypy._cpcompat import md5, sha, ntob
-from cherrypy.lib import httpauth
-
-from cherrypy.test import helper
-
-class HTTPAuthTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self):
- return "This is public."
- index.exposed = True
-
- class DigestProtected:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- class BasicProtected:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- class BasicProtected2:
- def index(self):
- return "Hello %s, you've been authorized." % cherrypy.request.login
- index.exposed = True
-
- def fetch_users():
- return {'test': 'test'}
-
- def sha_password_encrypter(password):
- return sha(ntob(password)).hexdigest()
-
- def fetch_password(username):
- return sha(ntob('test')).hexdigest()
-
- conf = {'/digest': {'tools.digest_auth.on': True,
- 'tools.digest_auth.realm': 'localhost',
- 'tools.digest_auth.users': fetch_users},
- '/basic': {'tools.basic_auth.on': True,
- 'tools.basic_auth.realm': 'localhost',
- 'tools.basic_auth.users': {'test': md5(ntob('test')).hexdigest()}},
- '/basic2': {'tools.basic_auth.on': True,
- 'tools.basic_auth.realm': 'localhost',
- 'tools.basic_auth.users': fetch_password,
- 'tools.basic_auth.encrypt': sha_password_encrypter}}
-
- root = Root()
- root.digest = DigestProtected()
- root.basic = BasicProtected()
- root.basic2 = BasicProtected2()
- cherrypy.tree.mount(root, config=conf)
- setup_server = staticmethod(setup_server)
-
-
- def testPublic(self):
- self.getPage("/")
- self.assertStatus('200 OK')
- self.assertHeader('Content-Type', 'text/html;charset=utf-8')
- self.assertBody('This is public.')
-
- def testBasic(self):
- self.getPage("/basic/")
- self.assertStatus(401)
- self.assertHeader('WWW-Authenticate', 'Basic realm="localhost"')
-
- self.getPage('/basic/', [('Authorization', 'Basic dGVzdDp0ZX60')])
- self.assertStatus(401)
-
- self.getPage('/basic/', [('Authorization', 'Basic dGVzdDp0ZXN0')])
- self.assertStatus('200 OK')
- self.assertBody("Hello test, you've been authorized.")
-
- def testBasic2(self):
- self.getPage("/basic2/")
- self.assertStatus(401)
- self.assertHeader('WWW-Authenticate', 'Basic realm="localhost"')
-
- self.getPage('/basic2/', [('Authorization', 'Basic dGVzdDp0ZX60')])
- self.assertStatus(401)
-
- self.getPage('/basic2/', [('Authorization', 'Basic dGVzdDp0ZXN0')])
- self.assertStatus('200 OK')
- self.assertBody("Hello test, you've been authorized.")
-
- def testDigest(self):
- self.getPage("/digest/")
- self.assertStatus(401)
-
- value = None
- for k, v in self.headers:
- if k.lower() == "www-authenticate":
- if v.startswith("Digest"):
- value = v
- break
-
- if value is None:
- self._handlewebError("Digest authentification scheme was not found")
-
- value = value[7:]
- items = value.split(', ')
- tokens = {}
- for item in items:
- key, value = item.split('=')
- tokens[key.lower()] = value
-
- missing_msg = "%s is missing"
- bad_value_msg = "'%s' was expecting '%s' but found '%s'"
- nonce = None
- if 'realm' not in tokens:
- self._handlewebError(missing_msg % 'realm')
- elif tokens['realm'] != '"localhost"':
- self._handlewebError(bad_value_msg % ('realm', '"localhost"', tokens['realm']))
- if 'nonce' not in tokens:
- self._handlewebError(missing_msg % 'nonce')
- else:
- nonce = tokens['nonce'].strip('"')
- if 'algorithm' not in tokens:
- self._handlewebError(missing_msg % 'algorithm')
- elif tokens['algorithm'] != '"MD5"':
- self._handlewebError(bad_value_msg % ('algorithm', '"MD5"', tokens['algorithm']))
- if 'qop' not in tokens:
- self._handlewebError(missing_msg % 'qop')
- elif tokens['qop'] != '"auth"':
- self._handlewebError(bad_value_msg % ('qop', '"auth"', tokens['qop']))
-
- # Test a wrong 'realm' value
- base_auth = 'Digest username="test", realm="wrong realm", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth = base_auth % (nonce, '', '00000001')
- params = httpauth.parseAuthorization(auth)
- response = httpauth._computeDigestResponse(params, 'test')
-
- auth = base_auth % (nonce, response, '00000001')
- self.getPage('/digest/', [('Authorization', auth)])
- self.assertStatus(401)
-
- # Test that must pass
- base_auth = 'Digest username="test", realm="localhost", nonce="%s", uri="/digest/", algorithm=MD5, response="%s", qop=auth, nc=%s, cnonce="1522e61005789929"'
-
- auth = base_auth % (nonce, '', '00000001')
- params = httpauth.parseAuthorization(auth)
- response = httpauth._computeDigestResponse(params, 'test')
-
- auth = base_auth % (nonce, response, '00000001')
- self.getPage('/digest/', [('Authorization', auth)])
- self.assertStatus('200 OK')
- self.assertBody("Hello test, you've been authorized.")
-
diff --git a/cherrypy/test/test_httplib.py b/cherrypy/test/test_httplib.py
deleted file mode 100644
index 5dc40fd2..00000000
--- a/cherrypy/test/test_httplib.py
+++ /dev/null
@@ -1,29 +0,0 @@
-"""Tests for cherrypy/lib/httputil.py."""
-
-import unittest
-from cherrypy.lib import httputil
-
-
-class UtilityTests(unittest.TestCase):
-
- def test_urljoin(self):
- # Test all slash+atom combinations for SCRIPT_NAME and PATH_INFO
- self.assertEqual(httputil.urljoin("/sn/", "/pi/"), "/sn/pi/")
- self.assertEqual(httputil.urljoin("/sn/", "/pi"), "/sn/pi")
- self.assertEqual(httputil.urljoin("/sn/", "/"), "/sn/")
- self.assertEqual(httputil.urljoin("/sn/", ""), "/sn/")
- self.assertEqual(httputil.urljoin("/sn", "/pi/"), "/sn/pi/")
- self.assertEqual(httputil.urljoin("/sn", "/pi"), "/sn/pi")
- self.assertEqual(httputil.urljoin("/sn", "/"), "/sn/")
- self.assertEqual(httputil.urljoin("/sn", ""), "/sn")
- self.assertEqual(httputil.urljoin("/", "/pi/"), "/pi/")
- self.assertEqual(httputil.urljoin("/", "/pi"), "/pi")
- self.assertEqual(httputil.urljoin("/", "/"), "/")
- self.assertEqual(httputil.urljoin("/", ""), "/")
- self.assertEqual(httputil.urljoin("", "/pi/"), "/pi/")
- self.assertEqual(httputil.urljoin("", "/pi"), "/pi")
- self.assertEqual(httputil.urljoin("", "/"), "/")
- self.assertEqual(httputil.urljoin("", ""), "/")
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/cherrypy/test/test_json.py b/cherrypy/test/test_json.py
deleted file mode 100644
index a02c0767..00000000
--- a/cherrypy/test/test_json.py
+++ /dev/null
@@ -1,79 +0,0 @@
-import cherrypy
-from cherrypy.test import helper
-
-from cherrypy._cpcompat import json
-
-class JsonTest(helper.CPWebCase):
- def setup_server():
- class Root(object):
- def plain(self):
- return 'hello'
- plain.exposed = True
-
- def json_string(self):
- return 'hello'
- json_string.exposed = True
- json_string._cp_config = {'tools.json_out.on': True}
-
- def json_list(self):
- return ['a', 'b', 42]
- json_list.exposed = True
- json_list._cp_config = {'tools.json_out.on': True}
-
- def json_dict(self):
- return {'answer': 42}
- json_dict.exposed = True
- json_dict._cp_config = {'tools.json_out.on': True}
-
- def json_post(self):
- if cherrypy.request.json == [13, 'c']:
- return 'ok'
- else:
- return 'nok'
- json_post.exposed = True
- json_post._cp_config = {'tools.json_in.on': True}
-
- root = Root()
- cherrypy.tree.mount(root)
- setup_server = staticmethod(setup_server)
-
- def test_json_output(self):
- if json is None:
- self.skip("json not found ")
- return
-
- self.getPage("/plain")
- self.assertBody("hello")
-
- self.getPage("/json_string")
- self.assertBody('"hello"')
-
- self.getPage("/json_list")
- self.assertBody('["a", "b", 42]')
-
- self.getPage("/json_dict")
- self.assertBody('{"answer": 42}')
-
- def test_json_input(self):
- if json is None:
- self.skip("json not found ")
- return
-
- body = '[13, "c"]'
- headers = [('Content-Type', 'application/json'),
- ('Content-Length', str(len(body)))]
- self.getPage("/json_post", method="POST", headers=headers, body=body)
- self.assertBody('ok')
-
- body = '[13, "c"]'
- headers = [('Content-Type', 'text/plain'),
- ('Content-Length', str(len(body)))]
- self.getPage("/json_post", method="POST", headers=headers, body=body)
- self.assertStatus(415, 'Expected an application/json content type')
-
- body = '[13, -]'
- headers = [('Content-Type', 'application/json'),
- ('Content-Length', str(len(body)))]
- self.getPage("/json_post", method="POST", headers=headers, body=body)
- self.assertStatus(400, 'Invalid JSON document')
-
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
deleted file mode 100644
index 5a13cd4a..00000000
--- a/cherrypy/test/test_logging.py
+++ /dev/null
@@ -1,149 +0,0 @@
-"""Basic tests for the CherryPy core: request handling."""
-
-import os
-localDir = os.path.dirname(__file__)
-
-import cherrypy
-
-access_log = os.path.join(localDir, "access.log")
-error_log = os.path.join(localDir, "error.log")
-
-# Some unicode strings.
-tartaros = u'\u03a4\u1f71\u03c1\u03c4\u03b1\u03c1\u03bf\u03c2'
-erebos = u'\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com'
-
-
-def setup_server():
- class Root:
-
- def index(self):
- return "hello"
- index.exposed = True
-
- def uni_code(self):
- cherrypy.request.login = tartaros
- cherrypy.request.remote.name = erebos
- uni_code.exposed = True
-
- def slashes(self):
- cherrypy.request.request_line = r'GET /slashed\path HTTP/1.1'
- slashes.exposed = True
-
- def whitespace(self):
- # User-Agent = "User-Agent" ":" 1*( product | comment )
- # comment = "(" *( ctext | quoted-pair | comment ) ")"
- # ctext =
- # TEXT =
- # LWS = [CRLF] 1*( SP | HT )
- cherrypy.request.headers['User-Agent'] = 'Browzuh (1.0\r\n\t\t.3)'
- whitespace.exposed = True
-
- def as_string(self):
- return "content"
- as_string.exposed = True
-
- def as_yield(self):
- yield "content"
- as_yield.exposed = True
-
- def error(self):
- raise ValueError()
- error.exposed = True
- error._cp_config = {'tools.log_tracebacks.on': True}
-
- root = Root()
-
-
- cherrypy.config.update({'log.error_file': error_log,
- 'log.access_file': access_log,
- })
- cherrypy.tree.mount(root)
-
-
-
-from cherrypy.test import helper, logtest
-
-class AccessLogTests(helper.CPWebCase, logtest.LogCase):
- setup_server = staticmethod(setup_server)
-
- logfile = access_log
-
- def testNormalReturn(self):
- self.markLog()
- self.getPage("/as_string",
- headers=[('Referer', 'http://www.cherrypy.org/'),
- ('User-Agent', 'Mozilla/5.0')])
- self.assertBody('content')
- self.assertStatus(200)
-
- intro = '%s - - [' % self.interface()
-
- self.assertLog(-1, intro)
-
- if [k for k, v in self.headers if k.lower() == 'content-length']:
- self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 7 '
- '"http://www.cherrypy.org/" "Mozilla/5.0"'
- % self.prefix())
- else:
- self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 - '
- '"http://www.cherrypy.org/" "Mozilla/5.0"'
- % self.prefix())
-
- def testNormalYield(self):
- self.markLog()
- self.getPage("/as_yield")
- self.assertBody('content')
- self.assertStatus(200)
-
- intro = '%s - - [' % self.interface()
-
- self.assertLog(-1, intro)
- if [k for k, v in self.headers if k.lower() == 'content-length']:
- self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 7 "" ""' %
- self.prefix())
- else:
- self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
- % self.prefix())
-
- def testEscapedOutput(self):
- # Test unicode in access log pieces.
- self.markLog()
- self.getPage("/uni_code")
- self.assertStatus(200)
- self.assertLog(-1, repr(tartaros.encode('utf8'))[1:-1])
- # Test the erebos value. Included inline for your enlightenment.
- # Note the 'r' prefix--those backslashes are literals.
- self.assertLog(-1, r'\xce\x88\xcf\x81\xce\xb5\xce\xb2\xce\xbf\xcf\x82')
-
- # Test backslashes in output.
- self.markLog()
- self.getPage("/slashes")
- self.assertStatus(200)
- self.assertLog(-1, r'"GET /slashed\\path HTTP/1.1"')
-
- # Test whitespace in output.
- self.markLog()
- self.getPage("/whitespace")
- self.assertStatus(200)
- # Again, note the 'r' prefix.
- self.assertLog(-1, r'"Browzuh (1.0\r\n\t\t.3)"')
-
-
-class ErrorLogTests(helper.CPWebCase, logtest.LogCase):
- setup_server = staticmethod(setup_server)
-
- logfile = error_log
-
- def testTracebacks(self):
- # Test that tracebacks get written to the error log.
- self.markLog()
- ignore = helper.webtest.ignored_exceptions
- ignore.append(ValueError)
- try:
- self.getPage("/error")
- self.assertInBody("raise ValueError()")
- self.assertLog(0, 'HTTP Traceback (most recent call last):')
- self.assertLog(-3, 'raise ValueError()')
- finally:
- ignore.pop()
-
diff --git a/cherrypy/test/test_mime.py b/cherrypy/test/test_mime.py
deleted file mode 100644
index 605071b8..00000000
--- a/cherrypy/test/test_mime.py
+++ /dev/null
@@ -1,128 +0,0 @@
-"""Tests for various MIME issues, including the safe_multipart Tool."""
-
-import cherrypy
-from cherrypy._cpcompat import ntob, ntou, sorted
-
-def setup_server():
-
- class Root:
-
- def multipart(self, parts):
- return repr(parts)
- multipart.exposed = True
-
- def multipart_form_data(self, **kwargs):
- return repr(list(sorted(kwargs.items())))
- multipart_form_data.exposed = True
-
- def flashupload(self, Filedata, Upload, Filename):
- return ("Upload: %r, Filename: %r, Filedata: %r" %
- (Upload, Filename, Filedata.file.read()))
- flashupload.exposed = True
-
- cherrypy.config.update({'server.max_request_body_size': 0})
- cherrypy.tree.mount(Root())
-
-
-# Client-side code #
-
-from cherrypy.test import helper
-
-class MultipartTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_multipart(self):
- text_part = ntou("This is the text version")
- html_part = ntou("""
-
-
-
-
-
-
-This is the HTML version
-
-
-""")
- body = '\r\n'.join([
- "--123456789",
- "Content-Type: text/plain; charset='ISO-8859-1'",
- "Content-Transfer-Encoding: 7bit",
- "",
- text_part,
- "--123456789",
- "Content-Type: text/html; charset='ISO-8859-1'",
- "",
- html_part,
- "--123456789--"])
- headers = [
- ('Content-Type', 'multipart/mixed; boundary=123456789'),
- ('Content-Length', str(len(body))),
- ]
- self.getPage('/multipart', headers, "POST", body)
- self.assertBody(repr([text_part, html_part]))
-
- def test_multipart_form_data(self):
- body='\r\n'.join(['--X',
- 'Content-Disposition: form-data; name="foo"',
- '',
- 'bar',
- '--X',
- # Test a param with more than one value.
- # See http://www.cherrypy.org/ticket/1028
- 'Content-Disposition: form-data; name="baz"',
- '',
- '111',
- '--X',
- 'Content-Disposition: form-data; name="baz"',
- '',
- '333',
- '--X--'])
- self.getPage('/multipart_form_data', method='POST',
- headers=[("Content-Type", "multipart/form-data;boundary=X"),
- ("Content-Length", str(len(body))),
- ],
- body=body),
- self.assertBody(repr([('baz', [u'111', u'333']), ('foo', u'bar')]))
-
-
-class SafeMultipartHandlingTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_Flash_Upload(self):
- headers = [
- ('Accept', 'text/*'),
- ('Content-Type', 'multipart/form-data; '
- 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'),
- ('User-Agent', 'Shockwave Flash'),
- ('Host', 'www.example.com:8080'),
- ('Content-Length', '499'),
- ('Connection', 'Keep-Alive'),
- ('Cache-Control', 'no-cache'),
- ]
- filedata = ntob('\r\n'
- '\r\n'
- '\r\n')
- body = (ntob(
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
- 'Content-Disposition: form-data; name="Filename"\r\n'
- '\r\n'
- '.project\r\n'
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
- 'Content-Disposition: form-data; '
- 'name="Filedata"; filename=".project"\r\n'
- 'Content-Type: application/octet-stream\r\n'
- '\r\n')
- + filedata +
- ntob('\r\n'
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
- 'Content-Disposition: form-data; name="Upload"\r\n'
- '\r\n'
- 'Submit Query\r\n'
- # Flash apps omit the trailing \r\n on the last line:
- '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--'
- ))
- self.getPage('/flashupload', headers, "POST", body)
- self.assertBody("Upload: u'Submit Query', Filename: u'.project', "
- "Filedata: %r" % filedata)
-
diff --git a/cherrypy/test/test_misc_tools.py b/cherrypy/test/test_misc_tools.py
deleted file mode 100644
index fb94e860..00000000
--- a/cherrypy/test/test_misc_tools.py
+++ /dev/null
@@ -1,202 +0,0 @@
-import os
-localDir = os.path.dirname(__file__)
-logfile = os.path.join(localDir, "test_misc_tools.log")
-
-import cherrypy
-from cherrypy import tools
-
-
-def setup_server():
- class Root:
- def index(self):
- yield "Hello, world"
- index.exposed = True
- h = [("Content-Language", "en-GB"), ('Content-Type', 'text/plain')]
- tools.response_headers(headers=h)(index)
-
- def other(self):
- return "salut"
- other.exposed = True
- other._cp_config = {
- 'tools.response_headers.on': True,
- 'tools.response_headers.headers': [("Content-Language", "fr"),
- ('Content-Type', 'text/plain')],
- 'tools.log_hooks.on': True,
- }
-
-
- class Accept:
- _cp_config = {'tools.accept.on': True}
-
- def index(self):
- return 'Atom feed'
- index.exposed = True
-
- # In Python 2.4+, we could use a decorator instead:
- # @tools.accept('application/atom+xml')
- def feed(self):
- return """
-
- Unknown Blog
-"""
- feed.exposed = True
- feed._cp_config = {'tools.accept.media': 'application/atom+xml'}
-
- def select(self):
- # We could also write this: mtype = cherrypy.lib.accept.accept(...)
- mtype = tools.accept.callable(['text/html', 'text/plain'])
- if mtype == 'text/html':
- return "Page Title
"
- else:
- return "PAGE TITLE"
- select.exposed = True
-
- class Referer:
- def accept(self):
- return "Accepted!"
- accept.exposed = True
- reject = accept
-
- class AutoVary:
- def index(self):
- # Read a header directly with 'get'
- ae = cherrypy.request.headers.get('Accept-Encoding')
- # Read a header directly with '__getitem__'
- cl = cherrypy.request.headers['Host']
- # Read a header directly with '__contains__'
- hasif = 'If-Modified-Since' in cherrypy.request.headers
- # Read a header directly with 'has_key'
- has = cherrypy.request.headers.has_key('Range')
- # Call a lib function
- mtype = tools.accept.callable(['text/html', 'text/plain'])
- return "Hello, world!"
- index.exposed = True
-
- conf = {'/referer': {'tools.referer.on': True,
- 'tools.referer.pattern': r'http://[^/]*example\.com',
- },
- '/referer/reject': {'tools.referer.accept': False,
- 'tools.referer.accept_missing': True,
- },
- '/autovary': {'tools.autovary.on': True},
- }
-
- root = Root()
- root.referer = Referer()
- root.accept = Accept()
- root.autovary = AutoVary()
- cherrypy.tree.mount(root, config=conf)
- cherrypy.config.update({'log.error_file': logfile})
-
-
-from cherrypy.test import helper
-
-class ResponseHeadersTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def testResponseHeadersDecorator(self):
- self.getPage('/')
- self.assertHeader("Content-Language", "en-GB")
- self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
-
- def testResponseHeaders(self):
- self.getPage('/other')
- self.assertHeader("Content-Language", "fr")
- self.assertHeader('Content-Type', 'text/plain;charset=utf-8')
-
-
-class RefererTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def testReferer(self):
- self.getPage('/referer/accept')
- self.assertErrorPage(403, 'Forbidden Referer header.')
-
- self.getPage('/referer/accept',
- headers=[('Referer', 'http://www.example.com/')])
- self.assertStatus(200)
- self.assertBody('Accepted!')
-
- # Reject
- self.getPage('/referer/reject')
- self.assertStatus(200)
- self.assertBody('Accepted!')
-
- self.getPage('/referer/reject',
- headers=[('Referer', 'http://www.example.com/')])
- self.assertErrorPage(403, 'Forbidden Referer header.')
-
-
-class AcceptTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_Accept_Tool(self):
- # Test with no header provided
- self.getPage('/accept/feed')
- self.assertStatus(200)
- self.assertInBody('Unknown Blog')
-
- # Specify exact media type
- self.getPage('/accept/feed', headers=[('Accept', 'application/atom+xml')])
- self.assertStatus(200)
- self.assertInBody('Unknown Blog')
-
- # Specify matching media range
- self.getPage('/accept/feed', headers=[('Accept', 'application/*')])
- self.assertStatus(200)
- self.assertInBody('Unknown Blog')
-
- # Specify all media ranges
- self.getPage('/accept/feed', headers=[('Accept', '*/*')])
- self.assertStatus(200)
- self.assertInBody('Unknown Blog')
-
- # Specify unacceptable media types
- self.getPage('/accept/feed', headers=[('Accept', 'text/html')])
- self.assertErrorPage(406,
- "Your client sent this Accept header: text/html. "
- "But this resource only emits these media types: "
- "application/atom+xml.")
-
- # Test resource where tool is 'on' but media is None (not set).
- self.getPage('/accept/')
- self.assertStatus(200)
- self.assertBody('Atom feed')
-
- def test_accept_selection(self):
- # Try both our expected media types
- self.getPage('/accept/select', [('Accept', 'text/html')])
- self.assertStatus(200)
- self.assertBody('Page Title
')
- self.getPage('/accept/select', [('Accept', 'text/plain')])
- self.assertStatus(200)
- self.assertBody('PAGE TITLE')
- self.getPage('/accept/select', [('Accept', 'text/plain, text/*;q=0.5')])
- self.assertStatus(200)
- self.assertBody('PAGE TITLE')
-
- # text/* and */* should prefer text/html since it comes first
- # in our 'media' argument to tools.accept
- self.getPage('/accept/select', [('Accept', 'text/*')])
- self.assertStatus(200)
- self.assertBody('Page Title
')
- self.getPage('/accept/select', [('Accept', '*/*')])
- self.assertStatus(200)
- self.assertBody('Page Title
')
-
- # Try unacceptable media types
- self.getPage('/accept/select', [('Accept', 'application/xml')])
- self.assertErrorPage(406,
- "Your client sent this Accept header: application/xml. "
- "But this resource only emits these media types: "
- "text/html, text/plain.")
-
-
-class AutoVaryTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def testAutoVary(self):
- self.getPage('/autovary/')
- self.assertHeader(
- "Vary", 'Accept, Accept-Charset, Accept-Encoding, Host, If-Modified-Since, Range')
-
diff --git a/cherrypy/test/test_objectmapping.py b/cherrypy/test/test_objectmapping.py
deleted file mode 100644
index 46816fcb..00000000
--- a/cherrypy/test/test_objectmapping.py
+++ /dev/null
@@ -1,403 +0,0 @@
-import cherrypy
-from cherrypy._cptree import Application
-from cherrypy.test import helper
-
-script_names = ["", "/foo", "/users/fred/blog", "/corp/blog"]
-
-
-class ObjectMappingTest(helper.CPWebCase):
-
- def setup_server():
- class Root:
- def index(self, name="world"):
- return name
- index.exposed = True
-
- def foobar(self):
- return "bar"
- foobar.exposed = True
-
- def default(self, *params, **kwargs):
- return "default:" + repr(params)
- default.exposed = True
-
- def other(self):
- return "other"
- other.exposed = True
-
- def extra(self, *p):
- return repr(p)
- extra.exposed = True
-
- def redirect(self):
- raise cherrypy.HTTPRedirect('dir1/', 302)
- redirect.exposed = True
-
- def notExposed(self):
- return "not exposed"
-
- def confvalue(self):
- return cherrypy.request.config.get("user")
- confvalue.exposed = True
-
- def redirect_via_url(self, path):
- raise cherrypy.HTTPRedirect(cherrypy.url(path))
- redirect_via_url.exposed = True
-
- def translate_html(self):
- return "OK"
- translate_html.exposed = True
-
- def mapped_func(self, ID=None):
- return "ID is %s" % ID
- mapped_func.exposed = True
- setattr(Root, "Von B\xfclow", mapped_func)
-
-
- class Exposing:
- def base(self):
- return "expose works!"
- cherrypy.expose(base)
- cherrypy.expose(base, "1")
- cherrypy.expose(base, "2")
-
- class ExposingNewStyle(object):
- def base(self):
- return "expose works!"
- cherrypy.expose(base)
- cherrypy.expose(base, "1")
- cherrypy.expose(base, "2")
-
-
- class Dir1:
- def index(self):
- return "index for dir1"
- index.exposed = True
-
- def myMethod(self):
- return "myMethod from dir1, path_info is:" + repr(cherrypy.request.path_info)
- myMethod.exposed = True
- myMethod._cp_config = {'tools.trailing_slash.extra': True}
-
- def default(self, *params):
- return "default for dir1, param is:" + repr(params)
- default.exposed = True
-
-
- class Dir2:
- def index(self):
- return "index for dir2, path is:" + cherrypy.request.path_info
- index.exposed = True
-
- def script_name(self):
- return cherrypy.tree.script_name()
- script_name.exposed = True
-
- def cherrypy_url(self):
- return cherrypy.url("/extra")
- cherrypy_url.exposed = True
-
- def posparam(self, *vpath):
- return "/".join(vpath)
- posparam.exposed = True
-
-
- class Dir3:
- def default(self):
- return "default for dir3, not exposed"
-
- class Dir4:
- def index(self):
- return "index for dir4, not exposed"
-
- class DefNoIndex:
- def default(self, *args):
- raise cherrypy.HTTPRedirect("contact")
- default.exposed = True
-
- # MethodDispatcher code
- class ByMethod:
- exposed = True
-
- def __init__(self, *things):
- self.things = list(things)
-
- def GET(self):
- return repr(self.things)
-
- def POST(self, thing):
- self.things.append(thing)
-
- class Collection:
- default = ByMethod('a', 'bit')
-
- Root.exposing = Exposing()
- Root.exposingnew = ExposingNewStyle()
- Root.dir1 = Dir1()
- Root.dir1.dir2 = Dir2()
- Root.dir1.dir2.dir3 = Dir3()
- Root.dir1.dir2.dir3.dir4 = Dir4()
- Root.defnoindex = DefNoIndex()
- Root.bymethod = ByMethod('another')
- Root.collection = Collection()
-
- d = cherrypy.dispatch.MethodDispatcher()
- for url in script_names:
- conf = {'/': {'user': (url or "/").split("/")[-2]},
- '/bymethod': {'request.dispatch': d},
- '/collection': {'request.dispatch': d},
- }
- cherrypy.tree.mount(Root(), url, conf)
-
-
- class Isolated:
- def index(self):
- return "made it!"
- index.exposed = True
-
- cherrypy.tree.mount(Isolated(), "/isolated")
-
- class AnotherApp:
-
- exposed = True
-
- def GET(self):
- return "milk"
-
- cherrypy.tree.mount(AnotherApp(), "/app", {'/': {'request.dispatch': d}})
- setup_server = staticmethod(setup_server)
-
-
- def testObjectMapping(self):
- for url in script_names:
- prefix = self.script_name = url
-
- self.getPage('/')
- self.assertBody('world')
-
- self.getPage("/dir1/myMethod")
- self.assertBody("myMethod from dir1, path_info is:'/dir1/myMethod'")
-
- self.getPage("/this/method/does/not/exist")
- self.assertBody("default:('this', 'method', 'does', 'not', 'exist')")
-
- self.getPage("/extra/too/much")
- self.assertBody("('too', 'much')")
-
- self.getPage("/other")
- self.assertBody('other')
-
- self.getPage("/notExposed")
- self.assertBody("default:('notExposed',)")
-
- self.getPage("/dir1/dir2/")
- self.assertBody('index for dir2, path is:/dir1/dir2/')
-
- # Test omitted trailing slash (should be redirected by default).
- self.getPage("/dir1/dir2")
- self.assertStatus(301)
- self.assertHeader('Location', '%s/dir1/dir2/' % self.base())
-
- # Test extra trailing slash (should be redirected if configured).
- self.getPage("/dir1/myMethod/")
- self.assertStatus(301)
- self.assertHeader('Location', '%s/dir1/myMethod' % self.base())
-
- # Test that default method must be exposed in order to match.
- self.getPage("/dir1/dir2/dir3/dir4/index")
- self.assertBody("default for dir1, param is:('dir2', 'dir3', 'dir4', 'index')")
-
- # Test *vpath when default() is defined but not index()
- # This also tests HTTPRedirect with default.
- self.getPage("/defnoindex")
- self.assertStatus((302, 303))
- self.assertHeader('Location', '%s/contact' % self.base())
- self.getPage("/defnoindex/")
- self.assertStatus((302, 303))
- self.assertHeader('Location', '%s/defnoindex/contact' % self.base())
- self.getPage("/defnoindex/page")
- self.assertStatus((302, 303))
- self.assertHeader('Location', '%s/defnoindex/contact' % self.base())
-
- self.getPage("/redirect")
- self.assertStatus('302 Found')
- self.assertHeader('Location', '%s/dir1/' % self.base())
-
- if not getattr(cherrypy.server, "using_apache", False):
- # Test that we can use URL's which aren't all valid Python identifiers
- # This should also test the %XX-unquoting of URL's.
- self.getPage("/Von%20B%fclow?ID=14")
- self.assertBody("ID is 14")
-
- # Test that %2F in the path doesn't get unquoted too early;
- # that is, it should not be used to separate path components.
- # See ticket #393.
- self.getPage("/page%2Fname")
- self.assertBody("default:('page/name',)")
-
- self.getPage("/dir1/dir2/script_name")
- self.assertBody(url)
- self.getPage("/dir1/dir2/cherrypy_url")
- self.assertBody("%s/extra" % self.base())
-
- # Test that configs don't overwrite each other from diferent apps
- self.getPage("/confvalue")
- self.assertBody((url or "/").split("/")[-2])
-
- self.script_name = ""
-
- # Test absoluteURI's in the Request-Line
- self.getPage('http://%s:%s/' % (self.interface(), self.PORT))
- self.assertBody('world')
-
- self.getPage('http://%s:%s/abs/?service=http://192.168.0.1/x/y/z' %
- (self.interface(), self.PORT))
- self.assertBody("default:('abs',)")
-
- self.getPage('/rel/?service=http://192.168.120.121:8000/x/y/z')
- self.assertBody("default:('rel',)")
-
- # Test that the "isolated" app doesn't leak url's into the root app.
- # If it did leak, Root.default() would answer with
- # "default:('isolated', 'doesnt', 'exist')".
- self.getPage("/isolated/")
- self.assertStatus("200 OK")
- self.assertBody("made it!")
- self.getPage("/isolated/doesnt/exist")
- self.assertStatus("404 Not Found")
-
- # Make sure /foobar maps to Root.foobar and not to the app
- # mounted at /foo. See http://www.cherrypy.org/ticket/573
- self.getPage("/foobar")
- self.assertBody("bar")
-
- def test_translate(self):
- self.getPage("/translate_html")
- self.assertStatus("200 OK")
- self.assertBody("OK")
-
- self.getPage("/translate.html")
- self.assertStatus("200 OK")
- self.assertBody("OK")
-
- self.getPage("/translate-html")
- self.assertStatus("200 OK")
- self.assertBody("OK")
-
- def test_redir_using_url(self):
- for url in script_names:
- prefix = self.script_name = url
-
- # Test the absolute path to the parent (leading slash)
- self.getPage('/redirect_via_url?path=./')
- self.assertStatus(('302 Found', '303 See Other'))
- self.assertHeader('Location', '%s/' % self.base())
-
- # Test the relative path to the parent (no leading slash)
- self.getPage('/redirect_via_url?path=./')
- self.assertStatus(('302 Found', '303 See Other'))
- self.assertHeader('Location', '%s/' % self.base())
-
- # Test the absolute path to the parent (leading slash)
- self.getPage('/redirect_via_url/?path=./')
- self.assertStatus(('302 Found', '303 See Other'))
- self.assertHeader('Location', '%s/' % self.base())
-
- # Test the relative path to the parent (no leading slash)
- self.getPage('/redirect_via_url/?path=./')
- self.assertStatus(('302 Found', '303 See Other'))
- self.assertHeader('Location', '%s/' % self.base())
-
- def testPositionalParams(self):
- self.getPage("/dir1/dir2/posparam/18/24/hut/hike")
- self.assertBody("18/24/hut/hike")
-
- # intermediate index methods should not receive posparams;
- # only the "final" index method should do so.
- self.getPage("/dir1/dir2/5/3/sir")
- self.assertBody("default for dir1, param is:('dir2', '5', '3', 'sir')")
-
- # test that extra positional args raises an 404 Not Found
- # See http://www.cherrypy.org/ticket/733.
- self.getPage("/dir1/dir2/script_name/extra/stuff")
- self.assertStatus(404)
-
- def testExpose(self):
- # Test the cherrypy.expose function/decorator
- self.getPage("/exposing/base")
- self.assertBody("expose works!")
-
- self.getPage("/exposing/1")
- self.assertBody("expose works!")
-
- self.getPage("/exposing/2")
- self.assertBody("expose works!")
-
- self.getPage("/exposingnew/base")
- self.assertBody("expose works!")
-
- self.getPage("/exposingnew/1")
- self.assertBody("expose works!")
-
- self.getPage("/exposingnew/2")
- self.assertBody("expose works!")
-
- def testMethodDispatch(self):
- self.getPage("/bymethod")
- self.assertBody("['another']")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- self.getPage("/bymethod", method="HEAD")
- self.assertBody("")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- self.getPage("/bymethod", method="POST", body="thing=one")
- self.assertBody("")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- self.getPage("/bymethod")
- self.assertBody("['another', u'one']")
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- self.getPage("/bymethod", method="PUT")
- self.assertErrorPage(405)
- self.assertHeader('Allow', 'GET, HEAD, POST')
-
- # Test default with posparams
- self.getPage("/collection/silly", method="POST")
- self.getPage("/collection", method="GET")
- self.assertBody("['a', 'bit', 'silly']")
-
- # Test custom dispatcher set on app root (see #737).
- self.getPage("/app")
- self.assertBody("milk")
-
- def testTreeMounting(self):
- class Root(object):
- def hello(self):
- return "Hello world!"
- hello.exposed = True
-
- # When mounting an application instance,
- # we can't specify a different script name in the call to mount.
- a = Application(Root(), '/somewhere')
- self.assertRaises(ValueError, cherrypy.tree.mount, a, '/somewhereelse')
-
- # When mounting an application instance...
- a = Application(Root(), '/somewhere')
- # ...we MUST allow in identical script name in the call to mount...
- cherrypy.tree.mount(a, '/somewhere')
- self.getPage('/somewhere/hello')
- self.assertStatus(200)
- # ...and MUST allow a missing script_name.
- del cherrypy.tree.apps['/somewhere']
- cherrypy.tree.mount(a)
- self.getPage('/somewhere/hello')
- self.assertStatus(200)
-
- # In addition, we MUST be able to create an Application using
- # script_name == None for access to the wsgi_environ.
- a = Application(Root(), script_name=None)
- # However, this does not apply to tree.mount
- self.assertRaises(TypeError, cherrypy.tree.mount, a, None)
-
diff --git a/cherrypy/test/test_proxy.py b/cherrypy/test/test_proxy.py
deleted file mode 100644
index 2fbb619a..00000000
--- a/cherrypy/test/test_proxy.py
+++ /dev/null
@@ -1,129 +0,0 @@
-import cherrypy
-from cherrypy.test import helper
-
-script_names = ["", "/path/to/myapp"]
-
-
-class ProxyTest(helper.CPWebCase):
-
- def setup_server():
-
- # Set up site
- cherrypy.config.update({
- 'tools.proxy.on': True,
- 'tools.proxy.base': 'www.mydomain.test',
- })
-
- # Set up application
-
- class Root:
-
- def __init__(self, sn):
- # Calculate a URL outside of any requests.
- self.thisnewpage = cherrypy.url("/this/new/page", script_name=sn)
-
- def pageurl(self):
- return self.thisnewpage
- pageurl.exposed = True
-
- def index(self):
- raise cherrypy.HTTPRedirect('dummy')
- index.exposed = True
-
- def remoteip(self):
- return cherrypy.request.remote.ip
- remoteip.exposed = True
-
- def xhost(self):
- raise cherrypy.HTTPRedirect('blah')
- xhost.exposed = True
- xhost._cp_config = {'tools.proxy.local': 'X-Host',
- 'tools.trailing_slash.extra': True,
- }
-
- def base(self):
- return cherrypy.request.base
- base.exposed = True
-
- def ssl(self):
- return cherrypy.request.base
- ssl.exposed = True
- ssl._cp_config = {'tools.proxy.scheme': 'X-Forwarded-Ssl'}
-
- def newurl(self):
- return ("Browse to this page."
- % cherrypy.url("/this/new/page"))
- newurl.exposed = True
-
- for sn in script_names:
- cherrypy.tree.mount(Root(sn), sn)
- setup_server = staticmethod(setup_server)
-
- def testProxy(self):
- self.getPage("/")
- self.assertHeader('Location',
- "%s://www.mydomain.test%s/dummy" %
- (self.scheme, self.prefix()))
-
- # Test X-Forwarded-Host (Apache 1.3.33+ and Apache 2)
- self.getPage("/", headers=[('X-Forwarded-Host', 'http://www.example.test')])
- self.assertHeader('Location', "http://www.example.test/dummy")
- self.getPage("/", headers=[('X-Forwarded-Host', 'www.example.test')])
- self.assertHeader('Location', "%s://www.example.test/dummy" % self.scheme)
- # Test multiple X-Forwarded-Host headers
- self.getPage("/", headers=[
- ('X-Forwarded-Host', 'http://www.example.test, www.cherrypy.test'),
- ])
- self.assertHeader('Location', "http://www.example.test/dummy")
-
- # Test X-Forwarded-For (Apache2)
- self.getPage("/remoteip",
- headers=[('X-Forwarded-For', '192.168.0.20')])
- self.assertBody("192.168.0.20")
- self.getPage("/remoteip",
- headers=[('X-Forwarded-For', '67.15.36.43, 192.168.0.20')])
- self.assertBody("192.168.0.20")
-
- # Test X-Host (lighttpd; see https://trac.lighttpd.net/trac/ticket/418)
- self.getPage("/xhost", headers=[('X-Host', 'www.example.test')])
- self.assertHeader('Location', "%s://www.example.test/blah" % self.scheme)
-
- # Test X-Forwarded-Proto (lighttpd)
- self.getPage("/base", headers=[('X-Forwarded-Proto', 'https')])
- self.assertBody("https://www.mydomain.test")
-
- # Test X-Forwarded-Ssl (webfaction?)
- self.getPage("/ssl", headers=[('X-Forwarded-Ssl', 'on')])
- self.assertBody("https://www.mydomain.test")
-
- # Test cherrypy.url()
- for sn in script_names:
- # Test the value inside requests
- self.getPage(sn + "/newurl")
- self.assertBody("Browse to this page.")
- self.getPage(sn + "/newurl", headers=[('X-Forwarded-Host',
- 'http://www.example.test')])
- self.assertBody("Browse to this page.")
-
- # Test the value outside requests
- port = ""
- if self.scheme == "http" and self.PORT != 80:
- port = ":%s" % self.PORT
- elif self.scheme == "https" and self.PORT != 443:
- port = ":%s" % self.PORT
- host = self.HOST
- if host in ('0.0.0.0', '::'):
- import socket
- host = socket.gethostname()
- expected = ("%s://%s%s%s/this/new/page"
- % (self.scheme, host, port, sn))
- self.getPage(sn + "/pageurl")
- self.assertBody(expected)
-
- # Test trailing slash (see http://www.cherrypy.org/ticket/562).
- self.getPage("/xhost/", headers=[('X-Host', 'www.example.test')])
- self.assertHeader('Location', "%s://www.example.test/xhost"
- % self.scheme)
-
diff --git a/cherrypy/test/test_refleaks.py b/cherrypy/test/test_refleaks.py
deleted file mode 100644
index 4df1f082..00000000
--- a/cherrypy/test/test_refleaks.py
+++ /dev/null
@@ -1,119 +0,0 @@
-"""Tests for refleaks."""
-
-import gc
-from cherrypy._cpcompat import HTTPConnection, HTTPSConnection, ntob
-import threading
-
-import cherrypy
-from cherrypy import _cprequest
-
-
-data = object()
-
-def get_instances(cls):
- return [x for x in gc.get_objects() if isinstance(x, cls)]
-
-
-from cherrypy.test import helper
-
-
-class ReferenceTests(helper.CPWebCase):
-
- def setup_server():
-
- class Root:
- def index(self, *args, **kwargs):
- cherrypy.request.thing = data
- return "Hello world!"
- index.exposed = True
-
- def gc_stats(self):
- output = ["Statistics:"]
-
- # Uncollectable garbage
-
- # gc_collect isn't perfectly synchronous, because it may
- # break reference cycles that then take time to fully
- # finalize. Call it twice and hope for the best.
- gc.collect()
- unreachable = gc.collect()
- if unreachable:
- output.append("\n%s unreachable objects:" % unreachable)
- trash = {}
- for x in gc.garbage:
- trash[type(x)] = trash.get(type(x), 0) + 1
- trash = [(v, k) for k, v in trash.items()]
- trash.sort()
- for pair in trash:
- output.append(" " + repr(pair))
-
- # Request references
- reqs = get_instances(_cprequest.Request)
- lenreqs = len(reqs)
- if lenreqs < 2:
- output.append("\nMissing Request reference. Should be 1 in "
- "this request thread and 1 in the main thread.")
- elif lenreqs > 2:
- output.append("\nToo many Request references (%r)." % lenreqs)
- for req in reqs:
- output.append("Referrers for %s:" % repr(req))
- for ref in gc.get_referrers(req):
- if ref is not reqs:
- output.append(" %s" % repr(ref))
-
- # Response references
- resps = get_instances(_cprequest.Response)
- lenresps = len(resps)
- if lenresps < 2:
- output.append("\nMissing Response reference. Should be 1 in "
- "this request thread and 1 in the main thread.")
- elif lenresps > 2:
- output.append("\nToo many Response references (%r)." % lenresps)
- for resp in resps:
- output.append("Referrers for %s:" % repr(resp))
- for ref in gc.get_referrers(resp):
- if ref is not resps:
- output.append(" %s" % repr(ref))
-
- return "\n".join(output)
- gc_stats.exposed = True
-
- cherrypy.tree.mount(Root())
- setup_server = staticmethod(setup_server)
-
-
- def test_threadlocal_garbage(self):
- success = []
-
- def getpage():
- host = '%s:%s' % (self.interface(), self.PORT)
- if self.scheme == 'https':
- c = HTTPSConnection(host)
- else:
- c = HTTPConnection(host)
- try:
- c.putrequest('GET', '/')
- c.endheaders()
- response = c.getresponse()
- body = response.read()
- self.assertEqual(response.status, 200)
- self.assertEqual(body, ntob("Hello world!"))
- finally:
- c.close()
- success.append(True)
-
- ITERATIONS = 25
- ts = []
- for _ in range(ITERATIONS):
- t = threading.Thread(target=getpage)
- ts.append(t)
- t.start()
-
- for t in ts:
- t.join()
-
- self.assertEqual(len(success), ITERATIONS)
-
- self.getPage("/gc_stats")
- self.assertBody("Statistics:")
-
diff --git a/cherrypy/test/test_request_obj.py b/cherrypy/test/test_request_obj.py
deleted file mode 100644
index 91ee4fd0..00000000
--- a/cherrypy/test/test_request_obj.py
+++ /dev/null
@@ -1,722 +0,0 @@
-"""Basic tests for the cherrypy.Request object."""
-
-import os
-localDir = os.path.dirname(__file__)
-import sys
-import types
-from cherrypy._cpcompat import IncompleteRead, ntob, unicodestr
-
-import cherrypy
-from cherrypy import _cptools, tools
-from cherrypy.lib import httputil
-
-defined_http_methods = ("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE",
- "TRACE", "PROPFIND")
-
-
-# Client-side code #
-
-from cherrypy.test import helper
-
-class RequestObjectTests(helper.CPWebCase):
-
- def setup_server():
- class Root:
-
- def index(self):
- return "hello"
- index.exposed = True
-
- def scheme(self):
- return cherrypy.request.scheme
- scheme.exposed = True
-
- root = Root()
-
-
- class TestType(type):
- """Metaclass which automatically exposes all functions in each subclass,
- and adds an instance of the subclass as an attribute of root.
- """
- def __init__(cls, name, bases, dct):
- type.__init__(cls, name, bases, dct)
- for value in dct.values():
- if isinstance(value, types.FunctionType):
- value.exposed = True
- setattr(root, name.lower(), cls())
- class Test(object):
- __metaclass__ = TestType
-
-
- class Params(Test):
-
- def index(self, thing):
- return repr(thing)
-
- def ismap(self, x, y):
- return "Coordinates: %s, %s" % (x, y)
-
- def default(self, *args, **kwargs):
- return "args: %s kwargs: %s" % (args, kwargs)
- default._cp_config = {'request.query_string_encoding': 'latin1'}
-
-
- class ParamErrorsCallable(object):
- exposed = True
- def __call__(self):
- return "data"
-
- class ParamErrors(Test):
-
- def one_positional(self, param1):
- return "data"
- one_positional.exposed = True
-
- def one_positional_args(self, param1, *args):
- return "data"
- one_positional_args.exposed = True
-
- def one_positional_args_kwargs(self, param1, *args, **kwargs):
- return "data"
- one_positional_args_kwargs.exposed = True
-
- def one_positional_kwargs(self, param1, **kwargs):
- return "data"
- one_positional_kwargs.exposed = True
-
- def no_positional(self):
- return "data"
- no_positional.exposed = True
-
- def no_positional_args(self, *args):
- return "data"
- no_positional_args.exposed = True
-
- def no_positional_args_kwargs(self, *args, **kwargs):
- return "data"
- no_positional_args_kwargs.exposed = True
-
- def no_positional_kwargs(self, **kwargs):
- return "data"
- no_positional_kwargs.exposed = True
-
- callable_object = ParamErrorsCallable()
-
- def raise_type_error(self, **kwargs):
- raise TypeError("Client Error")
- raise_type_error.exposed = True
-
- def raise_type_error_with_default_param(self, x, y=None):
- return '%d' % 'a' # throw an exception
- raise_type_error_with_default_param.exposed = True
-
- def callable_error_page(status, **kwargs):
- return "Error %s - Well, I'm very sorry but you haven't paid!" % status
-
-
- class Error(Test):
-
- _cp_config = {'tools.log_tracebacks.on': True,
- }
-
- def reason_phrase(self):
- raise cherrypy.HTTPError("410 Gone fishin'")
-
- def custom(self, err='404'):
- raise cherrypy.HTTPError(int(err), "No, really, not found!")
- custom._cp_config = {'error_page.404': os.path.join(localDir, "static/index.html"),
- 'error_page.401': callable_error_page,
- }
-
- def custom_default(self):
- return 1 + 'a' # raise an unexpected error
- custom_default._cp_config = {'error_page.default': callable_error_page}
-
- def noexist(self):
- raise cherrypy.HTTPError(404, "No, really, not found!")
- noexist._cp_config = {'error_page.404': "nonexistent.html"}
-
- def page_method(self):
- raise ValueError()
-
- def page_yield(self):
- yield "howdy"
- raise ValueError()
-
- def page_streamed(self):
- yield "word up"
- raise ValueError()
- yield "very oops"
- page_streamed._cp_config = {"response.stream": True}
-
- def cause_err_in_finalize(self):
- # Since status must start with an int, this should error.
- cherrypy.response.status = "ZOO OK"
- cause_err_in_finalize._cp_config = {'request.show_tracebacks': False}
-
- def rethrow(self):
- """Test that an error raised here will be thrown out to the server."""
- raise ValueError()
- rethrow._cp_config = {'request.throw_errors': True}
-
-
- class Expect(Test):
-
- def expectation_failed(self):
- expect = cherrypy.request.headers.elements("Expect")
- if expect and expect[0].value != '100-continue':
- raise cherrypy.HTTPError(400)
- raise cherrypy.HTTPError(417, 'Expectation Failed')
-
- class Headers(Test):
-
- def default(self, headername):
- """Spit back out the value for the requested header."""
- return cherrypy.request.headers[headername]
-
- def doubledheaders(self):
- # From http://www.cherrypy.org/ticket/165:
- # "header field names should not be case sensitive sayes the rfc.
- # if i set a headerfield in complete lowercase i end up with two
- # header fields, one in lowercase, the other in mixed-case."
-
- # Set the most common headers
- hMap = cherrypy.response.headers
- hMap['content-type'] = "text/html"
- hMap['content-length'] = 18
- hMap['server'] = 'CherryPy headertest'
- hMap['location'] = ('%s://%s:%s/headers/'
- % (cherrypy.request.local.ip,
- cherrypy.request.local.port,
- cherrypy.request.scheme))
-
- # Set a rare header for fun
- hMap['Expires'] = 'Thu, 01 Dec 2194 16:00:00 GMT'
-
- return "double header test"
-
- def ifmatch(self):
- val = cherrypy.request.headers['If-Match']
- assert isinstance(val, unicodestr)
- cherrypy.response.headers['ETag'] = val
- return val
-
-
- class HeaderElements(Test):
-
- def get_elements(self, headername):
- e = cherrypy.request.headers.elements(headername)
- return "\n".join([unicodestr(x) for x in e])
-
-
- class Method(Test):
-
- def index(self):
- m = cherrypy.request.method
- if m in defined_http_methods or m == "CONNECT":
- return m
-
- if m == "LINK":
- raise cherrypy.HTTPError(405)
- else:
- raise cherrypy.HTTPError(501)
-
- def parameterized(self, data):
- return data
-
- def request_body(self):
- # This should be a file object (temp file),
- # which CP will just pipe back out if we tell it to.
- return cherrypy.request.body
-
- def reachable(self):
- return "success"
-
- class Divorce:
- """HTTP Method handlers shouldn't collide with normal method names.
- For example, a GET-handler shouldn't collide with a method named 'get'.
-
- If you build HTTP method dispatching into CherryPy, rewrite this class
- to use your new dispatch mechanism and make sure that:
- "GET /divorce HTTP/1.1" maps to divorce.index() and
- "GET /divorce/get?ID=13 HTTP/1.1" maps to divorce.get()
- """
-
- documents = {}
-
- def index(self):
- yield "Choose your document
\n"
- yield "\n"
- for id, contents in self.documents.items():
- yield (" - %s: %s
\n"
- % (id, id, contents))
- yield "
"
- index.exposed = True
-
- def get(self, ID):
- return ("Divorce document %s: %s" %
- (ID, self.documents.get(ID, "empty")))
- get.exposed = True
-
- root.divorce = Divorce()
-
-
- class ThreadLocal(Test):
-
- def index(self):
- existing = repr(getattr(cherrypy.request, "asdf", None))
- cherrypy.request.asdf = "rassfrassin"
- return existing
-
- appconf = {
- '/method': {'request.methods_with_bodies': ("POST", "PUT", "PROPFIND")},
- }
- cherrypy.tree.mount(root, config=appconf)
- setup_server = staticmethod(setup_server)
-
- def test_scheme(self):
- self.getPage("/scheme")
- self.assertBody(self.scheme)
-
- def testParams(self):
- self.getPage("/params/?thing=a")
- self.assertBody("u'a'")
-
- self.getPage("/params/?thing=a&thing=b&thing=c")
- self.assertBody("[u'a', u'b', u'c']")
-
- # Test friendly error message when given params are not accepted.
- cherrypy.config.update({"request.show_mismatched_params": True})
- self.getPage("/params/?notathing=meeting")
- self.assertInBody("Missing parameters: thing")
- self.getPage("/params/?thing=meeting¬athing=meeting")
- self.assertInBody("Unexpected query string parameters: notathing")
-
- # Test ability to turn off friendly error messages
- cherrypy.config.update({"request.show_mismatched_params": False})
- self.getPage("/params/?notathing=meeting")
- self.assertInBody("Not Found")
- self.getPage("/params/?thing=meeting¬athing=meeting")
- self.assertInBody("Not Found")
-
- # Test "% HEX HEX"-encoded URL, param keys, and values
- self.getPage("/params/%d4%20%e3/cheese?Gruy%E8re=Bulgn%e9ville")
- self.assertBody(r"args: ('\xd4 \xe3', 'cheese') "
- r"kwargs: {'Gruy\xe8re': u'Bulgn\xe9ville'}")
-
- # Make sure that encoded = and & get parsed correctly
- self.getPage("/params/code?url=http%3A//cherrypy.org/index%3Fa%3D1%26b%3D2")
- self.assertBody(r"args: ('code',) "
- r"kwargs: {'url': u'http://cherrypy.org/index?a=1&b=2'}")
-
- # Test coordinates sent by
- self.getPage("/params/ismap?223,114")
- self.assertBody("Coordinates: 223, 114")
-
- # Test "name[key]" dict-like params
- self.getPage("/params/dictlike?a[1]=1&a[2]=2&b=foo&b[bar]=baz")
- self.assertBody(
- "args: ('dictlike',) "
- "kwargs: {'a[1]': u'1', 'b[bar]': u'baz', 'b': u'foo', 'a[2]': u'2'}")
-
- def testParamErrors(self):
-
- # test that all of the handlers work when given
- # the correct parameters in order to ensure that the
- # errors below aren't coming from some other source.
- for uri in (
- '/paramerrors/one_positional?param1=foo',
- '/paramerrors/one_positional_args?param1=foo',
- '/paramerrors/one_positional_args/foo',
- '/paramerrors/one_positional_args/foo/bar/baz',
- '/paramerrors/one_positional_args_kwargs?param1=foo¶m2=bar',
- '/paramerrors/one_positional_args_kwargs/foo?param2=bar¶m3=baz',
- '/paramerrors/one_positional_args_kwargs/foo/bar/baz?param2=bar¶m3=baz',
- '/paramerrors/one_positional_kwargs?param1=foo¶m2=bar¶m3=baz',
- '/paramerrors/one_positional_kwargs/foo?param4=foo¶m2=bar¶m3=baz',
- '/paramerrors/no_positional',
- '/paramerrors/no_positional_args/foo',
- '/paramerrors/no_positional_args/foo/bar/baz',
- '/paramerrors/no_positional_args_kwargs?param1=foo¶m2=bar',
- '/paramerrors/no_positional_args_kwargs/foo?param2=bar',
- '/paramerrors/no_positional_args_kwargs/foo/bar/baz?param2=bar¶m3=baz',
- '/paramerrors/no_positional_kwargs?param1=foo¶m2=bar',
- '/paramerrors/callable_object',
- ):
- self.getPage(uri)
- self.assertStatus(200)
-
- # query string parameters are part of the URI, so if they are wrong
- # for a particular handler, the status MUST be a 404.
- error_msgs = [
- 'Missing parameters',
- 'Nothing matches the given URI',
- 'Multiple values for parameters',
- 'Unexpected query string parameters',
- 'Unexpected body parameters',
- ]
- for uri, msg in (
- ('/paramerrors/one_positional', error_msgs[0]),
- ('/paramerrors/one_positional?foo=foo', error_msgs[0]),
- ('/paramerrors/one_positional/foo/bar/baz', error_msgs[1]),
- ('/paramerrors/one_positional/foo?param1=foo', error_msgs[2]),
- ('/paramerrors/one_positional/foo?param1=foo¶m2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo?param1=foo¶m2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo/bar/baz?param2=foo', error_msgs[3]),
- ('/paramerrors/one_positional_args_kwargs/foo/bar/baz?param1=bar¶m3=baz', error_msgs[2]),
- ('/paramerrors/one_positional_kwargs/foo?param1=foo¶m2=bar¶m3=baz', error_msgs[2]),
- ('/paramerrors/no_positional/boo', error_msgs[1]),
- ('/paramerrors/no_positional?param1=foo', error_msgs[3]),
- ('/paramerrors/no_positional_args/boo?param1=foo', error_msgs[3]),
- ('/paramerrors/no_positional_kwargs/boo?param1=foo', error_msgs[1]),
- ('/paramerrors/callable_object?param1=foo', error_msgs[3]),
- ('/paramerrors/callable_object/boo', error_msgs[1]),
- ):
- for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
- self.getPage(uri)
- self.assertStatus(404)
- if show_mismatched_params:
- self.assertInBody(msg)
- else:
- self.assertInBody("Not Found")
-
- # if body parameters are wrong, a 400 must be returned.
- for uri, body, msg in (
- ('/paramerrors/one_positional/foo', 'param1=foo', error_msgs[2]),
- ('/paramerrors/one_positional/foo', 'param1=foo¶m2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo', 'param1=foo¶m2=foo', error_msgs[2]),
- ('/paramerrors/one_positional_args/foo/bar/baz', 'param2=foo', error_msgs[4]),
- ('/paramerrors/one_positional_args_kwargs/foo/bar/baz', 'param1=bar¶m3=baz', error_msgs[2]),
- ('/paramerrors/one_positional_kwargs/foo', 'param1=foo¶m2=bar¶m3=baz', error_msgs[2]),
- ('/paramerrors/no_positional', 'param1=foo', error_msgs[4]),
- ('/paramerrors/no_positional_args/boo', 'param1=foo', error_msgs[4]),
- ('/paramerrors/callable_object', 'param1=foo', error_msgs[4]),
- ):
- for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
- self.getPage(uri, method='POST', body=body)
- self.assertStatus(400)
- if show_mismatched_params:
- self.assertInBody(msg)
- else:
- self.assertInBody("Bad Request")
-
-
- # even if body parameters are wrong, if we get the uri wrong, then
- # it's a 404
- for uri, body, msg in (
- ('/paramerrors/one_positional?param2=foo', 'param1=foo', error_msgs[3]),
- ('/paramerrors/one_positional/foo/bar', 'param2=foo', error_msgs[1]),
- ('/paramerrors/one_positional_args/foo/bar?param2=foo', 'param3=foo', error_msgs[3]),
- ('/paramerrors/one_positional_kwargs/foo/bar', 'param2=bar¶m3=baz', error_msgs[1]),
- ('/paramerrors/no_positional?param1=foo', 'param2=foo', error_msgs[3]),
- ('/paramerrors/no_positional_args/boo?param2=foo', 'param1=foo', error_msgs[3]),
- ('/paramerrors/callable_object?param2=bar', 'param1=foo', error_msgs[3]),
- ):
- for show_mismatched_params in (True, False):
- cherrypy.config.update({'request.show_mismatched_params': show_mismatched_params})
- self.getPage(uri, method='POST', body=body)
- self.assertStatus(404)
- if show_mismatched_params:
- self.assertInBody(msg)
- else:
- self.assertInBody("Not Found")
-
- # In the case that a handler raises a TypeError we should
- # let that type error through.
- for uri in (
- '/paramerrors/raise_type_error',
- '/paramerrors/raise_type_error_with_default_param?x=0',
- '/paramerrors/raise_type_error_with_default_param?x=0&y=0',
- ):
- self.getPage(uri, method='GET')
- self.assertStatus(500)
- self.assertTrue('Client Error', self.body)
-
- def testErrorHandling(self):
- self.getPage("/error/missing")
- self.assertStatus(404)
- self.assertErrorPage(404, "The path '/error/missing' was not found.")
-
- ignore = helper.webtest.ignored_exceptions
- ignore.append(ValueError)
- try:
- valerr = '\n raise ValueError()\nValueError'
- self.getPage("/error/page_method")
- self.assertErrorPage(500, pattern=valerr)
-
- self.getPage("/error/page_yield")
- self.assertErrorPage(500, pattern=valerr)
-
- if (cherrypy.server.protocol_version == "HTTP/1.0" or
- getattr(cherrypy.server, "using_apache", False)):
- self.getPage("/error/page_streamed")
- # Because this error is raised after the response body has
- # started, the status should not change to an error status.
- self.assertStatus(200)
- self.assertBody("word up")
- else:
- # Under HTTP/1.1, the chunked transfer-coding is used.
- # The HTTP client will choke when the output is incomplete.
- self.assertRaises((ValueError, IncompleteRead), self.getPage,
- "/error/page_streamed")
-
- # No traceback should be present
- self.getPage("/error/cause_err_in_finalize")
- msg = "Illegal response status from server ('ZOO' is non-numeric)."
- self.assertErrorPage(500, msg, None)
- finally:
- ignore.pop()
-
- # Test HTTPError with a reason-phrase in the status arg.
- self.getPage('/error/reason_phrase')
- self.assertStatus("410 Gone fishin'")
-
- # Test custom error page for a specific error.
- self.getPage("/error/custom")
- self.assertStatus(404)
- self.assertBody("Hello, world\r\n" + (" " * 499))
-
- # Test custom error page for a specific error.
- self.getPage("/error/custom?err=401")
- self.assertStatus(401)
- self.assertBody("Error 401 Unauthorized - Well, I'm very sorry but you haven't paid!")
-
- # Test default custom error page.
- self.getPage("/error/custom_default")
- self.assertStatus(500)
- self.assertBody("Error 500 Internal Server Error - Well, I'm very sorry but you haven't paid!".ljust(513))
-
- # Test error in custom error page (ticket #305).
- # Note that the message is escaped for HTML (ticket #310).
- self.getPage("/error/noexist")
- self.assertStatus(404)
- msg = ("No, <b>really</b>, not found!
"
- "In addition, the custom error page failed:\n
"
- "IOError: [Errno 2] No such file or directory: 'nonexistent.html'")
- self.assertInBody(msg)
-
- if getattr(cherrypy.server, "using_apache", False):
- pass
- else:
- # Test throw_errors (ticket #186).
- self.getPage("/error/rethrow")
- self.assertInBody("raise ValueError()")
-
- def testExpect(self):
- e = ('Expect', '100-continue')
- self.getPage("/headerelements/get_elements?headername=Expect", [e])
- self.assertBody('100-continue')
-
- self.getPage("/expect/expectation_failed", [e])
- self.assertStatus(417)
-
- def testHeaderElements(self):
- # Accept-* header elements should be sorted, with most preferred first.
- h = [('Accept', 'audio/*; q=0.2, audio/basic')]
- self.getPage("/headerelements/get_elements?headername=Accept", h)
- self.assertStatus(200)
- self.assertBody("audio/basic\n"
- "audio/*;q=0.2")
-
- h = [('Accept', 'text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c')]
- self.getPage("/headerelements/get_elements?headername=Accept", h)
- self.assertStatus(200)
- self.assertBody("text/x-c\n"
- "text/html\n"
- "text/x-dvi;q=0.8\n"
- "text/plain;q=0.5")
-
- # Test that more specific media ranges get priority.
- h = [('Accept', 'text/*, text/html, text/html;level=1, */*')]
- self.getPage("/headerelements/get_elements?headername=Accept", h)
- self.assertStatus(200)
- self.assertBody("text/html;level=1\n"
- "text/html\n"
- "text/*\n"
- "*/*")
-
- # Test Accept-Charset
- h = [('Accept-Charset', 'iso-8859-5, unicode-1-1;q=0.8')]
- self.getPage("/headerelements/get_elements?headername=Accept-Charset", h)
- self.assertStatus("200 OK")
- self.assertBody("iso-8859-5\n"
- "unicode-1-1;q=0.8")
-
- # Test Accept-Encoding
- h = [('Accept-Encoding', 'gzip;q=1.0, identity; q=0.5, *;q=0')]
- self.getPage("/headerelements/get_elements?headername=Accept-Encoding", h)
- self.assertStatus("200 OK")
- self.assertBody("gzip;q=1.0\n"
- "identity;q=0.5\n"
- "*;q=0")
-
- # Test Accept-Language
- h = [('Accept-Language', 'da, en-gb;q=0.8, en;q=0.7')]
- self.getPage("/headerelements/get_elements?headername=Accept-Language", h)
- self.assertStatus("200 OK")
- self.assertBody("da\n"
- "en-gb;q=0.8\n"
- "en;q=0.7")
-
- # Test malformed header parsing. See http://www.cherrypy.org/ticket/763.
- self.getPage("/headerelements/get_elements?headername=Content-Type",
- # Note the illegal trailing ";"
- headers=[('Content-Type', 'text/html; charset=utf-8;')])
- self.assertStatus(200)
- self.assertBody("text/html;charset=utf-8")
-
- def test_repeated_headers(self):
- # Test that two request headers are collapsed into one.
- # See http://www.cherrypy.org/ticket/542.
- self.getPage("/headers/Accept-Charset",
- headers=[("Accept-Charset", "iso-8859-5"),
- ("Accept-Charset", "unicode-1-1;q=0.8")])
- self.assertBody("iso-8859-5, unicode-1-1;q=0.8")
-
- # Tests that each header only appears once, regardless of case.
- self.getPage("/headers/doubledheaders")
- self.assertBody("double header test")
- hnames = [name.title() for name, val in self.headers]
- for key in ['Content-Length', 'Content-Type', 'Date',
- 'Expires', 'Location', 'Server']:
- self.assertEqual(hnames.count(key), 1, self.headers)
-
- def test_encoded_headers(self):
- # First, make sure the innards work like expected.
- self.assertEqual(httputil.decode_TEXT(u"=?utf-8?q?f=C3=BCr?="), u"f\xfcr")
-
- if cherrypy.server.protocol_version == "HTTP/1.1":
- # Test RFC-2047-encoded request and response header values
- u = u'\u212bngstr\xf6m'
- c = u"=E2=84=ABngstr=C3=B6m"
- self.getPage("/headers/ifmatch", [('If-Match', u'=?utf-8?q?%s?=' % c)])
- # The body should be utf-8 encoded.
- self.assertBody("\xe2\x84\xabngstr\xc3\xb6m")
- # But the Etag header should be RFC-2047 encoded (binary)
- self.assertHeader("ETag", u'=?utf-8?b?4oSrbmdzdHLDtm0=?=')
-
- # Test a *LONG* RFC-2047-encoded request and response header value
- self.getPage("/headers/ifmatch",
- [('If-Match', u'=?utf-8?q?%s?=' % (c * 10))])
- self.assertBody("\xe2\x84\xabngstr\xc3\xb6m" * 10)
- # Note: this is different output for Python3, but it decodes fine.
- etag = self.assertHeader("ETag",
- '=?utf-8?b?4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
- '4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
- '4oSrbmdzdHLDtm3ihKtuZ3N0csO2beKEq25nc3Ryw7Zt'
- '4oSrbmdzdHLDtm0=?=')
- self.assertEqual(httputil.decode_TEXT(etag), u * 10)
-
- def test_header_presence(self):
- # If we don't pass a Content-Type header, it should not be present
- # in cherrypy.request.headers
- self.getPage("/headers/Content-Type",
- headers=[])
- self.assertStatus(500)
-
- # If Content-Type is present in the request, it should be present in
- # cherrypy.request.headers
- self.getPage("/headers/Content-Type",
- headers=[("Content-type", "application/json")])
- self.assertBody("application/json")
-
- def test_basic_HTTPMethods(self):
- helper.webtest.methods_with_bodies = ("POST", "PUT", "PROPFIND")
-
- # Test that all defined HTTP methods work.
- for m in defined_http_methods:
- self.getPage("/method/", method=m)
-
- # HEAD requests should not return any body.
- if m == "HEAD":
- self.assertBody("")
- elif m == "TRACE":
- # Some HTTP servers (like modpy) have their own TRACE support
- self.assertEqual(self.body[:5], ntob("TRACE"))
- else:
- self.assertBody(m)
-
- # Request a PUT method with a form-urlencoded body
- self.getPage("/method/parameterized", method="PUT",
- body="data=on+top+of+other+things")
- self.assertBody("on top of other things")
-
- # Request a PUT method with a file body
- b = "one thing on top of another"
- h = [("Content-Type", "text/plain"),
- ("Content-Length", str(len(b)))]
- self.getPage("/method/request_body", headers=h, method="PUT", body=b)
- self.assertStatus(200)
- self.assertBody(b)
-
- # Request a PUT method with a file body but no Content-Type.
- # See http://www.cherrypy.org/ticket/790.
- b = ntob("one thing on top of another")
- self.persistent = True
- try:
- conn = self.HTTP_CONN
- conn.putrequest("PUT", "/method/request_body", skip_host=True)
- conn.putheader("Host", self.HOST)
- conn.putheader('Content-Length', str(len(b)))
- conn.endheaders()
- conn.send(b)
- response = conn.response_class(conn.sock, method="PUT")
- response.begin()
- self.assertEqual(response.status, 200)
- self.body = response.read()
- self.assertBody(b)
- finally:
- self.persistent = False
-
- # Request a PUT method with no body whatsoever (not an empty one).
- # See http://www.cherrypy.org/ticket/650.
- # Provide a C-T or webtest will provide one (and a C-L) for us.
- h = [("Content-Type", "text/plain")]
- self.getPage("/method/reachable", headers=h, method="PUT")
- self.assertStatus(411)
-
- # Request a custom method with a request body
- b = ('\n\n'
- ''
- '')
- h = [('Content-Type', 'text/xml'),
- ('Content-Length', str(len(b)))]
- self.getPage("/method/request_body", headers=h, method="PROPFIND", body=b)
- self.assertStatus(200)
- self.assertBody(b)
-
- # Request a disallowed method
- self.getPage("/method/", method="LINK")
- self.assertStatus(405)
-
- # Request an unknown method
- self.getPage("/method/", method="SEARCH")
- self.assertStatus(501)
-
- # For method dispatchers: make sure that an HTTP method doesn't
- # collide with a virtual path atom. If you build HTTP-method
- # dispatching into the core, rewrite these handlers to use
- # your dispatch idioms.
- self.getPage("/divorce/get?ID=13")
- self.assertBody('Divorce document 13: empty')
- self.assertStatus(200)
- self.getPage("/divorce/", method="GET")
- self.assertBody('Choose your document
\n')
- self.assertStatus(200)
-
- def test_CONNECT_method(self):
- if getattr(cherrypy.server, "using_apache", False):
- return self.skip("skipped due to known Apache differences... ")
-
- self.getPage("/method/", method="CONNECT")
- self.assertBody("CONNECT")
-
- def testEmptyThreadlocals(self):
- results = []
- for x in range(20):
- self.getPage("/threadlocal/")
- results.append(self.body)
- self.assertEqual(results, [ntob("None")] * 20)
-
diff --git a/cherrypy/test/test_routes.py b/cherrypy/test/test_routes.py
deleted file mode 100644
index a8062f8f..00000000
--- a/cherrypy/test/test_routes.py
+++ /dev/null
@@ -1,69 +0,0 @@
-import os
-curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
-
-import cherrypy
-
-from cherrypy.test import helper
-import nose
-
-class RoutesDispatchTest(helper.CPWebCase):
-
- def setup_server():
-
- try:
- import routes
- except ImportError:
- raise nose.SkipTest('Install routes to test RoutesDispatcher code')
-
- class Dummy:
- def index(self):
- return "I said good day!"
-
- class City:
-
- def __init__(self, name):
- self.name = name
- self.population = 10000
-
- def index(self, **kwargs):
- return "Welcome to %s, pop. %s" % (self.name, self.population)
- index._cp_config = {'tools.response_headers.on': True,
- 'tools.response_headers.headers': [('Content-Language', 'en-GB')]}
-
- def update(self, **kwargs):
- self.population = kwargs['pop']
- return "OK"
-
- d = cherrypy.dispatch.RoutesDispatcher()
- d.connect(action='index', name='hounslow', route='/hounslow',
- controller=City('Hounslow'))
- d.connect(name='surbiton', route='/surbiton', controller=City('Surbiton'),
- action='index', conditions=dict(method=['GET']))
- d.mapper.connect('/surbiton', controller='surbiton',
- action='update', conditions=dict(method=['POST']))
- d.connect('main', ':action', controller=Dummy())
-
- conf = {'/': {'request.dispatch': d}}
- cherrypy.tree.mount(root=None, config=conf)
- setup_server = staticmethod(setup_server)
-
- def test_Routes_Dispatch(self):
- self.getPage("/hounslow")
- self.assertStatus("200 OK")
- self.assertBody("Welcome to Hounslow, pop. 10000")
-
- self.getPage("/foo")
- self.assertStatus("404 Not Found")
-
- self.getPage("/surbiton")
- self.assertStatus("200 OK")
- self.assertBody("Welcome to Surbiton, pop. 10000")
-
- self.getPage("/surbiton", method="POST", body="pop=1327")
- self.assertStatus("200 OK")
- self.assertBody("OK")
- self.getPage("/surbiton")
- self.assertStatus("200 OK")
- self.assertHeader("Content-Language", "en-GB")
- self.assertBody("Welcome to Surbiton, pop. 1327")
-
diff --git a/cherrypy/test/test_session.py b/cherrypy/test/test_session.py
deleted file mode 100755
index 874023e2..00000000
--- a/cherrypy/test/test_session.py
+++ /dev/null
@@ -1,464 +0,0 @@
-import os
-localDir = os.path.dirname(__file__)
-import sys
-import threading
-import time
-
-import cherrypy
-from cherrypy._cpcompat import copykeys, HTTPConnection, HTTPSConnection
-from cherrypy.lib import sessions
-from cherrypy.lib.httputil import response_codes
-
-def http_methods_allowed(methods=['GET', 'HEAD']):
- method = cherrypy.request.method.upper()
- if method not in methods:
- cherrypy.response.headers['Allow'] = ", ".join(methods)
- raise cherrypy.HTTPError(405)
-
-cherrypy.tools.allow = cherrypy.Tool('on_start_resource', http_methods_allowed)
-
-
-def setup_server():
-
- class Root:
-
- _cp_config = {'tools.sessions.on': True,
- 'tools.sessions.storage_type' : 'ram',
- 'tools.sessions.storage_path' : localDir,
- 'tools.sessions.timeout': (1.0 / 60),
- 'tools.sessions.clean_freq': (1.0 / 60),
- }
-
- def clear(self):
- cherrypy.session.cache.clear()
- clear.exposed = True
-
- def data(self):
- cherrypy.session['aha'] = 'foo'
- return repr(cherrypy.session._data)
- data.exposed = True
-
- def testGen(self):
- counter = cherrypy.session.get('counter', 0) + 1
- cherrypy.session['counter'] = counter
- yield str(counter)
- testGen.exposed = True
-
- def testStr(self):
- counter = cherrypy.session.get('counter', 0) + 1
- cherrypy.session['counter'] = counter
- return str(counter)
- testStr.exposed = True
-
- def setsessiontype(self, newtype):
- self.__class__._cp_config.update({'tools.sessions.storage_type': newtype})
- if hasattr(cherrypy, "session"):
- del cherrypy.session
- cls = getattr(sessions, newtype.title() + 'Session')
- if cls.clean_thread:
- cls.clean_thread.stop()
- cls.clean_thread.unsubscribe()
- del cls.clean_thread
- setsessiontype.exposed = True
- setsessiontype._cp_config = {'tools.sessions.on': False}
-
- def index(self):
- sess = cherrypy.session
- c = sess.get('counter', 0) + 1
- time.sleep(0.01)
- sess['counter'] = c
- return str(c)
- index.exposed = True
-
- def keyin(self, key):
- return str(key in cherrypy.session)
- keyin.exposed = True
-
- def delete(self):
- cherrypy.session.delete()
- sessions.expire()
- return "done"
- delete.exposed = True
-
- def delkey(self, key):
- del cherrypy.session[key]
- return "OK"
- delkey.exposed = True
-
- def blah(self):
- return self._cp_config['tools.sessions.storage_type']
- blah.exposed = True
-
- def iredir(self):
- raise cherrypy.InternalRedirect('/blah')
- iredir.exposed = True
-
- def restricted(self):
- return cherrypy.request.method
- restricted.exposed = True
- restricted._cp_config = {'tools.allow.on': True,
- 'tools.allow.methods': ['GET']}
-
- def regen(self):
- cherrypy.tools.sessions.regenerate()
- return "logged in"
- regen.exposed = True
-
- def length(self):
- return str(len(cherrypy.session))
- length.exposed = True
-
- def session_cookie(self):
- # Must load() to start the clean thread.
- cherrypy.session.load()
- return cherrypy.session.id
- session_cookie.exposed = True
- session_cookie._cp_config = {
- 'tools.sessions.path': '/session_cookie',
- 'tools.sessions.name': 'temp',
- 'tools.sessions.persistent': False}
-
- cherrypy.tree.mount(Root())
-
-
-from cherrypy.test import helper
-
-class SessionTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def tearDown(self):
- # Clean up sessions.
- for fname in os.listdir(localDir):
- if fname.startswith(sessions.FileSession.SESSION_PREFIX):
- os.unlink(os.path.join(localDir, fname))
-
- def test_0_Session(self):
- self.getPage('/setsessiontype/ram')
- self.getPage('/clear')
-
- # Test that a normal request gets the same id in the cookies.
- # Note: this wouldn't work if /data didn't load the session.
- self.getPage('/data')
- self.assertBody("{'aha': 'foo'}")
- c = self.cookies[0]
- self.getPage('/data', self.cookies)
- self.assertEqual(self.cookies[0], c)
-
- self.getPage('/testStr')
- self.assertBody('1')
- cookie_parts = dict([p.strip().split('=')
- for p in self.cookies[0][1].split(";")])
- # Assert there is an 'expires' param
- self.assertEqual(set(cookie_parts.keys()),
- set(['session_id', 'expires', 'Path']))
- self.getPage('/testGen', self.cookies)
- self.assertBody('2')
- self.getPage('/testStr', self.cookies)
- self.assertBody('3')
- self.getPage('/data', self.cookies)
- self.assertBody("{'aha': 'foo', 'counter': 3}")
- self.getPage('/length', self.cookies)
- self.assertBody('2')
- self.getPage('/delkey?key=counter', self.cookies)
- self.assertStatus(200)
-
- self.getPage('/setsessiontype/file')
- self.getPage('/testStr')
- self.assertBody('1')
- self.getPage('/testGen', self.cookies)
- self.assertBody('2')
- self.getPage('/testStr', self.cookies)
- self.assertBody('3')
- self.getPage('/delkey?key=counter', self.cookies)
- self.assertStatus(200)
-
- # Wait for the session.timeout (1 second)
- time.sleep(2)
- self.getPage('/')
- self.assertBody('1')
- self.getPage('/length', self.cookies)
- self.assertBody('1')
-
- # Test session __contains__
- self.getPage('/keyin?key=counter', self.cookies)
- self.assertBody("True")
- cookieset1 = self.cookies
-
- # Make a new session and test __len__ again
- self.getPage('/')
- self.getPage('/length', self.cookies)
- self.assertBody('2')
-
- # Test session delete
- self.getPage('/delete', self.cookies)
- self.assertBody("done")
- self.getPage('/delete', cookieset1)
- self.assertBody("done")
- f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
- self.assertEqual(f(), [])
-
- # Wait for the cleanup thread to delete remaining session files
- self.getPage('/')
- f = lambda: [x for x in os.listdir(localDir) if x.startswith('session-')]
- self.assertNotEqual(f(), [])
- time.sleep(2)
- self.assertEqual(f(), [])
-
- def test_1_Ram_Concurrency(self):
- self.getPage('/setsessiontype/ram')
- self._test_Concurrency()
-
- def test_2_File_Concurrency(self):
- self.getPage('/setsessiontype/file')
- self._test_Concurrency()
-
- def _test_Concurrency(self):
- client_thread_count = 5
- request_count = 30
-
- # Get initial cookie
- self.getPage("/")
- self.assertBody("1")
- cookies = self.cookies
-
- data_dict = {}
- errors = []
-
- def request(index):
- if self.scheme == 'https':
- c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT))
- else:
- c = HTTPConnection('%s:%s' % (self.interface(), self.PORT))
- for i in range(request_count):
- c.putrequest('GET', '/')
- for k, v in cookies:
- c.putheader(k, v)
- c.endheaders()
- response = c.getresponse()
- body = response.read()
- if response.status != 200 or not body.isdigit():
- errors.append((response.status, body))
- else:
- data_dict[index] = max(data_dict[index], int(body))
- # Uncomment the following line to prove threads overlap.
-## sys.stdout.write("%d " % index)
-
- # Start requests from each of
- # concurrent clients
- ts = []
- for c in range(client_thread_count):
- data_dict[c] = 0
- t = threading.Thread(target=request, args=(c,))
- ts.append(t)
- t.start()
-
- for t in ts:
- t.join()
-
- hitcount = max(data_dict.values())
- expected = 1 + (client_thread_count * request_count)
-
- for e in errors:
- print(e)
- self.assertEqual(hitcount, expected)
-
- def test_3_Redirect(self):
- # Start a new session
- self.getPage('/testStr')
- self.getPage('/iredir', self.cookies)
- self.assertBody("file")
-
- def test_4_File_deletion(self):
- # Start a new session
- self.getPage('/testStr')
- # Delete the session file manually and retry.
- id = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
- path = os.path.join(localDir, "session-" + id)
- os.unlink(path)
- self.getPage('/testStr', self.cookies)
-
- def test_5_Error_paths(self):
- self.getPage('/unknown/page')
- self.assertErrorPage(404, "The path '/unknown/page' was not found.")
-
- # Note: this path is *not* the same as above. The above
- # takes a normal route through the session code; this one
- # skips the session code's before_handler and only calls
- # before_finalize (save) and on_end (close). So the session
- # code has to survive calling save/close without init.
- self.getPage('/restricted', self.cookies, method='POST')
- self.assertErrorPage(405, response_codes[405])
-
- def test_6_regenerate(self):
- self.getPage('/testStr')
- # grab the cookie ID
- id1 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
- self.getPage('/regen')
- self.assertBody('logged in')
- id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
- self.assertNotEqual(id1, id2)
-
- self.getPage('/testStr')
- # grab the cookie ID
- id1 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
- self.getPage('/testStr',
- headers=[('Cookie',
- 'session_id=maliciousid; '
- 'expires=Sat, 27 Oct 2017 04:18:28 GMT; Path=/;')])
- id2 = self.cookies[0][1].split(";", 1)[0].split("=", 1)[1]
- self.assertNotEqual(id1, id2)
- self.assertNotEqual(id2, 'maliciousid')
-
- def test_7_session_cookies(self):
- self.getPage('/setsessiontype/ram')
- self.getPage('/clear')
- self.getPage('/session_cookie')
- # grab the cookie ID
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
- # Assert there is no 'expires' param
- self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
- id1 = cookie_parts['temp']
- self.assertEqual(copykeys(sessions.RamSession.cache), [id1])
-
- # Send another request in the same "browser session".
- self.getPage('/session_cookie', self.cookies)
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
- # Assert there is no 'expires' param
- self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
- self.assertBody(id1)
- self.assertEqual(copykeys(sessions.RamSession.cache), [id1])
-
- # Simulate a browser close by just not sending the cookies
- self.getPage('/session_cookie')
- # grab the cookie ID
- cookie_parts = dict([p.strip().split('=') for p in self.cookies[0][1].split(";")])
- # Assert there is no 'expires' param
- self.assertEqual(set(cookie_parts.keys()), set(['temp', 'Path']))
- # Assert a new id has been generated...
- id2 = cookie_parts['temp']
- self.assertNotEqual(id1, id2)
- self.assertEqual(set(sessions.RamSession.cache.keys()), set([id1, id2]))
-
- # Wait for the session.timeout on both sessions
- time.sleep(2.5)
- cache = copykeys(sessions.RamSession.cache)
- if cache:
- if cache == [id2]:
- self.fail("The second session did not time out.")
- else:
- self.fail("Unknown session id in cache: %r", cache)
-
-
-import socket
-try:
- import memcache
-
- host, port = '127.0.0.1', 11211
- for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
- socket.SOCK_STREAM):
- af, socktype, proto, canonname, sa = res
- s = None
- try:
- s = socket.socket(af, socktype, proto)
- # See http://groups.google.com/group/cherrypy-users/
- # browse_frm/thread/bbfe5eb39c904fe0
- s.settimeout(1.0)
- s.connect((host, port))
- s.close()
- except socket.error:
- if s:
- s.close()
- raise
- break
-except (ImportError, socket.error):
- class MemcachedSessionTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test(self):
- return self.skip("memcached not reachable ")
-else:
- class MemcachedSessionTest(helper.CPWebCase):
- setup_server = staticmethod(setup_server)
-
- def test_0_Session(self):
- self.getPage('/setsessiontype/memcached')
-
- self.getPage('/testStr')
- self.assertBody('1')
- self.getPage('/testGen', self.cookies)
- self.assertBody('2')
- self.getPage('/testStr', self.cookies)
- self.assertBody('3')
- self.getPage('/length', self.cookies)
- self.assertErrorPage(500)
- self.assertInBody("NotImplementedError")
- self.getPage('/delkey?key=counter', self.cookies)
- self.assertStatus(200)
-
- # Wait for the session.timeout (1 second)
- time.sleep(1.25)
- self.getPage('/')
- self.assertBody('1')
-
- # Test session __contains__
- self.getPage('/keyin?key=counter', self.cookies)
- self.assertBody("True")
-
- # Test session delete
- self.getPage('/delete', self.cookies)
- self.assertBody("done")
-
- def test_1_Concurrency(self):
- client_thread_count = 5
- request_count = 30
-
- # Get initial cookie
- self.getPage("/")
- self.assertBody("1")
- cookies = self.cookies
-
- data_dict = {}
-
- def request(index):
- for i in range(request_count):
- self.getPage("/", cookies)
- # Uncomment the following line to prove threads overlap.
-## sys.stdout.write("%d " % index)
- if not self.body.isdigit():
- self.fail(self.body)
- data_dict[index] = v = int(self.body)
-
- # Start concurrent requests from
- # each of clients
- ts = []
- for c in range(client_thread_count):
- data_dict[c] = 0
- t = threading.Thread(target=request, args=(c,))
- ts.append(t)
- t.start()
-
- for t in ts:
- t.join()
-
- hitcount = max(data_dict.values())
- expected = 1 + (client_thread_count * request_count)
- self.assertEqual(hitcount, expected)
-
- def test_3_Redirect(self):
- # Start a new session
- self.getPage('/testStr')
- self.getPage('/iredir', self.cookies)
- self.assertBody("memcached")
-
- def test_5_Error_paths(self):
- self.getPage('/unknown/page')
- self.assertErrorPage(404, "The path '/unknown/page' was not found.")
-
- # Note: this path is *not* the same as above. The above
- # takes a normal route through the session code; this one
- # skips the session code's before_handler and only calls
- # before_finalize (save) and on_end (close). So the session
- # code has to survive calling save/close without init.
- self.getPage('/restricted', self.cookies, method='POST')
- self.assertErrorPage(405, response_codes[405])
-
diff --git a/cherrypy/test/test_sessionauthenticate.py b/cherrypy/test/test_sessionauthenticate.py
deleted file mode 100644
index ab1fe51e..00000000
--- a/cherrypy/test/test_sessionauthenticate.py
+++ /dev/null
@@ -1,62 +0,0 @@
-import cherrypy
-from cherrypy.test import helper
-
-
-class SessionAuthenticateTest(helper.CPWebCase):
-
- def setup_server():
-
- def check(username, password):
- # Dummy check_username_and_password function
- if username != 'test' or password != 'password':
- return 'Wrong login/password'
-
- def augment_params():
- # A simple tool to add some things to request.params
- # This is to check to make sure that session_auth can handle request
- # params (ticket #780)
- cherrypy.request.params["test"] = "test"
-
- cherrypy.tools.augment_params = cherrypy.Tool('before_handler',
- augment_params, None, priority=30)
-
- class Test:
-
- _cp_config = {'tools.sessions.on': True,
- 'tools.session_auth.on': True,
- 'tools.session_auth.check_username_and_password': check,
- 'tools.augment_params.on': True,
- }
-
- def index(self, **kwargs):
- return "Hi %s, you are logged in" % cherrypy.request.login
- index.exposed = True
-
- cherrypy.tree.mount(Test())
- setup_server = staticmethod(setup_server)
-
-
- def testSessionAuthenticate(self):
- # request a page and check for login form
- self.getPage('/')
- self.assertInBody('