1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
import os
import sys
import webtest
from webtest.debugapp import debug_app
from webtest.compat import to_bytes
from webtest.compat import print_stderr
from webtest.app import AppError
from tests.compat import unittest
import webbrowser
def test_print_unicode():
print_stderr('°C')
class TestTesting(unittest.TestCase):
def setUp(self):
self.app = webtest.TestApp(debug_app)
def test_url_class(self):
class U:
def __str__(self):
return '/'
res = self.app.get(U())
self.assertEqual(res.status_int, 200)
def test_testing(self):
res = self.app.get('/')
self.assertEqual(res.status_int, 200)
self.assertEqual(res.headers['content-type'], 'text/plain')
self.assertEqual(res.content_type, 'text/plain')
res = self.app.request('/', method='GET')
self.assertEqual(res.status_int, 200)
self.assertEqual(res.headers['content-type'], 'text/plain')
self.assertEqual(res.content_type, 'text/plain')
res = self.app.head('/')
self.assertEqual(res.status_int, 200)
self.assertEqual(res.headers['content-type'], 'text/plain')
self.assertTrue(res.content_length > 0)
self.assertEqual(res.body, to_bytes(''))
res = self.app.head('/', xhr=True)
self.assertEqual(res.status_int, 200)
def test_post_unicode(self):
res = self.app.post(
'/', params=dict(a='é'),
content_type='application/x-www-form-urlencoded;charset=utf8')
res.mustcontain('a=%C3%A9')
def test_post_unicode_body(self):
res = self.app.post(
'/', params='é',
content_type='text/plain; charset=utf8')
self.assertTrue(res.body.endswith(b'\xc3\xa9'))
res.mustcontain('é')
def test_post_params(self):
res = self.app.post('/', params=dict(a=1))
res.mustcontain('a=1')
res = self.app.post('/', params=[('a', '1')])
res.mustcontain('a=1')
res = self.app.post_json('/', params=dict(a=1))
res.mustcontain('{"a": 1}')
res = self.app.post_json('/', params=False)
res.mustcontain('false')
def test_put_params(self):
res = self.app.put('/', params=dict(a=1))
res.mustcontain('a=1')
res = self.app.put_json('/', params=dict(a=1))
res.mustcontain('{"a": 1}')
res = self.app.put_json('/', params=False)
res.mustcontain('false')
def test_delete_params(self):
res = self.app.delete('/', params=dict(a=1))
res.mustcontain('a=1')
res = self.app.delete_json('/', params=dict(a=1))
res.mustcontain('{"a": 1}')
def test_options(self):
res = self.app.options('/')
self.assertEqual(res.status_int, 200)
def test_exception(self):
self.assertRaises(Exception, self.app.get, '/?error=t')
self.assertRaises(webtest.AppError, self.app.get,
'/?status=404%20Not%20Found')
def test_bad_content_type(self):
resp = self.app.get('/')
self.assertRaises(AttributeError, lambda: resp.json)
resp = self.app.get('/?header-content-type=application/json')
self.assertRaises(AttributeError, lambda: resp.pyquery)
self.assertRaises(AttributeError, lambda: resp.lxml)
self.assertRaises(AttributeError, lambda: resp.xml)
def test_app_from_config_file(self):
config = os.path.join(os.path.dirname(__file__), 'deploy.ini')
app = webtest.TestApp('config:%s#main' % config)
resp = app.get('/')
self.assertEqual(resp.status_int, 200)
def test_errors(self):
try:
self.app.get('/?errorlog=somelogs')
assert(False, "An AppError should be raised")
except AppError:
e = sys.exc_info()[1]
assert str(e) \
== "Application had errors logged:\nsomelogs"
def test_request_obj(self):
res = self.app.get('/')
res = self.app.request(res.request)
def test_showbrowser(self):
open_new = webbrowser.open_new
self.filename = ''
def open_new(f):
self.filename = f
webbrowser.open_new = open_new
res = self.app.get('/')
res.showbrowser()
assert self.filename.startswith('file://'), self.filename
def test_303(self):
res = self.app.get('/?status=302%20Redirect&header-location=/foo')
self.assertEqual(res.status_int, 302)
print(res.location)
self.assertEqual(res.location, 'http://localhost/foo', res)
self.assertEqual(res.headers['location'], 'http://localhost/foo')
res = res.follow()
self.assertEqual(res.request.url, 'http://localhost/foo')
self.assertIn('Response: 200 OK', str(res))
self.assertIn('200 OK', repr(res))
self.app.get('/?status=303%20redirect', status='3*')
def test_204(self):
self.app.post('/?status=204%20OK')
def test_404(self):
self.app.get('/?status=404%20Not%20Found', status=404)
self.assertRaises(webtest.AppError, self.app.get, '/', status=404)
def test_print_stderr(self):
res = self.app.get('/')
res.charset = 'utf-8'
res.text = '°C'
print_stderr(str(res))
res.charset = None
print_stderr(str(res))
def test_app_error(self):
res = self.app.get('/')
res.charset = 'utf-8'
res.text = '°C'
AppError('%s %s %s %s', res.status, '', res.request.url, res)
res.charset = None
AppError('%s %s %s %s', res.status, '', res.request.url, res)
def test_exception_repr(self):
res = self.app.get('/')
res.charset = 'utf-8'
res.text = '°C'
str(AssertionError(res))
res.charset = None
str(AssertionError(res))
def test_fake_dict(self):
class FakeDict:
def items(self):
return [('a', '10'), ('a', '20')]
self.app.post('/params', params=FakeDict())
|