WSGI middleware layer is now handling result iterators correctly.

This commit is contained in:
Bogdan Berce 2015-01-27 08:33:41 -08:00
Родитель 9a0850abdb
Коммит 7975cd8f79
2 изменённых файлов: 22 добавлений и 6 удалений

Просмотреть файл

@ -67,14 +67,25 @@ class WSGIApplication(object):
name = '/'
if 'PATH_INFO' in environ:
name = environ['PATH_INFO']
if not name:
name = '/'
closure = Closure()
closure.status = '200 OK'
def status_interceptor(status_string, headers_array):
def status_interceptor(status_string, headers_array, exc_info=None):
closure.status = status_string
start_response(status_string, headers_array)
start_response(status_string, headers_array, exc_info)
response = self._wsgi_application(environ, status_interceptor)
try:
iterable_response = iter(response)
except TypeError:
iterable_response = None
if iterable_response:
for part in iterable_response:
yield part
response_code = re.match('\s*(?P<code>\d+)', closure.status).group('code')
success = True
if int(response_code) >= 400:
@ -91,4 +102,6 @@ class WSGIApplication(object):
duration = int((end_time - start_time).total_seconds() * 1000)
self.client.track_request(name, url, success, start_time.isoformat() + 'Z', duration, response_code, http_method)
return response
if not iterable_response:
return response

Просмотреть файл

@ -33,6 +33,9 @@ class TestWSGIApplication(unittest.TestCase):
}
result = wrapper(env, mock_start_response)
result_string = None
for part in result:
result_string = part
data = sender.data[0][0]
self.assertEqual('Microsoft.ApplicationInsights.Request', data.name)
@ -44,7 +47,7 @@ class TestWSGIApplication(unittest.TestCase):
self.assertTrue(data.data.base_data.success)
self.assertEqual('/foo/bar?a=b', data.data.base_data.url)
self.assertIsNotNone(data.data.base_data.id)
self.assertEqual('Hello World!', result)
self.assertEqual(b'Hello World!', result_string)
self.assertEqual(1, mock_start_response_calls)
self.assertEqual('201 BLAH', mock_start_response_status)
self.assertEqual([('Content-type', 'text/plain')], mock_start_response_headers)
@ -67,7 +70,7 @@ mock_start_response_status = None
mock_start_response_headers = None
def mock_start_response(status, headers):
def mock_start_response(status, headers, exc_info=None):
global mock_start_response_calls
global mock_start_response_status
global mock_start_response_headers
@ -80,7 +83,7 @@ def mock_app(environ, start_response):
status = '201 BLAH'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return "Hello World!"
return [b'Hello World!']
class MockSynchronousSender: