From f2726df39c3ec4e50448361e3be4cfba45e4fa93 Mon Sep 17 00:00:00 2001 From: Dolph Mathews Date: Fri, 20 Jan 2012 14:22:20 -0600 Subject: Added Vary header to support caching (bug 913895) Change-Id: I26dca09ace688a25cf207214808e8a9c919621c4 --- keystone/test/client/test_request_specs.py | 31 ++++++++++++++++++++++++++++++ keystone/utils.py | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/keystone/test/client/test_request_specs.py b/keystone/test/client/test_request_specs.py index 74494056..39118a07 100644 --- a/keystone/test/client/test_request_specs.py +++ b/keystone/test/client/test_request_specs.py @@ -2,6 +2,37 @@ import unittest2 as unittest from keystone.test.functional import common +class TestResponseHeaders(common.FunctionalTestCase): + """Tests API's response headers""" + use_server = True + + def test_vary_header_on_error(self): + """A Vary header should be provided to support caching responses.""" + r = self.admin_request(path='/tokens/not-a-valid-token', + assert_status=404) + self.assertIn('X-Auth-Token', r.getheader('Vary')) + + def test_vary_header_on_admin(self): + """A Vary header should be provided to support caching responses.""" + r = self.admin_request(path='/tokens/%s' % self.admin_token) + self.assertIn('X-Auth-Token', r.getheader('Vary')) + + def test_vary_header_on_service(self): + """A Vary header should be provided to support caching responses.""" + r = self.service_request(path='/tenants') + self.assertIn('X-Auth-Token', r.getheader('Vary')) + + def test_vary_header_on_legacy(self): + """A Vary header should be provided to support caching responses.""" + r = self.admin_request('1.1/tenants') + self.assertIn('X-Auth-Token', r.getheader('Vary')) + + def test_vary_header_on_static(self): + """A Vary header should not be provided on a static request.""" + r = self.service_request('2.0/') + self.assertEqual(None, r.getheader('Vary')) + + class TestUrlHandling(common.FunctionalTestCase): """Tests API's global URL handling behaviors""" use_server = True diff --git a/keystone/utils.py b/keystone/utils.py index 8933245c..27f704f6 100755 --- a/keystone/utils.py +++ b/keystone/utils.py @@ -147,6 +147,7 @@ def send_error(code, req, result): resp = Response() resp.headers['content-type'] = None + resp.headers['Vary'] = 'X-Auth-Token' resp.status = code if result: @@ -168,6 +169,7 @@ def send_result(code, req, result=None): resp = Response() resp.headers['content-type'] = None + resp.headers['Vary'] = 'X-Auth-Token' resp.status = code if code > 399: return resp @@ -190,6 +192,8 @@ def send_legacy_result(code, headers): if 'content-type' not in headers: headers['content-type'] = "text/plain" + headers['Vary'] = 'X-Auth-Token' + resp.headers = headers resp.status = code if code > 399: -- cgit