Package cherrypy :: Package test :: Module test_encoding
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_encoding

  1  from cherrypy.test import test 
  2  test.prefer_parent_path() 
  3   
  4  import sys 
  5  import gzip, StringIO 
  6  from httplib import IncompleteRead 
  7  import cherrypy 
  8  europoundUnicode = u'\x80\xa3' 
  9  europoundUtf8 = u'\x80\xa3'.encode('utf-8') 
 10  sing = u"\u6bdb\u6cfd\u4e1c: Sing, Little Birdie?" 
 11  sing8 = sing.encode('utf-8') 
 12  sing16 = sing.encode('utf-16') 
 13   
 14   
15 -def setup_server():
16 class Root: 17 def index(self, param): 18 assert param == europoundUnicode 19 yield europoundUnicode
20 index.exposed = True 21 22 def mao_zedong(self): 23 return sing 24 mao_zedong.exposed = True 25 26 def utf8(self): 27 return sing8 28 utf8.exposed = True 29 utf8._cp_config = {'tools.encode.encoding': 'utf-8'} 30 31 def reqparams(self, *args, **kwargs): 32 return repr(cherrypy.request.params) 33 reqparams.exposed = True 34 35 class GZIP: 36 def index(self): 37 yield "Hello, world" 38 index.exposed = True 39 40 def noshow(self): 41 # Test for ticket #147, where yield showed no exceptions (content- 42 # encoding was still gzip even though traceback wasn't zipped). 43 raise IndexError() 44 yield "Here be dragons" 45 noshow.exposed = True 46 47 def noshow_stream(self): 48 # Test for ticket #147, where yield showed no exceptions (content- 49 # encoding was still gzip even though traceback wasn't zipped). 50 raise IndexError() 51 yield "Here be dragons" 52 noshow_stream.exposed = True 53 noshow_stream._cp_config = {'response.stream': True} 54 55 cherrypy.config.update({ 56 'environment': 'test_suite', 57 'tools.encode.on': True, 58 'tools.decode.on': True, 59 }) 60 61 root = Root() 62 root.gzip = GZIP() 63 cherrypy.tree.mount(root, config={'/gzip': {'tools.gzip.on': True}}) 64 65 66 67 from cherrypy.test import helper 68 69
70 -class EncodingTests(helper.CPWebCase):
71
72 - def testDecoding(self):
73 europoundUtf8 = europoundUnicode.encode('utf-8') 74 self.getPage('/?param=%s' % europoundUtf8) 75 self.assertBody(europoundUtf8) 76 77 # Make sure that encoded utf8 gets parsed correctly 78 self.getPage("/reqparams?q=%C2%A3") 79 self.assertBody(r"{'q': u'\xa3'}")
80
81 - def testEncoding(self):
82 # Default encoding should be utf-8 83 self.getPage('/mao_zedong') 84 self.assertBody(sing8) 85 86 # Ask for utf-16. 87 self.getPage('/mao_zedong', [('Accept-Charset', 'utf-16')]) 88 self.assertHeader('Content-Type', 'text/html;charset=utf-16') 89 self.assertBody(sing16) 90 91 # Ask for multiple encodings. ISO-8859-1 should fail, and utf-16 92 # should be produced. 93 self.getPage('/mao_zedong', [('Accept-Charset', 94 'iso-8859-1;q=1, utf-16;q=0.5')]) 95 self.assertBody(sing16) 96 97 # The "*" value should default to our default_encoding, utf-8 98 self.getPage('/mao_zedong', [('Accept-Charset', '*;q=1, utf-7;q=.2')]) 99 self.assertBody(sing8) 100 101 # Only allow iso-8859-1, which should fail and raise 406. 102 self.getPage('/mao_zedong', [('Accept-Charset', 'iso-8859-1, *;q=0')]) 103 self.assertStatus("406 Not Acceptable") 104 self.assertInBody("Your client sent this Accept-Charset header: " 105 "iso-8859-1, *;q=0. We tried these charsets: " 106 "iso-8859-1.") 107 108 # Ask for x-mac-ce, which should be unknown. See ticket #569. 109 self.getPage('/mao_zedong', [('Accept-Charset', 110 'us-ascii, ISO-8859-1, x-mac-ce')]) 111 self.assertStatus("406 Not Acceptable") 112 self.assertInBody("Your client sent this Accept-Charset header: " 113 "us-ascii, ISO-8859-1, x-mac-ce. We tried these " 114 "charsets: x-mac-ce, us-ascii, ISO-8859-1.") 115 116 # Test the 'encoding' arg to encode. 117 self.getPage('/utf8') 118 self.assertBody(sing8) 119 self.getPage('/utf8', [('Accept-Charset', 'us-ascii, ISO-8859-1')]) 120 self.assertStatus("406 Not Acceptable")
121
122 - def testGzip(self):
123 zbuf = StringIO.StringIO() 124 zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) 125 zfile.write("Hello, world") 126 zfile.close() 127 128 self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip")]) 129 self.assertInBody(zbuf.getvalue()[:3]) 130 self.assertHeader("Vary", "Accept-Encoding") 131 self.assertHeader("Content-Encoding", "gzip") 132 133 # Test when gzip is denied. 134 self.getPage('/gzip/', headers=[("Accept-Encoding", "identity")]) 135 self.assertNoHeader("Vary") 136 self.assertBody("Hello, world") 137 138 self.getPage('/gzip/', headers=[("Accept-Encoding", "gzip;q=0")]) 139 self.assertNoHeader("Vary") 140 self.assertBody("Hello, world") 141 142 self.getPage('/gzip/', headers=[("Accept-Encoding", "*;q=0")]) 143 self.assertStatus(406) 144 self.assertNoHeader("Vary") 145 self.assertErrorPage(406, "identity, gzip") 146 147 # Test for ticket #147 148 self.getPage('/gzip/noshow', headers=[("Accept-Encoding", "gzip")]) 149 self.assertNoHeader('Content-Encoding') 150 self.assertStatus(500) 151 self.assertErrorPage(500, pattern="IndexError\n") 152 153 # In this case, there's nothing we can do to deliver a 154 # readable page, since 1) the gzip header is already set, 155 # and 2) we may have already written some of the body. 156 # The fix is to never stream yields when using gzip. 157 if (cherrypy.server.protocol_version == "HTTP/1.0" or 158 getattr(cherrypy.server, "using_apache", False)): 159 self.getPage('/gzip/noshow_stream', 160 headers=[("Accept-Encoding", "gzip")]) 161 self.assertHeader('Content-Encoding', 'gzip') 162 self.assertInBody('\x1f\x8b\x08\x00') 163 else: 164 # The wsgiserver will simply stop sending data, and the HTTP client 165 # will error due to an incomplete chunk-encoded stream. 166 self.assertRaises((ValueError, IncompleteRead), self.getPage, 167 '/gzip/noshow_stream', 168 headers=[("Accept-Encoding", "gzip")])
169 170 if __name__ == "__main__": 171 setup_server() 172 helper.testmain() 173