| 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.tree.mount(Root()) |
|---|
| 23 |
cherrypy.config.update({ |
|---|
| 24 |
'log_to_screen': False, |
|---|
| 25 |
'environment': 'production', |
|---|
| 26 |
'tools.encode.on': True, |
|---|
| 27 |
'tools.decode.on': True, |
|---|
| 28 |
}) |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
import helper |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class DecodingEncodingTest(helper.CPWebCase): |
|---|
| 35 |
|
|---|
| 36 |
def testDecodingEncoding(self): |
|---|
| 37 |
europoundUtf8 = europoundUnicode.encode('utf-8') |
|---|
| 38 |
self.getPage('/?param=%s' % europoundUtf8) |
|---|
| 39 |
self.assertBody(europoundUtf8) |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
self.getPage('/mao_zedong') |
|---|
| 43 |
self.assertBody(sing8) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
self.getPage('/mao_zedong', [('Accept-Charset', 'utf-16')]) |
|---|
| 47 |
self.assertBody(sing16) |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
self.getPage('/mao_zedong', [('Accept-Charset', |
|---|
| 52 |
'iso-8859-1;q=1, utf-16;q=0.5')]) |
|---|
| 53 |
self.assertBody(sing16) |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
self.getPage('/mao_zedong', [('Accept-Charset', '*;q=1, utf-7;q=.2')]) |
|---|
| 57 |
self.assertBody(sing8) |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 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 |
|
|---|
| 67 |
if __name__ == "__main__": |
|---|
| 68 |
setup_server() |
|---|
| 69 |
helper.testmain() |
|---|