|
| 1 | +from ddtrace.contrib.pyramid import trace_pyramid |
| 2 | + |
| 3 | +from pyramid.response import Response |
| 4 | +from pyramid.config import Configurator |
| 5 | +from pyramid.renderers import render_to_response |
| 6 | +from pyramid.httpexceptions import ( |
| 7 | + HTTPInternalServerError, |
| 8 | + HTTPFound, |
| 9 | + HTTPNotFound, |
| 10 | + HTTPException, |
| 11 | + HTTPNoContent, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def create_app(settings, instrument): |
| 16 | + """Return a pyramid wsgi app""" |
| 17 | + |
| 18 | + def index(request): |
| 19 | + return Response('idx') |
| 20 | + |
| 21 | + def error(request): |
| 22 | + raise HTTPInternalServerError("oh no") |
| 23 | + |
| 24 | + def exception(request): |
| 25 | + 1 / 0 |
| 26 | + |
| 27 | + def json(request): |
| 28 | + return {'a': 1} |
| 29 | + |
| 30 | + def renderer(request): |
| 31 | + return render_to_response('template.pt', {'foo': 'bar'}, request=request) |
| 32 | + |
| 33 | + def raise_redirect(request): |
| 34 | + raise HTTPFound() |
| 35 | + |
| 36 | + def raise_no_content(request): |
| 37 | + raise HTTPNoContent() |
| 38 | + |
| 39 | + def custom_exception_view(context, request): |
| 40 | + """Custom view that forces a HTTPException when no views |
| 41 | + are found to handle given request |
| 42 | + """ |
| 43 | + if 'raise_exception' in request.url: |
| 44 | + raise HTTPNotFound() |
| 45 | + else: |
| 46 | + return HTTPNotFound() |
| 47 | + |
| 48 | + config = Configurator(settings=settings) |
| 49 | + config.add_route('index', '/') |
| 50 | + config.add_route('error', '/error') |
| 51 | + config.add_route('exception', '/exception') |
| 52 | + config.add_route('json', '/json') |
| 53 | + config.add_route('renderer', '/renderer') |
| 54 | + config.add_route('raise_redirect', '/redirect') |
| 55 | + config.add_route('raise_no_content', '/nocontent') |
| 56 | + config.add_view(index, route_name='index') |
| 57 | + config.add_view(error, route_name='error') |
| 58 | + config.add_view(exception, route_name='exception') |
| 59 | + config.add_view(json, route_name='json', renderer='json') |
| 60 | + config.add_view(renderer, route_name='renderer', renderer='template.pt') |
| 61 | + config.add_view(raise_redirect, route_name='raise_redirect') |
| 62 | + config.add_view(raise_no_content, route_name='raise_no_content') |
| 63 | + # required to reproduce a regression test |
| 64 | + config.add_notfound_view(custom_exception_view) |
| 65 | + # required for rendering tests |
| 66 | + renderer = config.testing_add_renderer('template.pt') |
| 67 | + |
| 68 | + if instrument: |
| 69 | + trace_pyramid(config) |
| 70 | + |
| 71 | + return config.make_wsgi_app(), renderer |
0 commit comments