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

root/branches/cp3-wsgi-remix/test/test_gzip.py

Revision 1114 (checked in by fumanchu, 2 years ago)

Removed "filter" from lots of places, including renaming of tests.

  • Property svn:eol-style set to native
Line 
1 import test
2 test.prefer_parent_path()
3
4 import gzip, StringIO
5 import cherrypy
6
7 def setup_server():
8     class Root:
9         def index(self):
10             yield "Hello, world"
11         index.exposed = True
12        
13         def noshow(self):
14             # Test for ticket #147, where yield showed no exceptions (content-
15             # encoding was still gzip even though traceback wasn't zipped).
16             raise IndexError()
17             yield "Here be dragons"
18         noshow.exposed = True
19        
20         def noshow_stream(self):
21             # Test for ticket #147, where yield showed no exceptions (content-
22             # encoding was still gzip even though traceback wasn't zipped).
23             raise IndexError()
24             yield "Here be dragons"
25         noshow_stream.exposed = True
26         noshow_stream._cp_config = {'stream_response': True}
27    
28     cherrypy.tree.mount(Root())
29     cherrypy.config.update({
30         'global': {'log_to_screen': False,
31                    'environment': 'production',
32                    'show_tracebacks': True,
33                    'tools.gzip.on': True,
34                    },
35     })
36
37
38 import helper
39
40 europoundUtf8 = u'\x80\xa3'.encode('utf-8')
41
42 class GzipTest(helper.CPWebCase):
43    
44     def testGzip(self):
45         zbuf = StringIO.StringIO()
46         zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9)
47         zfile.write("Hello, world")
48         zfile.close()
49        
50         self.getPage('/', headers=[("Accept-Encoding", "gzip")])
51         self.assertInBody(zbuf.getvalue()[:3])
52         self.assertHeader("Vary", "Accept-Encoding")
53        
54         # Test when gzip is denied.
55         self.getPage('/', headers=[("Accept-Encoding", "identity")])
56         self.assertNoHeader("Vary")
57         self.assertBody("Hello, world")
58        
59         self.getPage('/', headers=[("Accept-Encoding", "gzip;q=0")])
60         self.assertNoHeader("Vary")
61         self.assertBody("Hello, world")
62        
63         self.getPage('/', headers=[("Accept-Encoding", "*;q=0")])
64         self.assertStatus(406)
65         self.assertNoHeader("Vary")
66         self.assertErrorPage(406, "identity, gzip")
67        
68         # Test for ticket #147
69         self.getPage('/noshow', headers=[("Accept-Encoding", "gzip")])
70         self.assertNoHeader('Content-Encoding')
71         self.assertStatus(500)
72         self.assertErrorPage(500, pattern="IndexError\n")
73        
74         # In this case, there's nothing we can do to deliver a
75         # readable page, since 1) the gzip header is already set,
76         # and 2) we may have already written some of the body.
77         # The fix is to never stream yields when using gzip.
78         self.getPage('/noshow_stream',
79                      headers=[("Accept-Encoding", "gzip")])
80         self.assertHeader('Content-Encoding', 'gzip')
81         self.assertMatchesBody(r"Unrecoverable error in the server.$")
82
83
84 if __name__ == "__main__":
85     setup_server()
86     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets