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

root/branches/cherrypy-2.1/cherrypy/test/test_gzip_filter.py

Revision 699 (checked in by fumanchu, 3 years ago)

Final cleanups for [698] and #321. cherrypy.server is now an instance of cherrpy._cpserver.Server, instead of a module.

Line 
1 """
2 Copyright (c) 2004, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 import gzip, StringIO
30 import cherrypy
31
32 class Root:
33     def index(self):
34         yield "Hello, world"
35     index.exposed = True
36    
37     def noshow(self):
38         # Test for ticket #147, where yield showed no exceptions (content-
39         # encoding was still gzip even though traceback wasn't zipped).
40         raise IndexError()
41         yield "Here be dragons"
42     noshow.exposed = True
43    
44     def noshow_stream(self):
45         # Test for ticket #147, where yield showed no exceptions (content-
46         # encoding was still gzip even though traceback wasn't zipped).
47         raise IndexError()
48         yield "Here be dragons"
49     noshow_stream.exposed = True
50
51 cherrypy.root = Root()
52 cherrypy.config.update({
53     'global': {'server.logToScreen': False,
54                'server.environment': 'production',
55                'server.showTracebacks': True,
56                'gzipFilter.on': True,
57                },
58     '/noshow_stream': {'streamResponse': True},
59 })
60
61
62 import helper
63
64 europoundUtf8 = u'\x80\xa3'.encode('utf-8')
65
66 class GzipFilterTest(helper.CPWebCase):
67    
68     def testGzipFilter(self):
69         zbuf = StringIO.StringIO()
70         zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
71         zfile.write("Hello, world")
72         zfile.close()
73        
74         self.getPage('/', headers=[("Accept-Encoding", "gzip")])
75         self.assertInBody(zbuf.getvalue()[:3])
76        
77         # Test for ticket #147
78         helper.webtest.ignored_exceptions.append(IndexError)
79         try:
80             self.getPage('/noshow', headers=[("Accept-Encoding", "gzip")])
81             self.assertNoHeader('Content-Encoding')
82             self.assertStatus('500 Internal error')
83             self.assertErrorPage(500, pattern="IndexError\n")
84            
85             # In this case, there's nothing we can do to deliver a
86             # readable page, since 1) the gzip header is already set,
87             # and 2) we may have already written some of the body.
88             # The fix is to never stream yields when using gzip.
89             if cherrypy.server.httpserver is None:
90                 self.assertRaises(IndexError, self.getPage,
91                                   '/noshow_stream',
92                                   [("Accept-Encoding", "gzip")])
93             else:
94                 self.getPage('/noshow_stream',
95                              headers=[("Accept-Encoding", "gzip")])
96                 self.assertHeader('Content-Encoding', 'gzip')
97                 self.assertMatchesBody(r"Unrecoverable error in the server.$")
98         finally:
99             helper.webtest.ignored_exceptions.pop()
100
101
102 if __name__ == "__main__":
103     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets