Example #1
0
def test_process_greenlets():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    import gevent

    svc = mock.Mock()

    mock_result_logger = mock.Mock(spec=AbstractResultsLogger)

    app = ProxyFlask(svc, ['localhost'], ['localhost'],
                     result_loggers=[mock_result_logger])

    req, expected_req = _mock_request()

    resp, expected_resp, elapsed_time = _mock_response()
    resp2, expected_resp2, elapsed_time2 = _mock_response()

    ident = lambda x: x

    greenlets = [
        gevent.spawn(ident, x)
        for x in [(resp, elapsed_time), (resp2, elapsed_time2)]
    ]

    app.process_greenlets(
        app.format_request(req.path, req.method, req.headers, req.args,
                           req.form, req), greenlets)

    mock_result_logger.log_result.assert_called_with({
        'request': {
            'original': expected_req,
            'modified': expected_req
        },
        'results': [expected_resp, expected_resp2]
    })
Example #2
0
def test_format_response_resp():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost'],
    )

    resp, expected, elapsed_time = _mock_response()

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #3
0
def test_format_request():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost']
    )

    req, expected = _mock_request()

    eq_(app.format_request(req.path, req.method, req.headers, req.args, req.form, req), {'original': expected, 'modified': expected})
Example #4
0
def test_format_response_resp():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost'],
    )

    resp, expected, elapsed_time = _mock_response()

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #5
0
def test_format_request():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    app = ProxyFlask(svc, ['localhost'], ['localhost'])

    req, expected = _mock_request()

    eq_(
        app.format_request(req.path, req.method, req.headers, req.args,
                           req.form, req), {
                               'original': expected,
                               'modified': expected
                           })
Example #6
0
def test_format_response_unknown():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    resp = mock.Mock()

    app = ProxyFlask(svc, ['localhost'], ['localhost'])
    elapsed_time = 1337.0

    expected = {
        'repr': repr(resp),
        'elapsed_time': elapsed_time,
        'type': 'unknown'
    }

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #7
0
def test_log_result_no_exceptions():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    svc = mock.Mock()

    rs_log = mock.Mock(spec=AbstractResultsLogger)
    rs_log.log_result.side_effect = Exception('Test exception')

    app = ProxyFlask(svc, ['localhost'], ['localhost'],
                     result_loggers=[rs_log])

    app.log_result('hello')

    assert (rs_log.log_result.called)
    eq_(rs_log.log_result.call_count, 1)
    rs_log.log_result.assert_called_with('hello')
Example #8
0
def test_no_new_servers():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()
    app = ProxyFlask(
        svc,
        ['localhost'],
        [],
    )
Example #9
0
def test_format_response_exception():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    resp = mock.Mock(spec=Exception)
    resp.message = "Exception Message!!!!!"

    app = ProxyFlask(svc, ['localhost'], ['localhost'])
    elapsed_time = 1337.0

    expected = {
        'message': resp.message,
        'repr': repr(resp),
        'elapsed_time': elapsed_time,
        'type': 'exception'
    }

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #10
0
def test_log_result():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    svc = mock.Mock()

    class MockResultLogger(AbstractResultsLogger):
        pass

    rs_log = MockResultLogger()
    rs_log.log_result = mock.Mock()

    app = ProxyFlask(svc, ['localhost'], ['localhost'],
                     result_loggers=[rs_log, rs_log])

    app.log_result('hello')

    assert (rs_log.log_result.called)
    eq_(rs_log.log_result.call_count, 2)
    eq_(rs_log.log_result.call_args_list, [call('hello'), call('hello')])
Example #11
0
def test_log_result_no_exceptions():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    svc = mock.Mock()

    rs_log = mock.Mock(spec=AbstractResultsLogger)
    rs_log.log_result.side_effect = Exception('Test exception')

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost'],
        result_loggers=[rs_log]
    )

    app.log_result('hello')

    assert(rs_log.log_result.called)
    eq_(rs_log.log_result.call_count, 1)
    rs_log.log_result.assert_called_with('hello')
Example #12
0
def test_timer_normal():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    func = mock.Mock(return_value='meh')

    app = ProxyFlask(svc, ['localhost'], ['localhost'])

    result, elapsed = app.timer(func, 'hello', world='lol')

    eq_(result, 'meh')
    func.assert_called_with('hello', world='lol')
    assert elapsed >= 0

    exception = Exception('test')
    func = mock.Mock(side_effect=exception)
    result, elapsed = app.timer(func, 'hello', world='lol')
    eq_(result, exception)
    func.assert_called_with('hello', world='lol')
    assert elapsed >= 0
Example #13
0
def test_format_response_unknown():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    resp = mock.Mock()

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost']
    )
    elapsed_time = 1337.0

    expected = {
                'repr': repr(resp),
                'elapsed_time': elapsed_time,
                'type': 'unknown'
            }

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #14
0
def test_format_response_exception():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    resp = mock.Mock(spec=Exception)
    resp.message = "Exception Message!!!!!"

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost']
    )
    elapsed_time = 1337.0

    expected = {
                'message': resp.message,
                'repr': repr(resp),
                'elapsed_time': elapsed_time,
                'type': 'exception'
            }

    eq_(app.format_response(resp, elapsed_time=elapsed_time), expected)
Example #15
0
def test_log_result():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    svc = mock.Mock()

    class MockResultLogger(AbstractResultsLogger):
        pass

    rs_log = MockResultLogger()
    rs_log.log_result = mock.Mock()

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost'],
        result_loggers=[rs_log, rs_log]
    )

    app.log_result('hello')

    assert(rs_log.log_result.called)
    eq_(rs_log.log_result.call_count, 2)
    eq_(rs_log.log_result.call_args_list, [call('hello'), call('hello')])
Example #16
0
def test_process_greenlets():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    import gevent

    svc = mock.Mock()

    mock_result_logger = mock.Mock(spec=AbstractResultsLogger)

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost'],
        result_loggers=[mock_result_logger]
    )

    req, expected_req = _mock_request()

    resp, expected_resp, elapsed_time = _mock_response()
    resp2, expected_resp2, elapsed_time2 = _mock_response()

    ident = lambda x: x

    greenlets = [gevent.spawn(ident, x) for x in [(resp, elapsed_time), (resp2, elapsed_time2)]]

    app.process_greenlets(app.format_request(req.path, req.method, req.headers, req.args, req.form, req), greenlets)

    mock_result_logger.log_result.assert_called_with({
            'request': {
                'original': expected_req,
                'modified': expected_req
            },
            'results': [
                expected_resp,
                expected_resp2
            ]
        })
Example #17
0
def test_timer_normal():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()

    func = mock.Mock(return_value='meh')

    app = ProxyFlask(
        svc,
        ['localhost'],
        ['localhost']
    )

    result, elapsed = app.timer(func, 'hello', world='lol')

    eq_(result, 'meh')
    func.assert_called_with('hello', world='lol')
    assert elapsed >= 0

    exception = Exception('test')
    func = mock.Mock(side_effect=exception)
    result, elapsed = app.timer(func, 'hello', world='lol')
    eq_(result, exception)
    func.assert_called_with('hello', world='lol')
    assert elapsed >= 0
Example #18
0
def test_catch_all_default():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    import shadow.proxy.web
    import gevent

    shadow.proxy.web.config = {
        'safe_mode': True
    }

    svc = gevent

    req, expected_req = _mock_request()

    shadow.proxy.web.request = req

    mock_result_logger = mock.Mock(spec=AbstractResultsLogger)

    additional_headers = [('add_header', 'header_value'), ('header', 'altered_header_value')]
    additional_get = [('add_get', 'get_value')]
    additional_post = [('add_post', 'post_value')]

    additional_true_headers = [('add_true_header', 'true_header_value'), ('true_header', 'altered_true_header_value')]
    additional_true_get = [('add_true_get', 'get_true_value')]
    additional_true_post = [('add_true_post', 'post_true_value')]

    app = ProxyFlask(
        svc,
        ['old_server'],
        ['new_server'],
        new_servers_timeout=1337.0,
        old_servers_timeout=1339.0,
        old_servers_additional_headers=additional_true_headers,
        old_servers_additional_get_params=additional_true_get,
        old_servers_additional_post_params=additional_true_post,
        new_servers_additional_headers=additional_headers,
        new_servers_additional_get_params=additional_get,
        new_servers_additional_post_params=additional_post,
        result_loggers=[mock_result_logger]
    )

    with mock.patch.object(app, 'session') as requests:

        requests.request = mock.Mock()

        resp, expected_resp, elapsed_time = _mock_response()

        requests.request.return_value = resp

        path = "/"

        # mock timer to return the randomly generated time
        app.timer = lambda timed_func, *args, **kwargs: (timed_func(*args, **kwargs), elapsed_time)

        app.catch_all(path)

        requests.request.assert_has_calls([call(
            url="old_server/",
            headers=dict(expected_req['headers'].items() + additional_true_headers),
            data=dict(expected_req['post'].items() + additional_true_post),
            params=dict(expected_req['get'].items() + additional_true_get),
            timeout=1339.0,
            method=expected_req['method'],
            config=shadow.proxy.web.config
        ), call(
            url="new_server/",
            headers=dict(expected_req['headers'].items() + additional_headers),
            data=dict(expected_req['post'].items() + additional_post),
            params=dict(expected_req['get'].items() + additional_get),
            timeout=1337.0,
            method=expected_req['method'],
            config=shadow.proxy.web.config
        )], any_order=True)

        mock_result_logger.log_result.assert_called_with({
                'request': {
                    'modified': expected_req,
                    'original': {
                        'url': req.path,
                        'method': req.method,
                        'headers': dict([(unicode(k), unicode(v)) for k, v in req.headers.items()]),
                        'get': dict([(unicode(k), unicode(v)) for k, v in req.args.items()]),
                        'post': dict([(unicode(k), unicode(v)) for k, v in req.form.items()])
                    }
                },
                'results': [
                    expected_resp,
                    expected_resp
                ]
            })
Example #19
0
def test_invalid_results_loggers():
    from shadow.proxy.web import ProxyFlask
    svc = mock.Mock()
    app = ProxyFlask(svc, ['localhost'], ['localhost'],
                     result_loggers=['invalid_results_logger'])
Example #20
0
def test_catch_all_default():
    from shadow.proxy.web import ProxyFlask
    from shadow.proxy.web import AbstractResultsLogger
    import shadow.proxy.web
    import gevent

    shadow.proxy.web.config = {'safe_mode': True}

    svc = gevent

    req, expected_req = _mock_request()

    shadow.proxy.web.request = req

    mock_result_logger = mock.Mock(spec=AbstractResultsLogger)

    additional_headers = [('add_header', 'header_value'),
                          ('header', 'altered_header_value')]
    additional_get = [('add_get', 'get_value')]
    additional_post = [('add_post', 'post_value')]

    additional_true_headers = [('add_true_header', 'true_header_value'),
                               ('true_header', 'altered_true_header_value')]
    additional_true_get = [('add_true_get', 'get_true_value')]
    additional_true_post = [('add_true_post', 'post_true_value')]

    app = ProxyFlask(svc, ['old_server'], ['new_server'],
                     new_servers_timeout=1337.0,
                     old_servers_timeout=1339.0,
                     old_servers_additional_headers=additional_true_headers,
                     old_servers_additional_get_params=additional_true_get,
                     old_servers_additional_post_params=additional_true_post,
                     new_servers_additional_headers=additional_headers,
                     new_servers_additional_get_params=additional_get,
                     new_servers_additional_post_params=additional_post,
                     result_loggers=[mock_result_logger])

    with mock.patch.object(app, 'session') as requests:

        requests.request = mock.Mock()

        resp, expected_resp, elapsed_time = _mock_response()

        requests.request.return_value = resp

        path = "/"

        # mock timer to return the randomly generated time
        app.timer = lambda timed_func, *args, **kwargs: (timed_func(
            *args, **kwargs), elapsed_time)

        app.catch_all(path)

        requests.request.assert_has_calls([
            call(
                url="old_server/",
                headers=dict(expected_req['headers'].items() +
                             additional_true_headers),
                data=dict(expected_req['post'].items() + additional_true_post),
                params=dict(expected_req['get'].items() + additional_true_get),
                timeout=1339.0,
                method=expected_req['method'],
                config=shadow.proxy.web.config),
            call(url="new_server/",
                 headers=dict(expected_req['headers'].items() +
                              additional_headers),
                 data=dict(expected_req['post'].items() + additional_post),
                 params=dict(expected_req['get'].items() + additional_get),
                 timeout=1337.0,
                 method=expected_req['method'],
                 config=shadow.proxy.web.config)
        ],
                                          any_order=True)

        mock_result_logger.log_result.assert_called_with({
            'request': {
                'modified': expected_req,
                'original': {
                    'url':
                    req.path,
                    'method':
                    req.method,
                    'headers':
                    dict([(unicode(k), unicode(v))
                          for k, v in req.headers.items()]),
                    'get':
                    dict([(unicode(k), unicode(v))
                          for k, v in req.args.items()]),
                    'post':
                    dict([(unicode(k), unicode(v))
                          for k, v in req.form.items()])
                }
            },
            'results': [expected_resp, expected_resp]
        })