Skip to content

Commit

Permalink
bug 1285969: Set cache control header for public app (mozilla-releng#98
Browse files Browse the repository at this point in the history
…). r=rail,mostlygeek
  • Loading branch information
bhearsum authored Jul 13, 2016
1 parent 8d5ad72 commit 2bd805a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
6 changes: 3 additions & 3 deletions auslib/dockerflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def version():
if version_file and path.exists(version_file):
with open(app.config["VERSION_FILE"]) as f:
version_json = f.read()
return Response(version_json, mimetype="application/json")
return Response(version_json, mimetype="application/json", headers={"Cache-Control": "no-cache"})
else:
return jsonify({
"source": "https://github.com/mozilla/balrog",
Expand All @@ -30,11 +30,11 @@ def heartbeat():
# Counting the rules should be a trivial enough operation that it won't
# cause notable load, but will verify that the database works.
dbo.rules.countRules()
return Response("OK!", headers={"Cache-Control": "max-age: 20"})
return Response("OK!", headers={"Cache-Control": "no-cache"})

@app.route("/__lbheartbeat__")
def lbheartbeat():
"""Per the Dockerflow spec:
Respond to /__lbheartbeat__ with an HTTP 200. This is for load balancer
checks and should not check any dependent services."""
return "OK!"
return Response("OK!", headers={"Cache-Control": "no-cache"})
3 changes: 2 additions & 1 deletion auslib/test/admin/views/test_dockerflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def testHeartbeat(self):
ret = self.client.get("/__heartbeat__")
self.assertEqual(ret.status_code, 200)
self.assertEqual(cr.call_count, 1)
self.assertTrue("Cache-Control" in ret.headers)
self.assertEqual(ret.headers["Cache-Control"], "no-cache")

def testHeartbeatWithException(self):
with mock.patch("auslib.global_state.dbo.rules.countRules") as cr:
Expand All @@ -33,3 +33,4 @@ def testHeartbeatWithException(self):
def testLbHeartbeat(self):
ret = self.client.get("/__lbheartbeat__")
self.assertEqual(ret.status_code, 200)
self.assertEqual(ret.headers["Cache-Control"], "no-cache")
14 changes: 14 additions & 0 deletions auslib/test/web/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,20 @@ def setUp(self):
self.client = app.test_client()
self.view = ClientRequestView()

def testCacheControlIsSet(self):
ret = self.client.get('/update/3/c/15.0/1/p/l/a/a/default/a/update.xml')
self.assertEqual(ret.headers.get("Cache-Control"), "public,max-age=60")

def testCacheControlIsNotSetFor404(self):
ret = self.client.get('/whizzybang')
self.assertEqual(ret.headers.get("Cache-Control"), None)

def testCacheControlIsNotSetFor500(self):
with mock.patch('auslib.web.views.client.ClientRequestView.get') as m:
m.side_effect = Exception('I break!')
ret = self.client.get('/update/4/b/1.0/1/p/l/a/a/a/a/1/update.xml')
self.assertEqual(ret.headers.get("Cache-Control"), None)

def testEmptySnippetOn404(self):
ret = self.client.get('/whizzybang')
self.assertEqual(ret.status_code, 200)
Expand Down
3 changes: 2 additions & 1 deletion auslib/test/web/test_dockerflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def testHeartbeat(self):
ret = self.client.get("/__heartbeat__")
self.assertEqual(ret.status_code, 200)
self.assertEqual(cr.call_count, 1)
self.assertTrue("Cache-Control" in ret.headers)
self.assertEqual(ret.headers["Cache-Control"], "no-cache")

def testHeartbeatWithException(self):
with mock.patch("auslib.global_state.dbo.rules.countRules") as cr:
Expand All @@ -33,3 +33,4 @@ def testHeartbeatWithException(self):
def testLbHeartbeat(self):
ret = self.client.get("/__lbheartbeat__")
self.assertEqual(ret.status_code, 200)
self.assertEqual(ret.headers["Cache-Control"], "no-cache")
5 changes: 4 additions & 1 deletion auslib/web/views/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@


class ClientRequestView(MethodView):

def __init__(self, *args, **kwargs):
# By default, we want a cache that can be shared across requests from different users ("public")
# and a maximum age of 60 seconds, to keep our TTL low.
self.cacheControl = app.config.get("CACHE_CONTROL", "public,max-age=60")
self.log = logging.getLogger(self.__class__.__name__)
MethodView.__init__(self, *args, **kwargs)

Expand Down Expand Up @@ -101,5 +103,6 @@ def get(self, **url):
xml = "\n".join(xml)
self.log.debug("Sending XML: %s", xml)
response = make_response(xml)
response.headers["Cache-Control"] = self.cacheControl
response.mimetype = "text/xml"
return response
3 changes: 3 additions & 0 deletions uwsgi/public.wsgi
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ application.config["SPECIAL_FORCE_HOSTS"] = SPECIAL_FORCE_HOSTS
# about the current code (version number, commit hash), but doesn't exist in
# the repo itself
application.config["VERSION_FILE"] = "/app/version.json"

if os.environ.get("CACHE_CONTROL"):
application.config["CACHE_CONTROL"] = os.environ["CACHE_CONTROL"]

0 comments on commit 2bd805a

Please sign in to comment.