Skip to content

Commit

Permalink
Python 3: make test_wsgiserver pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Oct 21, 2017
1 parent 2cf78fa commit 0050107
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/zope/server/http/tests/test_wsgiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class Conflict(Exception):
Pseudo ZODB conflict error.
"""

ERROR_RESPONSE = "error occurred"
RESPONSE = "normal response"
ERROR_RESPONSE = b"error occurred"
RESPONSE = b"normal response"

class DummyException(Exception):
value = "Dummy Exception to test start_response"
Expand Down Expand Up @@ -181,7 +181,7 @@ def application(environ, start_response):

self.port = self.server.socket.getsockname()[1]
self.run_loop = 1
self.thread = Thread(target=self.loop)
self.thread = Thread(target=self.loop, name='test_wsgiserver')
self.thread.start()
sleep(0.1) # Give the thread some time to start.

Expand All @@ -196,7 +196,7 @@ def loop(self):
while self.run_loop:
poll(0.1, socket_map)

def invokeRequest(self, path='/', add_headers=None, request_body='',
def invokeRequest(self, path='/', add_headers=None, request_body=b'',
return_response=False):
h = HTTPConnection(LOCALHOST, self.port)
h.putrequest('GET', path)
Expand All @@ -216,7 +216,7 @@ def invokeRequest(self, path='/', add_headers=None, request_body='',
if length:
response_body = response.read(length)
else:
response_body = ''
response_body = b''

self.assertEqual(length, len(response_body))

Expand All @@ -228,7 +228,7 @@ def testDeeperPath(self):
self.assertEqual(status, 200)
expect_response = 'URL invoked: http://%s:%d/folder/item' % (
LOCALHOST, self.port)
self.assertEqual(response_body, expect_response)
self.assertEqual(response_body, expect_response.encode())

def testNotFound(self):
status, response_body = self.invokeRequest('/foo/bar')
Expand Down Expand Up @@ -271,52 +271,52 @@ def testServerAsProxy(self):
self.assertEqual(
response.getheader('Via'), 'zope.server.http (Browser)')
# And the content got here too.
self.assertEqual(response.read(), 'Proxied Content')
self.assertEqual(response.read(), b'Proxied Content')

def testWSGIVariables(self):
# Assert that the environment contains all required WSGI variables
status, response_body = self.invokeRequest('/wsgi')
wsgi_variables = set(response_body.split())
wsgi_variables = set(response_body.decode().split())
self.assertEqual(wsgi_variables,
set(['wsgi.version', 'wsgi.url_scheme', 'wsgi.input',
'wsgi.errors', 'wsgi.multithread',
'wsgi.multiprocess', 'wsgi.run_once']))

def testWSGIVersion(self):
status, response_body = self.invokeRequest('/wsgi/version')
self.assertEqual("(1, 0)", response_body)
self.assertEqual(b"(1, 0)", response_body)

def testWSGIURLScheme(self):
status, response_body = self.invokeRequest('/wsgi/url_scheme')
self.assertEqual('http', response_body)
self.assertEqual(b'http', response_body)

def testWSGIMultithread(self):
status, response_body = self.invokeRequest('/wsgi/multithread')
self.assertEqual('True', response_body)
self.assertEqual(b'True', response_body)

def testWSGIMultiprocess(self):
status, response_body = self.invokeRequest('/wsgi/multiprocess')
self.assertEqual('True', response_body)
self.assertEqual(b'True', response_body)

def testWSGIRunOnce(self):
status, response_body = self.invokeRequest('/wsgi/run_once')
self.assertEqual('False', response_body)
self.assertEqual(b'False', response_body)

def testWSGIProxy(self):
status, response_body = self.invokeRequest(
'https://zope.org:8080/wsgi/proxy_scheme')
self.assertEqual('https', response_body)
self.assertEqual(b'https', response_body)
status, response_body = self.invokeRequest(
'https://zope.org:8080/wsgi/proxy_host')
self.assertEqual('zope.org:8080', response_body)
self.assertEqual(b'zope.org:8080', response_body)

def test_ensure_multiple_task_write_calls(self):
# In order to get data out as fast as possible, the WSGI server needs
# to call task.write() multiple times.
orig_app = self.server.application
def app(eviron, start_response):
start_response('200 Ok', [])
return ['This', 'is', 'my', 'response.']
return [b'This', b'is', b'my', b'response.']
self.server.application = app

class FakeTask:
Expand Down Expand Up @@ -444,13 +444,14 @@ def __init__(self, value):
def __iter__(self):
return self

def next(self):
return self._iter.next()
def __next__(self):
return next(self._iter)
next = __next__

def close(self):
self.closed = True

iterator = CloseableIterator(["Klaatu", "barada", "nikto"])
iterator = CloseableIterator([b"Klaatu", b"barada", b"nikto"])
def app(environ, start_response):
start_response("200 Ok", [], None)
return iterator
Expand Down Expand Up @@ -484,7 +485,7 @@ def _getServerClass(self):
def testWSGIVariables(self):
# Assert that the environment contains all required WSGI variables
status, response_body = self.invokeRequest('/wsgi')
wsgi_variables = set(response_body.split())
wsgi_variables = set(response_body.decode().split())
self.assertEqual(wsgi_variables,
set(['wsgi.version', 'wsgi.url_scheme', 'wsgi.input',
'wsgi.errors', 'wsgi.multithread',
Expand Down

0 comments on commit 0050107

Please sign in to comment.