Ticket #17834: etag.patch

File etag.patch, 1.6 KB (added by Paul Egan, 13 years ago)

Patch to not set etag if response content is empty - with test

  • django/utils/cache.py

     
    9494            pass
    9595
    9696def _set_response_etag(response):
    97     response['ETag'] = '"%s"' % hashlib.md5(response.content).hexdigest()
     97    if response.content:
     98        response['ETag'] = '"%s"' % hashlib.md5(response.content).hexdigest()
    9899    return response
    99100
    100101def patch_response_headers(response, cache_timeout=None):
  • tests/regressiontests/cache/tests.py

     
    1818from django.core.cache.backends.base import (CacheKeyWarning,
    1919    InvalidCacheBackendError)
    2020from django.db import router
    21 from django.http import HttpResponse, HttpRequest, QueryDict
     21from django.http import HttpResponse, HttpRequest, QueryDict, HttpResponseRedirect
    2222from django.middleware.cache import (FetchFromCacheMiddleware,
    2323    UpdateCacheMiddleware, CacheMiddleware)
    2424from django.template import Template
     
    17391739        self.assertFalse(response.has_header('ETag'))
    17401740        response = response.render()
    17411741        self.assertTrue(response.has_header('ETag'))
     1742        # Response with empty body should not be given implicit ETag
     1743        response = HttpResponseRedirect('/')
     1744        patch_response_headers(response)
     1745        self.assertFalse(response.has_header('ETag'))
    17421746
    17431747TestWithTemplateResponse = override_settings(
    17441748        CACHE_MIDDLEWARE_KEY_PREFIX='settingsprefix',
Back to Top