/
opt
/
imh-python
/
lib
/
python3.9
/
site-packages
/
cherrypy
/
test
/
/opt/imh-python/lib/python3.9/site-packages/cherrypy/test
mkdir
upload
Name
Size
Mode
Actions
static/
-
0755
rm
__pycache__/
-
0755
rm
benchmark.py
12563
0644
edit
dl
rm
checkerdemo.py
1861
0644
edit
dl
rm
fastcgi.conf
686
0644
edit
dl
rm
fcgi.conf
486
0644
edit
dl
rm
helper.py
16369
0644
edit
dl
rm
logtest.py
8151
0644
edit
dl
rm
modfastcgi.py
4652
0644
edit
dl
rm
modfcgid.py
4237
0644
edit
dl
rm
modpy.py
4978
0644
edit
dl
rm
modwsgi.py
4834
0644
edit
dl
rm
sessiondemo.py
5425
0644
edit
dl
rm
style.css
17
0644
edit
dl
rm
test.pem
2254
0644
edit
dl
rm
test_auth_basic.py
4499
0644
edit
dl
rm
test_auth_digest.py
4454
0644
edit
dl
rm
test_bus.py
9960
0644
edit
dl
rm
test_caching.py
14386
0644
edit
dl
rm
test_config.py
8836
0644
edit
dl
rm
test_config_server.py
4037
0644
edit
dl
rm
test_conn.py
30744
0644
edit
dl
rm
test_core.py
30408
0644
edit
dl
rm
test_dynamicobjectmapping.py
12331
0644
edit
dl
rm
test_encoding.py
17535
0644
edit
dl
rm
test_etags.py
3093
0644
edit
dl
rm
test_http.py
10939
0644
edit
dl
rm
test_httputil.py
2412
0644
edit
dl
rm
test_iterator.py
5724
0644
edit
dl
rm
test_json.py
2860
0644
edit
dl
rm
test_logging.py
8315
0644
edit
dl
rm
test_mime.py
4538
0644
edit
dl
rm
test_misc_tools.py
7094
0644
edit
dl
rm
test_native.py
971
0644
edit
dl
rm
test_objectmapping.py
14504
0644
edit
dl
rm
test_params.py
1862
0644
edit
dl
rm
test_plugins.py
334
0644
edit
dl
rm
test_proxy.py
5630
0644
edit
dl
rm
test_refleaks.py
1555
0644
edit
dl
rm
test_request_obj.py
37097
0644
edit
dl
rm
test_routes.py
2583
0644
edit
dl
rm
test_session.py
17949
0644
edit
dl
rm
test_sessionauthenticate.py
2013
0644
edit
dl
rm
test_states.py
16703
0644
edit
dl
rm
test_static.py
16702
0644
edit
dl
rm
test_tools.py
17851
0644
edit
dl
rm
test_tutorials.py
6964
0644
edit
dl
rm
test_virtualhost.py
4021
0644
edit
dl
rm
test_wsgiapps.py
3997
0644
edit
dl
rm
test_wsgi_ns.py
2812
0644
edit
dl
rm
test_wsgi_unix_socket.py
2228
0644
edit
dl
rm
test_wsgi_vhost.py
1034
0644
edit
dl
rm
test_xmlrpc.py
4584
0644
edit
dl
rm
webtest.py
262
0644
edit
dl
rm
_test_decorators.py
951
0644
edit
dl
rm
_test_states_demo.py
1876
0644
edit
dl
rm
__init__.py
396
0644
edit
dl
rm
Edit:
/opt/imh-python/lib/python3.9/site-packages/cherrypy/test/test_logging.py
(8315B)
"""Basic tests for the CherryPy core: request handling.""" import logging from cheroot.test import webtest import pytest import requests # FIXME: Temporary using it directly, better switch import cherrypy from cherrypy.test.logtest import LogCase # Some unicode strings. tartaros = u'\u03a4\u1f71\u03c1\u03c4\u03b1\u03c1\u03bf\u03c2' erebos = u'\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com' @pytest.fixture def access_log_file(tmp_path_factory): return tmp_path_factory.mktemp('logs') / 'access.log' @pytest.fixture def error_log_file(tmp_path_factory): return tmp_path_factory.mktemp('logs') / 'access.log' @pytest.fixture def server(configure_server): cherrypy.engine.start() cherrypy.engine.wait(cherrypy.engine.states.STARTED) yield shutdown_server() def shutdown_server(): cherrypy.engine.exit() cherrypy.engine.block() for name, server in getattr(cherrypy, 'servers', {}).copy().items(): server.unsubscribe() del cherrypy.servers[name] @pytest.fixture def configure_server(access_log_file, error_log_file): class Root: @cherrypy.expose def index(self): return 'hello' @cherrypy.expose def uni_code(self): cherrypy.request.login = tartaros cherrypy.request.remote.name = erebos @cherrypy.expose def slashes(self): cherrypy.request.request_line = r'GET /slashed\path HTTP/1.1' @cherrypy.expose def whitespace(self): # User-Agent = "User-Agent" ":" 1*( product | comment ) # comment = "(" *( ctext | quoted-pair | comment ) ")" # ctext = <any TEXT excluding "(" and ")"> # TEXT = <any OCTET except CTLs, but including LWS> # LWS = [CRLF] 1*( SP | HT ) cherrypy.request.headers['User-Agent'] = 'Browzuh (1.0\r\n\t\t.3)' @cherrypy.expose def as_string(self): return 'content' @cherrypy.expose def as_yield(self): yield 'content' @cherrypy.expose @cherrypy.config(**{'tools.log_tracebacks.on': True}) def error(self): raise ValueError() root = Root() cherrypy.config.reset() cherrypy.config.update({ 'server.socket_host': webtest.WebCase.HOST, 'server.socket_port': webtest.WebCase.PORT, 'server.protocol_version': webtest.WebCase.PROTOCOL, 'environment': 'test_suite', }) cherrypy.config.update({ 'log.error_file': str(error_log_file), 'log.access_file': str(access_log_file), }) cherrypy.tree.mount(root) @pytest.fixture def log_tracker(access_log_file): class LogTracker(LogCase): logfile = str(access_log_file) return LogTracker() def test_normal_return(log_tracker, server): log_tracker.markLog() host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT resp = requests.get( 'http://%s:%s/as_string' % (host, port), headers={ 'Referer': 'http://www.cherrypy.org/', 'User-Agent': 'Mozilla/5.0', }, ) expected_body = 'content' assert resp.text == expected_body assert resp.status_code == 200 intro = '%s - - [' % host log_tracker.assertLog(-1, intro) content_length = len(expected_body) if not any( k for k, v in resp.headers.items() if k.lower() == 'content-length' ): content_length = '-' log_tracker.assertLog( -1, '] "GET /as_string HTTP/1.1" 200 %s ' '"http://www.cherrypy.org/" "Mozilla/5.0"' % content_length, ) def test_normal_yield(log_tracker, server): log_tracker.markLog() host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT resp = requests.get( 'http://%s:%s/as_yield' % (host, port), headers={ 'User-Agent': '', }, ) expected_body = 'content' assert resp.text == expected_body assert resp.status_code == 200 intro = '%s - - [' % host log_tracker.assertLog(-1, intro) content_length = len(expected_body) if not any( k for k, v in resp.headers.items() if k.lower() == 'content-length' ): content_length = '-' log_tracker.assertLog( -1, '] "GET /as_yield HTTP/1.1" 200 %s "" ""' % content_length, ) def test_custom_log_format(log_tracker, monkeypatch, server): """Test a customized access_log_format string, which is a feature of _cplogging.LogManager.access().""" monkeypatch.setattr( 'cherrypy._cplogging.LogManager.access_log_format', '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}" {o}', ) log_tracker.markLog() host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT requests.get( 'http://%s:%s/as_string' % (host, port), headers={ 'Referer': 'REFERER', 'User-Agent': 'USERAGENT', 'Host': 'HOST', }, ) log_tracker.assertLog(-1, '%s - - [' % host) log_tracker.assertLog( -1, '] "GET /as_string HTTP/1.1" ' '200 7 "REFERER" "USERAGENT" HOST', ) def test_timez_log_format(log_tracker, monkeypatch, server): """Test a customized access_log_format string, which is a feature of _cplogging.LogManager.access().""" monkeypatch.setattr( 'cherrypy._cplogging.LogManager.access_log_format', '{h} {l} {u} {z} "{r}" {s} {b} "{f}" "{a}" {o}', ) log_tracker.markLog() expected_time = str(cherrypy._cplogging.LazyRfc3339UtcTime()) monkeypatch.setattr( 'cherrypy._cplogging.LazyRfc3339UtcTime', lambda: expected_time, ) host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT requests.get( 'http://%s:%s/as_string' % (host, port), headers={ 'Referer': 'REFERER', 'User-Agent': 'USERAGENT', 'Host': 'HOST', }, ) log_tracker.assertLog(-1, '%s - - ' % host) log_tracker.assertLog(-1, expected_time) log_tracker.assertLog( -1, ' "GET /as_string HTTP/1.1" ' '200 7 "REFERER" "USERAGENT" HOST', ) def test_UUIDv4_parameter_log_format(log_tracker, monkeypatch, server): """Test rendering of UUID4 within access log.""" monkeypatch.setattr( 'cherrypy._cplogging.LogManager.access_log_format', '{i}', ) log_tracker.markLog() host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT requests.get('http://%s:%s/as_string' % (host, port)) log_tracker.assertValidUUIDv4() def test_escaped_output(log_tracker, server): # Test unicode in access log pieces. log_tracker.markLog() host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT resp = requests.get('http://%s:%s/uni_code' % (host, port)) assert resp.status_code == 200 # The repr of a bytestring includes a b'' prefix log_tracker.assertLog(-1, repr(tartaros.encode('utf8'))[2:-1]) # Test the erebos value. Included inline for your enlightenment. # Note the 'r' prefix--those backslashes are literals. log_tracker.assertLog( -1, r'\xce\x88\xcf\x81\xce\xb5\xce\xb2\xce\xbf\xcf\x82', ) # Test backslashes in output. log_tracker.markLog() resp = requests.get('http://%s:%s/slashes' % (host, port)) assert resp.status_code == 200 log_tracker.assertLog(-1, b'"GET /slashed\\path HTTP/1.1"') # Test whitespace in output. log_tracker.markLog() resp = requests.get('http://%s:%s/whitespace' % (host, port)) assert resp.status_code == 200 # Again, note the 'r' prefix. log_tracker.assertLog(-1, r'"Browzuh (1.0\r\n\t\t.3)"') def test_tracebacks(server, caplog): host = webtest.interface(webtest.WebCase.HOST) port = webtest.WebCase.PORT with caplog.at_level(logging.ERROR, logger='cherrypy.error'): resp = requests.get('http://%s:%s/error' % (host, port)) rec = caplog.records[0] exc_cls, exc_msg = rec.exc_info[0], rec.message assert 'raise ValueError()' in resp.text assert 'HTTP' in exc_msg assert exc_cls is ValueError
Save
cmd:
run