summaryrefslogtreecommitdiff
path: root/test/test_outputfilter.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_outputfilter.py')
-rw-r--r--test/test_outputfilter.py152
1 files changed, 152 insertions, 0 deletions
diff --git a/test/test_outputfilter.py b/test/test_outputfilter.py
new file mode 100644
index 0000000..7f8b412
--- /dev/null
+++ b/test/test_outputfilter.py
@@ -0,0 +1,152 @@
+# -*- coding: utf-8 -*-
+'''Everything returned by Bottle()._cast() MUST be WSGI compatiple.'''
+
+import unittest
+import bottle
+from tools import ServerTestBase, tob
+from StringIO import StringIO
+
+class TestOutputFilter(ServerTestBase):
+ ''' Tests for WSGI functionality, routing and output casting (decorators) '''
+
+ def test_bytes(self):
+ self.app.route('/')(lambda: tob('test'))
+ self.assertBody('test')
+
+ def test_bytearray(self):
+ self.app.route('/')(lambda: map(tob, ['t', 'e', 'st']))
+ self.assertBody('test')
+
+ def test_emptylist(self):
+ self.app.route('/')(lambda: [])
+ self.assertBody('')
+
+ def test_none(self):
+ self.app.route('/')(lambda: None)
+ self.assertBody('')
+
+ def test_illegal(self):
+ self.app.route('/')(lambda: 1234)
+ self.assertStatus(500)
+ self.assertInBody('Unhandled exception')
+
+ def test_error(self):
+ self.app.route('/')(lambda: 1/0)
+ self.assertStatus(500)
+ self.assertInBody('ZeroDivisionError')
+
+ def test_fatal_error(self):
+ @self.app.route('/')
+ def test(): raise KeyboardInterrupt()
+ self.assertRaises(KeyboardInterrupt, self.assertStatus, 500)
+
+ def test_file(self):
+ self.app.route('/')(lambda: StringIO('test'))
+ self.assertBody('test')
+
+ def test_unicode(self):
+ self.app.route('/')(lambda: u'äöüß')
+ self.assertBody(u'äöüß'.encode('utf8'))
+
+ self.app.route('/')(lambda: [u'äö',u'üß'])
+ self.assertBody(u'äöüß'.encode('utf8'))
+
+ @self.app.route('/')
+ def test5():
+ bottle.response.content_type='text/html; charset=iso-8859-15'
+ return u'äöüß'
+ self.assertBody(u'äöüß'.encode('iso-8859-15'))
+
+ @self.app.route('/')
+ def test5():
+ bottle.response.content_type='text/html'
+ return u'äöüß'
+ self.assertBody(u'äöüß'.encode('utf8'))
+
+ def test_json(self):
+ self.app.route('/')(lambda: {'a': 1})
+ self.assertBody(bottle.json_dumps({'a': 1}))
+ self.assertHeader('Content-Type','application/json')
+
+ def test_custom(self):
+ self.app.route('/')(lambda: {'a': 1, 'b': 2})
+ self.app.add_filter(dict, lambda x: x.keys())
+ self.assertBody('ab')
+
+ def test_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ bottle.response.header['Test-Header'] = 'test'
+ yield 'foo'
+ self.assertBody('foo')
+ self.assertHeader('Test-Header', 'test')
+
+ def test_empty_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield
+ bottle.response.header['Test-Header'] = 'test'
+ self.assertBody('')
+ self.assertHeader('Test-Header', 'test')
+
+ def test_error_in_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield 1/0
+ self.assertStatus(500)
+ self.assertInBody('ZeroDivisionError')
+
+ def test_fatal_error_in_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield
+ raise KeyboardInterrupt()
+ self.assertRaises(KeyboardInterrupt, self.assertStatus, 500)
+
+ def test_httperror_in_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield
+ bottle.abort(404, 'teststring')
+ self.assertInBody('teststring')
+ self.assertInBody('Error 404: Not Found')
+ self.assertStatus(404)
+
+ def test_httpresponse_in_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield bottle.HTTPResponse('test')
+ self.assertBody('test')
+
+ def test_unicode_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield u'äöüß'
+ self.assertBody(u'äöüß'.encode('utf8'))
+
+ def test_invalid_generator_callback(self):
+ @self.app.route('/')
+ def test():
+ yield 1234
+ self.assertStatus(500)
+ self.assertInBody('Unsupported response type')
+
+ def test_cookie(self):
+ """ WSGI: Cookies """
+ @bottle.route('/cookie')
+ def test():
+ bottle.response.COOKIES['a']="a"
+ bottle.response.set_cookie('b', 'b')
+ bottle.response.set_cookie('c', 'c', path='/')
+ return 'hello'
+ try:
+ c = self.urlopen('/cookie')['header'].get_all('Set-Cookie', '')
+ except:
+ c = self.urlopen('/cookie')['header'].get('Set-Cookie', '').split(',')
+ c = [x.strip() for x in c]
+ self.assertTrue('a=a' in c)
+ self.assertTrue('b=b' in c)
+ self.assertTrue('c=c; Path=/' in c)
+
+if __name__ == '__main__':
+ unittest.main()