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

root/branches/cherrypy-2.x/cherrypy/test/test_decodingencoding_filter.py

Revision 1396 (checked in by lawouach, 2 years ago)

Fixed #569

  • Property svn:eol-style set to native
Line 
1 import test
2 test.prefer_parent_path()
3
4 import cherrypy
5 europoundUnicode = u'\x80\xa3'
6 sing = u"\u6bdb\u6cfd\u4e1c: Sing, Little Birdie?"
7 sing8 = sing.encode('utf-8')
8 sing16 = sing.encode('utf-16')
9
10
11 def setup_server():
12     class Root:
13         def index(self, param):
14             assert param == europoundUnicode
15             yield europoundUnicode
16         index.exposed = True
17        
18         def mao_zedong(self):
19             return sing
20         mao_zedong.exposed = True
21
22     cherrypy.root = Root()
23     cherrypy.config.update({
24             'server.log_to_screen': False,
25             'server.environment': 'production',
26             'encoding_filter.on': True,
27             'decoding_filter.on': True
28     })
29
30
31 import helper
32
33
34 class DecodingEncodingFilterTest(helper.CPWebCase):
35    
36     def testDecodingEncodingFilter(self):
37         europoundUtf8 = europoundUnicode.encode('utf-8')
38         self.getPage('/?param=%s' % europoundUtf8)
39         self.assertBody(europoundUtf8)
40        
41         # Default encoding should be utf-8
42         self.getPage('/mao_zedong')
43         self.assertBody(sing8)
44        
45         # Ask for utf-16.
46         self.getPage('/mao_zedong', [('Accept-Charset', 'utf-16')])
47         self.assertBody(sing16)
48        
49         # Ask for multiple encodings. ISO-8859-1 should fail, and utf-16
50         # should be produced.
51         self.getPage('/mao_zedong', [('Accept-Charset',
52                                       'iso-8859-1;q=1, utf-16;q=0.5')])
53         self.assertBody(sing16)
54        
55         # The "*" value should default to our default_encoding, utf-8
56         self.getPage('/mao_zedong', [('Accept-Charset', '*;q=1, utf-7;q=.2')])
57         self.assertBody(sing8)
58        
59         # Only allow iso-8859-1, which should fail and raise 406.
60         self.getPage('/mao_zedong', [('Accept-Charset', 'iso-8859-1, *;q=0')])
61         self.assertStatus("406 Not Acceptable")
62         self.assertInBody("Your client sent this Accept-Charset header: "
63                           "iso-8859-1, *;q=0. We tried these charsets: "
64                           "iso-8859-1.")
65
66         # Ask for x-mac-ce, which should be unknown. See ticket #569.
67         self.getPage('/mao_zedong', [('Accept-Charset',
68                                       'us-ascii, ISO-8859-1, x-mac-ce')])
69         self.assertStatus("406 Not Acceptable")
70         self.assertInBody("Your client sent this Accept-Charset header: "
71                           "us-ascii, ISO-8859-1, x-mac-ce. We tried these "
72                           "charsets: x-mac-ce, us-ascii, ISO-8859-1.")
73
74 if __name__ == "__main__":
75     setup_server()
76     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets