2007-12-02 23:25:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2012-01-09 21:42:03 +00:00
|
|
|
import gzip
|
2012-08-15 15:41:09 +00:00
|
|
|
from io import BytesIO
|
2013-01-01 21:28:48 +00:00
|
|
|
import random
|
|
|
|
import re
|
2013-10-18 11:25:30 +00:00
|
|
|
from unittest import skipIf
|
2011-05-05 20:49:26 +00:00
|
|
|
|
2010-12-04 07:28:12 +00:00
|
|
|
from django.conf import settings
|
2011-05-05 20:49:26 +00:00
|
|
|
from django.core import mail
|
2014-11-04 20:19:10 +00:00
|
|
|
from django.http import (
|
|
|
|
HttpRequest, HttpResponse, StreamingHttpResponse, HttpResponsePermanentRedirect,
|
|
|
|
HttpResponseRedirect,
|
|
|
|
)
|
2011-05-30 22:27:47 +00:00
|
|
|
from django.middleware.clickjacking import XFrameOptionsMiddleware
|
2013-01-01 21:28:48 +00:00
|
|
|
from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddleware
|
2011-03-01 14:28:06 +00:00
|
|
|
from django.middleware.http import ConditionalGetMiddleware
|
2012-01-09 21:42:03 +00:00
|
|
|
from django.middleware.gzip import GZipMiddleware
|
2014-03-21 13:21:43 +00:00
|
|
|
from django.test import TestCase, RequestFactory, override_settings
|
2012-08-15 20:42:18 +00:00
|
|
|
from django.utils import six
|
2013-02-02 12:48:55 +00:00
|
|
|
from django.utils.encoding import force_str
|
2012-07-20 16:53:11 +00:00
|
|
|
from django.utils.six.moves import xrange
|
2012-08-15 15:41:09 +00:00
|
|
|
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='middleware.urls')
|
2007-12-02 23:25:55 +00:00
|
|
|
class CommonMiddlewareTest(TestCase):
|
2008-10-08 08:36:41 +00:00
|
|
|
|
2007-12-02 23:25:55 +00:00
|
|
|
def _get_request(self, path):
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
}
|
2014-01-14 15:43:27 +00:00
|
|
|
request.path = request.path_info = "/%s" % path
|
2007-12-02 23:25:55 +00:00
|
|
|
return request
|
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_have_slash(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that URLs with slashes go unmolested.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('slash/')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_slashless_resource(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that matches to explicit slashless URLs go unmolested.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('noslash')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_slashless_unknown(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that APPEND_SLASH doesn't redirect to unknown resources.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('unknown')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_redirect(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertEqual(r.url, 'http://testserver/slash/')
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, DEBUG=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that while in debug mode, an exception is raised with a warning
|
|
|
|
when a failed attempt is made to POST to an URL which would normally be
|
|
|
|
redirected to a slashed version.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('slash')
|
|
|
|
request.method = 'POST'
|
2013-01-01 21:46:54 +00:00
|
|
|
with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'):
|
2007-12-02 23:25:55 +00:00
|
|
|
CommonMiddleware().process_request(request)
|
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=False)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_disabled(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests disabling append slash functionality.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('slash')
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2007-12-02 23:25:55 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2007-12-02 23:25:55 +00:00
|
|
|
def test_append_slash_quoted(self):
|
|
|
|
"""
|
2008-01-03 02:14:29 +00:00
|
|
|
Tests that URLs which require quoting are redirected to their slash
|
|
|
|
version ok.
|
2007-12-02 23:25:55 +00:00
|
|
|
"""
|
|
|
|
request = self._get_request('needsquoting#')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(
|
2013-02-13 08:55:43 +00:00
|
|
|
r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://testserver/needsquoting%23/')
|
2008-10-07 08:22:50 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
2008-10-07 08:22:50 +00:00
|
|
|
def test_prepend_www(self):
|
|
|
|
request = self._get_request('path/')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(
|
2013-02-13 08:55:43 +00:00
|
|
|
r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/path/')
|
2008-10-07 08:22:50 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2008-10-07 08:22:50 +00:00
|
|
|
def test_prepend_www_append_slash_have_slash(self):
|
|
|
|
request = self._get_request('slash/')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2013-02-13 08:55:43 +00:00
|
|
|
self.assertEqual(r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/slash/')
|
2008-10-07 08:22:50 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2008-10-07 08:22:50 +00:00
|
|
|
def test_prepend_www_append_slash_slashless(self):
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(r.status_code, 301)
|
2013-02-13 08:55:43 +00:00
|
|
|
self.assertEqual(r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/slash/')
|
2010-12-04 07:28:12 +00:00
|
|
|
|
2010-03-07 20:03:04 +00:00
|
|
|
# The following tests examine expected behavior given a custom urlconf that
|
|
|
|
# overrides the default one through the request object.
|
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_have_slash_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that URLs with slashes go unmolested.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/slash/')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_slashless_resource_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that matches to explicit slashless URLs go unmolested.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/noslash')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_slashless_unknown_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that APPEND_SLASH doesn't redirect to unknown resources.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/unknown')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_redirect_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/slash')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
r = CommonMiddleware().process_request(request)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIsNotNone(r,
|
2013-01-01 21:46:54 +00:00
|
|
|
"CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
|
|
|
|
self.assertEqual(r.status_code, 301)
|
2014-01-14 15:43:27 +00:00
|
|
|
self.assertEqual(r.url, 'http://testserver/customurlconf/slash/')
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, DEBUG=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that while in debug mode, an exception is raised with a warning
|
|
|
|
when a failed attempt is made to POST to an URL which would normally be
|
|
|
|
redirected to a slashed version.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/slash')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
request.method = 'POST'
|
|
|
|
with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'):
|
|
|
|
CommonMiddleware().process_request(request)
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=False)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_disabled_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests disabling append slash functionality.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/slash')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
self.assertEqual(CommonMiddleware().process_request(request), None)
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_append_slash_quoted_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
"""
|
|
|
|
Tests that URLs which require quoting are redirected to their slash
|
|
|
|
version ok.
|
|
|
|
"""
|
|
|
|
request = self._get_request('customurlconf/needsquoting#')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
r = CommonMiddleware().process_request(request)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIsNotNone(r,
|
2013-01-01 21:46:54 +00:00
|
|
|
"CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
|
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(
|
2013-02-13 08:55:43 +00:00
|
|
|
r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://testserver/customurlconf/needsquoting%23/')
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_prepend_www_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
request = self._get_request('customurlconf/path/')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(
|
2013-02-13 08:55:43 +00:00
|
|
|
r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/customurlconf/path/')
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
request = self._get_request('customurlconf/slash/')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(r.status_code, 301)
|
2013-02-13 08:55:43 +00:00
|
|
|
self.assertEqual(r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/customurlconf/slash/')
|
2010-03-07 20:03:04 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
2010-03-07 20:03:04 +00:00
|
|
|
def test_prepend_www_append_slash_slashless_custom_urlconf(self):
|
2013-01-01 21:46:54 +00:00
|
|
|
request = self._get_request('customurlconf/slash')
|
2013-02-26 12:19:18 +00:00
|
|
|
request.urlconf = 'middleware.extra_urls'
|
2013-01-01 21:46:54 +00:00
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(r.status_code, 301)
|
2013-02-13 08:55:43 +00:00
|
|
|
self.assertEqual(r.url,
|
2014-01-14 15:43:27 +00:00
|
|
|
'http://www.testserver/customurlconf/slash/')
|
2011-03-01 14:28:06 +00:00
|
|
|
|
2012-11-03 20:26:59 +00:00
|
|
|
# Other tests
|
|
|
|
|
|
|
|
def test_non_ascii_query_string_does_not_crash(self):
|
|
|
|
"""Regression test for #15152"""
|
|
|
|
request = self._get_request('slash')
|
2013-02-02 12:48:55 +00:00
|
|
|
request.META['QUERY_STRING'] = force_str('drink=café')
|
2012-11-03 20:26:59 +00:00
|
|
|
response = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(response.status_code, 301)
|
|
|
|
|
2014-11-04 20:19:10 +00:00
|
|
|
def test_response_redirect_class(self):
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(r.status_code, 301)
|
|
|
|
self.assertEqual(r.url, 'http://testserver/slash/')
|
|
|
|
self.assertIsInstance(r, HttpResponsePermanentRedirect)
|
|
|
|
|
|
|
|
def test_response_redirect_class_subclass(self):
|
|
|
|
class MyCommonMiddleware(CommonMiddleware):
|
|
|
|
response_redirect_class = HttpResponseRedirect
|
|
|
|
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = MyCommonMiddleware().process_request(request)
|
|
|
|
self.assertEqual(r.status_code, 302)
|
|
|
|
self.assertEqual(r.url, 'http://testserver/slash/')
|
|
|
|
self.assertIsInstance(r, HttpResponseRedirect)
|
|
|
|
|
2011-05-05 20:49:26 +00:00
|
|
|
|
2013-03-11 22:30:02 +00:00
|
|
|
@override_settings(
|
|
|
|
IGNORABLE_404_URLS=(re.compile(r'foo'),),
|
|
|
|
MANAGERS=('PHB@dilbert.com',),
|
|
|
|
)
|
2013-01-01 21:28:48 +00:00
|
|
|
class BrokenLinkEmailsMiddlewareTest(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.req = HttpRequest()
|
|
|
|
self.req.META = {
|
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
}
|
|
|
|
self.req.path = self.req.path_info = 'regular_url/that/does/not/exist'
|
|
|
|
self.resp = self.client.get(self.req.path)
|
|
|
|
|
|
|
|
def test_404_error_reporting(self):
|
|
|
|
self.req.META['HTTP_REFERER'] = '/another/url/'
|
|
|
|
BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
self.assertIn('Broken', mail.outbox[0].subject)
|
|
|
|
|
|
|
|
def test_404_error_reporting_no_referer(self):
|
|
|
|
BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
|
|
|
|
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'
|
|
|
|
BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
|
2013-05-18 10:37:22 +00:00
|
|
|
@skipIf(six.PY3, "HTTP_REFERER is str type on Python 3")
|
|
|
|
def test_404_error_nonascii_referrer(self):
|
|
|
|
# Such referer strings should not happen, but anyway, if it happens,
|
|
|
|
# let's not crash
|
|
|
|
self.req.META['HTTP_REFERER'] = b'http://testserver/c/\xd0\xbb\xd0\xb8/'
|
|
|
|
BrokenLinkEmailsMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
|
2013-05-24 15:55:50 +00:00
|
|
|
def test_custom_request_checker(self):
|
|
|
|
class SubclassedMiddleware(BrokenLinkEmailsMiddleware):
|
|
|
|
ignored_user_agent_patterns = (re.compile(r'Spider.*'),
|
|
|
|
re.compile(r'Robot.*'))
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2013-05-24 15:55:50 +00:00
|
|
|
def is_ignorable_request(self, request, uri, domain, referer):
|
|
|
|
'''Check user-agent in addition to normal checks.'''
|
|
|
|
if super(SubclassedMiddleware, self).is_ignorable_request(request, uri, domain, referer):
|
|
|
|
return True
|
|
|
|
user_agent = request.META['HTTP_USER_AGENT']
|
|
|
|
return any(pattern.search(user_agent) for pattern in
|
2013-11-03 04:36:09 +00:00
|
|
|
self.ignored_user_agent_patterns)
|
2013-05-24 15:55:50 +00:00
|
|
|
|
|
|
|
self.req.META['HTTP_REFERER'] = '/another/url/'
|
|
|
|
self.req.META['HTTP_USER_AGENT'] = 'Spider machine 3.4'
|
|
|
|
SubclassedMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(len(mail.outbox), 0)
|
|
|
|
self.req.META['HTTP_USER_AGENT'] = 'My user agent'
|
|
|
|
SubclassedMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
2013-01-01 21:28:48 +00:00
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='middleware.cond_get_urls')
|
2011-03-01 14:28:06 +00:00
|
|
|
class ConditionalGetMiddlewareTest(TestCase):
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2011-03-01 14:28:06 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.req = HttpRequest()
|
|
|
|
self.req.META = {
|
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
}
|
|
|
|
self.req.path = self.req.path_info = "/"
|
|
|
|
self.resp = self.client.get(self.req.path)
|
|
|
|
|
|
|
|
# Tests for the Date header
|
|
|
|
|
|
|
|
def test_date_header_added(self):
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('Date', self.resp)
|
2011-03-01 14:28:06 +00:00
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('Date', self.resp)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
|
|
|
# Tests for the Content-Length header
|
|
|
|
|
|
|
|
def test_content_length_header_added(self):
|
|
|
|
content_length = len(self.resp.content)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('Content-Length', self.resp)
|
2011-03-01 14:28:06 +00:00
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIn('Content-Length', self.resp)
|
2011-03-01 14:28:06 +00:00
|
|
|
self.assertEqual(int(self.resp['Content-Length']), content_length)
|
|
|
|
|
2012-10-20 15:40:14 +00:00
|
|
|
def test_content_length_header_not_added(self):
|
|
|
|
resp = StreamingHttpResponse('content')
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('Content-Length', resp)
|
2012-10-20 15:40:14 +00:00
|
|
|
resp = ConditionalGetMiddleware().process_response(self.req, resp)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('Content-Length', resp)
|
2012-10-20 15:40:14 +00:00
|
|
|
|
2011-03-01 14:28:06 +00:00
|
|
|
def test_content_length_header_not_changed(self):
|
|
|
|
bad_content_length = len(self.resp.content) + 10
|
|
|
|
self.resp['Content-Length'] = bad_content_length
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(int(self.resp['Content-Length']), bad_content_length)
|
|
|
|
|
|
|
|
# Tests for the ETag header
|
|
|
|
|
|
|
|
def test_if_none_match_and_no_etag(self):
|
|
|
|
self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
|
|
|
def test_no_if_none_match_and_etag(self):
|
|
|
|
self.resp['ETag'] = 'eggs'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
|
|
|
def test_if_none_match_and_same_etag(self):
|
|
|
|
self.req.META['HTTP_IF_NONE_MATCH'] = self.resp['ETag'] = 'spam'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 304)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
|
|
|
def test_if_none_match_and_different_etag(self):
|
|
|
|
self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
|
|
|
|
self.resp['ETag'] = 'eggs'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
2014-04-14 23:44:05 +00:00
|
|
|
def test_if_none_match_and_redirect(self):
|
|
|
|
self.req.META['HTTP_IF_NONE_MATCH'] = self.resp['ETag'] = 'spam'
|
|
|
|
self.resp['Location'] = '/'
|
|
|
|
self.resp.status_code = 301
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(self.resp.status_code, 301)
|
|
|
|
|
|
|
|
def test_if_none_match_and_client_error(self):
|
|
|
|
self.req.META['HTTP_IF_NONE_MATCH'] = self.resp['ETag'] = 'spam'
|
|
|
|
self.resp.status_code = 400
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(self.resp.status_code, 400)
|
|
|
|
|
2012-10-20 15:40:14 +00:00
|
|
|
@override_settings(USE_ETAGS=True)
|
|
|
|
def test_etag(self):
|
|
|
|
req = HttpRequest()
|
|
|
|
res = HttpResponse('content')
|
|
|
|
self.assertTrue(
|
|
|
|
CommonMiddleware().process_response(req, res).has_header('ETag'))
|
|
|
|
|
|
|
|
@override_settings(USE_ETAGS=True)
|
|
|
|
def test_etag_streaming_response(self):
|
|
|
|
req = HttpRequest()
|
|
|
|
res = StreamingHttpResponse(['content'])
|
|
|
|
res['ETag'] = 'tomatoes'
|
|
|
|
self.assertEqual(
|
|
|
|
CommonMiddleware().process_response(req, res).get('ETag'),
|
|
|
|
'tomatoes')
|
|
|
|
|
|
|
|
@override_settings(USE_ETAGS=True)
|
|
|
|
def test_no_etag_streaming_response(self):
|
|
|
|
req = HttpRequest()
|
|
|
|
res = StreamingHttpResponse(['content'])
|
|
|
|
self.assertFalse(
|
|
|
|
CommonMiddleware().process_response(req, res).has_header('ETag'))
|
|
|
|
|
2011-03-01 14:28:06 +00: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'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-03-01 14:28:06 +00:00
|
|
|
|
|
|
|
def test_no_if_modified_since_and_last_modified(self):
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-03-01 14:28:06 +00: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'
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 304)
|
2011-03-01 14:28:06 +00: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'
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:35:44 GMT'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 304)
|
2011-03-01 14:28:06 +00: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'
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:41:44 GMT'
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
2011-03-03 15:04:39 +00:00
|
|
|
self.assertEqual(self.resp.status_code, 200)
|
2011-05-30 22:27:47 +00:00
|
|
|
|
2014-04-14 23:44:05 +00:00
|
|
|
def test_if_modified_since_and_redirect(self):
|
|
|
|
self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:35:44 GMT'
|
|
|
|
self.resp['Location'] = '/'
|
|
|
|
self.resp.status_code = 301
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(self.resp.status_code, 301)
|
|
|
|
|
|
|
|
def test_if_modified_since_and_client_error(self):
|
|
|
|
self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
|
|
|
|
self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:35:44 GMT'
|
|
|
|
self.resp.status_code = 400
|
|
|
|
self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(self.resp.status_code, 400)
|
|
|
|
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
class XFrameOptionsMiddlewareTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the X-Frame-Options clickjacking prevention middleware.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_same_origin(self):
|
|
|
|
"""
|
|
|
|
Tests that the X_FRAME_OPTIONS setting can be set to SAMEORIGIN to
|
|
|
|
have the middleware use that value for the HTTP header.
|
|
|
|
"""
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='sameorigin'):
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
2011-05-30 22:27:47 +00:00
|
|
|
HttpResponse())
|
2013-01-01 21:46:54 +00:00
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
def test_deny(self):
|
|
|
|
"""
|
|
|
|
Tests that the X_FRAME_OPTIONS setting can be set to DENY to
|
|
|
|
have the middleware use that value for the HTTP header.
|
|
|
|
"""
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='DENY'):
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='deny'):
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
def test_defaults_sameorigin(self):
|
|
|
|
"""
|
|
|
|
Tests that if the X_FRAME_OPTIONS setting is not set then it defaults
|
|
|
|
to SAMEORIGIN.
|
|
|
|
"""
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS=None):
|
|
|
|
del settings.X_FRAME_OPTIONS # restored by override_settings
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
def test_dont_set_if_set(self):
|
|
|
|
"""
|
|
|
|
Tests that if the X-Frame-Options header is already set then the
|
|
|
|
middleware does not attempt to override it.
|
|
|
|
"""
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='DENY'):
|
|
|
|
response = HttpResponse()
|
|
|
|
response['X-Frame-Options'] = 'SAMEORIGIN'
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
response)
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
|
|
|
response = HttpResponse()
|
|
|
|
response['X-Frame-Options'] = 'DENY'
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
response)
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
def test_response_exempt(self):
|
|
|
|
"""
|
|
|
|
Tests that if the response has a xframe_options_exempt attribute set
|
|
|
|
to False then it still sets the header, but if it's set to True then
|
|
|
|
it does not.
|
|
|
|
"""
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
|
|
|
response = HttpResponse()
|
|
|
|
response.xframe_options_exempt = False
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
response)
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
|
|
|
|
|
|
|
response = HttpResponse()
|
|
|
|
response.xframe_options_exempt = True
|
|
|
|
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
response)
|
|
|
|
self.assertEqual(r.get('X-Frame-Options', None), None)
|
2011-05-30 22:27:47 +00:00
|
|
|
|
|
|
|
def test_is_extendable(self):
|
|
|
|
"""
|
|
|
|
Tests that the XFrameOptionsMiddleware method that determines the
|
|
|
|
X-Frame-Options header value can be overridden based on something in
|
|
|
|
the request or response.
|
|
|
|
"""
|
|
|
|
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'
|
|
|
|
|
2013-01-01 21:46:54 +00:00
|
|
|
with override_settings(X_FRAME_OPTIONS='DENY'):
|
|
|
|
response = HttpResponse()
|
|
|
|
response.sameorigin = True
|
|
|
|
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
response)
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
|
|
|
|
|
|
|
request = HttpRequest()
|
|
|
|
request.sameorigin = True
|
|
|
|
r = OtherXFrameOptionsMiddleware().process_response(request,
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
|
|
|
|
|
|
|
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
|
|
|
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
|
|
|
|
HttpResponse())
|
|
|
|
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
2012-01-09 21:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GZipMiddlewareTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests the GZip middleware.
|
|
|
|
"""
|
2012-08-15 20:42:18 +00:00
|
|
|
short_string = b"This string is too short to be worth compressing."
|
|
|
|
compressible_string = b'a' * 500
|
|
|
|
uncompressible_string = b''.join(six.int2byte(random.randint(0, 255)) for _ in xrange(500))
|
2012-10-20 15:40:14 +00:00
|
|
|
sequence = [b'a' * 500, b'b' * 200, b'a' * 300]
|
2012-01-09 21:42:03 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.req = HttpRequest()
|
|
|
|
self.req.META = {
|
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
}
|
|
|
|
self.req.path = self.req.path_info = "/"
|
|
|
|
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'
|
2012-10-20 15:40:14 +00:00
|
|
|
self.stream_resp = StreamingHttpResponse(self.sequence)
|
|
|
|
self.stream_resp['Content-Type'] = 'text/html; charset=UTF-8'
|
2012-01-09 21:42:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decompress(gzipped_string):
|
2012-08-15 15:41:09 +00:00
|
|
|
return gzip.GzipFile(mode='rb', fileobj=BytesIO(gzipped_string)).read()
|
2012-01-09 21:42:03 +00:00
|
|
|
|
|
|
|
def test_compress_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression is performed on responses with compressible content.
|
|
|
|
"""
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.resp)
|
2012-08-15 20:42:18 +00:00
|
|
|
self.assertEqual(self.decompress(r.content), self.compressible_string)
|
2012-01-09 21:42:03 +00:00
|
|
|
self.assertEqual(r.get('Content-Encoding'), 'gzip')
|
|
|
|
self.assertEqual(r.get('Content-Length'), str(len(r.content)))
|
|
|
|
|
2012-10-20 15:40:14 +00:00
|
|
|
def test_compress_streaming_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression is performed on responses with streaming content.
|
|
|
|
"""
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.stream_resp)
|
|
|
|
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'))
|
|
|
|
|
2012-01-09 21:42:03 +00:00
|
|
|
def test_compress_non_200_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression is performed on responses with a status other than 200.
|
|
|
|
See #10762.
|
|
|
|
"""
|
|
|
|
self.resp.status_code = 404
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.resp)
|
2012-08-15 20:42:18 +00:00
|
|
|
self.assertEqual(self.decompress(r.content), self.compressible_string)
|
2012-01-09 21:42:03 +00:00
|
|
|
self.assertEqual(r.get('Content-Encoding'), 'gzip')
|
|
|
|
|
|
|
|
def test_no_compress_short_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression isn't performed on responses with short content.
|
|
|
|
"""
|
|
|
|
self.resp.content = self.short_string
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(r.content, self.short_string)
|
|
|
|
self.assertEqual(r.get('Content-Encoding'), None)
|
|
|
|
|
|
|
|
def test_no_compress_compressed_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression isn't performed on responses that are already compressed.
|
|
|
|
"""
|
|
|
|
self.resp['Content-Encoding'] = 'deflate'
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(r.content, self.compressible_string)
|
|
|
|
self.assertEqual(r.get('Content-Encoding'), 'deflate')
|
|
|
|
|
|
|
|
def test_no_compress_uncompressible_response(self):
|
|
|
|
"""
|
|
|
|
Tests that compression isn't performed on responses with uncompressible content.
|
|
|
|
"""
|
|
|
|
self.resp.content = self.uncompressible_string
|
|
|
|
r = GZipMiddleware().process_response(self.req, self.resp)
|
|
|
|
self.assertEqual(r.content, self.uncompressible_string)
|
|
|
|
self.assertEqual(r.get('Content-Encoding'), None)
|
2012-02-09 18:57:13 +00:00
|
|
|
|
|
|
|
|
2012-03-30 09:08:29 +00:00
|
|
|
@override_settings(USE_ETAGS=True)
|
2012-02-09 18:57:13 +00:00
|
|
|
class ETagGZipMiddlewareTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests if the ETag middleware behaves correctly with GZip middleware.
|
|
|
|
"""
|
2012-08-15 20:42:18 +00:00
|
|
|
compressible_string = b'a' * 500
|
2012-02-09 18:57:13 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.rf = RequestFactory()
|
|
|
|
|
|
|
|
def test_compress_response(self):
|
|
|
|
"""
|
|
|
|
Tests that ETag is changed after gzip compression is performed.
|
|
|
|
"""
|
|
|
|
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
|
|
|
|
response = GZipMiddleware().process_response(request,
|
|
|
|
CommonMiddleware().process_response(request,
|
|
|
|
HttpResponse(self.compressible_string)))
|
|
|
|
gzip_etag = response.get('ETag')
|
|
|
|
|
|
|
|
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='')
|
|
|
|
response = GZipMiddleware().process_response(request,
|
|
|
|
CommonMiddleware().process_response(request,
|
|
|
|
HttpResponse(self.compressible_string)))
|
|
|
|
nogzip_etag = response.get('ETag')
|
|
|
|
|
|
|
|
self.assertNotEqual(gzip_etag, nogzip_etag)
|