Permalink
Browse files

Bug 771144 - Should be able to log requests made through mozhttp web …

…server
  • Loading branch information...
1 parent 434d717 commit 90b72275ab491f7360f5f10640fe1379994d3957 @wlach wlach committed Jul 5, 2012
Showing with 56 additions and 1 deletion.
  1. +13 −1 mozhttpd/mozhttpd/mozhttpd.py
  2. +1 −0 mozhttpd/tests/manifest.ini
  3. +42 −0 mozhttpd/tests/requestlog.py
@@ -17,6 +17,7 @@
import urlparse
import re
import iface
+import time
from SocketServer import ThreadingMixIn
class EasyServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
@@ -63,9 +64,16 @@ class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
docroot = os.getcwd() # current working directory at time of import
proxy_host_dirs = False
+ request_log = []
+ log_requests = False
request = None
def _try_handler(self, method):
+ if self.log_requests:
+ self.request_log.append({ 'method': method,
+ 'path': self.request.path,
+ 'time': time.time() })
+
handlers = [handler for handler in self.urlhandlers
if handler['method'] == method]
for handler in handlers:
@@ -181,7 +189,7 @@ class MozHttpd(object):
"""
def __init__(self, host="127.0.0.1", port=8888, docroot=None,
- urlhandlers=None, proxy_host_dirs=False):
+ urlhandlers=None, proxy_host_dirs=False, log_requests=False):
self.host = host
self.port = int(port)
self.docroot = docroot
@@ -190,11 +198,15 @@ def __init__(self, host="127.0.0.1", port=8888, docroot=None,
self.proxy_host_dirs = proxy_host_dirs
self.httpd = None
self.urlhandlers = urlhandlers or []
+ self.log_requests = log_requests
+ self.request_log = []
class RequestHandlerInstance(RequestHandler):
docroot = self.docroot
urlhandlers = self.urlhandlers
proxy_host_dirs = self.proxy_host_dirs
+ request_log = self.request_log
+ log_requests = self.log_requests
self.handler_class = RequestHandlerInstance
@@ -1,2 +1,3 @@
[filelisting.py]
[api.py]
+[requestlog.py]
@@ -0,0 +1,42 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this file,
+# You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import mozhttpd
+import urllib2
+import os
+import unittest
+import time
+
+here = os.path.dirname(os.path.abspath(__file__))
+
+class RequestLogTest(unittest.TestCase):
+
+ def check_logging(self, log_requests=False):
+ filelist = os.listdir(here)
+
+ httpd = mozhttpd.MozHttpd(port=0, docroot=here, log_requests=log_requests)
+ httpd.start(block=False)
+ url = "http://%s:%s/" % ('127.0.0.1', httpd.httpd.server_port)
+ f = urllib2.urlopen(url)
+ data = f.read()
+
+ return httpd.request_log
+
+ def test_logging_enabled(self):
+ request_log = self.check_logging(log_requests=True)
+
+ self.assertEqual(len(request_log), 1)
+
+ log_entry = request_log[0]
+ self.assertEqual(log_entry['method'], 'GET')
+ self.assertEqual(log_entry['path'], '/')
+ self.assertEqual(type(log_entry['time']), float)
+
+ def test_logging_disabled(self):
+ request_log = self.check_logging(log_requests=False)
+
+ self.assertEqual(len(request_log), 0)
+
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit 90b7227

Please sign in to comment.