Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BGDIINF_SB-2704: set Cache-Control Header for temporary 5xx responses #120

Merged
merged 2 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ def add_cors_header(response):
return response


# Set Cache Headers
@app.after_request
def add_cache_header(response):
if request.method not in ('GET', 'HEAD', 'OPTIONS'):
return response

# overwrite with these 5xx cache settings
ltclm marked this conversation as resolved.
Show resolved Hide resolved
# no cache on these 5xx errors, they are supposed to be temporary
if response.status_code in (502, 503, 504, 507):
response.headers['Cache-Control'] = 'no-cache'
# short cache duration for other 5xx errors
elif response.status_code >= 500:
response.headers['Cache-Control'] = 'public, max-age=10'
return response


# Helper method for add_cors_header
def get_registered_method(url_rule):
'''Returns the list of registered method for the given endpoint'''
Expand Down
9 changes: 8 additions & 1 deletion tests/unit_tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def __liveness_test_get_request(self, headers):
def __readiness_test_get_request(self, headers, expected_status=200):
response = self.test_instance.get(url_for('readiness'), headers=headers)
self.check_response(response, expected_status=expected_status)
self.assertNotIn('Cache-Control', response.headers)
if expected_status < 500:
self.assertNotIn('Cache-Control', response.headers)
elif expected_status in (502, 503, 504, 507):
self.assertIn('Cache-Control', response.headers)
self.assertIn('no-cache', response.headers['Cache-Control'])
elif expected_status >= 500:
self.assertIn('Cache-Control', response.headers)
self.assertIn('max-age', response.headers['Cache-Control'])
self.assertEqual(response.content_type, "application/json")

def test_liveness_intern_origin(self):
Expand Down