Replace all assert statements in test code.

Use self.assertFoo where TestCase instances are available, otherwise
raise an exception manually.
This commit is contained in:
Ben Darnell 2012-06-13 10:42:14 -07:00
parent a303c01acf
commit 4c2306093c
8 changed files with 44 additions and 24 deletions

View File

@ -25,13 +25,15 @@ class OpenIdClientLoginHandler(RequestHandler, OpenIdMixin):
self.authenticate_redirect()
def on_user(self, user):
assert user is not None
if user is None:
raise Exception("user is None")
self.finish(user)
class OpenIdServerAuthenticateHandler(RequestHandler):
def post(self):
assert self.get_argument('openid.mode') == 'check_authentication'
if self.get_argument('openid.mode') != 'check_authentication':
raise Exception("incorrect openid.mode %r")
self.write('is_valid:true')
@ -54,11 +56,13 @@ class OAuth1ClientLoginHandler(RequestHandler, OAuthMixin):
self.authorize_redirect(http_client=self.settings['http_client'])
def on_user(self, user):
assert user is not None
if user is None:
raise Exception("user is None")
self.finish(user)
def _oauth_get_user(self, access_token, callback):
assert access_token == dict(key=b('uiop'), secret=b('5678')), access_token
if access_token != dict(key=b('uiop'), secret=b('5678')):
raise Exception("incorrect access token %r" % access_token)
callback(dict(email='foo@example.com'))

View File

@ -33,7 +33,7 @@ class GenTest(AsyncTestCase):
def f():
(yield gen.Callback("k1"))()
res = yield gen.Wait("k1")
assert res is None
self.assertTrue(res is None)
self.stop()
self.run_gen(f)

View File

@ -38,7 +38,8 @@ class HelloWorldRequestHandler(RequestHandler):
self.expected_protocol = protocol
def get(self):
assert self.request.protocol == self.expected_protocol
if self.request.protocol != self.expected_protocol:
raise Exception("unexpected protocol")
self.finish("Hello world")
def post(self):

View File

@ -138,7 +138,7 @@ class TestIOStream(AsyncHTTPTestCase, LogTrapTestCase):
self.stop()
def final_callback(data):
assert not data
self.assertFalse(data)
final_called.append(True)
self.stop()
server.read_bytes(6, callback=final_callback,

View File

@ -60,7 +60,7 @@ class ProcessTest(LogTrapTestCase):
signal.alarm(5) # master process
try:
id = fork_processes(3, max_restarts=3)
assert id is not None
self.assertTrue(id is not None)
signal.alarm(5) # child processes
except SystemExit, e:
# if we exit cleanly from fork_processes, all the child processes

View File

@ -74,14 +74,16 @@ class NoContentHandler(RequestHandler):
class SeeOther303PostHandler(RequestHandler):
def post(self):
assert self.request.body == b("blah")
if self.request.body != b("blah"):
raise Exception("unexpected body %r" % self.request.body)
self.set_header("Location", "/303_get")
self.set_status(303)
class SeeOther303GetHandler(RequestHandler):
def get(self):
assert not self.request.body
if self.request.body:
raise Exception("unexpected body %r" % self.request.body)
self.write("ok")

View File

@ -57,7 +57,8 @@ def save_signal_handlers():
saved = {}
for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGCHLD]:
saved[sig] = signal.getsignal(sig)
assert "twisted" not in repr(saved), repr(saved)
if "twisted" in repr(saved):
raise Exception("twisted signal handlers already installed")
return saved

View File

@ -51,7 +51,7 @@ class SecureCookieTest(LogTrapTestCase):
handler.set_secure_cookie('foo', binascii.a2b_hex(b('d76df8e7aefc')))
cookie = handler._cookies['foo']
match = re.match(b(r'12345678\|([0-9]+)\|([0-9a-f]+)'), cookie)
assert match
self.assertTrue(match)
timestamp = match.group(1)
sig = match.group(2)
self.assertEqual(
@ -68,7 +68,7 @@ class SecureCookieTest(LogTrapTestCase):
# tamper with the cookie
handler._cookies['foo'] = utf8('1234|5678%s|%s' % (timestamp, sig))
# it gets rejected
assert handler.get_secure_cookie('foo') is None
self.assertTrue(handler.get_secure_cookie('foo') is None)
def test_arbitrary_bytes(self):
# Secure cookies accept arbitrary data (which is base64 encoded).
@ -250,13 +250,19 @@ class EchoHandler(RequestHandler):
# In httpserver.py (i.e. self.request.arguments), they're left
# as bytes. Keys are always native strings.
for key in self.request.arguments:
assert type(key) == str, repr(key)
if type(key) != str:
raise Exception("incorrect type for key: %r" % type(key))
for value in self.request.arguments[key]:
assert type(value) == bytes_type, repr(value)
if type(value) != bytes_type:
raise Exception("incorrect type for value: %r" %
type(value))
for value in self.get_arguments(key):
assert type(value) == unicode, repr(value)
if type(value) != unicode:
raise Exception("incorrect type for value: %r" %
type(value))
for arg in path_args:
assert type(arg) == unicode, repr(arg)
if type(arg) != unicode:
raise Exception("incorrect type for path arg: %r" % type(arg))
self.write(dict(path=self.request.path,
path_args=path_args,
args=recursive_unicode(self.request.arguments)))
@ -313,7 +319,9 @@ class TypeCheckHandler(RequestHandler):
# Secure cookies return bytes because they can contain arbitrary
# data, but regular cookies are native strings.
assert self.cookies.keys() == ['asdf']
if self.cookies.keys() != ['asdf']:
raise Exception("unexpected values for cookie keys: %r" %
self.cookies.keys())
self.check_type('get_secure_cookie', self.get_secure_cookie('asdf'), bytes_type)
self.check_type('get_cookie', self.get_cookie('asdf'), str)
@ -343,7 +351,8 @@ class TypeCheckHandler(RequestHandler):
class DecodeArgHandler(RequestHandler):
def decode_argument(self, value, name=None):
assert type(value) == bytes_type, repr(value)
if type(value) != bytes_type:
raise Exception("unexpected type for value: %r" % type(value))
# use self.request.arguments directly to avoid recursion
if 'encoding' in self.request.arguments:
return value.decode(to_unicode(self.request.arguments['encoding'][0]))
@ -432,8 +441,10 @@ class HeaderInjectionHandler(RequestHandler):
self.set_header("X-Foo", "foo\r\nX-Bar: baz")
raise Exception("Didn't get expected exception")
except ValueError, e:
assert "Unsafe header value" in str(e)
self.finish(b("ok"))
if "Unsafe header value" in str(e):
self.finish(b("ok"))
else:
raise
class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
@ -703,10 +714,10 @@ class StaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
def test_static_files(self):
response = self.fetch('/robots.txt')
assert b("Disallow: /") in response.body
self.assertTrue(b("Disallow: /") in response.body)
response = self.fetch('/static/robots.txt')
assert b("Disallow: /") in response.body
self.assertTrue(b("Disallow: /") in response.body)
def test_static_url(self):
response = self.fetch("/static_url/robots.txt")
@ -740,7 +751,8 @@ class CustomStaticFileTest(AsyncHTTPTestCase, LogTrapTestCase):
class MyStaticFileHandler(StaticFileHandler):
def get(self, path):
path = self.parse_url_path(path)
assert path == "foo.txt"
if path != "foo.txt":
raise Exception("unexpected path: %r" % path)
self.write("bar")
@classmethod