Exemplo n.º 1
0
def test_response_not_altered(mock_client):
    """Assert that the response was not changed by the middleware."""
    timed_app = StatsdTimingMiddleware(application, mock_client)
    app = TestApp(timed_app)
    response = app.get('/test')

    assert response.body.decode() == u'The request method was GET'
Exemplo n.º 2
0
def test_exception_response(mock_client, mock_close, time_exceptions):
    """Assert that we time exceptions (during the response) depending on the time_exceptions param."""
    timed_app = StatsdTimingMiddleware(raising_application, mock_client, time_exceptions=time_exceptions)
    app = TestApp(timed_app)
    with mock.patch.object(mock_client, 'timer', autospec=True) as mock_timer:
        with pytest.raises(Exception):
            app.get('/test')
    assert mock_timer.return_value.stop.called == time_exceptions
    assert not mock_close.called
    if time_exceptions:
        assert mock_timer.call_args[0] == ('test.GET.200.Exception',)
Exemplo n.º 3
0
    def create_wsgi(app, **kwargs):
        application = wsgi_factory(app, **kwargs)

        host = app.config.get('STATSD_HOST')
        port = app.config.get('STATSD_PORT', 8125)
        prefix = app.config.get('STATSD_PREFIX')

        if host and port and prefix:
            client = StatsClient(prefix=prefix, host=host, port=port)
            return StatsdTimingMiddleware(application, client)
        return application
Exemplo n.º 4
0
def test_timer(mock_client):
    """Test the timer functionality.

    Check the following:
    - timer.stop() is called
    - timer.ms is not None
    - the key is generated as expected, i.e. UNDERSCORED_PATH_INFO.REQUEST_METHOD.RESPONSE_CODE.
    """
    with mock.patch.object(mock_client, 'timer', autospec=True) as mock_timer:
        timed_app = StatsdTimingMiddleware(application, mock_client)
        app = TestApp(timed_app)
        app.get('/test/some/thing.ext/?param=one&two=3')

    assert mock_timer.return_value.stop.called
    assert mock_timer.return_value.ms is not None
    assert mock_timer.call_args[0] == ('test_some_thing_ext.GET.200',)
Exemplo n.º 5
0
    def create_wsgi(app, **kwargs):
        application = wsgi_factory(app, **kwargs)

        # Remove X-Forwarded-For headers because Flask-Security doesn't know
        # how to deal with them properly. Note REMOTE_ADDR has already been
        # set correctly at this point by the ``wsgi_proxyfix`` factory.
        if app.config.get('WSGI_PROXIES'):
            application = HeaderRewriterFix(application,
                                            remove_headers=['X-Forwarded-For'])

        host = app.config.get('STATSD_HOST')
        port = app.config.get('STATSD_PORT', 8125)
        prefix = app.config.get('STATSD_PREFIX')

        if host and port and prefix:
            client = StatsClient(prefix=prefix, host=host, port=port)
            return StatsdTimingMiddleware(application, client)
        return application
Exemplo n.º 6
0
def test_exception_iter(mock_client, mock_close, monkeypatch, time_exceptions):
    """Assert that the response close is called when exists even if there's exception during the iteration."""
    mock_close.next_called = False

    def response_next(self):
        mock_close.next_called = True
        raise Exception()

    if six.PY3:
        monkeypatch.setattr(AppIterRange, '__next__', response_next)
    else:
        monkeypatch.setattr(AppIterRange, 'next', response_next)

    timed_app = StatsdTimingMiddleware(application, mock_client, time_exceptions=time_exceptions)
    app = TestApp(timed_app)
    with mock.patch.object(mock_client, 'timer', autospec=True) as mock_timer:
        with pytest.raises(Exception):
            app.get('/test')
    assert mock_close.called
    assert mock_close.next_called
    if time_exceptions:
        assert mock_timer.call_args[0] == ('test.GET.200.Exception',)
Exemplo n.º 7
0
def test_close_called(mock_client, mock_close):
    """Assert that the response close is called when exists."""
    timed_app = StatsdTimingMiddleware(application, mock_client)
    app = TestApp(timed_app)
    app.get('/test')
    assert mock_close.called
Exemplo n.º 8
0
    def make_middleware(self, app, config):
        if (StatsdPlugin.LOG_TIMES == False) or not StatsdPlugin.HOST:
            return app

        application = StatsdTimingMiddleware(app, self.client)
        return application