зеркало из https://github.com/mozilla/PollBot.git
Add Cache-Control headers.
This commit is contained in:
Родитель
6d086d06ee
Коммит
ac4c75a866
|
@ -12,9 +12,10 @@ API Changelog
|
|||
- archive-date and archive-date-l10n return a missing status for
|
||||
anything else than nightly versions.
|
||||
- Add the devedition-beta-versions-matches endpoint and task.
|
||||
- Add Cache-Control headers.
|
||||
|
||||
|
||||
1.0 (2017-08-08)
|
||||
----------------
|
||||
|
||||
- First version of PollBot
|
||||
- First version of PollBot.
|
||||
|
|
|
@ -13,6 +13,7 @@ CHANGELOG
|
|||
- Fix the ESR download links task url (#66)
|
||||
- Add a task to validate if devedition and beta version matches (#78)
|
||||
- Redirects URL ending by a / to URL without the / in case of 404. (#54)
|
||||
- Add Cache-Control headers (#43)
|
||||
|
||||
|
||||
0.1.0 (2017-08-08)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import logging
|
||||
from aiohttp import web
|
||||
import os
|
||||
|
||||
logger = logging.getLogger(__package__)
|
||||
|
||||
|
@ -8,8 +9,26 @@ def setup_middlewares(app):
|
|||
error_middleware = error_pages({404: handle_404,
|
||||
500: handle_500})
|
||||
app.middlewares.append(error_middleware)
|
||||
app.middlewares.append(cache_control_middleware)
|
||||
|
||||
|
||||
# Cache-Control middleware
|
||||
CACHE_MAX_AGE = int(os.getenv("CACHE_MAX_AGE", "30"))
|
||||
NO_CACHE_ENDPOINTS = ['/v1/', '/v1/__version__', '/v1/__heartbeat__', '/v1/__lbheartbeat__']
|
||||
|
||||
|
||||
async def cache_control_middleware(app, handler):
|
||||
async def middleware_handler(request):
|
||||
response = await handler(request)
|
||||
cache_control_value = "public; max-age={}".format(CACHE_MAX_AGE)
|
||||
if request.path in NO_CACHE_ENDPOINTS or CACHE_MAX_AGE <= 0:
|
||||
cache_control_value = "no-cache"
|
||||
response.headers.setdefault("Cache-Control", cache_control_value)
|
||||
return response
|
||||
return middleware_handler
|
||||
|
||||
|
||||
# Error page middlewares
|
||||
def error_pages(overrides):
|
||||
async def middleware(app, handler):
|
||||
async def middleware_handler(request):
|
||||
|
|
|
@ -8,6 +8,7 @@ from aiohttp import web
|
|||
|
||||
from pollbot import __version__ as pollbot_version, HTTP_API_VERSION
|
||||
from pollbot.app import get_app
|
||||
from pollbot.middlewares import NO_CACHE_ENDPOINTS
|
||||
from pollbot.exceptions import TaskError
|
||||
from pollbot.views.release import status_response
|
||||
from pollbot.utils import Status
|
||||
|
@ -380,3 +381,23 @@ async def test_ongoing_versions_view(cli):
|
|||
assert "release" in body
|
||||
assert "beta" in body
|
||||
assert "nightly" in body
|
||||
|
||||
|
||||
@pytest.mark.parametrize("endpoint", NO_CACHE_ENDPOINTS)
|
||||
async def test_endpoint_have_got_cache_control_headers(cli, endpoint):
|
||||
resp = await cli.get(endpoint)
|
||||
assert "Cache-Control" in resp.headers
|
||||
assert resp.headers["Cache-Control"] == "no-cache"
|
||||
|
||||
|
||||
async def test_product_endpoint_have_got_cache_control_headers(cli):
|
||||
resp = await cli.get("/v1/firefox/54.0")
|
||||
assert "Cache-Control" in resp.headers
|
||||
assert resp.headers["Cache-Control"] == "public; max-age=30"
|
||||
|
||||
|
||||
async def test_cache_control_header_max_age_can_be_parametrized(cli):
|
||||
with mock.patch("pollbot.middlewares.CACHE_MAX_AGE", 10):
|
||||
resp = await cli.get("/v1/firefox/54.0")
|
||||
assert "Cache-Control" in resp.headers
|
||||
assert resp.headers["Cache-Control"] == "public; max-age=10"
|
||||
|
|
Загрузка…
Ссылка в новой задаче