def make_context(status, headers): context = wsgi._ApplicationContext() context._status = status context._headers = headers context.sendfile(mock_file) context.end_body_chunks() return context
def test_run_application_sendfile(self): mock_file = unittest.mock.Mock() mock_app = unittest.mock.AsyncMock() mock_app.return_value = wsgi.FileWrapper(mock_file) session = wsgi.HttpSession(None, mock_app, {}) context = wsgi._ApplicationContext() run_task = tasks.spawn(session._run_application(context, {})) get_task = tasks.spawn(self.get_body_chunks(context)) kernels.run(timeout=0.01) self.assertTrue(context._chunks.is_closed()) self.assertTrue(run_task.is_completed()) run_task.get_result_nonblocking() self.assertTrue(get_task.is_completed()) self.assertEqual(get_task.get_result_nonblocking(), []) self.assertIs(context.file, mock_file) # `close` is not called because ownership is transferred to # context. mock_file.close.assert_not_called()
def make_context(status, headers, *body_chunks): context = wsgi._ApplicationContext() context._status = status context._headers = headers context._chunks = queues.Queue() # Unset capacity for test. for chunk in body_chunks: context._chunks.put_nonblocking(chunk) context.end_body_chunks() return context
def test_run_application_non_aiter(self): mock_app = unittest.mock.AsyncMock() mock_app.return_value = [b'x', b'', b'', b'', b'y'] session = wsgi.HttpSession(None, mock_app, {}) context = wsgi._ApplicationContext() run_task = tasks.spawn(session._run_application(context, {})) get_task = tasks.spawn(self.get_body_chunks(context)) kernels.run(timeout=0.01) self.assertTrue(context._chunks.is_closed()) self.assertTrue(run_task.is_completed()) run_task.get_result_nonblocking() self.assertTrue(get_task.is_completed()) self.assertEqual(get_task.get_result_nonblocking(), [b'x', b'y'])
def test_run_application_aiter(self): class MockBody: def __init__(self): self._iter = iter([b'x', b'', b'', b'', b'y']) self.closed = False def __aiter__(self): return self async def __anext__(self): try: return next(self._iter) except StopIteration: raise StopAsyncIteration from None def close(self): self.closed = True mock_body = MockBody() mock_app = unittest.mock.AsyncMock() mock_app.side_effect = [mock_body] session = wsgi.HttpSession(None, mock_app, {}) context = wsgi._ApplicationContext() run_task = tasks.spawn(session._run_application(context, {})) get_task = tasks.spawn(self.get_body_chunks(context)) kernels.run(timeout=0.01) self.assertTrue(mock_body.closed) self.assertTrue(context._chunks.is_closed()) self.assertTrue(run_task.is_completed()) run_task.get_result_nonblocking() self.assertTrue(get_task.is_completed()) self.assertEqual(get_task.get_result_nonblocking(), [b'x', b'y'])
def setUp(self): super().setUp() self.context = wsgi._ApplicationContext()