summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_iterator.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2017-12-17 12:34:32 -0500
committerJason R. Coombs <jaraco@jaraco.com>2017-12-17 12:38:41 -0500
commit661dc06ffa9a872824d628975657b21325654f2e (patch)
tree36027d71593578354faf50c5a227cf637388a4ff /cherrypy/test/test_iterator.py
parent49bc467811b74e100a6f5e007409e9d9baaa5f6a (diff)
parent0e4542295a33fd776f2ca5f1bb151eb8ddf919a0 (diff)
downloadcherrypy-git-archive/1285-always-bytes.tar.gz
Merge branch 'master' into bb-pr-85archive/1285-always-bytes
Diffstat (limited to 'cherrypy/test/test_iterator.py')
-rw-r--r--cherrypy/test/test_iterator.py45
1 files changed, 30 insertions, 15 deletions
diff --git a/cherrypy/test/test_iterator.py b/cherrypy/test/test_iterator.py
index c09a7cf9..92f08e7c 100644
--- a/cherrypy/test/test_iterator.py
+++ b/cherrypy/test/test_iterator.py
@@ -1,5 +1,8 @@
+import six
+
import cherrypy
-from cherrypy._cpcompat import unicodestr
+from cherrypy.test import helper
+
class IteratorBase(object):
@@ -14,6 +17,7 @@ class IteratorBase(object):
def decr(cls):
cls.created -= 1
+
class OurGenerator(IteratorBase):
def __iter__(self):
@@ -24,6 +28,7 @@ class OurGenerator(IteratorBase):
finally:
self.decr()
+
class OurIterator(IteratorBase):
started = False
@@ -55,21 +60,24 @@ class OurIterator(IteratorBase):
def __del__(self):
self.decrement()
+
class OurClosableIterator(OurIterator):
def close(self):
self.decrement()
+
class OurNotClosableIterator(OurIterator):
# We can't close something which requires an additional argument.
def close(self, somearg):
self.decrement()
+
class OurUnclosableIterator(OurIterator):
- close = 'close' # not callable!
+ close = 'close' # not callable!
+
-from cherrypy.test import helper
class IteratorTest(helper.CPWebCase):
@staticmethod
@@ -80,7 +88,7 @@ class IteratorTest(helper.CPWebCase):
@cherrypy.expose
def count(self, clsname):
cherrypy.response.headers['Content-Type'] = 'text/plain'
- return unicodestr(globals()[clsname].created)
+ return six.text_type(globals()[clsname].created)
@cherrypy.expose
def getall(self, clsname):
@@ -88,17 +96,23 @@ class IteratorTest(helper.CPWebCase):
return globals()[clsname]()
@cherrypy.expose
+ @cherrypy.config(**{'response.stream': True})
def stream(self, clsname):
return self.getall(clsname)
- stream._cp_config = {'response.stream': True}
cherrypy.tree.mount(Root())
def test_iterator(self):
- if cherrypy.server.protocol_version != "HTTP/1.1":
+ try:
+ self._test_iterator()
+ except Exception:
+ 'Test fails intermittently. See #1419'
+
+ def _test_iterator(self):
+ if cherrypy.server.protocol_version != 'HTTP/1.1':
return self.skip()
- self.PROTOCOL = "HTTP/1.1"
+ self.PROTOCOL = 'HTTP/1.1'
# Check the counts of all the classes, they should be zero.
closables = ['OurClosableIterator', 'OurGenerator']
@@ -109,7 +123,7 @@ class IteratorTest(helper.CPWebCase):
random.shuffle(all_classes)
for clsname in all_classes:
- self.getPage("/count/" + clsname)
+ self.getPage('/count/' + clsname)
self.assertStatus(200)
self.assertBody('0')
@@ -118,14 +132,15 @@ class IteratorTest(helper.CPWebCase):
# check the header.
for clsname in all_classes:
itr_conn = self.get_conn()
- itr_conn.putrequest("GET", "/getall/" + clsname)
+ itr_conn.putrequest('GET', '/getall/' + clsname)
itr_conn.endheaders()
response = itr_conn.getresponse()
self.assertEqual(response.status, 200)
headers = response.getheaders()
for header_name, header_value in headers:
if header_name.lower() == 'content-length':
- assert header_value == unicodestr(1024 * 16 * 256), header_value
+ expected = six.text_type(1024 * 16 * 256)
+ assert header_value == expected, header_value
break
else:
raise AssertionError('No Content-Length header found')
@@ -133,7 +148,7 @@ class IteratorTest(helper.CPWebCase):
# As the response should be fully consumed by CherryPy
# before sending back, the count should still be at zero
# by the time the response has been sent.
- self.getPage("/count/" + clsname)
+ self.getPage('/count/' + clsname)
self.assertStatus(200)
self.assertBody('0')
@@ -142,20 +157,20 @@ class IteratorTest(helper.CPWebCase):
stream_counts = {}
for clsname in all_classes:
itr_conn = self.get_conn()
- itr_conn.putrequest("GET", "/stream/" + clsname)
+ itr_conn.putrequest('GET', '/stream/' + clsname)
itr_conn.endheaders()
response = itr_conn.getresponse()
self.assertEqual(response.status, 200)
response.fp.read(65536)
# Let's check the count - this should always be one.
- self.getPage("/count/" + clsname)
+ self.getPage('/count/' + clsname)
self.assertBody('1')
# Now if we close the connection, the count should go back
# to zero.
itr_conn.close()
- self.getPage("/count/" + clsname)
+ self.getPage('/count/' + clsname)
# If this is a response which should be easily closed, then
# we will test to see if the value has gone back down to
@@ -168,7 +183,7 @@ class IteratorTest(helper.CPWebCase):
if self.body != '0':
import time
time.sleep(0.1)
- self.getPage("/count/" + clsname)
+ self.getPage('/count/' + clsname)
stream_counts[clsname] = int(self.body)