def test_with_additional_skip_headers(self, mock_send_notice):
        self.app.config.update(
            HONEYBADGER_EXCLUDE_HEADERS='Authorization, X-Dark-Land')
        HoneybadgerFlask(self.app, report_exceptions=True)

        @self.app.route('/error')
        def error():
            return 1 / 0

        self.app.test_client().get('/error',
                                   headers={
                                       'X-Wizard-Color': 'grey',
                                       'Authorization': 'Bearer 123',
                                       'X-Dark-Land': 'Mordor'
                                   })

        expected_headers = {'X-Wizard-Color': 'grey'}
        expected_headers.update(self.default_headers)

        self.assert_send_notice_once_with(mock_send_notice,
                                          url='http://localhost/error',
                                          component='tests.flask_tests',
                                          action='error',
                                          params={},
                                          session={},
                                          cgi_data=expected_headers,
                                          context={})
    def test_session(self, mock_send_notice):
        self.app.config.update(
            dict(HONEYBADGER_API_KEY='abcd',
                 HONEYBADGER_PARAMS_FILTERS='skip,password',
                 SECRET_KEY='key'))
        HoneybadgerFlask(self.app, report_exceptions=True)

        @self.app.route('/error')
        def error():
            session['answer'] = '42'
            session['password'] = '******'

            1 / 0

        self.app.test_client().get('/error?a=1&b=2&b=3')

        self.assert_send_notice_once_with(mock_send_notice,
                                          url='http://localhost/error',
                                          component='tests.flask_tests',
                                          action='error',
                                          params={
                                              'a': ['1'],
                                              'b': ['2', '3']
                                          },
                                          session={
                                              'answer': '42',
                                              'password': '******'
                                          },
                                          cgi_data=self.default_headers,
                                          context={})
    def test_do_not_report(self, mock_send_notice):
        HoneybadgerFlask(self.app)

        @self.app.route('/error')
        def error():
            return 1 / 0

        self.app.test_client().get('/error?a=1&b=2&b=3')

        mock_send_notice.assert_not_called()
    def test_without_auto_reporting(self, mock_send_notice):
        self.app = flask.Flask(__name__)
        HoneybadgerFlask(self.app)

        @self.app.route('/error')
        def error():
            return 1 / 0

        self.app.test_client().get('/error')

        mock_send_notice.assert_not_called()
    def test_without_generators(self, mock_send_notice):
        HoneybadgerFlask(self.app, report_exceptions=True)

        @self.app.route('/error')
        def error():
            return 1 / 0

        self.app.test_client().get('/error?a=1&b=2&b=3')

        self.assert_send_notice_once_with(mock_send_notice,
                                          url='http://localhost/error',
                                          component='tests.flask_tests',
                                          action='error',
                                          params={
                                              'a': ['1'],
                                              'b': ['2', '3']
                                          },
                                          session={},
                                          cgi_data=self.default_headers,
                                          context={})
    def test_with_view_class(self, mock_send_notice):
        class ErrorView(MethodView):
            def get(self):
                return 1 / 0

        self.app.add_url_rule('/error', view_func=ErrorView.as_view('error'))

        HoneybadgerFlask(self.app, report_exceptions=True)

        self.app.test_client().get('/error?a=1&b=2&b=3')

        self.assert_send_notice_once_with(
            mock_send_notice,
            url='http://localhost/error',
            component='tests.flask_tests.ErrorView',
            action='error',
            params={
                'a': ['1'],
                'b': ['2', '3']
            },
            session={},
            cgi_data=self.default_headers,
            context={})
    def test_post_form(self, mock_send_notice):
        self.app.config.update(HONEYBADGER_API_KEY='abcd',
                               HONEYBADGER_PARAMS_FILTERS='skip,password')
        HoneybadgerFlask(self.app, report_exceptions=True)

        @self.app.route('/error', methods=['POST'])
        def error():
            return 1 / 0

        self.app.test_client().post('/error?a=1&b=2&b=3',
                                    data={
                                        'foo': 'bar',
                                        'password': '******',
                                        'a': 'newvalue',
                                        'skip': 'secret'
                                    })

        self.assert_send_notice_once_with(
            mock_send_notice,
            url='http://localhost/error',
            component='tests.flask_tests',
            action='error',
            params={
                'a': ['newvalue'],
                'b': ['2', '3'],
                'foo': ['bar'],
                'skip': '[FILTERED]',
                'password': '******'
            },
            session={},
            cgi_data={
                'Content-Length': '46',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Host': 'localhost',
                'User-Agent': 'werkzeug/%s' % werkzeug.__version__
            },
            context={})
    def test_with_headers(self, mock_send_notice):
        HoneybadgerFlask(self.app, report_exceptions=True)

        @self.app.route('/error')
        def error():
            return 1 / 0

        self.app.test_client().get('/error',
                                   headers={
                                       'X-Wizard-Color': 'grey',
                                       'Authorization': 'Bearer 123'
                                   })

        expected_headers = {'X-Wizard-Color': 'grey'}
        expected_headers.update(self.default_headers)

        self.assert_send_notice_once_with(mock_send_notice,
                                          url='http://localhost/error',
                                          component='tests.flask_tests',
                                          action='error',
                                          params={},
                                          session={},
                                          cgi_data=expected_headers,
                                          context={})
    def test_with_blueprint(self, mock_send_notice):
        bp = Blueprint('blueprint', __name__)

        @bp.route('/error')
        def error():
            return 1 / 0

        self.app.register_blueprint(bp)

        HoneybadgerFlask(self.app, report_exceptions=True)

        self.app.test_client().get('/error?a=1&b=2&b=3')

        self.assert_send_notice_once_with(mock_send_notice,
                                          url='http://localhost/error',
                                          component='tests.flask_tests',
                                          action='blueprint.error',
                                          params={
                                              'a': ['1'],
                                              'b': ['2', '3']
                                          },
                                          session={},
                                          cgi_data=self.default_headers,
                                          context={})
import logging

from flask import Flask

from honeybadger_extensions import HoneybadgerFlask
from blueprint import simple_page

logger = logging.getLogger(__name__)

app = Flask(__name__)
app.config['HONEYBADGER_ENVIRONMENT'] = 'test'
app.config['HONEYBADGER_API_KEY'] = '<your key>'
app.config[
    'HONEYBADGER_EXCLUDE_HEADERS'] = 'Authorization, Proxy-Authorization, X-Custom-Key'
app.config['HONEYBADGER_PARAMS_FILTERS'] = 'password, secret, credit-card'
HoneybadgerFlask(app, report_exceptions=True)

app.register_blueprint(simple_page)
from flask import Flask, request

from honeybadger_extensions import HoneybadgerFlask

logger = logging.getLogger(__name__)


def generic_div(a, b):
    """Simple function to divide two numbers"""
    logger.debug('Called generic_div({}, {})'.format(a, b))
    return a / b


app = Flask(__name__)
app.config['HONEYBADGER_ENVIRONMENT'] = 'test'
app.config['HONEYBADGER_API_KEY'] = '<your key>'
app.config['HONEYBADGER_EXCLUDE_HEADERS'] = 'Authorization, Proxy-Authorization, X-Custom-Key'
app.config['HONEYBADGER_PARAMS_FILTERS'] = 'password, secret, credit-card'
HoneybadgerFlask(app, report_exceptions=True, context_generators={
    'request-id': lambda: request.headers.get('X-Request-ID')
})


@app.route('/')
def index():
    a = int(request.args.get('a'))
    b = int(request.args.get('b'))

    logger.info('Dividing two numbers {} {}'.format(a, b))
    return str(generic_div(a, b))