Skip to content

Commit

Permalink
Merge pull request #123 from geoadmin/bug-BGDIINF_SB-2879-cache-header
Browse files Browse the repository at this point in the history
BGDIINF_SB-2879: Added cache-control header
  • Loading branch information
ltshb authored May 2, 2023
2 parents 3ed4597 + bd90aa3 commit 7d775dc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ The service is configured by Environment Variable:
| DTM_BASE_PATH | `'/var/local/profile/'` | Raster and COMB files location |
| PRELOAD_RASTER_FILES | `False` | Preload raster files at startup. If not set they will be loaded during first request |
| ALTI_WORKERS | `0` | Number of workers. `0` or negative value means that the number of worker are computed from the number of cpu |
| DFT_CACHE_HEADER | `public, max-age=86400` | Default cache settings for successful GET, HEAD and OPTIONS requests |
2 changes: 2 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def add_cache_header(response):
# short cache duration for other 5xx errors
elif response.status_code >= 500:
response.headers['Cache-Control'] = 'public, max-age=10'
else:
response.headers['Cache-Control'] = settings.DFT_CACHE_HEADER
return response


Expand Down
1 change: 1 addition & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

DTM_BASE_PATH = Path(os.getenv('DTM_BASE_PATH', '/var/local/profile/'))
PRELOAD_RASTER_FILES = strtobool(os.getenv('PRELOAD_RASTER_FILES', 'False'))
DFT_CACHE_HEADER = os.getenv('DFT_CACHE_HEADER', 'public, max-age=86400')

TRAP_HTTP_EXCEPTIONS = True

Expand Down
9 changes: 0 additions & 9 deletions tests/unit_tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ class CheckerTests(BaseRouteTestCase):
def __liveness_test_get_request(self, headers):
response = self.test_instance.get(url_for('liveness'), headers=headers)
self.check_response(response)
self.assertNotIn('Cache-Control', response.headers)
self.assertEqual(response.content_type, "application/json")

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)
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
12 changes: 12 additions & 0 deletions tests/unit_tests/test_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ def test_do_not_fail_when_no_origin(self, mock_georaster_utils):
expected_height=HEIGHT_DTM2
)

@patch('app.routes.georaster_utils')
def test_cache_header(self, mock_georaster_utils):
response = self.__prepare_mock_and_test_get(
mock_georaster_utils=mock_georaster_utils,
params={
'easting': EAST_LV95, 'northing': NORTH_LV95
}
)
self.assertIn('Cache-Control', response.headers)
self.assertIn('public', response.headers['Cache-Control'])
self.assertIn('max-age=', response.headers['Cache-Control'])

@patch('app.routes.georaster_utils')
def test_height_no_sr_assuming_lv03(self, mock_georaster_utils):
self.__assert_height(
Expand Down
13 changes: 13 additions & 0 deletions tests/unit_tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ def test_do_not_fail_when_no_origin(self, mock_georaster_utils):
expected_status=200
)

@patch('app.routes.georaster_utils')
def test_cache_header(self, mock_georaster_utils):
response = self.prepare_mock_and_test_get(
mock_georaster_utils=mock_georaster_utils,
params={
'sr': 2056, 'geom': create_json(4, 2056)
},
expected_status=200
)
self.assertIn('Cache-Control', response.headers)
self.assertIn('public', response.headers['Cache-Control'])
self.assertIn('max-age=', response.headers['Cache-Control'])

@patch('app.routes.georaster_utils')
def test_profile_invalid_sr_json_valid(self, mock_georaster_utils):
resp = self.prepare_mock_and_test_get(
Expand Down

0 comments on commit 7d775dc

Please sign in to comment.