diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-05-05 11:59:14 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2011-05-05 11:59:14 (GMT) |
commit | 0372e106841e2d641556b6f5146d59cbe189534f (patch) | |
tree | 8305ae6560d213bbab352ebc343afa33fc81af8a /Lib | |
parent | 9cc432d93799652cb21ac4473d6875ed3de01937 (diff) | |
download | cpython-0372e106841e2d641556b6f5146d59cbe189534f.zip cpython-0372e106841e2d641556b6f5146d59cbe189534f.tar.gz cpython-0372e106841e2d641556b6f5146d59cbe189534f.tar.bz2 |
Improved coverage and fixed bug in HTTPHandler with POST requests.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/logging/handlers.py | 6 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 42 |
2 files changed, 36 insertions, 12 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 603a534..0086808 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -29,7 +29,7 @@ from stat import ST_DEV, ST_INO, ST_MTIME import queue try: import threading -except ImportError: +except ImportError: #pragma: no cover threading = None try: @@ -1044,7 +1044,9 @@ class HTTPHandler(logging.Handler): s = ('u%s:%s' % self.credentials).encode('utf-8') s = 'Basic ' + base64.b64encode(s).strip() h.putheader('Authorization', s) - h.endheaders(data if self.method == "POST" else None) + h.endheaders() + if self.method == "POST": + h.send(data.encode('utf-8')) h.getresponse() #can't do anything with the result except (KeyboardInterrupt, SystemExit): #pragma: no cover raise diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index bf0ad77..2e6de4c 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1464,22 +1464,34 @@ class HTTPHandlerTest(BaseTest): BaseTest.tearDown(self) def handle_request(self, request): + self.command = request.command self.log_data = urlparse(request.path) + if self.command == 'POST': + try: + rlen = int(request.headers['Content-Length']) + self.post_data = request.rfile.read(rlen) + except: + self.post_data = None request.send_response(200) self.handled.set() def test_output(self): # The log message sent to the SysLogHandler is properly received. logger = logging.getLogger("http") - msg = "sp\xe4m" - logger.error(msg) - self.handled.wait() - self.assertEqual(self.log_data.path, '/frob') - d = parse_qs(self.log_data.query) - self.assertEqual(d['name'], ['http']) - self.assertEqual(d['funcName'], ['test_output']) - self.assertEqual(d['msg'], [msg]) - + for method in ('GET', 'POST'): + self.h_hdlr.method = method + msg = "sp\xe4m" + logger.error(msg) + self.handled.wait() + self.assertEqual(self.log_data.path, '/frob') + self.assertEqual(self.command, method) + if method == 'GET': + d = parse_qs(self.log_data.query) + else: + d = parse_qs(self.post_data.decode('utf-8')) + self.assertEqual(d['name'], ['http']) + self.assertEqual(d['funcName'], ['test_output']) + self.assertEqual(d['msg'], [msg]) class MemoryTest(BaseTest): @@ -3470,7 +3482,8 @@ class RotatingFileHandlerTest(BaseFileTest): class TimedRotatingFileHandlerTest(BaseFileTest): # other test methods added below def test_rollover(self): - fh = logging.handlers.TimedRotatingFileHandler(self.fn, 'S') + fh = logging.handlers.TimedRotatingFileHandler(self.fn, 'S', + backupCount=1) r = logging.makeLogRecord({'msg': 'testing'}) fh.emit(r) self.assertLogFile(self.fn) @@ -3481,6 +3494,15 @@ class TimedRotatingFileHandlerTest(BaseFileTest): suffix = prevsec.strftime(".%Y-%m-%d_%H-%M-%S") self.assertLogFile(self.fn + suffix) + def test_invalid(self): + assertRaises = self.assertRaises + assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, + self.fn, 'X') + assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, + self.fn, 'W') + assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, + self.fn, 'W7') + def secs(**kw): return datetime.timedelta(**kw) // datetime.timedelta(seconds=1) |