<s> import gzip <EOL> import re <EOL> import random <EOL> import StringIO <EOL> from django . conf import settings <EOL> from django . core import mail <EOL> from django . http import HttpRequest <EOL> from django . http import HttpResponse <EOL> from django . middleware . clickjacking import XFrameOptionsMiddleware <EOL> from django . middleware . common import CommonMiddleware <EOL> from django . middleware . http import ConditionalGetMiddleware <EOL> from django . middleware . gzip import GZipMiddleware <EOL> from django . test import TestCase , RequestFactory <EOL> from django . test . utils import override_settings <EOL> class CommonMiddlewareTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . append_slash = settings . APPEND_SLASH <EOL> self . prepend_www = settings . PREPEND_WWW <EOL> self . ignorable_404_urls = settings . IGNORABLE_404_URLS <EOL> self . send_broken_email_links = settings . SEND_BROKEN_LINK_EMAILS <EOL> def tearDown ( self ) : <EOL> settings . APPEND_SLASH = self . append_slash <EOL> settings . PREPEND_WWW = self . prepend_www <EOL> settings . IGNORABLE_404_URLS = self . ignorable_404_urls <EOL> settings . SEND_BROKEN_LINK_EMAILS = self . send_broken_email_links <EOL> def _get_request ( self , path ) : <EOL> request = HttpRequest ( ) <EOL> request . META = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> request . path = request . path_info = "<STR_LIT>" % path <EOL> return request <EOL> def test_append_slash_have_slash ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_slashless_resource ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_slashless_unknown ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_append_slash_no_redirect_on_POST_in_DEBUG ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> settings . DEBUG = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . method = '<STR_LIT:POST>' <EOL> self . assertRaises ( <EOL> RuntimeError , <EOL> CommonMiddleware ( ) . process_request , <EOL> request ) <EOL> try : <EOL> CommonMiddleware ( ) . process_request ( request ) <EOL> except RuntimeError , e : <EOL> self . assertTrue ( '<STR_LIT>' in str ( e ) ) <EOL> settings . DEBUG = False <EOL> def test_append_slash_disabled ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = False <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_quoted ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = False <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www_append_slash_have_slash ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www_append_slash_slashless ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_append_slash_have_slash_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_slashless_resource_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_slashless_unknown_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_redirect_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertFalse ( r is None , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> settings . DEBUG = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> request . method = '<STR_LIT:POST>' <EOL> self . assertRaises ( <EOL> RuntimeError , <EOL> CommonMiddleware ( ) . process_request , <EOL> request ) <EOL> try : <EOL> CommonMiddleware ( ) . process_request ( request ) <EOL> except RuntimeError , e : <EOL> self . assertTrue ( '<STR_LIT>' in str ( e ) ) <EOL> settings . DEBUG = False <EOL> def test_append_slash_disabled_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = False <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> self . assertEqual ( CommonMiddleware ( ) . process_request ( request ) , None ) <EOL> def test_append_slash_quoted_custom_urlconf ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertFalse ( r is None , <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www_custom_urlconf ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = False <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www_append_slash_have_slash_custom_urlconf ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_prepend_www_append_slash_slashless_custom_urlconf ( self ) : <EOL> settings . PREPEND_WWW = True <EOL> settings . APPEND_SLASH = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . urlconf = '<STR_LIT>' <EOL> r = CommonMiddleware ( ) . process_request ( request ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> def test_404_error_reporting ( self ) : <EOL> settings . IGNORABLE_404_URLS = ( re . compile ( r'<STR_LIT:foo>' ) , ) <EOL> settings . SEND_BROKEN_LINK_EMAILS = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response = self . client . get ( request . path ) <EOL> CommonMiddleware ( ) . process_response ( request , response ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertIn ( '<STR_LIT>' , mail . outbox [ <NUM_LIT:0> ] . subject ) <EOL> def test_404_error_reporting_no_referer ( self ) : <EOL> settings . IGNORABLE_404_URLS = ( re . compile ( r'<STR_LIT:foo>' ) , ) <EOL> settings . SEND_BROKEN_LINK_EMAILS = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> response = self . client . get ( request . path ) <EOL> CommonMiddleware ( ) . process_response ( request , response ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> def test_404_error_reporting_ignored_url ( self ) : <EOL> settings . IGNORABLE_404_URLS = ( re . compile ( r'<STR_LIT:foo>' ) , ) <EOL> settings . SEND_BROKEN_LINK_EMAILS = True <EOL> request = self . _get_request ( '<STR_LIT>' ) <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> response = self . client . get ( request . path ) <EOL> CommonMiddleware ( ) . process_response ( request , response ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> class ConditionalGetMiddlewareTest ( TestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . req = HttpRequest ( ) <EOL> self . req . META = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> self . req . path = self . req . path_info = "<STR_LIT:/>" <EOL> self . resp = self . client . get ( self . req . path ) <EOL> def test_date_header_added ( self ) : <EOL> self . assertFalse ( '<STR_LIT>' in self . resp ) <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertTrue ( '<STR_LIT>' in self . resp ) <EOL> def test_content_length_header_added ( self ) : <EOL> content_length = len ( self . resp . content ) <EOL> self . assertFalse ( '<STR_LIT>' in self . resp ) <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertTrue ( '<STR_LIT>' in self . resp ) <EOL> self . assertEqual ( int ( self . resp [ '<STR_LIT>' ] ) , content_length ) <EOL> def test_content_length_header_not_changed ( self ) : <EOL> bad_content_length = len ( self . resp . content ) + <NUM_LIT:10> <EOL> self . resp [ '<STR_LIT>' ] = bad_content_length <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( int ( self . resp [ '<STR_LIT>' ] ) , bad_content_length ) <EOL> def test_if_none_match_and_no_etag ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> def test_no_if_none_match_and_etag ( self ) : <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> def test_if_none_match_and_same_etag ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT> ) <EOL> def test_if_none_match_and_different_etag ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> def test_if_modified_since_and_no_last_modified ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> def test_no_if_modified_since_and_last_modified ( self ) : <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> def test_if_modified_since_and_same_last_modified ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT> ) <EOL> def test_if_modified_since_and_last_modified_in_the_past ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT> ) <EOL> def test_if_modified_since_and_last_modified_in_the_future ( self ) : <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = ConditionalGetMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . resp . status_code , <NUM_LIT:200> ) <EOL> class XFrameOptionsMiddlewareTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . x_frame_options = settings . X_FRAME_OPTIONS <EOL> def tearDown ( self ) : <EOL> settings . X_FRAME_OPTIONS = self . x_frame_options <EOL> def test_same_origin ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_deny ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_defaults_sameorigin ( self ) : <EOL> """<STR_LIT>""" <EOL> del settings . X_FRAME_OPTIONS <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_dont_set_if_set ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> response = HttpResponse ( ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> response ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> response = HttpResponse ( ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> response ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_response_exempt ( self ) : <EOL> """<STR_LIT>""" <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> response = HttpResponse ( ) <EOL> response . xframe_options_exempt = False <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> response ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> response = HttpResponse ( ) <EOL> response . xframe_options_exempt = True <EOL> r = XFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> response ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' , None ) , None ) <EOL> def test_is_extendable ( self ) : <EOL> """<STR_LIT>""" <EOL> class OtherXFrameOptionsMiddleware ( XFrameOptionsMiddleware ) : <EOL> def get_xframe_options_value ( self , request , response ) : <EOL> if getattr ( request , '<STR_LIT>' , False ) : <EOL> return '<STR_LIT>' <EOL> if getattr ( response , '<STR_LIT>' , False ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> response = HttpResponse ( ) <EOL> response . sameorigin = True <EOL> r = OtherXFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> response ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> request = HttpRequest ( ) <EOL> request . sameorigin = True <EOL> r = OtherXFrameOptionsMiddleware ( ) . process_response ( request , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> settings . X_FRAME_OPTIONS = '<STR_LIT>' <EOL> r = OtherXFrameOptionsMiddleware ( ) . process_response ( HttpRequest ( ) , <EOL> HttpResponse ( ) ) <EOL> self . assertEqual ( r [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> class GZipMiddlewareTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> short_string = "<STR_LIT>" <EOL> compressible_string = '<STR_LIT:a>' * <NUM_LIT> <EOL> uncompressible_string = '<STR_LIT>' . join ( chr ( random . randint ( <NUM_LIT:0> , <NUM_LIT:255> ) ) for _ in xrange ( <NUM_LIT> ) ) <EOL> def setUp ( self ) : <EOL> self . req = HttpRequest ( ) <EOL> self . req . META = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> self . req . path = self . req . path_info = "<STR_LIT:/>" <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp = HttpResponse ( ) <EOL> self . resp . status_code = <NUM_LIT:200> <EOL> self . resp . content = self . compressible_string <EOL> self . resp [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> @ staticmethod <EOL> def decompress ( gzipped_string ) : <EOL> return gzip . GzipFile ( mode = '<STR_LIT:rb>' , fileobj = StringIO . StringIO ( gzipped_string ) ) . read ( ) <EOL> def test_compress_response ( self ) : <EOL> """<STR_LIT>""" <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . decompress ( r . content ) , self . compressible_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , str ( len ( r . content ) ) ) <EOL> def test_compress_non_200_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . resp . status_code = <NUM_LIT> <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( self . decompress ( r . content ) , self . compressible_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_no_compress_short_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . resp . content = self . short_string <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( r . content , self . short_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , None ) <EOL> def test_no_compress_compressed_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . resp [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( r . content , self . compressible_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_no_compress_ie_js_requests ( self ) : <EOL> """<STR_LIT>""" <EOL> self . req . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . resp [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( r . content , self . compressible_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , None ) <EOL> def test_no_compress_uncompressible_response ( self ) : <EOL> """<STR_LIT>""" <EOL> self . resp . content = self . uncompressible_string <EOL> r = GZipMiddleware ( ) . process_response ( self . req , self . resp ) <EOL> self . assertEqual ( r . content , self . uncompressible_string ) <EOL> self . assertEqual ( r . get ( '<STR_LIT>' ) , None ) <EOL> class ETagGZipMiddlewareTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> compressible_string = '<STR_LIT:a>' * <NUM_LIT> <EOL> def setUp ( self ) : <EOL> self . rf = RequestFactory ( ) <EOL> def test_compress_response ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . rf . get ( '<STR_LIT:/>' , HTTP_ACCEPT_ENCODING = '<STR_LIT>' ) <EOL> response = GZipMiddleware ( ) . process_response ( request , <EOL> CommonMiddleware ( ) . process_response ( request , <EOL> HttpResponse ( self . compressible_string ) ) ) <EOL> gzip_etag = response . get ( '<STR_LIT>' ) <EOL> request = self . rf . get ( '<STR_LIT:/>' , HTTP_ACCEPT_ENCODING = '<STR_LIT>' ) <EOL> response = GZipMiddleware ( ) . process_response ( request , <EOL> CommonMiddleware ( ) . process_response ( request , <EOL> HttpResponse ( self . compressible_string ) ) ) <EOL> nogzip_etag = response . get ( '<STR_LIT>' ) <EOL> self . assertNotEqual ( gzip_etag , nogzip_etag ) <EOL> ETagGZipMiddlewareTest = override_settings ( <EOL> USE_ETAGS = True , <EOL> ) ( ETagGZipMiddlewareTest ) </s>
