コード例 #1
0
class HttpProxyTest(unittest.TestCase):
    download_handler_cls = HttpDownloadHandler

    def setUp(self):
        site = server.Site(UriResource(), timeout=None)
        wrapper = WrappingFactory(site)
        self.port = reactor.listenTCP(0, wrapper, interface='127.0.0.1')
        self.portno = self.port.getHost().port
        self.download_handler = HttpDownloadHandler(
            Settings({'CONCURRENT_REQUESTS_PER_DOMAIN': 8}))
        self.download_request = self.download_handler.download_request

    @defer.inlineCallbacks
    def tearDown(self):
        yield self.port.stopListening()
        if hasattr(self.download_handler, 'close'):
            yield self.download_handler.close()

    def getURL(self, path):
        return 'http://127.0.0.1:%d/%s' % (self.portno, path)

    def test_download_with_proxy(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, 'http://example.com/')

        http_proxy = self.getURL('')
        request = Request('http://example.com', proxy=http_proxy)
        return self.download_request(request).addCallback(_test)

    def test_download_with_proxy_https_noconnect(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, 'https://example.com/')

        http_proxy = '%s?noconnect' % self.getURL('')
        request = Request('https://example.com', proxy=http_proxy)
        return self.download_request(request).addCallback(_test)

    def test_download_without_proxy(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, '/path/to/resource')

        request = Request(self.getURL('path/to/resource'))
        return self.download_request(request).addCallback(_test)
コード例 #2
0
class HttpProxyTest(unittest.TestCase):
    download_handler_cls = HttpDownloadHandler

    def setUp(self):
        site = server.Site(UriResource(), timeout=None)
        wrapper = WrappingFactory(site)
        self.port = reactor.listenTCP(0, wrapper, interface='127.0.0.1')
        self.portno = self.port.getHost().port
        self.download_handler = HttpDownloadHandler(Settings({'CONCURRENT_REQUESTS_PER_DOMAIN': 8}))
        self.download_request = self.download_handler.download_request

    @defer.inlineCallbacks
    def tearDown(self):
        yield self.port.stopListening()
        if hasattr(self.download_handler, 'close'):
            yield self.download_handler.close()

    def getURL(self, path):
        return 'http://127.0.0.1:%d/%s' % (self.portno, path)

    def test_download_with_proxy(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, 'http://example.com/')

        http_proxy = self.getURL('')
        request = Request('http://example.com', proxy=http_proxy)
        return self.download_request(request).addCallback(_test)

    def test_download_with_proxy_https_noconnect(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, 'https://example.com/')

        http_proxy = '%s?noconnect' % self.getURL('')
        request = Request('https://example.com', proxy=http_proxy)
        return self.download_request(request).addCallback(_test)

    def test_download_without_proxy(self):
        def _test(response):
            self.assertEquals(response.status, 200)
            self.assertEquals(response.url, request.url)
            self.assertEquals(response.body, '/path/to/resource')

        request = Request(self.getURL('path/to/resource'))
        return self.download_request(request).addCallback(_test)