/usr/lib/python3/dist-packages/werkzeug/testsuite/utils.py is in python3-werkzeug 0.9.4+dfsg-1.1ubuntu2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | # -*- coding: utf-8 -*-
"""
werkzeug.testsuite.utils
~~~~~~~~~~~~~~~~~~~~~~~~
General utilities.
:copyright: (c) 2013 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import with_statement
import unittest
from datetime import datetime
from functools import partial
from werkzeug.testsuite import WerkzeugTestCase
from werkzeug import utils
from werkzeug.datastructures import Headers
from werkzeug.http import parse_date, http_date
from werkzeug.wrappers import BaseResponse
from werkzeug.test import Client, run_wsgi_app
from werkzeug._compat import text_type, implements_iterator
class GeneralUtilityTestCase(WerkzeugTestCase):
def test_redirect(self):
resp = utils.redirect(u'/füübär')
self.assert_in(b'/f%C3%BC%C3%BCb%C3%A4r', resp.get_data())
self.assert_equal(resp.headers['Location'], '/f%C3%BC%C3%BCb%C3%A4r')
self.assert_equal(resp.status_code, 302)
resp = utils.redirect(u'http://☃.net/', 307)
self.assert_in(b'http://xn--n3h.net/', resp.get_data())
self.assert_equal(resp.headers['Location'], 'http://xn--n3h.net/')
self.assert_equal(resp.status_code, 307)
resp = utils.redirect('http://example.com/', 305)
self.assert_equal(resp.headers['Location'], 'http://example.com/')
self.assert_equal(resp.status_code, 305)
def test_redirect_no_unicode_header_keys(self):
# Make sure all headers are native keys. This was a bug at one point
# due to an incorrect conversion.
resp = utils.redirect('http://example.com/', 305)
for key, value in resp.headers.items():
self.assert_equal(type(key), str)
self.assert_equal(type(value), text_type)
self.assert_equal(resp.headers['Location'], 'http://example.com/')
self.assert_equal(resp.status_code, 305)
def test_redirect_xss(self):
location = 'http://example.com/?xss="><script>alert(1)</script>'
resp = utils.redirect(location)
self.assert_not_in(b'<script>alert(1)</script>', resp.get_data())
location = 'http://example.com/?xss="onmouseover="alert(1)'
resp = utils.redirect(location)
self.assert_not_in(b'href="http://example.com/?xss="onmouseover="alert(1)"', resp.get_data())
def test_cached_property(self):
foo = []
class A(object):
def prop(self):
foo.append(42)
return 42
prop = utils.cached_property(prop)
a = A()
p = a.prop
q = a.prop
self.assert_true(p == q == 42)
self.assert_equal(foo, [42])
foo = []
class A(object):
def _prop(self):
foo.append(42)
return 42
prop = utils.cached_property(_prop, name='prop')
del _prop
a = A()
p = a.prop
q = a.prop
self.assert_true(p == q == 42)
self.assert_equal(foo, [42])
def test_environ_property(self):
class A(object):
environ = {'string': 'abc', 'number': '42'}
string = utils.environ_property('string')
missing = utils.environ_property('missing', 'spam')
read_only = utils.environ_property('number')
number = utils.environ_property('number', load_func=int)
broken_number = utils.environ_property('broken_number', load_func=int)
date = utils.environ_property('date', None, parse_date, http_date,
read_only=False)
foo = utils.environ_property('foo')
a = A()
self.assert_equal(a.string, 'abc')
self.assert_equal(a.missing, 'spam')
def test_assign():
a.read_only = 'something'
self.assert_raises(AttributeError, test_assign)
self.assert_equal(a.number, 42)
self.assert_equal(a.broken_number, None)
self.assert_is_none(a.date)
a.date = datetime(2008, 1, 22, 10, 0, 0, 0)
self.assert_equal(a.environ['date'], 'Tue, 22 Jan 2008 10:00:00 GMT')
def test_escape(self):
class Foo(str):
def __html__(self):
return text_type(self)
self.assert_equal(utils.escape(None), '')
self.assert_equal(utils.escape(42), '42')
self.assert_equal(utils.escape('<>'), '<>')
self.assert_equal(utils.escape('"foo"'), '"foo"')
self.assert_equal(utils.escape(Foo('<foo>')), '<foo>')
def test_unescape(self):
self.assert_equal(utils.unescape('<ä>'), u'<ä>')
def test_run_wsgi_app(self):
def foo(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
yield '1'
yield '2'
yield '3'
app_iter, status, headers = run_wsgi_app(foo, {})
self.assert_equal(status, '200 OK')
self.assert_equal(list(headers), [('Content-Type', 'text/plain')])
self.assert_equal(next(app_iter), '1')
self.assert_equal(next(app_iter), '2')
self.assert_equal(next(app_iter), '3')
self.assert_raises(StopIteration, partial(next, app_iter))
got_close = []
@implements_iterator
class CloseIter(object):
def __init__(self):
self.iterated = False
def __iter__(self):
return self
def close(self):
got_close.append(None)
def __next__(self):
if self.iterated:
raise StopIteration()
self.iterated = True
return 'bar'
def bar(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return CloseIter()
app_iter, status, headers = run_wsgi_app(bar, {})
self.assert_equal(status, '200 OK')
self.assert_equal(list(headers), [('Content-Type', 'text/plain')])
self.assert_equal(next(app_iter), 'bar')
self.assert_raises(StopIteration, partial(next, app_iter))
app_iter.close()
self.assert_equal(run_wsgi_app(bar, {}, True)[0], ['bar'])
self.assert_equal(len(got_close), 2)
def test_import_string(self):
import cgi
from werkzeug.debug import DebuggedApplication
self.assert_is(utils.import_string('cgi.escape'), cgi.escape)
self.assert_is(utils.import_string(u'cgi.escape'), cgi.escape)
self.assert_is(utils.import_string('cgi:escape'), cgi.escape)
self.assert_is_none(utils.import_string('XXXXXXXXXXXX', True))
self.assert_is_none(utils.import_string('cgi.XXXXXXXXXXXX', True))
self.assert_is(utils.import_string(u'cgi.escape'), cgi.escape)
self.assert_is(utils.import_string(u'werkzeug.debug.DebuggedApplication'), DebuggedApplication)
self.assert_raises(ImportError, utils.import_string, 'XXXXXXXXXXXXXXXX')
self.assert_raises(ImportError, utils.import_string, 'cgi.XXXXXXXXXX')
def test_find_modules(self):
self.assert_equal(list(utils.find_modules('werkzeug.debug')), \
['werkzeug.debug.console', 'werkzeug.debug.repr',
'werkzeug.debug.tbtools'])
def test_html_builder(self):
html = utils.html
xhtml = utils.xhtml
self.assert_equal(html.p('Hello World'), '<p>Hello World</p>')
self.assert_equal(html.a('Test', href='#'), '<a href="#">Test</a>')
self.assert_equal(html.br(), '<br>')
self.assert_equal(xhtml.br(), '<br />')
self.assert_equal(html.img(src='foo'), '<img src="foo">')
self.assert_equal(xhtml.img(src='foo'), '<img src="foo" />')
self.assert_equal(html.html(
html.head(
html.title('foo'),
html.script(type='text/javascript')
)
), '<html><head><title>foo</title><script type="text/javascript">'
'</script></head></html>')
self.assert_equal(html('<foo>'), '<foo>')
self.assert_equal(html.input(disabled=True), '<input disabled>')
self.assert_equal(xhtml.input(disabled=True), '<input disabled="disabled" />')
self.assert_equal(html.input(disabled=''), '<input>')
self.assert_equal(xhtml.input(disabled=''), '<input />')
self.assert_equal(html.input(disabled=None), '<input>')
self.assert_equal(xhtml.input(disabled=None), '<input />')
self.assert_equal(html.script('alert("Hello World");'), '<script>' \
'alert("Hello World");</script>')
self.assert_equal(xhtml.script('alert("Hello World");'), '<script>' \
'/*<![CDATA[*/alert("Hello World");/*]]>*/</script>')
def test_validate_arguments(self):
take_none = lambda: None
take_two = lambda a, b: None
take_two_one_default = lambda a, b=0: None
self.assert_equal(utils.validate_arguments(take_two, (1, 2,), {}), ((1, 2), {}))
self.assert_equal(utils.validate_arguments(take_two, (1,), {'b': 2}), ((1, 2), {}))
self.assert_equal(utils.validate_arguments(take_two_one_default, (1,), {}), ((1, 0), {}))
self.assert_equal(utils.validate_arguments(take_two_one_default, (1, 2), {}), ((1, 2), {}))
self.assert_raises(utils.ArgumentValidationError,
utils.validate_arguments, take_two, (), {})
self.assert_equal(utils.validate_arguments(take_none, (1, 2,), {'c': 3}), ((), {}))
self.assert_raises(utils.ArgumentValidationError,
utils.validate_arguments, take_none, (1,), {}, drop_extra=False)
self.assert_raises(utils.ArgumentValidationError,
utils.validate_arguments, take_none, (), {'a': 1}, drop_extra=False)
def test_header_set_duplication_bug(self):
headers = Headers([
('Content-Type', 'text/html'),
('Foo', 'bar'),
('Blub', 'blah')
])
headers['blub'] = 'hehe'
headers['blafasel'] = 'humm'
self.assert_equal(headers, Headers([
('Content-Type', 'text/html'),
('Foo', 'bar'),
('blub', 'hehe'),
('blafasel', 'humm')
]))
def test_append_slash_redirect(self):
def app(env, sr):
return utils.append_slash_redirect(env)(env, sr)
client = Client(app, BaseResponse)
response = client.get('foo', base_url='http://example.org/app')
self.assert_equal(response.status_code, 301)
self.assert_equal(response.headers['Location'], 'http://example.org/app/foo/')
def test_cached_property_doc(self):
@utils.cached_property
def foo():
"""testing"""
return 42
self.assert_equal(foo.__doc__, 'testing')
self.assert_equal(foo.__name__, 'foo')
self.assert_equal(foo.__module__, __name__)
def test_secure_filename(self):
self.assert_equal(utils.secure_filename('My cool movie.mov'),
'My_cool_movie.mov')
self.assert_equal(utils.secure_filename('../../../etc/passwd'),
'etc_passwd')
self.assert_equal(utils.secure_filename(u'i contain cool \xfcml\xe4uts.txt'),
'i_contain_cool_umlauts.txt')
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(GeneralUtilityTestCase))
return suite
|