Exemple #1
0
    def _testProcess(self, uri, expectedURI, method="GET", data=""):
        """
        Build a request pointing at C{uri}, and check that a proxied request
        is created, pointing a C{expectedURI}.
        """
        transport = StringTransportWithDisconnection()
        channel = DummyChannel(transport)
        reactor = FakeReactor()
        proxy_factory = proxy.TurtleHTTPFactory({}, False, reactor)
        channel.factory = proxy_factory
        request = proxy.TurtleProxyRequest(channel, False, reactor)
        request.gotLength(len(data))
        request.handleContentChunk(data)
        request.requestReceived(method, 'http://example.com%s' % (uri,),
                                'HTTP/1.0')

        self.assertEquals(len(reactor.connect), 1)
        self.assertEquals(reactor.connect[0][0], "example.com")
        self.assertEquals(reactor.connect[0][1], 80)

        factory = reactor.connect[0][2]
        self.assertIsInstance(factory, proxy.TurtleProxyClientFactory)
        self.assertEquals(factory.command, method)
        self.assertEquals(factory.version, 'HTTP/1.0')
        self.assertEquals(factory.headers, {'host': 'example.com'})
        self.assertEquals(factory.data, data)
        self.assertEquals(factory.rest, expectedURI)
        self.assertEquals(factory.father, request)
Exemple #2
0
    def test_processWithPort(self):
        """
        Check that L{TurtleProxyRequest.process} correctly parse port in the incoming
        URL, and create a outgoing connection with this port.
        """
        transport = StringTransportWithDisconnection()
        channel = DummyChannel(transport)
        reactor = FakeReactor()
        proxy_factory = proxy.TurtleHTTPFactory({}, False, reactor)
        channel.factory = proxy_factory

        request = proxy.TurtleProxyRequest(channel, False, reactor)
        request.gotLength(0)
        request.requestReceived('GET', 'http://example.com:1234/foo/bar',
                                'HTTP/1.0')

        # That should create one connection, with the port parsed from the URL
        self.assertEquals(len(reactor.connect), 1)
        self.assertEquals(reactor.connect[0][0], "example.com")
        self.assertEquals(reactor.connect[0][1], 1234)
Exemple #3
0
    def test_filtering(self):
        """
        Check that L{TurtleProxyRequest.process} filters urls that
        it doesn't know.
        """
        class FakeThrottler(object):
            called_run = False
            called_run_asap = False
            def run(self, fun, *args, **kwargs):
                self.called_run = True
                return defer.maybeDeferred(fun, *args, **kwargs)

            def runasap(self, fun, *args, **kwargs):
                self.called_run_asap = True
                return defer.maybeDeferred(fun, *args, **kwargs)


        transport = StringTransportWithDisconnection()
        transport.protocol = LineSendingProtocol([], False)
        channel = DummyChannel(transport)
        reactor = FakeReactor()

        throttler = FakeThrottler()
        urlmap = {'delicious.com': throttler}
        proxy_factory = proxy.TurtleHTTPFactory(urlmap, True, reactor)
        channel.factory = proxy_factory

        request = proxy.TurtleProxyRequest(channel, False, reactor)
        request.gotLength(0)
        request.requestReceived('GET', 'http://example.com:1234/foo/bar',
                                'HTTP/1.0')

        # That should create one connection, with the port parsed from the URL
        self.assertEquals(len(reactor.connect), 0)


        request = proxy.TurtleProxyRequest(channel, False, reactor)
        request.gotLength(0)
        request.requestReceived('GET', 'http://delicious.com:1234/foo/bar',
                                'HTTP/1.0')

        # That should create one connection, with the port parsed from the URL
        self.assertEquals(len(reactor.connect), 1)
        self.assertEquals(reactor.connect[-1][0], "delicious.com")
        self.assertEquals(reactor.connect[-1][1], 1234)
        self.assertEquals(throttler.called_run, True)
        self.assertEquals(throttler.called_run_asap, False)




        request = proxy.TurtleProxyRequest(channel, False, reactor)
        request.gotLength(0)
        request.requestHeaders.addRawHeader('x-priority', 'interactive')
        request.requestReceived('GET', 'http://delicious.com:1234/foo/bar',
                                'HTTP/1.0')

        # That should create one connection, with the port parsed from the URL
        self.assertEquals(len(reactor.connect), 2)
        self.assertEquals(reactor.connect[-1][0], "delicious.com")
        self.assertEquals(reactor.connect[-1][1], 1234)
        self.assertEquals(throttler.called_run_asap, True)