Ejemplo n.º 1
0
    def test_headers(self):
        http = PoolManager(headers={'Foo': 'bar'})

        r = yield from http.request_encode_url('GET',
                                               '%s/headers' % self.base_url)
        returned_headers = json.loads((yield from r.data).decode())
        self.assertEqual(returned_headers.get('Foo'), 'bar')

        r = yield from http.request_encode_body('POST',
                                                '%s/headers' % self.base_url)
        returned_headers = json.loads((yield from r.data).decode())
        self.assertEqual(returned_headers.get('Foo'), 'bar')

        r = yield from http.request_encode_url('GET',
                                               '%s/headers' % self.base_url,
                                               headers={'Baz': 'quux'})
        returned_headers = json.loads((yield from r.data).decode())
        self.assertEqual(returned_headers.get('Foo'), None)
        self.assertEqual(returned_headers.get('Baz'), 'quux')

        r = yield from http.request_encode_body('GET',
                                                '%s/headers' % self.base_url,
                                                headers={'Baz': 'quux'})
        returned_headers = json.loads((yield from r.data).decode())
        self.assertEqual(returned_headers.get('Foo'), None)
        self.assertEqual(returned_headers.get('Baz'), 'quux')
Ejemplo n.º 2
0
    def test_cross_host_redirect(self):
        http = PoolManager()

        cross_host_location = '%s/echo?a=b' % self.base_url_alt
        try:
            yield from http.request('GET',
                                    '%s/redirect' % self.base_url,
                                    fields={'target': cross_host_location},
                                    timeout=0.1,
                                    retries=0)
            self.fail(
                "Request succeeded instead of raising an exception like it should."
            )

        except MaxRetryError:
            pass

        r = yield from http.request(
            'GET',
            '%s/redirect' % self.base_url,
            fields={'target': '%s/echo?a=b' % self.base_url_alt},
            timeout=0.1,
            retries=1)

        self.assertEqual(r._pool.host, self.host_alt)
Ejemplo n.º 3
0
    def init_poolmanager(self,
                         connections,
                         maxsize,
                         block=DEFAULT_POOLBLOCK,
                         **pool_kwargs):
        """Initializes a urllib3 PoolManager.

        This method should not be called from user code, and is only
        exposed for use when subclassing the
        :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.

        :param connections: The number of urllib3 connection pools to cache.
        :param maxsize: The maximum number of connections to save in the pool.
        :param block: Block when no free connections are available.
        :param pool_kwargs: Extra keyword arguments used to initialize the Pool Manager.
        """
        # save these values for pickling
        self._pool_connections = connections
        self._pool_maxsize = maxsize
        self._pool_block = block

        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       **pool_kwargs)
Ejemplo n.º 4
0
    def test_same_url(self):
        # Convince ourselves that normally we don't get the same object
        conn1 = connection_from_url('http://localhost:8081/foo')
        conn2 = connection_from_url('http://localhost:8081/bar')

        self.assertNotEqual(conn1, conn2)

        # Now try again using the PoolManager
        p = PoolManager(1)

        conn1 = p.connection_from_url('http://localhost:8081/foo')
        conn2 = p.connection_from_url('http://localhost:8081/bar')

        self.assertEqual(conn1, conn2)
Ejemplo n.º 5
0
    def test_redirect_to_relative_url(self):
        http = PoolManager()

        r = yield from http.request('GET',
                                    '%s/redirect' % self.base_url,
                                    fields={'target': '/redirect'},
                                    redirect=False)

        self.assertEqual(r.status, 303)

        r = yield from http.request('GET',
                                    '%s/redirect' % self.base_url,
                                    fields={'target': '/redirect'})

        self.assertEqual(r.status, 200)
        self.assertEqual((yield from r.data), b'Dummy server!')
Ejemplo n.º 6
0
    def test_manager_clear(self):

        p = PoolManager(5)

        conn_pool = p.connection_from_url('http://google.com')
        self.assertEqual(len(p.pools), 1)

        conn = conn_pool._get_conn()

        p.clear()
        self.assertEqual(len(p.pools), 0)

        self.aioAssertRaises(ClosedPoolError, conn_pool._get_conn)

        conn_pool._put_conn(conn)

        self.aioAssertRaises(ClosedPoolError, conn_pool._get_conn)

        self.assertEqual(len(p.pools), 0)
Ejemplo n.º 7
0
    def test_missing_port(self):
        # Can a URL that lacks an explicit port like ':80' succeed, or
        # will all such URLs fail with an error?

        http = PoolManager()

        # By globally adjusting `port_by_scheme` we pretend for a moment
        # that HTTP's default port is not 80, but is the port at which
        # our test server happens to be listening.
        port_by_scheme['http'] = self.port
        try:
            r = yield from http.request('GET',
                                        'http://%s/' % self.host,
                                        retries=0)
        finally:
            port_by_scheme['http'] = 80

        self.assertEqual(r.status, 200)
        self.assertEqual((yield from r.data), b'Dummy server!')
Ejemplo n.º 8
0
    def test_many_urls(self):
        urls = [
            "http://localhost:8081/foo",
            "http://www.google.com/mail",
            "http://localhost:8081/bar",
            "https://www.google.com/",
            "https://www.google.com/mail",
            "http://yahoo.com",
            "http://bing.com",
            "http://yahoo.com/",
        ]

        connections = set()

        p = PoolManager(10)

        for url in urls:
            conn = p.connection_from_url(url)
            connections.add(conn)

        self.assertEqual(len(connections), 5)
Ejemplo n.º 9
0
 def test_nohost(self):
     p = PoolManager(5)
     self.assertRaises(LocationValueError, p.connection_from_url,
                       'http://@')
     self.assertRaises(LocationValueError, p.connection_from_url, None)
Ejemplo n.º 10
0
 def test_ipv6(self):
     http = PoolManager()
     yield from http.request('GET', self.base_url)
Ejemplo n.º 11
0
    def test_http_with_ssl_keywords(self):
        http = PoolManager(ca_certs='REQUIRED')

        r = yield from http.request('GET',
                                    'http://%s:%s/' % (self.host, self.port))
        self.assertEqual(r.status, 200)