Commit 8d5c5603 authored by Hanno Schlichting's avatar Hanno Schlichting

flake8

parent a1f87de3
...@@ -48,7 +48,7 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -48,7 +48,7 @@ class WSGIResponseTests(unittest.TestCase):
response.finalize() response.finalize()
self.assertEqual(response.getHeader('Content-Length'), '7') self.assertEqual(response.getHeader('Content-Length'), '7')
def test_finalize_skips_setting_content_length_if_missing_w_streaming(self): def test_finalize_skips_content_length_if_missing_w_streaming(self):
response = self._makeOne() response = self._makeOne()
response._streaming = True response._streaming = True
response.body = 'TESTING' response.body = 'TESTING'
...@@ -85,7 +85,7 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -85,7 +85,7 @@ class WSGIResponseTests(unittest.TestCase):
from ZPublisher.Iterators import IUnboundStreamIterator from ZPublisher.Iterators import IUnboundStreamIterator
from zope.interface import implements from zope.interface import implements
class test_streamiterator: class TestStreamIterator(object):
implements(IUnboundStreamIterator) implements(IUnboundStreamIterator)
data = "hello" data = "hello"
done = 0 done = 0
...@@ -98,7 +98,7 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -98,7 +98,7 @@ class WSGIResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setStatus(200) response.setStatus(200)
body = test_streamiterator() body = TestStreamIterator()
response.setBody(body) response.setBody(body)
response.finalize() response.finalize()
self.assertTrue(body is response.body) self.assertTrue(body is response.body)
...@@ -108,7 +108,7 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -108,7 +108,7 @@ class WSGIResponseTests(unittest.TestCase):
from ZPublisher.Iterators import IStreamIterator from ZPublisher.Iterators import IStreamIterator
from zope.interface import implements from zope.interface import implements
class test_streamiterator: class TestStreamIterator(object):
implements(IStreamIterator) implements(IStreamIterator)
data = "hello" data = "hello"
done = 0 done = 0
...@@ -124,13 +124,13 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -124,13 +124,13 @@ class WSGIResponseTests(unittest.TestCase):
response = self._makeOne() response = self._makeOne()
response.setStatus(200) response.setStatus(200)
body = test_streamiterator() body = TestStreamIterator()
response.setBody(body) response.setBody(body)
response.finalize() response.finalize()
self.assertTrue(body is response.body) self.assertTrue(body is response.body)
self.assertEqual(response._streaming, 0) self.assertEqual(response._streaming, 0)
self.assertEqual(response.getHeader('Content-Length'), self.assertEqual(response.getHeader('Content-Length'),
'%d' % len(test_streamiterator.data)) '%d' % len(TestStreamIterator.data))
def test___str___raises(self): def test___str___raises(self):
response = self._makeOne() response = self._makeOne()
...@@ -138,7 +138,7 @@ class WSGIResponseTests(unittest.TestCase): ...@@ -138,7 +138,7 @@ class WSGIResponseTests(unittest.TestCase):
self.assertRaises(NotImplementedError, lambda: str(response)) self.assertRaises(NotImplementedError, lambda: str(response))
class Test_publish(unittest.TestCase): class TestPublish(unittest.TestCase):
def _callFUT(self, request, module_name, _get_module_info=None): def _callFUT(self, request, module_name, _get_module_info=None):
from ZPublisher.WSGIPublisher import publish from ZPublisher.WSGIPublisher import publish
...@@ -204,8 +204,8 @@ class Test_publish(unittest.TestCase): ...@@ -204,8 +204,8 @@ class Test_publish(unittest.TestCase):
self.assertEqual(response.realm, None) self.assertEqual(response.realm, None)
class Test_publish_module(unittest.TestCase): class TestPublishModule(unittest.TestCase):
def setUp(self): def setUp(self):
from zope.testing.cleanup import cleanUp from zope.testing.cleanup import cleanUp
...@@ -216,7 +216,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -216,7 +216,7 @@ class Test_publish_module(unittest.TestCase):
cleanUp() cleanUp()
def _callFUT(self, environ, start_response, def _callFUT(self, environ, start_response,
_publish=None, _response_factory=None, _request_factory=None): _publish=None, _response_factory=None, _request_factory=None):
from ZPublisher.WSGIPublisher import publish_module from ZPublisher.WSGIPublisher import publish_module
if _publish is not None: if _publish is not None:
if _response_factory is not None: if _response_factory is not None:
...@@ -228,7 +228,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -228,7 +228,7 @@ class Test_publish_module(unittest.TestCase):
else: else:
if _request_factory is not None: if _request_factory is not None:
return publish_module(environ, start_response, _publish, return publish_module(environ, start_response, _publish,
_request_factory=_request_factory) _request_factory=_request_factory)
return publish_module(environ, start_response, _publish) return publish_module(environ, start_response, _publish)
return publish_module(environ, start_response) return publish_module(environ, start_response)
...@@ -245,16 +245,16 @@ class Test_publish_module(unittest.TestCase): ...@@ -245,16 +245,16 @@ class Test_publish_module(unittest.TestCase):
def _makeEnviron(self, **kw): def _makeEnviron(self, **kw):
from StringIO import StringIO from StringIO import StringIO
environ = { environ = {
'SCRIPT_NAME' : '', 'SCRIPT_NAME': '',
'REQUEST_METHOD' : 'GET', 'REQUEST_METHOD': 'GET',
'QUERY_STRING' : '', 'QUERY_STRING': '',
'SERVER_NAME' : '127.0.0.1', 'SERVER_NAME': '127.0.0.1',
'REMOTE_ADDR': '127.0.0.1', 'REMOTE_ADDR': '127.0.0.1',
'wsgi.url_scheme': 'http', 'wsgi.url_scheme': 'http',
'SERVER_PORT': '8080', 'SERVER_PORT': '8080',
'HTTP_HOST': '127.0.0.1:8080', 'HTTP_HOST': '127.0.0.1:8080',
'SERVER_PROTOCOL' : 'HTTP/1.1', 'SERVER_PROTOCOL': 'HTTP/1.1',
'wsgi.input' : StringIO(''), 'wsgi.input': StringIO(''),
'CONTENT_LENGTH': '0', 'CONTENT_LENGTH': '0',
'HTTP_CONNECTION': 'keep-alive', 'HTTP_CONNECTION': 'keep-alive',
'CONTENT_TYPE': '' 'CONTENT_TYPE': ''
...@@ -338,11 +338,14 @@ class Test_publish_module(unittest.TestCase): ...@@ -338,11 +338,14 @@ class Test_publish_module(unittest.TestCase):
self.assertEqual(kw, {}) self.assertEqual(kw, {})
def test_response_body_is_file(self): def test_response_body_is_file(self):
class DummyFile(file): class DummyFile(file):
def __init__(self): def __init__(self):
pass pass
def read(self, *args, **kw): def read(self, *args, **kw):
raise NotImplementedError() raise NotImplementedError()
_response = DummyResponse() _response = DummyResponse()
_response._status = '200 OK' _response._status = '200 OK'
_response._headers = [('Content-Length', '4')] _response._headers = [('Content-Length', '4')]
...@@ -358,7 +361,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -358,7 +361,7 @@ class Test_publish_module(unittest.TestCase):
from ZPublisher.Iterators import IStreamIterator from ZPublisher.Iterators import IStreamIterator
from zope.interface import implements from zope.interface import implements
class test_streamiterator: class TestStreamIterator(object):
implements(IStreamIterator) implements(IStreamIterator)
data = "hello" data = "hello"
done = 0 done = 0
...@@ -372,7 +375,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -372,7 +375,7 @@ class Test_publish_module(unittest.TestCase):
_response = DummyResponse() _response = DummyResponse()
_response._status = '200 OK' _response._status = '200 OK'
_response._headers = [('Content-Length', '4')] _response._headers = [('Content-Length', '4')]
body = _response.body = test_streamiterator() body = _response.body = TestStreamIterator()
environ = self._makeEnviron() environ = self._makeEnviron()
start_response = DummyCallable() start_response = DummyCallable()
_publish = DummyCallable() _publish = DummyCallable()
...@@ -384,7 +387,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -384,7 +387,7 @@ class Test_publish_module(unittest.TestCase):
from ZPublisher.Iterators import IUnboundStreamIterator from ZPublisher.Iterators import IUnboundStreamIterator
from zope.interface import implements from zope.interface import implements
class test_unboundstreamiterator: class TestUnboundStreamIterator(object):
implements(IUnboundStreamIterator) implements(IUnboundStreamIterator)
data = "hello" data = "hello"
done = 0 done = 0
...@@ -397,7 +400,7 @@ class Test_publish_module(unittest.TestCase): ...@@ -397,7 +400,7 @@ class Test_publish_module(unittest.TestCase):
_response = DummyResponse() _response = DummyResponse()
_response._status = '200 OK' _response._status = '200 OK'
body = _response.body = test_unboundstreamiterator() body = _response.body = TestUnboundStreamIterator()
environ = self._makeEnviron() environ = self._makeEnviron()
start_response = DummyCallable() start_response = DummyCallable()
_publish = DummyCallable() _publish = DummyCallable()
...@@ -410,15 +413,17 @@ class Test_publish_module(unittest.TestCase): ...@@ -410,15 +413,17 @@ class Test_publish_module(unittest.TestCase):
start_response = DummyCallable() start_response = DummyCallable()
_request = DummyRequest() _request = DummyRequest()
_request._closed = False _request._closed = False
def _close(): def _close():
_request._closed = True _request._closed = True
_request.close = _close _request.close = _close
def _request_factory(stdin, environ, response): def _request_factory(stdin, environ, response):
return _request return _request
_publish = DummyCallable() _publish = DummyCallable()
_publish._result = DummyResponse() _publish._result = DummyResponse()
app_iter = self._callFUT(environ, start_response, _publish, self._callFUT(environ, start_response, _publish,
_request_factory=_request_factory) _request_factory=_request_factory)
self.assertTrue(_request._closed) self.assertTrue(_request._closed)
def test_request_not_closed_when_tm_middleware_active(self): def test_request_not_closed_when_tm_middleware_active(self):
...@@ -429,21 +434,25 @@ class Test_publish_module(unittest.TestCase): ...@@ -429,21 +434,25 @@ class Test_publish_module(unittest.TestCase):
start_response = DummyCallable() start_response = DummyCallable()
_request = DummyRequest() _request = DummyRequest()
_request._closed = False _request._closed = False
def _close(): def _close():
_request._closed = True _request._closed = True
_request.close = _close _request.close = _close
def _request_factory(stdin, environ, response): def _request_factory(stdin, environ, response):
return _request return _request
_publish = DummyCallable() _publish = DummyCallable()
_publish._result = DummyResponse() _publish._result = DummyResponse()
app_iter = self._callFUT(environ, start_response, _publish, self._callFUT(environ, start_response, _publish,
_request_factory=_request_factory) _request_factory=_request_factory)
self.assertFalse(_request._closed) self.assertFalse(_request._closed)
txn = transaction.get() txn = transaction.get()
self.assertTrue(txn in WSGIPublisher._request_closer_for_repoze_tm.requests) self.assertTrue(
txn in WSGIPublisher._request_closer_for_repoze_tm.requests)
txn.commit() txn.commit()
self.assertTrue(_request._closed) self.assertTrue(_request._closed)
self.assertFalse(txn in WSGIPublisher._request_closer_for_repoze_tm.requests) self.assertFalse(
txn in WSGIPublisher._request_closer_for_repoze_tm.requests)
# try again, but this time raise an exception and abort # try again, but this time raise an exception and abort
_request._closed = False _request._closed = False
_publish._raise = Exception('oops') _publish._raise = Exception('oops')
...@@ -452,9 +461,11 @@ class Test_publish_module(unittest.TestCase): ...@@ -452,9 +461,11 @@ class Test_publish_module(unittest.TestCase):
_request_factory=_request_factory) _request_factory=_request_factory)
self.assertFalse(_request._closed) self.assertFalse(_request._closed)
txn = transaction.get() txn = transaction.get()
self.assertTrue(txn in WSGIPublisher._request_closer_for_repoze_tm.requests) self.assertTrue(
txn in WSGIPublisher._request_closer_for_repoze_tm.requests)
txn.abort() txn.abort()
self.assertFalse(txn in WSGIPublisher._request_closer_for_repoze_tm.requests) self.assertFalse(
txn in WSGIPublisher._request_closer_for_repoze_tm.requests)
self.assertTrue(_request._closed) self.assertTrue(_request._closed)
...@@ -471,6 +482,7 @@ class DummyRequest(dict): ...@@ -471,6 +482,7 @@ class DummyRequest(dict):
self._traversed = (path, response, validated_hook) self._traversed = (path, response, validated_hook)
return self._traverse_to return self._traverse_to
class DummyResponse(object): class DummyResponse(object):
debug_mode = False debug_mode = False
after_list = () after_list = ()
...@@ -489,6 +501,7 @@ class DummyResponse(object): ...@@ -489,6 +501,7 @@ class DummyResponse(object):
body = property(lambda self: self._body, setBody) body = property(lambda self: self._body, setBody)
class DummyCallable(object): class DummyCallable(object):
_called_with = _raise = _result = None _called_with = _raise = _result = None
...@@ -498,19 +511,13 @@ class DummyCallable(object): ...@@ -498,19 +511,13 @@ class DummyCallable(object):
raise self._raise raise self._raise
return self._result return self._result
class DummyTM(object): class DummyTM(object):
_recorded = _raise = _result = None _recorded = _raise = _result = None
def recordMetaData(self, *args): def recordMetaData(self, *args):
self._recorded = args self._recorded = args
def noopStartResponse(status, headers): def noopStartResponse(status, headers):
pass pass
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(WSGIResponseTests),
unittest.makeSuite(Test_publish),
unittest.makeSuite(Test_publish_module),
))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment