Fixed bug in staticweb with log_headers

Change-Id: I7e5e254660e2ce35bc7fa95435db0a987d5acd0a
This commit is contained in:
gholt 2012-07-29 18:17:14 +00:00
parent 21616c2fc9
commit 3800fdc1a9
2 changed files with 35 additions and 2 deletions

View File

@ -471,8 +471,12 @@ class _StaticWebContext(WSGIContext):
client = env['HTTP_X_FORWARDED_FOR'].split(',')[0].strip()
logged_headers = None
if self.log_headers:
logged_headers = '\n'.join('%s: %s' % (k, v)
for k, v in req.headers.items())
headers = []
for k, v in env.iteritems():
if k.startswith('HTTP_'):
k = k[len('HTTP_'):].replace('_', '-').title()
headers.append((k, v))
logged_headers = '\n'.join('%s: %s' % (k, v) for k, v in headers)
self.access_logger.info(' '.join(quote(str(x)) for x in (
client or '-',
env.get('REMOTE_ADDR', '-'),

View File

@ -23,6 +23,7 @@ from contextlib import contextmanager
from webob import Request, Response
from swift.common.middleware import staticweb
from test.unit import FakeLogger
class FakeMemcache(object):
@ -624,5 +625,33 @@ class TestStaticWeb(unittest.TestCase):
self.assertEquals(resp.body, '1')
self.assertEquals(self.app.calls, 1)
def test_log_headers(self):
# Using a listing request since we know that calls StaticWeb's logging
# routines
self.test_staticweb.access_logger = FakeLogger()
self.test_staticweb.log_headers = True
req = Request.blank('/v1/a/c3/subdir/',
headers={'test-header': 'test-value'})
resp = req.get_response(self.test_staticweb)
self.assertEquals(resp.status_int, 200)
self.assert_('Listing of /v1/a/c3/subdir/' in resp.body)
infos = self.test_staticweb.access_logger.log_dict['info']
self.assertEquals(len(infos), 1)
info = infos[0][0][0]
self.assertTrue('Test-Header%3A%20test-value' in info, repr(info))
self.test_staticweb.access_logger = FakeLogger()
self.test_staticweb.log_headers = False
req = Request.blank('/v1/a/c3/subdir/',
headers={'test-header': 'test-value'})
resp = req.get_response(self.test_staticweb)
self.assertEquals(resp.status_int, 200)
self.assert_('Listing of /v1/a/c3/subdir/' in resp.body)
infos = self.test_staticweb.access_logger.log_dict['info']
self.assertEquals(len(infos), 1)
info = infos[0][0][0]
self.assertTrue('Test-Header%3A%20test-value' not in info, repr(info))
if __name__ == '__main__':
unittest.main()