Skip to content

Commit

Permalink
Really make the commonaccesslogger tests timezone independent.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 26, 2017
1 parent dc071ea commit 1971186
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/zope/server/http/commonaccesslogger.py
Expand Up @@ -52,8 +52,10 @@ def compute_timezone_for_log(cls, tz):
tz_for_log = None
tz_for_log_alt = None

_localtime = staticmethod(time.localtime)

def log_date_string(self, when):
logtime = time.localtime(when)
logtime = self._localtime(when)
Y, M, D, h, m, s = logtime[:6]

if not time.daylight:
Expand Down
32 changes: 19 additions & 13 deletions src/zope/server/http/tests/test_commonaccesslogger.py
Expand Up @@ -16,7 +16,13 @@
import unittest
import logging

from zope.server.http.commonaccesslogger import CommonAccessLogger
from zope.server.http.commonaccesslogger import CommonAccessLogger as _CommonAccessLogger

class CommonAccessLogger(_CommonAccessLogger):
def _localtime(self, when):
assert when == 123456789
return (1973, 11, 29, 21, 33, 9)


class TestCommonAccessLogger(unittest.TestCase):

Expand Down Expand Up @@ -44,30 +50,30 @@ def test_compute_timezone_for_log_positive(self):
def test_log_date_string_daylight(self):
import time
orig_dl = time.daylight
orig_tz = time.timezone
orig_az = time.altzone
time.daylight = True
time.timezone = -3600
time.altzone = -3600
try:
s = CommonAccessLogger().log_date_string(123456789)
finally:
time.daylight = orig_dl
time.timezone = orig_tz
time.altzone = orig_az

self.assertEqual(s, '29/Nov/1973:15:33:09 -0500')
self.assertEqual(s, '29/Nov/1973:21:33:09 +0100')

def test_log_date_string_non_daylight(self):
import time
orig_dl = time.daylight
orig_tz = time.altzone
orig_tz = time.timezone
time.daylight = False
time.altzone = -3600
time.timezone = -3600
try:
s = CommonAccessLogger().log_date_string(123456789)
finally:
time.daylight = orig_dl
time.altzone = orig_tz
time.timezone = orig_tz

self.assertEqual(s, '29/Nov/1973:15:33:09 -0600')
self.assertEqual(s, '29/Nov/1973:21:33:09 +0100')

def test_log_request(self):
import time
Expand All @@ -94,19 +100,19 @@ class Task(object):
def t():
return 123456789
orig_dl = time.daylight
orig_tz = time.timezone
orig_az = time.altzone
time.daylight = True
time.timezone = -3600
time.altzone = -3600
time.time = t
try:
output = Output()
cal = CommonAccessLogger(output, Resolver())
cal.log(Task())
finally:
time.daylight = orig_dl
time.timezone = orig_tz
time.altzone = orig_az
time.time = orig_t

self.assertEqual(
'host - anonymous [29/Nov/1973:15:33:09 -0500] "GET / HTTP/1.0" 200 OK 10 "" ""\n',
'host - anonymous [29/Nov/1973:21:33:09 +0100] "GET / HTTP/1.0" 200 OK 10 "" ""\n',
output.msgs[0])

0 comments on commit 1971186

Please sign in to comment.