Beispiel #1
0
def test_proxy_client_factory_prepare_mangle_req(mocker, freeze):

    freeze.freeze(datetime.datetime(2015, 1, 1, 3, 30, 15, 50))

    req = http.Request('GET / HTTP/1.1\r\n\r\n')
    mangreq = http.Request('BOOO / HTTP/1.1\r\n\r\n')

    def inc_day_mangle(x, y):
        freeze.delta(days=1)
        return mock_deferred((mangreq, True))

    rsave = mocker.patch.object(pappyproxy.http.Request, 'async_deep_save', autospec=True, side_effect=mock_req_async_save)
    mocker.patch('pappyproxy.context.in_scope').return_value = True
    mocker.patch('pappyproxy.macros.mangle_request', side_effect=inc_day_mangle)

    cf = ProxyClientFactory(req,
                            save_all=True,
                            stream_response=False,
                            return_transport=None)
    yield cf.prepare_request()

    assert cf.request == mangreq
    assert req.time_start == datetime.datetime(2015, 1, 1, 3, 30, 15, 50)
    assert cf.request.time_start == datetime.datetime(2015, 1, 2, 3, 30, 15, 50)
    assert cf.request.reqid is not None
    assert len(rsave.mock_calls) == 2
Beispiel #2
0
 def gen_connection(send_data):
     factory = ProxyClientFactory(http.Request(send_data))
     protocol = factory.buildProtocol(None)
     tr = FakeTransport(protocol, True)
     protocol.makeConnection(tr)
     sent = yield protocol.data_defer
     defer.returnValue((protocol, sent, factory.data_defer))
Beispiel #3
0
def test_proxy_client_factory_prepare_mangle_req(mocker, freeze):

    freeze.freeze(datetime.datetime(2015, 1, 1, 3, 30, 15, 50))

    req = http.Request('GET / HTTP/1.1\r\n\r\n')
    mangreq = http.Request('BOOO / HTTP/1.1\r\n\r\n')

    def inc_day_mangle(x, y):
        freeze.delta(days=1)
        return mock_deferred((mangreq, True))

    rsave = mocker.patch.object(pappyproxy.http.Request,
                                'async_deep_save',
                                autospec=True,
                                side_effect=mock_req_async_save)
    mocker.patch('pappyproxy.context.in_scope').return_value = True
    mocker.patch('pappyproxy.macros.mangle_request',
                 side_effect=inc_day_mangle)

    cf = ProxyClientFactory(req,
                            save_all=True,
                            stream_response=False,
                            return_transport=None)
    yield cf.prepare_request()

    assert cf.request == mangreq
    assert req.time_start == datetime.datetime(2015, 1, 1, 3, 30, 15, 50)
    assert cf.request.time_start == datetime.datetime(2015, 1, 2, 3, 30, 15,
                                                      50)
    assert cf.request.reqid is not None
    assert len(rsave.mock_calls) == 2
Beispiel #4
0
def test_proxy_client_factory_prepare_reqs_simple(mocker, freeze):
    import datetime
    freeze.freeze(datetime.datetime(2015, 1, 1, 3, 30, 15, 50))

    req = http.Request('GET / HTTP/1.1\r\n\r\n')

    rsave = mocker.patch.object(pappyproxy.http.Request,
                                'async_deep_save',
                                autospec=True,
                                side_effect=mock_req_async_save)
    rsave.return_value = mock_deferred()
    mocker.patch('pappyproxy.context.in_scope').return_value = True
    mocker.patch(
        'pappyproxy.macros.mangle_request').return_value = mock_deferred(
            (req, False))

    cf = ProxyClientFactory(req,
                            save_all=False,
                            stream_response=False,
                            return_transport=None)
    yield cf.prepare_request()
    assert req.time_start == datetime.datetime(2015, 1, 1, 3, 30, 15, 50)
    assert req.reqid is None
    assert not rsave.called
    assert len(rsave.mock_calls) == 0
Beispiel #5
0
def gen_client_protocol(req, stream_response=False):
    return_transport = TLSStringTransport()
    factory = ProxyClientFactory(req,
                                 save_all=True,
                                 stream_response=stream_response,
                                 return_transport=return_transport)
    protocol = factory.buildProtocol(('127.0.0.1', 0), _do_callback=False)
    tr = TLSStringTransport()
    protocol.makeConnection(tr)
    return protocol
Beispiel #6
0
def gen_client_protocol(req, stream_response=False):
    return_transport = TLSStringTransport()
    factory = ProxyClientFactory(req,
                                 save_all=True,
                                 stream_response=stream_response,
                                 return_transport=return_transport)
    protocol = factory.buildProtocol(('127.0.0.1', 0), _do_callback=False)
    tr = TLSStringTransport()
    protocol.makeConnection(tr)
    return protocol
Beispiel #7
0
    def gen_connection(send_data, new_req=False, new_rsp=False,
                       drop_req=False, drop_rsp=False):
        factory = ProxyClientFactory(http.Request(send_data))

        macro = gen_mangle_macro(new_req, new_rsp, drop_req, drop_rsp)
        factory.intercepting_macros['pappy_mangle'] = macro

        protocol = factory.buildProtocol(None)
        tr = FakeTransport(protocol, True)
        protocol.makeConnection(tr)
        sent = yield protocol.data_defer
        print sent
        defer.returnValue((protocol, sent, factory.data_defer))
Beispiel #8
0
    def gen_connection(send_data,
                       new_req=False,
                       new_rsp=False,
                       drop_req=False,
                       drop_rsp=False):
        factory = ProxyClientFactory(http.Request(send_data))

        macro = gen_mangle_macro(new_req, new_rsp, drop_req, drop_rsp)
        factory.intercepting_macros['pappy_mangle'] = macro

        protocol = factory.buildProtocol(None)
        tr = FakeTransport(protocol, True)
        protocol.makeConnection(tr)
        sent = yield protocol.data_defer
        print sent
        defer.returnValue((protocol, sent, factory.data_defer))
Beispiel #9
0
def test_proxy_client_factory_prepare_reqs_360_noscope_save(mocker, freeze):
    freeze.freeze(datetime.datetime(2015, 1, 1, 3, 30, 15, 50))

    req = http.Request('GET / HTTP/1.1\r\n\r\n')
    mangreq = http.Request('BOOO / HTTP/1.1\r\n\r\n')

    rsave = mocker.patch.object(pappyproxy.http.Request, 'async_deep_save', autospec=True, side_effect=mock_req_async_save)
    mocker.patch('pappyproxy.context.in_scope').return_value = False
    mocker.patch('pappyproxy.macros.mangle_request', side_effect=func_deleted)

    cf = ProxyClientFactory(req,
                            save_all=True,
                            stream_response=False,
                            return_transport=None)
    yield cf.prepare_request()
    assert req.time_start == None
    assert req.reqid is None
    assert not rsave.called
    assert len(rsave.mock_calls) == 0
Beispiel #10
0
def test_proxy_client_factory_prepare_reqs_360_noscope(mocker, freeze):
    import datetime
    freeze.freeze(datetime.datetime(2015, 1, 1, 3, 30, 15, 50))

    req = http.Request('GET / HTTP/1.1\r\n\r\n')

    rsave = mocker.patch('pappyproxy.http.Request.async_deep_save')
    rsave.return_value = mock_deferred()
    mocker.patch('pappyproxy.context.in_scope').return_value = False
    mocker.patch('pappyproxy.macros.mangle_request', new=func_deleted)

    cf = ProxyClientFactory(req,
                            save_all=True,
                            stream_response=False,
                            return_transport=None)
    yield cf.prepare_request()
    assert req.time_start == None
    assert req.reqid is None
    assert not rsave.called
    assert len(rsave.mock_calls) == 0