コード例 #1
0
def test_request_id_is_set_on_response(app):
    request_id.init_app(app)
    client = app.test_client()

    with app.app_context():
        response = client.get('/', headers={'DM-REQUEST-ID': 'generated'})
        assert response.headers['DM-Request-ID'] == 'generated'
コード例 #2
0
def test_request_id_is_set_on_response(app):
    request_id.init_app(app)
    client = app.test_client()

    with app.app_context():
        response = client.get('/', headers={'DM-REQUEST-ID': 'generated'})
        assert response.headers['DM-Request-ID'] == 'generated'.encode('utf-8')
コード例 #3
0
def test_request_id_is_set_on_error_response(app):
    request_id.init_app(app)
    client = app.test_client()

    @app.route('/')
    def error_route():
        raise Exception()

    with app.app_context():
        response = client.get('/', headers={'DM-REQUEST-ID': 'generated'})
        assert response.status_code == 500
        assert response.headers['DM-Request-ID'] == 'generated'
コード例 #4
0
def test_request_id_is_set_on_error_response(app):
    request_id.init_app(app)
    client = app.test_client()

    @app.route('/')
    def error_route():
        raise Exception()

    with app.app_context():
        response = client.get('/', headers={'DM-REQUEST-ID': 'generated'})
        assert response.status_code == 500
        assert response.headers['DM-Request-ID'] == 'generated'.encode('utf-8')
コード例 #5
0
def test_formatter_request_id(app):
    headers = {'DM-Request-Id': 'generated'}
    request_id.init_app(app)  # set CustomRequest class
    with app.test_request_context('/', headers=headers):
        assert RequestIdFilter().request_id == 'generated'
コード例 #6
0
def test_formatter_request_id(app_with_logging):
    headers = {'DM-Request-Id': 'generated'}
    request_id.init_app(app_with_logging)  # set CustomRequest class
    with app_with_logging.test_request_context('/', headers=headers):
        assert RequestIdFilter().request_id == 'generated'
コード例 #7
0
def init_app(
    application,
    config_object,
    bootstrap=None,
    data_api_client=None,
    db=None,
    login_manager=None,
    search_api_client=None,
    error_handlers=frontend_error_handlers,
):

    application.config.from_object(config_object)
    if hasattr(config_object, 'init_app'):
        config_object.init_app(application)

    application.config.from_object(__name__)

    # all belong to dmutils
    config.init_app(application)
    logging.init_app(application)
    proxy_fix.init_app(application)
    request_id.init_app(application)
    cookie_probe.init_app(application)

    if bootstrap:
        bootstrap.init_app(application)
    if data_api_client:
        data_api_client.init_app(application)
    if db:
        db.init_app(application)
    if login_manager:
        login_manager.init_app(application)
        dmutils.session.init_app(application)
    if search_api_client:
        search_api_client.init_app(application)

    # allow us to use <safepurepath:...> components in route patterns
    application.url_map.converters["safepurepath"] = SafePurePathConverter

    @application.after_request
    def add_header(response):
        # Block sites from rendering our views inside <iframe>, <embed>, etc by default.
        # Individual views may set their own value (e.g. 'SAMEORIGIN')
        if not response.headers.get('X-Frame-Options'):
            response.headers.setdefault('X-Frame-Options', 'DENY')
        return response

    # Make filters accessible in templates.
    application.add_template_filter(filters.capitalize_first)
    application.add_template_filter(filters.format_links)
    application.add_template_filter(filters.nbsp)
    application.add_template_filter(filters.smartjoin)
    application.add_template_filter(filters.preserve_line_breaks)
    application.add_template_filter(filters.sub_country_codes)
    # Make select formats available in templates.
    application.add_template_filter(formats.dateformat)
    application.add_template_filter(formats.datetimeformat)
    application.add_template_filter(formats.datetodatetimeformat)
    application.add_template_filter(formats.displaytimeformat)
    application.add_template_filter(formats.shortdateformat)
    application.add_template_filter(formats.timeformat)
    application.add_template_filter(formats.utcdatetimeformat)
    application.add_template_filter(formats.utctoshorttimelongdateformat)

    @application.context_processor
    def inject_global_template_variables():
        return dict(pluralize=pluralize,
                    **(application.config['BASE_TEMPLATE_DATA'] or {}))

    for exc_or_code, handler in error_handlers.items():
        application.register_error_handler(exc_or_code, handler)