michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import mozhttpd michael@0: import urllib2 michael@0: import os michael@0: import unittest michael@0: import time michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: class RequestLogTest(unittest.TestCase): michael@0: michael@0: def check_logging(self, log_requests=False): michael@0: filelist = os.listdir(here) michael@0: michael@0: httpd = mozhttpd.MozHttpd(port=0, docroot=here, log_requests=log_requests) michael@0: httpd.start(block=False) michael@0: url = "http://%s:%s/" % ('127.0.0.1', httpd.httpd.server_port) michael@0: f = urllib2.urlopen(url) michael@0: data = f.read() michael@0: michael@0: return httpd.request_log michael@0: michael@0: def test_logging_enabled(self): michael@0: request_log = self.check_logging(log_requests=True) michael@0: michael@0: self.assertEqual(len(request_log), 1) michael@0: michael@0: log_entry = request_log[0] michael@0: self.assertEqual(log_entry['method'], 'GET') michael@0: self.assertEqual(log_entry['path'], '/') michael@0: self.assertEqual(type(log_entry['time']), float) michael@0: michael@0: def test_logging_disabled(self): michael@0: request_log = self.check_logging(log_requests=False) michael@0: michael@0: self.assertEqual(len(request_log), 0) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()