Skip to content

Commit 1d138be

Browse files
Merge pull request #643 from geoadmin/PB-1403-test
Deduplicate and simplify Expires header test code.
2 parents 900c5ed + 4e7c66b commit 1d138be

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

app/tests/tests_10/test_generic_api.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ def test_delete_precondition(self):
422422

423423
class ApiCacheHeaderTestCase(S3TestMixin, MockS3PerTestMixin, StacBaseTestCase):
424424

425+
def assert_expires(
426+
self, response, before, how_soon=timedelta(seconds=0), tolerance=timedelta(seconds=1)
427+
):
428+
self.assertIn('Expires', response.headers)
429+
expires = datetime.strptime(response['Expires'], '%a, %d %b %Y %H:%M:%S GMT')
430+
expected = before + how_soon
431+
self.assertAlmostEqual(expected, expires, delta=tolerance)
432+
425433
def setUp(self):
426434
super().setUp()
427435
self.factory = Factory()
@@ -455,10 +463,7 @@ def test_get_cache_header_collections_and_search_default(self):
455463
'max-age=0, no-cache, no-store, must-revalidate, private',
456464
msg='Wrong cache-control values'
457465
)
458-
459-
self.assertTrue(response.has_header('Expires'), msg="Expires header missing")
460-
expires = datetime.strptime(response['Expires'], '%a, %d %b %Y %H:%M:%S GMT')
461-
self.assertAlmostEqual((expires - now).total_seconds(), 0, delta=1)
466+
self.assert_expires(response, now)
462467

463468
@override_settings(COLLECTIONS_AGGREGATE_CACHE_SECONDS=10)
464469
def test_get_cache_header_collections_and_search(self):
@@ -477,10 +482,9 @@ def test_get_cache_header_collections_and_search(self):
477482
'max-age=10, public',
478483
msg='Wrong cache-control values'
479484
)
480-
481-
self.assertTrue(response.has_header('Expires'), msg="Expires header missing")
482-
expires = datetime.strptime(response['Expires'], '%a, %d %b %Y %H:%M:%S GMT')
483-
self.assertAlmostEqual((expires - now).total_seconds(), 10, delta=1)
485+
self.assert_expires(
486+
response, now, how_soon=timedelta(seconds=10), tolerance=timedelta(seconds=1)
487+
)
484488

485489
@override_settings(CACHE_MIDDLEWARE_SECONDS=3600)
486490
def test_get_cache_header(self):
@@ -505,13 +509,9 @@ def test_get_cache_header(self):
505509
'max-age=3600, public',
506510
msg='Wrong cache-control values'
507511
)
508-
509-
self.assertTrue(response.has_header('Expires'), msg="Expires header missing")
510-
511-
expires = datetime.strptime(response['Expires'], '%a, %d %b %Y %H:%M:%S GMT')
512-
self.assertAlmostEqual((expires - now).total_seconds(),
513-
timedelta(seconds=3600).total_seconds(),
514-
delta=2)
512+
self.assert_expires(
513+
response, now, how_soon=timedelta(seconds=3600), tolerance=timedelta(seconds=2)
514+
)
515515

516516
def test_get_asset_object_cache_header(self):
517517
key = get_asset_path(self.item.model, self.asset["name"])
@@ -542,13 +542,9 @@ def test_head_cache_header(self):
542542
'max-age=3600, public',
543543
msg='Wrong cache-control values'
544544
)
545-
546-
self.assertTrue(response.has_header('Expires'), msg="Expires header missing")
547-
548-
expires = datetime.strptime(response['Expires'], '%a, %d %b %Y %H:%M:%S GMT')
549-
self.assertAlmostEqual((expires - now).total_seconds(),
550-
timedelta(seconds=3600).total_seconds(),
551-
delta=2)
545+
self.assert_expires(
546+
response, now, how_soon=timedelta(seconds=3600), tolerance=timedelta(seconds=2)
547+
)
552548

553549

554550
class ApiDynamicCacheHeaderTestCase(S3TestMixin, MockS3PerTestMixin, StacBaseTestCase):

0 commit comments

Comments
 (0)