def test_middleware__no_exc(app): @asyncio.coroutine def handler(request): return 'response' mw = yield from exc_handlers_middleware(app, handler) response = yield from mw('request') assert response == 'response'
def test_middleware__http_exc(app): @asyncio.coroutine def handler(request): raise HTTPMethodNotAllowed(method='GET', allowed_methods=[]) mw = yield from exc_handlers_middleware(app, handler) response = yield from mw('request') assert isinstance(response, HTTPMethodNotAllowed)
def test_middleware__not_found(app): @asyncio.coroutine def handler(request): raise RuntimeError() mw = yield from exc_handlers_middleware(app, handler) with pytest.raises(RuntimeError): yield from mw('request')
def test_middleware__exc_eq(app): @asyncio.coroutine def handler(request): raise RuntimeError() @asyncio.coroutine def exc_handler(request, exc): return 'response' app['exc_handlers'] = {RuntimeError: exc_handler} mw = yield from exc_handlers_middleware(app, handler) response = yield from mw('request') assert response == 'response'
def test_middleware__exc_inheritance(app): class Exc(RuntimeError): pass @asyncio.coroutine def handler(request): raise Exc() @asyncio.coroutine def exc_handler(request, exc): return 'response' app['exc_handlers'] = {RuntimeError: exc_handler} mw = yield from exc_handlers_middleware(app, handler) response = yield from mw('request') assert response == 'response'
def test_middleware__twice_exc(app): @asyncio.coroutine def handler(request): raise RuntimeError() @asyncio.coroutine def exc_handler_1(request, exc): raise ValueError() @asyncio.coroutine def exc_handler_2(request, exc): return 'response' app['exc_handlers'] = { RuntimeError: exc_handler_1, ValueError: exc_handler_2, } mw = yield from exc_handlers_middleware(app, handler) with pytest.raises(ValueError): yield from mw('request')