test_exception_handling.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # coding: utf-8
  2. import bottle
  3. from pyramid.config import Configurator
  4. from webtest import TestApp
  5. from hapic import Hapic
  6. from hapic.ext.pyramid import PyramidContext
  7. from tests.base import Base
  8. from tests.base import MyContext
  9. class TestExceptionHandling(Base):
  10. def test_func__catch_one_exception__ok__nominal_case(self):
  11. hapic = Hapic()
  12. # TODO BS 2018-05-04: Make this test non-bottle
  13. app = bottle.Bottle()
  14. context = MyContext(app=app)
  15. hapic.set_context(context)
  16. def my_view():
  17. raise ZeroDivisionError('An exception message')
  18. app.route('/my-view', method='GET', callback=my_view)
  19. context.handle_exception(ZeroDivisionError, http_code=400)
  20. test_app = TestApp(app)
  21. response = test_app.get('/my-view', status='*')
  22. assert 400 == response.status_code
  23. def test_func__catch_one_exception__ok__pyramid(self):
  24. # TODO - G.M - 17-05-2018 - Move/refactor this test
  25. # in order to have here only framework agnostic test
  26. # and framework_specific
  27. # test somewhere else.
  28. hapic = Hapic()
  29. configurator = Configurator(autocommit=True)
  30. context = PyramidContext(configurator)
  31. hapic.set_context(context)
  32. def my_view(context, request):
  33. raise ZeroDivisionError('An exception message')
  34. configurator.add_route('my_view','/my-view', request_method='GET')
  35. configurator.add_view(my_view, route_name='my_view', renderer='json')
  36. context.handle_exception(ZeroDivisionError, http_code=400)
  37. app = configurator.make_wsgi_app()
  38. test_app = TestApp(app)
  39. response = test_app.get('/my-view', status='*')
  40. assert 400 == response.status_code