Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1377

Show
Ignore:
Timestamp:
09/26/06 01:06:21
Author:
fumanchu
Message:

Consolidated gzip, encoding tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/test.py

    r1350 r1377  
    334334        'test_core', 
    335335        'test_tools', 
    336         'test_decodingencoding', 
     336        'test_encoding', 
    337337        'test_etags', 
    338         'test_gzip', 
    339338        'test_objectmapping', 
    340339        'test_misc_tools', 
  • trunk/cherrypy/test/test_encoding.py

    r1376 r1377  
    22test.prefer_parent_path() 
    33 
     4import gzip, StringIO 
    45import cherrypy 
    56europoundUnicode = u'\x80\xa3' 
     7europoundUtf8 = u'\x80\xa3'.encode('utf-8') 
    68sing = u"\u6bdb\u6cfd\u4e1c: Sing, Little Birdie?" 
    79sing8 = sing.encode('utf-8') 
     
    1921            return sing 
    2022        mao_zedong.exposed = True 
    21  
    22     cherrypy.tree.mount(Root()) 
     23     
     24    class GZIP: 
     25        def index(self): 
     26            yield "Hello, world" 
     27        index.exposed = True 
     28         
     29        def noshow(self): 
     30            # Test for ticket #147, where yield showed no exceptions (content- 
     31            # encoding was still gzip even though traceback wasn't zipped). 
     32            raise IndexError() 
     33            yield "Here be dragons" 
     34        noshow.exposed = True 
     35         
     36        def noshow_stream(self): 
     37            # Test for ticket #147, where yield showed no exceptions (content- 
     38            # encoding was still gzip even though traceback wasn't zipped). 
     39            raise IndexError() 
     40            yield "Here be dragons" 
     41        noshow_stream.exposed = True 
     42        noshow_stream._cp_config = {'response.stream': True} 
     43     
    2344    cherrypy.config.update({ 
    2445            'environment': 'test_suite', 
     
    2647            'tools.decode.on': True, 
    2748    }) 
     49     
     50    root = Root() 
     51    root.gzip = GZIP() 
     52    cherrypy.tree.mount(root, config={'/gzip': {'tools.gzip.on': True}}) 
     53 
    2854 
    2955 
     
    3157 
    3258 
    33 class DecodingEncodingTest(helper.CPWebCase): 
     59class EncodingTests(helper.CPWebCase): 
    3460     
    35     def testDecodingEncoding(self): 
     61    def testDecoding(self): 
    3662        europoundUtf8 = europoundUnicode.encode('utf-8') 
    3763        self.getPage('/?param=%s' % europoundUtf8) 
    3864        self.assertBody(europoundUtf8) 
    39          
     65     
     66    def testEncoding(self): 
    4067        # Default encoding should be utf-8 
    4168        self.getPage('/mao_zedong') 
     
    7097                          "us-ascii, ISO-8859-1, x-mac-ce. We tried these " 
    7198                          "charsets: x-mac-ce, us-ascii, ISO-8859-1.") 
     99     
     100    def testGzip(self): 
     101        zbuf = StringIO.StringIO() 
     102        zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) 
     103        zfile.write("Hello, world") 
     104        zfile.close() 
    72105         
     106        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")]) 
     107        self.assertInBody(zbuf.getvalue()[:3]) 
     108        self.assertHeader("Vary", "Accept-Encoding") 
     109         
     110        # Test when gzip is denied. 
     111        self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")]) 
     112        self.assertNoHeader("Vary") 
     113        self.assertBody("Hello, world") 
     114         
     115        self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")]) 
     116        self.assertNoHeader("Vary") 
     117        self.assertBody("Hello, world") 
     118         
     119        self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")]) 
     120        self.assertStatus(406) 
     121        self.assertNoHeader("Vary") 
     122        self.assertErrorPage(406, "identity, gzip") 
     123         
     124        # Test for ticket #147 
     125        self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")]) 
     126        self.assertNoHeader('Content-Encoding') 
     127        self.assertStatus(500) 
     128        self.assertErrorPage(500, pattern="IndexError\n") 
     129         
     130        # In this case, there's nothing we can do to deliver a 
     131        # readable page, since 1) the gzip header is already set, 
     132        # and 2) we may have already written some of the body. 
     133        # The fix is to never stream yields when using gzip. 
     134        self.getPage('/gzip/noshow_stream', 
     135                     headers=[("Accept-Encoding", "gzip")]) 
     136        self.assertHeader('Content-Encoding', 'gzip') 
     137        self.assertMatchesBody(r"Unrecoverable error in the server.$") 
     138 
    73139 
    74140if __name__ == "__main__": 

Hosted by WebFaction

Log in as guest/cpguest to create tickets