test_exception_handling.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. # FIXME - G.M - 17-05-2018 - Verify if:
  20. # - other view that work/raise an other exception do not go
  21. # into this code for handle this exceptions.
  22. # - response come clearly from there, not from web framework:
  23. # Check not only http code, but also body.
  24. context.handle_exception(ZeroDivisionError, http_code=400)
  25. test_app = TestApp(app)
  26. response = test_app.get('/my-view', status='*')
  27. assert 400 == response.status_code
  28. def test_func__catch_one_exception__ok__pyramid(self):
  29. # TODO - G.M - 17-05-2018 - Move/refactor this test
  30. # in order to have here only framework agnostic test
  31. # and framework_specific
  32. # test somewhere else.
  33. hapic = Hapic()
  34. configurator = Configurator(autocommit=True)
  35. context = PyramidContext(configurator)
  36. hapic.set_context(context)
  37. def my_view(context, request):
  38. raise ZeroDivisionError('An exception message')
  39. configurator.add_route('my_view','/my-view', request_method='GET')
  40. configurator.add_view(my_view, route_name='my_view', renderer='json')
  41. # FIXME - G.M - 17-05-2018 - Verify if:
  42. # - other view that work/raise an other exception do not go
  43. # into this code for handle this exceptions.
  44. # - response come clearly from there, not from web framework:
  45. # Check not only http code, but also body.
  46. context.handle_exception(ZeroDivisionError, http_code=400)
  47. app = configurator.make_wsgi_app()
  48. test_app = TestApp(app)
  49. response = test_app.get('/my-view', status='*')
  50. assert 400 == response.status_code