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