Ticket #16632: 16632.diff

File 16632.diff, 2.7 KB (added by kenth, 13 years ago)

patch with tests

  • django/http/utils.py

    diff --git a/django/http/utils.py b/django/http/utils.py
    index 5eea239..ea67942 100644
    a b def fix_IE_for_vary(request, response):  
    7676
    7777    # The first part of the Content-Type field will be the MIME type,
    7878    # everything after ';', such as character-set, can be ignored.
    79     if response['Content-Type'].split(';')[0] not in safe_mime_types:
     79    if response.get('Content-Type', '').split(';')[0] not in safe_mime_types:
    8080        try:
    8181            del response['Vary']
    8282        except KeyError:
  • tests/regressiontests/utils/http.py

    diff --git a/tests/regressiontests/utils/http.py b/tests/regressiontests/utils/http.py
    index 3f3a36c..7370b7d 100644
    a b  
    11from django.utils import http
    22from django.utils import unittest
    33from django.utils.datastructures import MultiValueDict
     4from django.http import HttpResponse, utils
     5from django.test import RequestFactory
    46
    57class TestUtilsHttp(unittest.TestCase):
    68
    class TestUtilsHttp(unittest.TestCase):  
    5153            'position=Developer&name=Adrian&name=Simon'
    5254        ]
    5355        self.assertTrue(result in acceptable_results)
     56
     57
     58    def test_ie_novary(self):
     59        """
     60        test ``fix_IE_for_vary`` response to user_agent, content-type
     61        Ref #16632
     62        """
     63
     64        # functions to generate responses
     65        def response_with_excluded_mime():
     66            r = HttpResponse(content_type="text/unsafe")
     67            r['Vary'] = 'Cookie'
     68            return r
     69
     70        def response_with_no_content():
     71            # 'Content-Type' always defaulted, so delete it
     72            r = response_with_excluded_mime()
     73            del r['Content-Type']
     74            return r
     75           
     76        # request with & without IE user agent
     77        rf = RequestFactory()
     78        request = rf.get('/')
     79        ie_request = rf.get('/', HTTP_USER_AGENT='MSIE')
     80   
     81        # test four cases: normal/ie * excluded_mime/no_content
     82        # not IE, excluded_mime_type
     83        response = response_with_excluded_mime()
     84        utils.fix_IE_for_vary(request, response)
     85        self.assertTrue('Vary' in response)
     86
     87        # IE, excluded_mime_type
     88        response = response_with_excluded_mime()
     89        utils.fix_IE_for_vary(ie_request, response)
     90        self.assertFalse('Vary' in response)
     91
     92        # not IE, no_content
     93        response = response_with_no_content()
     94        utils.fix_IE_for_vary(request, response)
     95        self.assertTrue('Vary' in response)
     96
     97        # IE, no_content
     98        response = response_with_no_content()
     99        utils.fix_IE_for_vary(ie_request, response)
     100        self.assertFalse('Vary' in response)
     101
Back to Top