test_debugapp.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 from __future__ import unicode_literals
00003 import os
00004 import sys
00005 import six
00006 import webtest
00007 from webtest.debugapp import debug_app
00008 from webtest.compat import PY3
00009 from webtest.compat import to_bytes
00010 from webtest.compat import print_stderr
00011 from webtest.app import AppError
00012 from tests.compat import unittest
00013 import webbrowser
00014 
00015 
00016 def test_print_unicode():
00017     print_stderr('°C')
00018 
00019 
00020 class TestTesting(unittest.TestCase):
00021 
00022     def setUp(self):
00023         self.app = webtest.TestApp(debug_app)
00024 
00025     def test_url_class(self):
00026         class U:
00027             def __str__(self):
00028                 return '/'
00029         res = self.app.get(U())
00030         self.assertEqual(res.status_int, 200)
00031 
00032     def test_testing(self):
00033         res = self.app.get('/')
00034         self.assertEqual(res.status_int, 200)
00035         self.assertEqual(res.headers['content-type'], 'text/plain')
00036         self.assertEqual(res.content_type, 'text/plain')
00037         res = self.app.request('/', method='GET')
00038         self.assertEqual(res.status_int, 200)
00039         self.assertEqual(res.headers['content-type'], 'text/plain')
00040         self.assertEqual(res.content_type, 'text/plain')
00041         res = self.app.head('/')
00042         self.assertEqual(res.status_int, 200)
00043         self.assertEqual(res.headers['content-type'], 'text/plain')
00044         self.assertTrue(res.content_length > 0)
00045         self.assertEqual(res.body, to_bytes(''))
00046         res = self.app.head('/', xhr=True)
00047         self.assertEqual(res.status_int, 200)
00048 
00049     def test_post_unicode(self):
00050         res = self.app.post(
00051             '/', params=dict(a='é'),
00052             content_type='application/x-www-form-urlencoded;charset=utf8')
00053         res.mustcontain('a=%C3%A9')
00054 
00055     def test_post_unicode_body(self):
00056         res = self.app.post(
00057             '/', params='é',
00058             content_type='text/plain; charset=utf8')
00059         self.assertTrue(res.body.endswith(b'\xc3\xa9'))
00060         res.mustcontain('é')
00061 
00062     def test_post_params(self):
00063         res = self.app.post('/', params=dict(a=1))
00064         res.mustcontain('a=1')
00065         res = self.app.post('/', params=[('a', '1')])
00066         res.mustcontain('a=1')
00067         res = self.app.post_json('/', params=dict(a=1))
00068         res.mustcontain('{"a": 1}')
00069         res = self.app.post_json('/', params=False)
00070         res.mustcontain('false')
00071 
00072     def test_put_params(self):
00073         res = self.app.put('/', params=dict(a=1))
00074         res.mustcontain('a=1')
00075         res = self.app.put_json('/', params=dict(a=1))
00076         res.mustcontain('{"a": 1}')
00077         res = self.app.put_json('/', params=False)
00078         res.mustcontain('false')
00079 
00080     def test_delete_params(self):
00081         res = self.app.delete('/', params=dict(a=1))
00082         res.mustcontain('a=1')
00083         res = self.app.delete_json('/', params=dict(a=1))
00084         res.mustcontain('{"a": 1}')
00085 
00086     def test_options(self):
00087         res = self.app.options('/')
00088         self.assertEqual(res.status_int, 200)
00089 
00090     def test_exception(self):
00091         self.assertRaises(Exception, self.app.get, '/?error=t')
00092         self.assertRaises(webtest.AppError, self.app.get,
00093                           '/?status=404%20Not%20Found')
00094 
00095     def test_bad_content_type(self):
00096         resp = self.app.get('/')
00097         self.assertRaises(AttributeError, lambda: resp.json)
00098         resp = self.app.get('/?header-content-type=application/json')
00099         self.assertRaises(AttributeError, lambda: resp.pyquery)
00100         self.assertRaises(AttributeError, lambda: resp.lxml)
00101         self.assertRaises(AttributeError, lambda: resp.xml)
00102 
00103     def test_app_from_config_file(self):
00104         config = os.path.join(os.path.dirname(__file__), 'deploy.ini')
00105         app = webtest.TestApp('config:%s#main' % config)
00106         resp = app.get('/')
00107         self.assertEqual(resp.status_int, 200)
00108 
00109     def test_errors(self):
00110         try:
00111             self.app.get('/?errorlog=somelogs')
00112             assert(False, "An AppError should be raised")
00113         except AppError:
00114             e = sys.exc_info()[1]
00115             assert six.text_type(e) \
00116                 == "Application had errors logged:\nsomelogs"
00117 
00118     def test_request_obj(self):
00119         res = self.app.get('/')
00120         res = self.app.request(res.request)
00121 
00122     def test_showbrowser(self):
00123         open_new = webbrowser.open_new
00124         self.filename = ''
00125 
00126         def open_new(f):
00127             self.filename = f
00128 
00129         webbrowser.open_new = open_new
00130         res = self.app.get('/')
00131         res.showbrowser()
00132         assert self.filename.startswith('file://'), self.filename
00133 
00134     def test_303(self):
00135         res = self.app.get('/?status=302%20Redirect&header-location=/foo')
00136         self.assertEqual(res.status_int, 302)
00137         print(res.location)
00138         self.assertEqual(res.location, 'http://localhost/foo', res)
00139         self.assertEqual(res.headers['location'], 'http://localhost/foo')
00140         res = res.follow()
00141         self.assertEqual(res.request.url, 'http://localhost/foo')
00142         self.assertIn('Response: 200 OK', str(res))
00143         self.assertIn('200 OK', repr(res))
00144         self.app.get('/?status=303%20redirect', status='3*')
00145 
00146     def test_204(self):
00147         self.app.post('/?status=204%20OK')
00148 
00149     def test_404(self):
00150         self.app.get('/?status=404%20Not%20Found', status=404)
00151         self.assertRaises(webtest.AppError, self.app.get, '/', status=404)
00152 
00153     def test_print_stderr(self):
00154         res = self.app.get('/')
00155         res.charset = 'utf-8'
00156         res.text = '°C'
00157         print_stderr(str(res))
00158 
00159         res.charset = None
00160         print_stderr(str(res))
00161 
00162     def test_app_error(self):
00163         res = self.app.get('/')
00164         res.charset = 'utf-8'
00165         res.text = '°C'
00166         AppError('%s %s %s %s', res.status, '', res.request.url, res)
00167         res.charset = None
00168         AppError('%s %s %s %s', res.status, '', res.request.url, res)
00169 
00170     def test_exception_repr(self):
00171         res = self.app.get('/')
00172         res.charset = 'utf-8'
00173         res.text = '°C'
00174         if not PY3:
00175             unicode(AssertionError(res))
00176         str(AssertionError(res))
00177         res.charset = None
00178         if not PY3:
00179             unicode(AssertionError(res))
00180         str(AssertionError(res))
00181 
00182     def test_fake_dict(self):
00183         class FakeDict(object):
00184             def items(self):
00185                 return [('a', '10'), ('a', '20')]
00186         self.app.post('/params', params=FakeDict())


webtest
Author(s): AlexV
autogenerated on Sat Mar 25 2017 03:32:05