2013-04-03 03:00:55 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2015-04-17 23:38:20 +02:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2013-04-03 03:00:55 +02:00
|
|
|
|
|
|
|
FULL_RESPONSE = "Test conditional get response"
|
|
|
|
LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47)
|
|
|
|
LAST_MODIFIED_STR = "Sun, 21 Oct 2007 23:21:47 GMT"
|
|
|
|
LAST_MODIFIED_NEWER_STR = "Mon, 18 Oct 2010 16:56:23 GMT"
|
|
|
|
LAST_MODIFIED_INVALID_STR = "Mon, 32 Oct 2010 16:56:23 GMT"
|
|
|
|
EXPIRED_LAST_MODIFIED_STR = "Sat, 20 Oct 2007 23:21:47 GMT"
|
2016-09-01 15:32:20 +02:00
|
|
|
ETAG = '"b4246ffc4f62314ca13147c9d4f76974"'
|
2016-09-13 05:26:24 +02:00
|
|
|
WEAK_ETAG = 'W/"b4246ffc4f62314ca13147c9d4f76974"' # weak match to ETAG
|
2016-09-01 15:32:20 +02:00
|
|
|
EXPIRED_ETAG = '"7fae4cd4b0f81e7d2914700043aa8ed6"'
|
2013-04-03 03:00:55 +02:00
|
|
|
|
2013-11-03 05:36:09 +01:00
|
|
|
|
2014-04-05 08:04:46 +02:00
|
|
|
@override_settings(ROOT_URLCONF="conditional_processing.urls")
|
2015-04-17 23:38:20 +02:00
|
|
|
class ConditionalGet(SimpleTestCase):
|
2013-04-03 03:00:55 +02:00
|
|
|
def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, FULL_RESPONSE.encode())
|
2017-06-06 21:37:14 +02:00
|
|
|
if response.request["REQUEST_METHOD"] in ("GET", "HEAD"):
|
|
|
|
if check_last_modified:
|
2020-07-14 13:32:24 +02:00
|
|
|
self.assertEqual(response.headers["Last-Modified"], LAST_MODIFIED_STR)
|
2017-06-06 21:37:14 +02:00
|
|
|
if check_etag:
|
2020-07-14 13:32:24 +02:00
|
|
|
self.assertEqual(response.headers["ETag"], ETAG)
|
2017-06-06 21:37:14 +02:00
|
|
|
else:
|
2020-07-14 13:32:24 +02:00
|
|
|
self.assertNotIn("Last-Modified", response.headers)
|
|
|
|
self.assertNotIn("ETag", response.headers)
|
2013-04-03 03:00:55 +02:00
|
|
|
|
|
|
|
def assertNotModified(self, response):
|
|
|
|
self.assertEqual(response.status_code, 304)
|
|
|
|
self.assertEqual(response.content, b"")
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_without_conditions(self):
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_if_modified_since(self):
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_NEWER_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_INVALID_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
2014-12-20 22:14:46 +01:00
|
|
|
def test_if_unmodified_since(self):
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_NEWER_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_INVALID_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_if_none_match(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = EXPIRED_ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
|
|
|
# Several etags in If-None-Match is a bit exotic but why not?
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = "%s, %s" % (ETAG, EXPIRED_ETAG)
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
|
2016-09-13 05:26:24 +02:00
|
|
|
def test_weak_if_none_match(self):
|
|
|
|
"""
|
|
|
|
If-None-Match comparisons use weak matching, so weak and strong ETags
|
|
|
|
with the same value result in a 304 response.
|
|
|
|
"""
|
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
|
|
|
response = self.client.get("/condition/weak_etag/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.put("/condition/weak_etag/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = WEAK_ETAG
|
|
|
|
response = self.client.get("/condition/weak_etag/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.put("/condition/weak_etag/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
|
|
|
def test_all_if_none_match(self):
|
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = "*"
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
response = self.client.get("/condition/no_etag/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False, check_etag=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_if_match(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = ETAG
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = EXPIRED_ETAG
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
|
|
|
def test_weak_if_match(self):
|
|
|
|
"""
|
|
|
|
If-Match comparisons use strong matching, so any comparison involving
|
|
|
|
a weak ETag return a 412 response.
|
|
|
|
"""
|
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = ETAG
|
|
|
|
response = self.client.get("/condition/weak_etag/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = WEAK_ETAG
|
|
|
|
response = self.client.get("/condition/weak_etag/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
|
|
|
def test_all_if_match(self):
|
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = "*"
|
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
response = self.client.get("/condition/no_etag/")
|
2013-04-03 03:00:55 +02:00
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_both_headers(self):
|
2022-11-04 13:33:09 +01:00
|
|
|
# See RFC 9110 Section 13.2.2.
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
|
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
2016-09-13 05:26:24 +02:00
|
|
|
self.assertNotModified(response)
|
2013-04-03 03:00:55 +02:00
|
|
|
|
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = EXPIRED_ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
2014-12-20 22:14:46 +01:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = EXPIRED_ETAG
|
2014-12-20 22:14:46 +01:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
|
|
|
def test_both_headers_2(self):
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = ETAG
|
2014-12-20 22:14:46 +01:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertFullResponse(response)
|
|
|
|
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = ETAG
|
2014-12-20 22:14:46 +01:00
|
|
|
response = self.client.get("/condition/")
|
2016-09-13 05:26:24 +02:00
|
|
|
self.assertFullResponse(response)
|
2014-12-20 22:14:46 +01:00
|
|
|
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = EXPIRED_ETAG
|
2014-12-20 22:14:46 +01:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
self.client.defaults["HTTP_IF_MATCH"] = EXPIRED_ETAG
|
2014-12-20 22:14:46 +01:00
|
|
|
response = self.client.get("/condition/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_1(self):
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.get("/condition/etag/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_2(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/etag/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.get("/condition/last_modified/")
|
|
|
|
self.assertFullResponse(response, check_etag=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_3(self):
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified/")
|
|
|
|
self.assertFullResponse(response, check_etag=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_4(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = EXPIRED_ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/etag/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_5(self):
|
2013-04-03 03:00:55 +02:00
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified2/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.get("/condition/etag2/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_single_condition_6(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/etag2/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
response = self.client.get("/condition/last_modified2/")
|
|
|
|
self.assertFullResponse(response, check_etag=False)
|
|
|
|
|
2014-12-20 22:14:46 +01:00
|
|
|
def test_single_condition_7(self):
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
response = self.client.get("/condition/etag/")
|
2016-09-13 05:26:24 +02:00
|
|
|
self.assertEqual(response.status_code, 412)
|
2014-12-20 22:14:46 +01:00
|
|
|
|
|
|
|
def test_single_condition_8(self):
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified/")
|
|
|
|
self.assertFullResponse(response, check_etag=False)
|
|
|
|
|
|
|
|
def test_single_condition_9(self):
|
|
|
|
self.client.defaults["HTTP_IF_UNMODIFIED_SINCE"] = EXPIRED_LAST_MODIFIED_STR
|
|
|
|
response = self.client.get("/condition/last_modified2/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
|
|
|
response = self.client.get("/condition/etag2/")
|
2016-09-13 05:26:24 +02:00
|
|
|
self.assertEqual(response.status_code, 412)
|
2014-12-20 22:14:46 +01:00
|
|
|
|
2014-10-20 22:22:02 +02:00
|
|
|
def test_single_condition_head(self):
|
|
|
|
self.client.defaults["HTTP_IF_MODIFIED_SINCE"] = LAST_MODIFIED_STR
|
|
|
|
response = self.client.head("/condition/")
|
|
|
|
self.assertNotModified(response)
|
|
|
|
|
2016-09-01 15:32:20 +02:00
|
|
|
def test_unquoted(self):
|
|
|
|
"""
|
|
|
|
The same quoted ETag should be set on the header regardless of whether
|
|
|
|
etag_func() in condition() returns a quoted or an unquoted ETag.
|
|
|
|
"""
|
|
|
|
response_quoted = self.client.get("/condition/etag/")
|
|
|
|
response_unquoted = self.client.get("/condition/unquoted_etag/")
|
|
|
|
self.assertEqual(response_quoted["ETag"], response_unquoted["ETag"])
|
|
|
|
|
|
|
|
# It's possible that the matching algorithm could use the wrong value even
|
|
|
|
# if the ETag header is set correctly correctly (as tested by
|
|
|
|
# test_unquoted()), so check that the unquoted value is matched.
|
|
|
|
def test_unquoted_if_none_match(self):
|
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = ETAG
|
|
|
|
response = self.client.get("/condition/unquoted_etag/")
|
|
|
|
self.assertNotModified(response)
|
2016-09-13 05:26:24 +02:00
|
|
|
response = self.client.put("/condition/unquoted_etag/")
|
|
|
|
self.assertEqual(response.status_code, 412)
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = EXPIRED_ETAG
|
|
|
|
response = self.client.get("/condition/unquoted_etag/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|
|
|
|
|
2014-07-08 01:08:42 +02:00
|
|
|
def test_invalid_etag(self):
|
2016-09-01 15:32:20 +02:00
|
|
|
self.client.defaults["HTTP_IF_NONE_MATCH"] = '"""'
|
2013-04-03 03:00:55 +02:00
|
|
|
response = self.client.get("/condition/etag/")
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|