2012-01-10 05:42:03 +08:00
|
|
|
import gzip
|
2013-01-02 05:28:48 +08:00
|
|
|
import random
|
|
|
|
import re
|
2017-01-07 19:11:46 +08:00
|
|
|
import struct
|
2015-01-28 20:35:27 +08:00
|
|
|
from io import BytesIO
|
2017-01-07 19:11:46 +08:00
|
|
|
from urllib.parse import quote
|
2011-05-06 04:49:26 +08:00
|
|
|
|
2010-12-04 15:28:12 +08:00
|
|
|
from django.conf import settings
|
2011-05-06 04:49:26 +08:00
|
|
|
from django.core import mail
|
2015-06-24 18:11:43 +08:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2014-11-05 04:19:10 +08:00
|
|
|
from django.http import (
|
2015-04-28 13:23:42 +08:00
|
|
|
FileResponse,
|
|
|
|
HttpRequest,
|
|
|
|
HttpResponse,
|
|
|
|
HttpResponseNotFound,
|
|
|
|
HttpResponsePermanentRedirect,
|
|
|
|
HttpResponseRedirect,
|
|
|
|
StreamingHttpResponse,
|
2014-11-05 04:19:10 +08:00
|
|
|
)
|
2011-05-31 06:27:47 +08:00
|
|
|
from django.middleware.clickjacking import XFrameOptionsMiddleware
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.middleware.common import BrokenLinkEmailsMiddleware, CommonMiddleware
|
2012-01-10 05:42:03 +08:00
|
|
|
from django.middleware.gzip import GZipMiddleware
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.middleware.http import ConditionalGetMiddleware
|
2017-09-03 09:09:22 +08:00
|
|
|
from django.test import RequestFactory, SimpleTestCase, override_settings
|
2017-01-07 19:11:46 +08:00
|
|
|
|
|
|
|
int2byte = struct.Struct(">B").pack
|
2012-08-15 23:41:09 +08:00
|
|
|
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response_empty(request):
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
|
|
|
def get_response_404(request):
|
|
|
|
return HttpResponseNotFound()
|
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="middleware.urls")
|
2015-04-18 05:38:20 +08:00
|
|
|
class CommonMiddlewareTest(SimpleTestCase):
|
2008-10-08 16:36:41 +08:00
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
rf = RequestFactory()
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_have_slash(self):
|
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
URLs with slashes should go unmolested.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash/")
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response_404).process_request(request))
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_slashless_resource(self):
|
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
Matches to explicit slashless URLs should go unmolested.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
2021-07-23 14:48:16 +08:00
|
|
|
return HttpResponse("Here's the text of the web page.")
|
2019-09-27 01:06:35 +08:00
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/noslash")
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response).process_request(request))
|
|
|
|
self.assertEqual(
|
|
|
|
CommonMiddleware(get_response)(request).content,
|
2021-07-23 14:48:16 +08:00
|
|
|
b"Here's the text of the web page.",
|
2019-09-27 01:06:35 +08:00
|
|
|
)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_slashless_unknown(self):
|
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
APPEND_SLASH should not redirect to unknown resources.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/unknown")
|
2019-09-27 01:06:35 +08:00
|
|
|
response = CommonMiddleware(get_response_404)(request)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_redirect(self):
|
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
APPEND_SLASH should redirect slashless URLs to a valid pattern.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2015-03-26 19:58:43 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_append_slash_redirect_querystring(self):
|
|
|
|
"""
|
|
|
|
APPEND_SLASH should preserve querystrings when redirecting.
|
|
|
|
"""
|
|
|
|
request = self.rf.get("/slash?test=1")
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = CommonMiddleware(get_response_404)(request)
|
|
|
|
self.assertEqual(resp.url, "/slash/?test=1")
|
2015-03-26 19:58:43 +08:00
|
|
|
|
2016-11-06 17:38:36 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_append_slash_redirect_querystring_have_slash(self):
|
|
|
|
"""
|
|
|
|
APPEND_SLASH should append slash to path when redirecting a request
|
|
|
|
with a querystring ending with slash.
|
|
|
|
"""
|
|
|
|
request = self.rf.get("/slash?test=slash/")
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = CommonMiddleware(get_response_404)(request)
|
|
|
|
self.assertIsInstance(resp, HttpResponsePermanentRedirect)
|
|
|
|
self.assertEqual(resp.url, "/slash/?test=slash/")
|
2016-11-06 17:38:36 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, DEBUG=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
While in debug mode, an exception is raised with a warning
|
2015-01-13 21:42:21 +08:00
|
|
|
when a failed attempt is made to POST, PUT, or PATCH to an URL which
|
|
|
|
would normally be redirected to a slashed version.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2015-01-13 21:42:21 +08:00
|
|
|
msg = "maintaining %s data. Change your form to point to testserver/slash/"
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2007-12-03 07:25:55 +08:00
|
|
|
request.method = "POST"
|
2015-12-31 22:22:14 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
2019-09-27 01:06:35 +08:00
|
|
|
CommonMiddleware(get_response_404)(request)
|
2015-01-13 21:42:21 +08:00
|
|
|
request = self.rf.get("/slash")
|
|
|
|
request.method = "PUT"
|
2015-12-31 22:22:14 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
2019-09-27 01:06:35 +08:00
|
|
|
CommonMiddleware(get_response_404)(request)
|
2015-01-13 21:42:21 +08:00
|
|
|
request = self.rf.get("/slash")
|
|
|
|
request.method = "PATCH"
|
2015-12-31 22:22:14 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
2019-09-27 01:06:35 +08:00
|
|
|
CommonMiddleware(get_response_404)(request)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=False)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_disabled(self):
|
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
Disabling append slash functionality should leave slashless URLs alone.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
2007-12-03 07:25:55 +08:00
|
|
|
|
2020-10-20 15:14:48 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_append_slash_opt_out(self):
|
|
|
|
"""
|
|
|
|
Views marked with @no_append_slash should be left alone.
|
|
|
|
"""
|
|
|
|
request = self.rf.get("/sensitive_fbv")
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
|
|
|
|
|
|
|
request = self.rf.get("/sensitive_cbv")
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-03 07:25:55 +08:00
|
|
|
def test_append_slash_quoted(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
URLs which require quoting should be redirected to their slash version.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get(quote("/needsquoting#"))
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404)(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "/needsquoting%23/")
|
2008-10-07 16:22:50 +08:00
|
|
|
|
2018-07-25 04:18:17 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_append_slash_leading_slashes(self):
|
|
|
|
"""
|
|
|
|
Paths starting with two slashes are escaped to prevent open redirects.
|
|
|
|
If there's a URL pattern that allows paths to start with two slashes, a
|
|
|
|
request with path //evil.com must not redirect to //evil.com/ (appended
|
|
|
|
slash) which is a schemaless absolute URL. The browser would navigate
|
|
|
|
to evil.com/.
|
|
|
|
"""
|
|
|
|
# Use 4 slashes because of RequestFactory behavior.
|
|
|
|
request = self.rf.get("////evil.com/security")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404).process_request(request)
|
2018-07-25 04:18:17 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(r.url, "/%2Fevil.com/security/")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404)(request)
|
2018-07-25 04:18:17 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(r.url, "/%2Fevil.com/security/")
|
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
2008-10-07 16:22:50 +08:00
|
|
|
def test_prepend_www(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/path/")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/path/")
|
2008-10-07 16:22:50 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2008-10-07 16:22:50 +08:00
|
|
|
def test_prepend_www_append_slash_have_slash(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash/")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/slash/")
|
2008-10-07 16:22:50 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2008-10-07 16:22:50 +08:00
|
|
|
def test_prepend_www_append_slash_slashless(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/slash/")
|
2010-12-04 15:28:12 +08:00
|
|
|
|
2015-10-23 02:46:42 +08:00
|
|
|
# The following tests examine expected behavior given a custom URLconf that
|
2010-03-08 04:03:04 +08:00
|
|
|
# overrides the default one through the request object.
|
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_have_slash_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
URLs with slashes should go unmolested.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash/")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response_404).process_request(request))
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_slashless_resource_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
Matches to explicit slashless URLs should go unmolested.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
2021-07-23 14:48:16 +08:00
|
|
|
return HttpResponse("web content")
|
2019-09-27 01:06:35 +08:00
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/noslash")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response).process_request(request))
|
2021-07-23 14:48:16 +08:00
|
|
|
self.assertEqual(
|
|
|
|
CommonMiddleware(get_response)(request).content, b"web content"
|
|
|
|
)
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_slashless_unknown_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
APPEND_SLASH should not redirect to unknown resources.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/unknown")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response_404).process_request(request))
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_redirect_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
APPEND_SLASH should redirect slashless URLs to a valid pattern.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404)(request)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertIsNotNone(
|
|
|
|
r,
|
|
|
|
"CommonMiddleware failed to return APPEND_SLASH redirect using "
|
|
|
|
"request.urlconf",
|
|
|
|
)
|
2013-01-02 05:46:54 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertEqual(r.url, "/customurlconf/slash/")
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, DEBUG=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
While in debug mode, an exception is raised with a warning
|
2013-01-02 05:46:54 +08:00
|
|
|
when a failed attempt is made to POST to an URL which would normally be
|
|
|
|
redirected to a slashed version.
|
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2013-01-02 05:46:54 +08:00
|
|
|
request.method = "POST"
|
2015-12-31 22:22:14 +08:00
|
|
|
with self.assertRaisesMessage(RuntimeError, "end in a slash"):
|
2019-09-27 01:06:35 +08:00
|
|
|
CommonMiddleware(get_response_404)(request)
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=False)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_disabled_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-03-26 19:58:43 +08:00
|
|
|
Disabling append slash functionality should leave slashless URLs alone.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertIsNone(CommonMiddleware(get_response_404).process_request(request))
|
|
|
|
self.assertEqual(CommonMiddleware(get_response_404)(request).status_code, 404)
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_append_slash_quoted_custom_urlconf(self):
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
URLs which require quoting should be redirected to their slash version.
|
2013-01-02 05:46:54 +08:00
|
|
|
"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get(quote("/customurlconf/needsquoting#"))
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404)(request)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertIsNotNone(
|
|
|
|
r,
|
|
|
|
"CommonMiddleware failed to return APPEND_SLASH redirect using "
|
|
|
|
"request.urlconf",
|
|
|
|
)
|
2013-01-02 05:46:54 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "/customurlconf/needsquoting%23/")
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_prepend_www_custom_urlconf(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/path/")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2013-01-02 05:46:54 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/customurlconf/path/")
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash/")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2013-01-02 05:46:54 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/customurlconf/slash/")
|
2010-03-08 04:03:04 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2010-03-08 04:03:04 +08:00
|
|
|
def test_prepend_www_append_slash_slashless_custom_urlconf(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/customurlconf/slash")
|
2013-02-26 20:19:18 +08:00
|
|
|
request.urlconf = "middleware.extra_urls"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2013-01-02 05:46:54 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.url, "http://www.testserver/customurlconf/slash/")
|
2011-03-01 22:28:06 +08:00
|
|
|
|
2016-06-18 16:51:38 +08:00
|
|
|
# Tests for the Content-Length header
|
|
|
|
|
|
|
|
def test_content_length_header_added(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
response = HttpResponse("content")
|
|
|
|
self.assertNotIn("Content-Length", response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
response = CommonMiddleware(get_response)(self.rf.get("/"))
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(int(response.headers["Content-Length"]), len(response.content))
|
2016-06-18 16:51:38 +08:00
|
|
|
|
|
|
|
def test_content_length_header_not_added_for_streaming_response(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
response = StreamingHttpResponse("content")
|
|
|
|
self.assertNotIn("Content-Length", response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
response = CommonMiddleware(get_response)(self.rf.get("/"))
|
2016-06-18 16:51:38 +08:00
|
|
|
self.assertNotIn("Content-Length", response)
|
|
|
|
|
|
|
|
def test_content_length_header_not_changed(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
bad_content_length = 500
|
|
|
|
|
|
|
|
def get_response(req):
|
|
|
|
response = HttpResponse()
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["Content-Length"] = bad_content_length
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
response = CommonMiddleware(get_response)(self.rf.get("/"))
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(int(response.headers["Content-Length"]), bad_content_length)
|
2016-06-18 16:51:38 +08:00
|
|
|
|
2012-11-04 04:26:59 +08:00
|
|
|
# Other tests
|
|
|
|
|
2014-11-13 13:08:35 +08:00
|
|
|
@override_settings(DISALLOWED_USER_AGENTS=[re.compile(r"foo")])
|
|
|
|
def test_disallowed_user_agents(self):
|
2015-06-27 22:08:59 +08:00
|
|
|
request = self.rf.get("/slash")
|
|
|
|
request.META["HTTP_USER_AGENT"] = "foo"
|
|
|
|
with self.assertRaisesMessage(PermissionDenied, "Forbidden user agent"):
|
2019-09-27 01:06:35 +08:00
|
|
|
CommonMiddleware(get_response_empty).process_request(request)
|
2014-11-13 13:08:35 +08:00
|
|
|
|
2012-11-04 04:26:59 +08:00
|
|
|
def test_non_ascii_query_string_does_not_crash(self):
|
|
|
|
"""Regression test for #15152"""
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2017-01-12 06:17:25 +08:00
|
|
|
request.META["QUERY_STRING"] = "drink=café"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_empty).process_request(request)
|
2015-04-28 13:23:42 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2012-11-04 04:26:59 +08:00
|
|
|
|
2014-11-05 04:19:10 +08:00
|
|
|
def test_response_redirect_class(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = CommonMiddleware(get_response_404)(request)
|
2014-11-05 04:19:10 +08:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertEqual(r.url, "/slash/")
|
2014-11-05 04:19:10 +08:00
|
|
|
self.assertIsInstance(r, HttpResponsePermanentRedirect)
|
|
|
|
|
|
|
|
def test_response_redirect_class_subclass(self):
|
|
|
|
class MyCommonMiddleware(CommonMiddleware):
|
|
|
|
response_redirect_class = HttpResponseRedirect
|
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
request = self.rf.get("/slash")
|
2019-09-27 01:06:35 +08:00
|
|
|
r = MyCommonMiddleware(get_response_404)(request)
|
2014-11-05 04:19:10 +08:00
|
|
|
self.assertEqual(r.status_code, 302)
|
2015-03-14 06:40:14 +08:00
|
|
|
self.assertEqual(r.url, "/slash/")
|
2014-11-05 04:19:10 +08:00
|
|
|
self.assertIsInstance(r, HttpResponseRedirect)
|
|
|
|
|
2011-05-06 04:49:26 +08:00
|
|
|
|
2013-03-12 06:30:02 +08:00
|
|
|
@override_settings(
|
2015-01-22 00:55:57 +08:00
|
|
|
IGNORABLE_404_URLS=[re.compile(r"foo")],
|
2019-06-29 07:50:43 +08:00
|
|
|
MANAGERS=[("PHD", "PHB@dilbert.com")],
|
2013-03-12 06:30:02 +08:00
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
class BrokenLinkEmailsMiddlewareTest(SimpleTestCase):
|
2013-01-02 05:28:48 +08:00
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
rf = RequestFactory()
|
|
|
|
|
2013-01-02 05:28:48 +08:00
|
|
|
def setUp(self):
|
2014-12-28 04:15:16 +08:00
|
|
|
self.req = self.rf.get("/regular_url/that/does/not/exist")
|
2019-09-27 01:06:35 +08:00
|
|
|
|
|
|
|
def get_response(self, req):
|
|
|
|
return self.client.get(req.path)
|
2013-01-02 05:28:48 +08:00
|
|
|
|
|
|
|
def test_404_error_reporting(self):
|
|
|
|
self.req.META["HTTP_REFERER"] = "/another/url/"
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2013-01-02 05:28:48 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
self.assertIn("Broken", mail.outbox[0].subject)
|
|
|
|
|
|
|
|
def test_404_error_reporting_no_referer(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2013-01-02 05:28:48 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
|
|
|
def test_404_error_reporting_ignored_url(self):
|
|
|
|
self.req.path = self.req.path_info = "foo_url/that/does/not/exist"
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2013-01-02 05:28:48 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2013-05-24 23:55:50 +08:00
|
|
|
def test_custom_request_checker(self):
|
|
|
|
class SubclassedMiddleware(BrokenLinkEmailsMiddleware):
|
2015-12-31 22:22:14 +08:00
|
|
|
ignored_user_agent_patterns = (
|
|
|
|
re.compile(r"Spider.*"),
|
|
|
|
re.compile(r"Robot.*"),
|
|
|
|
)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-05-24 23:55:50 +08:00
|
|
|
def is_ignorable_request(self, request, uri, domain, referer):
|
|
|
|
"""Check user-agent in addition to normal checks."""
|
2017-01-21 21:13:44 +08:00
|
|
|
if super().is_ignorable_request(request, uri, domain, referer):
|
2013-05-24 23:55:50 +08:00
|
|
|
return True
|
|
|
|
user_agent = request.META["HTTP_USER_AGENT"]
|
2015-12-31 22:22:14 +08:00
|
|
|
return any(
|
|
|
|
pattern.search(user_agent)
|
|
|
|
for pattern in self.ignored_user_agent_patterns
|
|
|
|
)
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-05-24 23:55:50 +08:00
|
|
|
self.req.META["HTTP_REFERER"] = "/another/url/"
|
|
|
|
self.req.META["HTTP_USER_AGENT"] = "Spider machine 3.4"
|
2019-09-27 01:06:35 +08:00
|
|
|
SubclassedMiddleware(self.get_response)(self.req)
|
2013-05-24 23:55:50 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
self.req.META["HTTP_USER_AGENT"] = "My user agent"
|
2019-09-27 01:06:35 +08:00
|
|
|
SubclassedMiddleware(self.get_response)(self.req)
|
2013-05-24 23:55:50 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2013-01-02 05:28:48 +08:00
|
|
|
|
2015-08-24 02:54:15 +08:00
|
|
|
def test_referer_equal_to_requested_url(self):
|
|
|
|
"""
|
|
|
|
Some bots set the referer to the current URL to avoid being blocked by
|
|
|
|
an referer check (#25302).
|
|
|
|
"""
|
|
|
|
self.req.META["HTTP_REFERER"] = self.req.path
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2015-08-24 02:54:15 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
2015-11-27 04:27:12 +08:00
|
|
|
|
2015-08-24 02:54:15 +08:00
|
|
|
# URL with scheme and domain should also be ignored
|
|
|
|
self.req.META["HTTP_REFERER"] = "http://testserver%s" % self.req.path
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2015-08-24 02:54:15 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2015-11-27 04:27:12 +08:00
|
|
|
# URL with a different scheme should be ignored as well because bots
|
|
|
|
# tend to use http:// in referers even when browsing HTTPS websites.
|
|
|
|
self.req.META["HTTP_X_PROTO"] = "https"
|
|
|
|
self.req.META["SERVER_PORT"] = 443
|
|
|
|
with self.settings(SECURE_PROXY_SSL_HEADER=("HTTP_X_PROTO", "https")):
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2015-11-27 04:27:12 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2015-08-24 02:54:15 +08:00
|
|
|
def test_referer_equal_to_requested_url_on_another_domain(self):
|
|
|
|
self.req.META["HTTP_REFERER"] = "http://anotherserver%s" % self.req.path
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2016-01-22 03:23:51 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=True)
|
|
|
|
def test_referer_equal_to_requested_url_without_trailing_slash_with_append_slash(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
self.req.path = self.req.path_info = "/regular_url/that/does/not/exist/"
|
|
|
|
self.req.META["HTTP_REFERER"] = self.req.path_info[:-1]
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2016-01-22 03:23:51 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
|
|
|
@override_settings(APPEND_SLASH=False)
|
|
|
|
def test_referer_equal_to_requested_url_without_trailing_slash_with_no_append_slash(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
self.req.path = self.req.path_info = "/regular_url/that/does/not/exist/"
|
|
|
|
self.req.META["HTTP_REFERER"] = self.req.path_info[:-1]
|
2019-09-27 01:06:35 +08:00
|
|
|
BrokenLinkEmailsMiddleware(self.get_response)(self.req)
|
2015-08-24 02:54:15 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="middleware.cond_get_urls")
|
2015-04-18 05:38:20 +08:00
|
|
|
class ConditionalGetMiddlewareTest(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2011-03-01 22:28:06 +08:00
|
|
|
def setUp(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
self.req = self.request_factory.get("/")
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers = {}
|
|
|
|
|
|
|
|
def get_response(self, req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
for key, value in self.resp_headers.items():
|
|
|
|
resp[key] = value
|
|
|
|
return resp
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
# Tests for the ETag header
|
|
|
|
|
2016-04-03 18:15:10 +08:00
|
|
|
def test_middleware_calculates_etag(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
self.assertNotEqual("", resp["ETag"])
|
2016-04-03 18:15:10 +08:00
|
|
|
|
|
|
|
def test_middleware_wont_overwrite_etag(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["ETag"] = "eggs"
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
|
|
|
self.assertEqual("eggs", resp["ETag"])
|
2016-04-03 18:15:10 +08:00
|
|
|
|
|
|
|
def test_no_etag_streaming_response(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
return StreamingHttpResponse(["content"])
|
|
|
|
|
|
|
|
self.assertFalse(
|
|
|
|
ConditionalGetMiddleware(get_response)(self.req).has_header("ETag")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-03 18:15:10 +08:00
|
|
|
|
2019-10-09 17:20:17 +08:00
|
|
|
def test_no_etag_response_empty_content(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
self.assertFalse(
|
|
|
|
ConditionalGetMiddleware(get_response)(self.req).has_header("ETag")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2019-10-09 17:20:17 +08:00
|
|
|
|
2016-04-03 18:15:10 +08:00
|
|
|
def test_no_etag_no_store_cache(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Cache-Control"] = "No-Cache, No-Store, Max-age=0"
|
|
|
|
self.assertFalse(
|
|
|
|
ConditionalGetMiddleware(self.get_response)(self.req).has_header("ETag")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-03 18:15:10 +08:00
|
|
|
|
|
|
|
def test_etag_extended_cache_control(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Cache-Control"] = 'my-directive="my-no-store"'
|
|
|
|
self.assertTrue(
|
|
|
|
ConditionalGetMiddleware(self.get_response)(self.req).has_header("ETag")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-03 18:15:10 +08:00
|
|
|
|
2011-03-01 22:28:06 +08:00
|
|
|
def test_if_none_match_and_no_etag(self):
|
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = "spam"
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_no_if_none_match_and_etag(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["ETag"] = "eggs"
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_if_none_match_and_same_etag(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = '"spam"'
|
|
|
|
self.resp_headers["ETag"] = '"spam"'
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 304)
|
2016-01-05 15:09:10 +08:00
|
|
|
|
2011-03-01 22:28:06 +08:00
|
|
|
def test_if_none_match_and_different_etag(self):
|
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = "spam"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["ETag"] = "eggs"
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
2014-04-15 07:44:05 +08:00
|
|
|
def test_if_none_match_and_redirect(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
resp["ETag"] = "spam"
|
|
|
|
resp["Location"] = "/"
|
|
|
|
resp.status_code = 301
|
|
|
|
return resp
|
|
|
|
|
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = "spam"
|
|
|
|
resp = ConditionalGetMiddleware(get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 301)
|
2014-04-15 07:44:05 +08:00
|
|
|
|
|
|
|
def test_if_none_match_and_client_error(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
resp["ETag"] = "spam"
|
|
|
|
resp.status_code = 400
|
|
|
|
return resp
|
|
|
|
|
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = "spam"
|
|
|
|
resp = ConditionalGetMiddleware(get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
2014-04-15 07:44:05 +08:00
|
|
|
|
2011-03-01 22:28:06 +08:00
|
|
|
# Tests for the Last-Modified header
|
|
|
|
|
|
|
|
def test_if_modified_since_and_no_last_modified(self):
|
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_no_if_modified_since_and_last_modified(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Last-Modified"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 200)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_if_modified_since_and_same_last_modified(self):
|
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Last-Modified"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
|
|
|
self.resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.resp.status_code, 304)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_if_modified_since_and_last_modified_in_the_past(self):
|
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Last-Modified"] = "Sat, 12 Feb 2011 17:35:44 GMT"
|
|
|
|
resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 304)
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
def test_if_modified_since_and_last_modified_in_the_future(self):
|
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
self.resp_headers["Last-Modified"] = "Sat, 12 Feb 2011 17:41:44 GMT"
|
|
|
|
self.resp = ConditionalGetMiddleware(self.get_response)(self.req)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-05-31 06:27:47 +08:00
|
|
|
|
2014-04-15 07:44:05 +08:00
|
|
|
def test_if_modified_since_and_redirect(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
resp["Last-Modified"] = "Sat, 12 Feb 2011 17:35:44 GMT"
|
|
|
|
resp["Location"] = "/"
|
|
|
|
resp.status_code = 301
|
|
|
|
return resp
|
|
|
|
|
2014-04-15 07:44:05 +08:00
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = ConditionalGetMiddleware(get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 301)
|
2014-04-15 07:44:05 +08:00
|
|
|
|
|
|
|
def test_if_modified_since_and_client_error(self):
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
resp["Last-Modified"] = "Sat, 12 Feb 2011 17:35:44 GMT"
|
|
|
|
resp.status_code = 400
|
|
|
|
return resp
|
|
|
|
|
2014-04-15 07:44:05 +08:00
|
|
|
self.req.META["HTTP_IF_MODIFIED_SINCE"] = "Sat, 12 Feb 2011 17:38:44 GMT"
|
2019-09-27 01:06:35 +08:00
|
|
|
resp = ConditionalGetMiddleware(get_response)(self.req)
|
|
|
|
self.assertEqual(resp.status_code, 400)
|
2014-04-15 07:44:05 +08:00
|
|
|
|
2016-10-11 12:08:37 +08:00
|
|
|
def test_not_modified_headers(self):
|
|
|
|
"""
|
|
|
|
The 304 Not Modified response should include only the headers required
|
|
|
|
by section 4.1 of RFC 7232, Last-Modified, and the cookies.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
resp = self.client.get(req.path_info)
|
|
|
|
resp["Date"] = "Sat, 12 Feb 2011 17:35:44 GMT"
|
|
|
|
resp["Last-Modified"] = "Sat, 12 Feb 2011 17:35:44 GMT"
|
|
|
|
resp["Expires"] = "Sun, 13 Feb 2011 17:35:44 GMT"
|
|
|
|
resp["Vary"] = "Cookie"
|
|
|
|
resp["Cache-Control"] = "public"
|
|
|
|
resp["Content-Location"] = "/alt"
|
|
|
|
resp["Content-Language"] = "en" # shouldn't be preserved
|
|
|
|
resp["ETag"] = '"spam"'
|
|
|
|
resp.set_cookie("key", "value")
|
|
|
|
return resp
|
|
|
|
|
|
|
|
self.req.META["HTTP_IF_NONE_MATCH"] = '"spam"'
|
|
|
|
|
|
|
|
new_response = ConditionalGetMiddleware(get_response)(self.req)
|
2016-10-11 12:08:37 +08:00
|
|
|
self.assertEqual(new_response.status_code, 304)
|
2019-09-27 01:06:35 +08:00
|
|
|
base_response = get_response(self.req)
|
2016-10-11 12:08:37 +08:00
|
|
|
for header in (
|
|
|
|
"Cache-Control",
|
|
|
|
"Content-Location",
|
|
|
|
"Date",
|
|
|
|
"ETag",
|
|
|
|
"Expires",
|
|
|
|
"Last-Modified",
|
|
|
|
"Vary",
|
|
|
|
):
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(
|
|
|
|
new_response.headers[header], base_response.headers[header]
|
|
|
|
)
|
2019-09-27 01:06:35 +08:00
|
|
|
self.assertEqual(new_response.cookies, base_response.cookies)
|
2016-10-11 12:08:37 +08:00
|
|
|
self.assertNotIn("Content-Language", new_response)
|
|
|
|
|
2016-10-13 12:31:48 +08:00
|
|
|
def test_no_unsafe(self):
|
|
|
|
"""
|
|
|
|
ConditionalGetMiddleware shouldn't return a conditional response on an
|
|
|
|
unsafe request. A response has already been generated by the time
|
|
|
|
ConditionalGetMiddleware is called, so it's too late to return a 412
|
|
|
|
Precondition Failed.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_200_response(req):
|
|
|
|
return HttpResponse(status=200)
|
|
|
|
|
|
|
|
response = ConditionalGetMiddleware(self.get_response)(self.req)
|
2020-07-14 19:32:24 +08:00
|
|
|
etag = response.headers["ETag"]
|
2018-11-27 03:01:27 +08:00
|
|
|
put_request = self.request_factory.put("/", HTTP_IF_MATCH=etag)
|
2019-09-27 01:06:35 +08:00
|
|
|
conditional_get_response = ConditionalGetMiddleware(get_200_response)(
|
|
|
|
put_request
|
|
|
|
)
|
2016-10-13 12:31:48 +08:00
|
|
|
self.assertEqual(
|
|
|
|
conditional_get_response.status_code, 200
|
|
|
|
) # should never be a 412
|
|
|
|
|
|
|
|
def test_no_head(self):
|
|
|
|
"""
|
|
|
|
ConditionalGetMiddleware shouldn't compute and return an ETag on a
|
|
|
|
HEAD request since it can't do so accurately without access to the
|
|
|
|
response body of the corresponding GET.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_200_response(req):
|
|
|
|
return HttpResponse(status=200)
|
|
|
|
|
2018-11-27 03:01:27 +08:00
|
|
|
request = self.request_factory.head("/")
|
2019-09-27 01:06:35 +08:00
|
|
|
conditional_get_response = ConditionalGetMiddleware(get_200_response)(request)
|
2016-10-13 12:31:48 +08:00
|
|
|
self.assertNotIn("ETag", conditional_get_response)
|
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class XFrameOptionsMiddlewareTest(SimpleTestCase):
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
|
|
|
Tests for the X-Frame-Options clickjacking prevention middleware.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_same_origin(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
The X_FRAME_OPTIONS setting can be set to SAMEORIGIN to have the
|
|
|
|
middleware use that value for the HTTP header.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2013-01-02 05:46:54 +08:00
|
|
|
with override_settings(X_FRAME_OPTIONS="SAMEORIGIN"):
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
with override_settings(X_FRAME_OPTIONS="sameorigin"):
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_deny(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
The X_FRAME_OPTIONS setting can be set to DENY to have the middleware
|
|
|
|
use that value for the HTTP header.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2013-01-02 05:46:54 +08:00
|
|
|
with override_settings(X_FRAME_OPTIONS="DENY"):
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
2013-01-02 05:46:54 +08:00
|
|
|
with override_settings(X_FRAME_OPTIONS="deny"):
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_defaults_sameorigin(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
If the X_FRAME_OPTIONS setting is not set then it defaults to
|
2019-09-07 15:52:10 +08:00
|
|
|
DENY.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2013-01-02 05:46:54 +08:00
|
|
|
with override_settings(X_FRAME_OPTIONS=None):
|
|
|
|
del settings.X_FRAME_OPTIONS # restored by override_settings
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_dont_set_if_set(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
If the X-Frame-Options header is already set then the middleware does
|
|
|
|
not attempt to override it.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def same_origin_response(request):
|
2013-01-02 05:46:54 +08:00
|
|
|
response = HttpResponse()
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["X-Frame-Options"] = "SAMEORIGIN"
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
2013-01-02 05:46:54 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def deny_response(request):
|
2013-01-02 05:46:54 +08:00
|
|
|
response = HttpResponse()
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["X-Frame-Options"] = "DENY"
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS="DENY"):
|
|
|
|
r = XFrameOptionsMiddleware(same_origin_response)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2019-09-27 01:06:35 +08:00
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS="SAMEORIGIN"):
|
|
|
|
r = XFrameOptionsMiddleware(deny_response)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_response_exempt(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
If the response has an xframe_options_exempt attribute set to False
|
|
|
|
then it still sets the header, but if it's set to True then it doesn't.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def xframe_exempt_response(request):
|
|
|
|
response = HttpResponse()
|
|
|
|
response.xframe_options_exempt = True
|
|
|
|
return response
|
|
|
|
|
|
|
|
def xframe_not_exempt_response(request):
|
2013-01-02 05:46:54 +08:00
|
|
|
response = HttpResponse()
|
|
|
|
response.xframe_options_exempt = False
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS="SAMEORIGIN"):
|
|
|
|
r = XFrameOptionsMiddleware(xframe_not_exempt_response)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2013-01-02 05:46:54 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
r = XFrameOptionsMiddleware(xframe_exempt_response)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertIsNone(r.headers.get("X-Frame-Options"))
|
2011-05-31 06:27:47 +08:00
|
|
|
|
|
|
|
def test_is_extendable(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
The XFrameOptionsMiddleware method that determines the X-Frame-Options
|
|
|
|
header value can be overridden based on something in the request or
|
|
|
|
response.
|
2011-05-31 06:27:47 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-05-31 06:27:47 +08:00
|
|
|
class OtherXFrameOptionsMiddleware(XFrameOptionsMiddleware):
|
|
|
|
# This is just an example for testing purposes...
|
|
|
|
def get_xframe_options_value(self, request, response):
|
|
|
|
if getattr(request, "sameorigin", False):
|
|
|
|
return "SAMEORIGIN"
|
|
|
|
if getattr(response, "sameorigin", False):
|
|
|
|
return "SAMEORIGIN"
|
|
|
|
return "DENY"
|
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def same_origin_response(request):
|
2013-01-02 05:46:54 +08:00
|
|
|
response = HttpResponse()
|
|
|
|
response.sameorigin = True
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS="DENY"):
|
|
|
|
r = OtherXFrameOptionsMiddleware(same_origin_response)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2013-01-02 05:46:54 +08:00
|
|
|
|
|
|
|
request = HttpRequest()
|
|
|
|
request.sameorigin = True
|
2019-09-27 01:06:35 +08:00
|
|
|
r = OtherXFrameOptionsMiddleware(get_response_empty)(request)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "SAMEORIGIN")
|
2013-01-02 05:46:54 +08:00
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS="SAMEORIGIN"):
|
2019-09-27 01:06:35 +08:00
|
|
|
r = OtherXFrameOptionsMiddleware(get_response_empty)(HttpRequest())
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(r.headers["X-Frame-Options"], "DENY")
|
2012-01-10 05:42:03 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class GZipMiddlewareTest(SimpleTestCase):
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Tests the GZipMiddleware.
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2012-08-16 04:42:18 +08:00
|
|
|
short_string = b"This string is too short to be worth compressing."
|
|
|
|
compressible_string = b"a" * 500
|
2017-01-07 19:11:46 +08:00
|
|
|
incompressible_string = b"".join(
|
|
|
|
int2byte(random.randint(0, 255)) for _ in range(500)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2012-10-20 23:40:14 +08:00
|
|
|
sequence = [b"a" * 500, b"b" * 200, b"a" * 300]
|
Fixed #24240 -- Allowed GZipping a Unicode StreamingHttpResponse
make_bytes() assumed that if the Content-Encoding header is set, then
everything had already been dealt with bytes-wise, but in a streaming
situation this was not necessarily the case.
make_bytes() is only called when necessary when working with a
StreamingHttpResponse iterable, but by that point the middleware has
added the Content-Encoding header and thus make_bytes() tried to call
bytes(value) (and dies). If it had been a normal HttpResponse,
make_bytes() would have been called when the content was set, well
before the middleware set the Content-Encoding header.
This commit removes the special casing when Content-Encoding is set,
allowing unicode strings to be encoded during the iteration before they
are e.g. gzipped. This behaviour was added a long time ago for #4969 and
it doesn't appear to be necessary any more, as everything is correctly
made into bytes at the appropriate places.
Two new tests, to show that supplying non-ASCII characters to a
StreamingHttpResponse works fine normally, and when passed through the
GZip middleware (the latter dies without the change to make_bytes()).
Removes the test with a nonsense Content-Encoding and Unicode input - if
this were to happen, it can still be encoded as bytes fine.
2015-01-29 05:43:23 +08:00
|
|
|
sequence_unicode = ["a" * 500, "é" * 200, "a" * 300]
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2012-01-10 05:42:03 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
self.req = self.request_factory.get("/")
|
2012-01-10 05:42:03 +08:00
|
|
|
self.req.META["HTTP_ACCEPT_ENCODING"] = "gzip, deflate"
|
|
|
|
self.req.META[
|
|
|
|
"HTTP_USER_AGENT"
|
|
|
|
] = "Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"
|
|
|
|
self.resp = HttpResponse()
|
|
|
|
self.resp.status_code = 200
|
|
|
|
self.resp.content = self.compressible_string
|
|
|
|
self.resp["Content-Type"] = "text/html; charset=UTF-8"
|
2019-09-27 01:06:35 +08:00
|
|
|
|
|
|
|
def get_response(self, request):
|
|
|
|
return self.resp
|
2012-01-10 05:42:03 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decompress(gzipped_string):
|
2015-07-02 05:37:10 +08:00
|
|
|
with gzip.GzipFile(mode="rb", fileobj=BytesIO(gzipped_string)) as f:
|
|
|
|
return f.read()
|
2012-01-10 05:42:03 +08:00
|
|
|
|
2016-10-14 19:41:42 +08:00
|
|
|
@staticmethod
|
|
|
|
def get_mtime(gzipped_string):
|
|
|
|
with gzip.GzipFile(mode="rb", fileobj=BytesIO(gzipped_string)) as f:
|
|
|
|
f.read() # must read the data before accessing the header
|
|
|
|
return f.mtime
|
|
|
|
|
2012-01-10 05:42:03 +08:00
|
|
|
def test_compress_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression is performed on responses with compressible content.
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
2019-09-27 01:06:35 +08:00
|
|
|
r = GZipMiddleware(self.get_response)(self.req)
|
2012-08-16 04:42:18 +08:00
|
|
|
self.assertEqual(self.decompress(r.content), self.compressible_string)
|
2012-01-10 05:42:03 +08:00
|
|
|
self.assertEqual(r.get("Content-Encoding"), "gzip")
|
|
|
|
self.assertEqual(r.get("Content-Length"), str(len(r.content)))
|
|
|
|
|
2012-10-20 23:40:14 +08:00
|
|
|
def test_compress_streaming_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression is performed on responses with streaming content.
|
2012-10-20 23:40:14 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_stream_response(request):
|
|
|
|
resp = StreamingHttpResponse(self.sequence)
|
|
|
|
resp["Content-Type"] = "text/html; charset=UTF-8"
|
|
|
|
return resp
|
|
|
|
|
|
|
|
r = GZipMiddleware(get_stream_response)(self.req)
|
2012-10-20 23:40:14 +08:00
|
|
|
self.assertEqual(self.decompress(b"".join(r)), b"".join(self.sequence))
|
|
|
|
self.assertEqual(r.get("Content-Encoding"), "gzip")
|
|
|
|
self.assertFalse(r.has_header("Content-Length"))
|
|
|
|
|
Fixed #24240 -- Allowed GZipping a Unicode StreamingHttpResponse
make_bytes() assumed that if the Content-Encoding header is set, then
everything had already been dealt with bytes-wise, but in a streaming
situation this was not necessarily the case.
make_bytes() is only called when necessary when working with a
StreamingHttpResponse iterable, but by that point the middleware has
added the Content-Encoding header and thus make_bytes() tried to call
bytes(value) (and dies). If it had been a normal HttpResponse,
make_bytes() would have been called when the content was set, well
before the middleware set the Content-Encoding header.
This commit removes the special casing when Content-Encoding is set,
allowing unicode strings to be encoded during the iteration before they
are e.g. gzipped. This behaviour was added a long time ago for #4969 and
it doesn't appear to be necessary any more, as everything is correctly
made into bytes at the appropriate places.
Two new tests, to show that supplying non-ASCII characters to a
StreamingHttpResponse works fine normally, and when passed through the
GZip middleware (the latter dies without the change to make_bytes()).
Removes the test with a nonsense Content-Encoding and Unicode input - if
this were to happen, it can still be encoded as bytes fine.
2015-01-29 05:43:23 +08:00
|
|
|
def test_compress_streaming_response_unicode(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression is performed on responses with streaming Unicode content.
|
Fixed #24240 -- Allowed GZipping a Unicode StreamingHttpResponse
make_bytes() assumed that if the Content-Encoding header is set, then
everything had already been dealt with bytes-wise, but in a streaming
situation this was not necessarily the case.
make_bytes() is only called when necessary when working with a
StreamingHttpResponse iterable, but by that point the middleware has
added the Content-Encoding header and thus make_bytes() tried to call
bytes(value) (and dies). If it had been a normal HttpResponse,
make_bytes() would have been called when the content was set, well
before the middleware set the Content-Encoding header.
This commit removes the special casing when Content-Encoding is set,
allowing unicode strings to be encoded during the iteration before they
are e.g. gzipped. This behaviour was added a long time ago for #4969 and
it doesn't appear to be necessary any more, as everything is correctly
made into bytes at the appropriate places.
Two new tests, to show that supplying non-ASCII characters to a
StreamingHttpResponse works fine normally, and when passed through the
GZip middleware (the latter dies without the change to make_bytes()).
Removes the test with a nonsense Content-Encoding and Unicode input - if
this were to happen, it can still be encoded as bytes fine.
2015-01-29 05:43:23 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_stream_response_unicode(request):
|
|
|
|
resp = StreamingHttpResponse(self.sequence_unicode)
|
|
|
|
resp["Content-Type"] = "text/html; charset=UTF-8"
|
|
|
|
return resp
|
|
|
|
|
|
|
|
r = GZipMiddleware(get_stream_response_unicode)(self.req)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.decompress(b"".join(r)),
|
2017-02-08 01:05:47 +08:00
|
|
|
b"".join(x.encode() for x in self.sequence_unicode),
|
2015-12-31 22:22:14 +08:00
|
|
|
)
|
Fixed #24240 -- Allowed GZipping a Unicode StreamingHttpResponse
make_bytes() assumed that if the Content-Encoding header is set, then
everything had already been dealt with bytes-wise, but in a streaming
situation this was not necessarily the case.
make_bytes() is only called when necessary when working with a
StreamingHttpResponse iterable, but by that point the middleware has
added the Content-Encoding header and thus make_bytes() tried to call
bytes(value) (and dies). If it had been a normal HttpResponse,
make_bytes() would have been called when the content was set, well
before the middleware set the Content-Encoding header.
This commit removes the special casing when Content-Encoding is set,
allowing unicode strings to be encoded during the iteration before they
are e.g. gzipped. This behaviour was added a long time ago for #4969 and
it doesn't appear to be necessary any more, as everything is correctly
made into bytes at the appropriate places.
Two new tests, to show that supplying non-ASCII characters to a
StreamingHttpResponse works fine normally, and when passed through the
GZip middleware (the latter dies without the change to make_bytes()).
Removes the test with a nonsense Content-Encoding and Unicode input - if
this were to happen, it can still be encoded as bytes fine.
2015-01-29 05:43:23 +08:00
|
|
|
self.assertEqual(r.get("Content-Encoding"), "gzip")
|
|
|
|
self.assertFalse(r.has_header("Content-Length"))
|
|
|
|
|
2015-01-04 01:06:24 +08:00
|
|
|
def test_compress_file_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression is performed on FileResponse.
|
2015-01-04 01:06:24 +08:00
|
|
|
"""
|
2016-01-24 00:47:07 +08:00
|
|
|
with open(__file__, "rb") as file1:
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
file_resp = FileResponse(file1)
|
|
|
|
file_resp["Content-Type"] = "text/html; charset=UTF-8"
|
|
|
|
return file_resp
|
|
|
|
|
|
|
|
r = GZipMiddleware(get_response)(self.req)
|
2016-01-24 00:47:07 +08:00
|
|
|
with open(__file__, "rb") as file2:
|
2015-01-04 01:06:24 +08:00
|
|
|
self.assertEqual(self.decompress(b"".join(r)), file2.read())
|
|
|
|
self.assertEqual(r.get("Content-Encoding"), "gzip")
|
|
|
|
self.assertIsNot(r.file_to_stream, file1)
|
|
|
|
|
2012-01-10 05:42:03 +08:00
|
|
|
def test_compress_non_200_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression is performed on responses with a status other than 200
|
|
|
|
(#10762).
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
|
|
|
self.resp.status_code = 404
|
2019-09-27 01:06:35 +08:00
|
|
|
r = GZipMiddleware(self.get_response)(self.req)
|
2012-08-16 04:42:18 +08:00
|
|
|
self.assertEqual(self.decompress(r.content), self.compressible_string)
|
2012-01-10 05:42:03 +08:00
|
|
|
self.assertEqual(r.get("Content-Encoding"), "gzip")
|
|
|
|
|
|
|
|
def test_no_compress_short_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression isn't performed on responses with short content.
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
|
|
|
self.resp.content = self.short_string
|
2019-09-27 01:06:35 +08:00
|
|
|
r = GZipMiddleware(self.get_response)(self.req)
|
2012-01-10 05:42:03 +08:00
|
|
|
self.assertEqual(r.content, self.short_string)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertIsNone(r.get("Content-Encoding"))
|
2012-01-10 05:42:03 +08:00
|
|
|
|
|
|
|
def test_no_compress_compressed_response(self):
|
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression isn't performed on responses that are already compressed.
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
|
|
|
self.resp["Content-Encoding"] = "deflate"
|
2019-09-27 01:06:35 +08:00
|
|
|
r = GZipMiddleware(self.get_response)(self.req)
|
2012-01-10 05:42:03 +08:00
|
|
|
self.assertEqual(r.content, self.compressible_string)
|
|
|
|
self.assertEqual(r.get("Content-Encoding"), "deflate")
|
|
|
|
|
2015-12-31 22:22:14 +08:00
|
|
|
def test_no_compress_incompressible_response(self):
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
Compression isn't performed on responses with incompressible content.
|
2012-01-10 05:42:03 +08:00
|
|
|
"""
|
2015-12-31 22:22:14 +08:00
|
|
|
self.resp.content = self.incompressible_string
|
2019-09-27 01:06:35 +08:00
|
|
|
r = GZipMiddleware(self.get_response)(self.req)
|
2015-12-31 22:22:14 +08:00
|
|
|
self.assertEqual(r.content, self.incompressible_string)
|
|
|
|
self.assertIsNone(r.get("Content-Encoding"))
|
2012-02-10 02:57:13 +08:00
|
|
|
|
2016-10-14 19:41:42 +08:00
|
|
|
def test_compress_deterministic(self):
|
|
|
|
"""
|
|
|
|
Compression results are the same for the same content and don't
|
|
|
|
include a modification time (since that would make the results
|
|
|
|
of compression non-deterministic and prevent
|
|
|
|
ConditionalGetMiddleware from recognizing conditional matches
|
|
|
|
on gzipped content).
|
|
|
|
"""
|
2019-09-27 01:06:35 +08:00
|
|
|
r1 = GZipMiddleware(self.get_response)(self.req)
|
|
|
|
r2 = GZipMiddleware(self.get_response)(self.req)
|
2016-10-14 19:41:42 +08:00
|
|
|
self.assertEqual(r1.content, r2.content)
|
|
|
|
self.assertEqual(self.get_mtime(r1.content), 0)
|
|
|
|
self.assertEqual(self.get_mtime(r2.content), 0)
|
|
|
|
|
2012-02-10 02:57:13 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ETagGZipMiddlewareTest(SimpleTestCase):
|
2012-02-10 02:57:13 +08:00
|
|
|
"""
|
2016-10-11 15:42:00 +08:00
|
|
|
ETags are handled properly by GZipMiddleware.
|
2012-02-10 02:57:13 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2014-12-28 04:15:16 +08:00
|
|
|
rf = RequestFactory()
|
2012-08-16 04:42:18 +08:00
|
|
|
compressible_string = b"a" * 500
|
2012-02-10 02:57:13 +08:00
|
|
|
|
2016-10-11 15:42:00 +08:00
|
|
|
def test_strong_etag_modified(self):
|
2012-02-10 02:57:13 +08:00
|
|
|
"""
|
2016-10-11 15:42:00 +08:00
|
|
|
GZipMiddleware makes a strong ETag weak.
|
2012-02-10 02:57:13 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
response = HttpResponse(self.compressible_string)
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["ETag"] = '"eggs"'
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
2012-02-10 02:57:13 +08:00
|
|
|
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
2019-09-27 01:06:35 +08:00
|
|
|
gzip_response = GZipMiddleware(get_response)(request)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(gzip_response.headers["ETag"], 'W/"eggs"')
|
2012-02-10 02:57:13 +08:00
|
|
|
|
2016-10-11 15:42:00 +08:00
|
|
|
def test_weak_etag_not_modified(self):
|
|
|
|
"""
|
|
|
|
GZipMiddleware doesn't modify a weak ETag.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
response = HttpResponse(self.compressible_string)
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["ETag"] = 'W/"eggs"'
|
2019-09-27 01:06:35 +08:00
|
|
|
return response
|
|
|
|
|
2016-10-11 15:42:00 +08:00
|
|
|
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
2019-09-27 01:06:35 +08:00
|
|
|
gzip_response = GZipMiddleware(get_response)(request)
|
2020-07-14 19:32:24 +08:00
|
|
|
self.assertEqual(gzip_response.headers["ETag"], 'W/"eggs"')
|
2016-10-11 15:42:00 +08:00
|
|
|
|
|
|
|
def test_etag_match(self):
|
|
|
|
"""
|
|
|
|
GZipMiddleware allows 304 Not Modified responses.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def get_response(req):
|
|
|
|
response = HttpResponse(self.compressible_string)
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_cond_response(req):
|
|
|
|
return ConditionalGetMiddleware(get_response)(req)
|
|
|
|
|
2016-10-11 15:42:00 +08:00
|
|
|
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
2019-09-27 01:06:35 +08:00
|
|
|
response = GZipMiddleware(get_cond_response)(request)
|
2020-07-14 19:32:24 +08:00
|
|
|
gzip_etag = response.headers["ETag"]
|
2016-10-11 15:42:00 +08:00
|
|
|
next_request = self.rf.get(
|
|
|
|
"/", HTTP_ACCEPT_ENCODING="gzip, deflate", HTTP_IF_NONE_MATCH=gzip_etag
|
|
|
|
)
|
2019-09-27 01:06:35 +08:00
|
|
|
next_response = ConditionalGetMiddleware(get_response)(next_request)
|
2016-10-11 15:42:00 +08:00
|
|
|
self.assertEqual(next_response.status_code, 304)
|