Ejemplo n.º 1
0
    def test_dns_resolve(self):
        old = util.gethostbyname

        def _gethost(*args):
            return '0.0.0.0'

        util.gethostbyname = _gethost

        try:
            res = dns_resolve('http://example.com')
            res2 = dns_resolve('http://example.com')  # cache
        finally:
            util.gethostbyname = old

        self.assertEqual(res, ('http://0.0.0.0:80', 'example.com', '0.0.0.0'))
        self.assertEqual(res, res2)
Ejemplo n.º 2
0
    def test_dns_resolve(self):
        old = util.gethostbyname

        def _gethost(*args):
            return '0.0.0.0'

        util.gethostbyname = _gethost

        try:
            res = dns_resolve('http://example.com')
            res2 = dns_resolve('http://example.com')   # cache
        finally:
            util.gethostbyname = old

        self.assertEqual(res, ('http://0.0.0.0:80', 'example.com', '0.0.0.0'))
        self.assertEqual(res, res2)
Ejemplo n.º 3
0
 def request(self, method, url, headers=None, **kwargs):
     if not url.startswith('https://') and self.dns_resolve:
         url, original, resolved = dns_resolve(url)
         if headers is None:
             headers = {}
         headers['Host'] = original
     return super(Session, self).request(
         method, url, headers=headers, **kwargs)
Ejemplo n.º 4
0
 def request(self, method, url, headers=None, **kwargs):
     if not url.startswith('https://'):
         url, original, resolved = dns_resolve(url)
         if headers is None:
             headers = {}
         headers['Host'] = original
     return super(Session, self).request(
         method, url, headers=headers, **kwargs)
Ejemplo n.º 5
0
 def test_dns_resolve_fallbacks_on_gethostbyname(self, hostbyname,
                                                 hostbyname_ex):
     # Older versions of gevent don't have the gethostbyname_ex method. Be
     # sure we fallback on the right version if the method doesn't exist.
     hostbyname_ex.side_effect = AttributeError()
     hostbyname.return_value = '0.0.0.0'
     self.assertEquals(dns_resolve('http://mozilla.org'),
                       ('http://0.0.0.0:80', 'mozilla.org', '0.0.0.0'))
     self.assertTrue(hostbyname.called)
Ejemplo n.º 6
0
 def test_dns_resolve_fallbacks_on_gethostbyname(self, hostbyname,
                                                 hostbyname_ex):
     # Older versions of gevent don't have the gethostbyname_ex method. Be
     # sure we fallback on the right version if the method doesn't exist.
     hostbyname_ex.side_effect = AttributeError()
     hostbyname.return_value = '0.0.0.0'
     self.assertEquals(dns_resolve(
         'http://mozilla.org'),
         ('http://0.0.0.0:80', 'mozilla.org', '0.0.0.0'))
     self.assertTrue(hostbyname.called)
Ejemplo n.º 7
0
    def test_dns_resolve(self):
        with mock.patch('loads.util.gevent_socket.gethostbyname_ex') as mocked:
            mocked.return_value = ('example.com', ['example.com'],
                                   ['0.0.0.0', '1.1.1.1'])

            # Initial query should populate the cache and return
            # randomly-selected resolved address.
            url, original, resolved = dns_resolve('http://example.com')
            self.assertEqual(original, 'example.com')
            self.assertEqual(url, 'http://' + resolved + ':80')
            self.assertTrue(resolved in ("0.0.0.0", "1.1.1.1"))
            self.assertEqual(mocked.call_count, 1)
            # Subsequent queries should be fulfilled from the cache
            # and should balance between all resolved addresses.
            addrs = set()
            for _ in xrange(10):
                addrs.add(dns_resolve('http://example.com')[2])
            self.assertEqual(addrs, set(('0.0.0.0', '1.1.1.1')))
            self.assertEqual(mocked.call_count, 1)
Ejemplo n.º 8
0
    def test_dns_resolve(self):
        with mock.patch('loads.util.gevent_socket.gethostbyname_ex') as mocked:
            mocked.return_value = ('example.com', ['example.com'],
                                   ['0.0.0.0', '1.1.1.1'])

            # Initial query should populate the cache and return
            # randomly-selected resolved address.
            url, original, resolved = dns_resolve('http://example.com')
            self.assertEqual(original, 'example.com')
            self.assertEqual(url, 'http://' + resolved + ':80')
            self.assertTrue(resolved in ("0.0.0.0", "1.1.1.1"))
            self.assertEqual(mocked.call_count, 1)
            # Subsequent queries should be fulfilled from the cache
            # and should balance between all resolved addresses.
            addrs = set()
            for _ in xrange(10):
                addrs.add(dns_resolve('http://example.com')[2])
            self.assertEqual(addrs, set(('0.0.0.0', '1.1.1.1')))
            self.assertEqual(mocked.call_count, 1)
Ejemplo n.º 9
0
    def send(self, request, **kwargs):
        """Do the actual request from within the session, doing some
        measures at the same time about the request (duration, status, etc).
        """
        request.url, original, resolved = dns_resolve(request.url)
        request.headers['Host'] = original

        # attach some information to the request object for later use.
        start = datetime.datetime.utcnow()
        res = _Session.send(self, request, **kwargs)
        res.started = start
        res.method = request.method
        self._analyse_request(res)
        return res
Ejemplo n.º 10
0
    def send(self, request, **kwargs):
        """Do the actual request from within the session, doing some
        measures at the same time about the request (duration, status, etc).
        """
        if not request.url.startswith('https://'):
            request.url, original, resolved = dns_resolve(request.url)
            request.headers['Host'] = original

        # attach some information to the request object for later use.
        start = datetime.datetime.utcnow()
        res = _Session.send(self, request, **kwargs)
        res.started = start
        res.method = request.method
        self._analyse_request(res)
        return res