def test_finish_exception(self, mock_logger): """Assert that if ``finish`` raises a RuntimeError, it's logged.""" responder = Responder(Mock()) responder.request.finish.side_effect = RuntimeError('Womp womp') responder.finish() responder.request.finish.assert_called_once_with() mock_logger.debug.assert_called_once_with('Womp womp')
def test_close(self, mock_reactor): """ `close` invokes the request.finish method on the Twisted request. """ responder = Responder(Mock()) responder.close() mock_reactor.callFromThread.assert_called_once_with(responder.finish)
def test_enter(self): """ `__enter__` returns the instance of the class. """ responder = Responder(Mock()) result = responder.__enter__() self.assertTrue(responder is result)
def test_write(self, mock_reactor): """ `write` forwards all data to the `request.write` method. """ responder = Responder(Mock()) responder.write('some data') mock_reactor.callFromThread.assert_called_once_with(responder.request.write, 'some data')
def test_exit(self): """ `__exit__` invokes the `close` method on the instance. """ responder = Responder(Mock()) responder.close = Mock() responder.__exit__(None, None, None) responder.close.assert_called_once_with()
def test_with(self, mock_reactor): """ The Responder class supports context managers. """ mock_request = Mock() with Responder(mock_request) as r: r.write('some data') mock_calls = mock_reactor.callFromThread.call_args_list self.assertEqual((mock_request.write, 'some data'), mock_calls[0][0]) self.assertEqual((r.finish,), mock_calls[1][0])
def test_finish(self): """Assert the ``finish`` method is called by its wrapper""" responder = Responder(Mock()) responder.finish() responder.request.finish.assert_called_once_with()