Exemplo n.º 1
0
    def test_pool_timeouts(self):
        with HTTPConnectionPool(host="localhost") as pool:
            conn = pool._new_conn()
            assert conn.__class__ == HTTP1Connection
            assert pool.timeout.__class__ == Timeout
            assert pool.timeout._read == Timeout.DEFAULT_TIMEOUT
            assert pool.timeout._connect == Timeout.DEFAULT_TIMEOUT
            assert pool.timeout.total is None

            pool = HTTPConnectionPool(host="localhost", timeout=SHORT_TIMEOUT)
            assert pool.timeout._read == SHORT_TIMEOUT
            assert pool.timeout._connect == SHORT_TIMEOUT
            assert pool.timeout.total is None
Exemplo n.º 2
0
    def test_pool_size(self):
        POOL_SIZE = 1
        with HTTPConnectionPool(host="localhost",
                                maxsize=POOL_SIZE,
                                block=True) as pool:

            def _raise(ex, *args):
                raise ex(*args)

            def _test(exception, expect, reason=None):
                pool._make_request = lambda *args, **kwargs: _raise(exception)
                with pytest.raises(expect) as excinfo:
                    pool.request("GET", "/")
                if reason is not None:
                    assert isinstance(excinfo.value.reason, reason)
                assert pool.pool.qsize() == POOL_SIZE

            # Make sure that all of the exceptions return the connection
            # to the pool
            _test(Empty, EmptyPoolError)
            _test(BaseSSLError, MaxRetryError, SSLError)
            _test(CertificateError, MaxRetryError, SSLError)

            # The pool should never be empty, and with these two exceptions
            # being raised, a retry will be triggered, but that retry will
            # fail, eventually raising MaxRetryError, not EmptyPoolError
            # See: https://github.com/urllib3/urllib3/issues/76
            pool._make_request = lambda *args, **kwargs: _raise(
                h11.RemoteProtocolError, "")
            with pytest.raises(MaxRetryError):
                pool.request("GET", "/", retries=1, pool_timeout=SHORT_TIMEOUT)
            assert pool.pool.qsize() == POOL_SIZE
Exemplo n.º 3
0
    def test_retry_exception_str(self):
        assert (str(
            MaxRetryError(
                HTTPConnectionPool(host="localhost"), "Test.",
                None)) == "HTTPConnectionPool(host='localhost', port=None): "
                "Max retries exceeded with url: Test. (Caused by None)")

        err = SocketError("Test")

        # using err.__class__ here, as socket.error is an alias for OSError
        # since Py3.3 and gets printed as this
        assert (str(
            MaxRetryError(
                HTTPConnectionPool(host="localhost"), "Test.",
                err)) == "HTTPConnectionPool(host='localhost', port=None): "
                "Max retries exceeded with url: Test. "
                "(Caused by %r)" % err)
Exemplo n.º 4
0
class TestPickle(object):
    @pytest.mark.parametrize(
        "exception",
        [
            HTTPError(None),
            MaxRetryError(None, None, None),
            LocationParseError(None),
            ConnectTimeoutError(None),
            HTTPError("foo"),
            HTTPError("foo", IOError("foo")),
            MaxRetryError(HTTPConnectionPool("localhost"), "/", None),
            LocationParseError("fake location"),
            ClosedPoolError(HTTPConnectionPool("localhost"), None),
            EmptyPoolError(HTTPConnectionPool("localhost"), None),
            ReadTimeoutError(HTTPConnectionPool("localhost"), "/", None),
        ],
    )
    def test_exceptions(self, exception):
        result = pickle.loads(pickle.dumps(exception))
        assert isinstance(result, type(exception))
Exemplo n.º 5
0
    def test_max_connections(self):
        with HTTPConnectionPool(host="localhost", maxsize=1,
                                block=True) as pool:
            pool._get_conn(timeout=SHORT_TIMEOUT)

            with pytest.raises(EmptyPoolError):
                pool._get_conn(timeout=SHORT_TIMEOUT)

            with pytest.raises(EmptyPoolError):
                pool.request("GET", "/", pool_timeout=SHORT_TIMEOUT)

            assert pool.num_connections == 1
Exemplo n.º 6
0
    def test_pool_edgecases(self):
        with HTTPConnectionPool(host="localhost", maxsize=1,
                                block=False) as pool:
            conn1 = pool._get_conn()
            conn2 = pool._get_conn()  # New because block=False

            pool._put_conn(conn1)
            pool._put_conn(conn2)  # Should be discarded

            assert conn1 == pool._get_conn()
            assert conn2 != pool._get_conn()

            assert pool.num_connections == 3
Exemplo n.º 7
0
        def _test(exception, *args):
            with HTTPConnectionPool(host="localhost", maxsize=1,
                                    block=True) as pool:
                # Verify that the request succeeds after two attempts, and that the
                # connection is left on the response object, instead of being
                # released back into the pool.
                pool._make_request = _raise_once_make_request_function(
                    exception, *args)
                response = pool.urlopen("GET",
                                        "/",
                                        retries=1,
                                        preload_content=False)
                assert pool.pool.qsize() == 0
                assert pool.num_connections == 2
                assert response.connection is not None

                response.release_conn()
                assert pool.pool.qsize() == 1
                assert response.connection is None
Exemplo n.º 8
0
 def test_no_host(self):
     with pytest.raises(LocationValueError):
         HTTPConnectionPool(None)
Exemplo n.º 9
0
 def test_exception_str(self):
     assert (str(
         EmptyPoolError(HTTPConnectionPool(host="localhost"), "Test.")) ==
             "HTTPConnectionPool(host='localhost', port=None): Test.")
Exemplo n.º 10
0
    def test_not_same_host_no_port_http(self, a, b):
        with HTTPConnectionPool(a) as c:
            assert not c.is_same_host(b)

        with HTTPConnectionPool(b) as c:
            assert not c.is_same_host(a)
Exemplo n.º 11
0
 def test_same_host_no_port_http(self, a, b):
     # This test was introduced in urllib3/urllib3#801 to deal with the fact that Hip
     # never initializes ConnectionPool objects with port=None.
     with HTTPConnectionPool(a) as c:
         assert c.is_same_host(b)