import unittest

from urllib3.poolmanager import PoolManager
from urllib3 import connection_from_url
from urllib3.exceptions import (
    ClosedPoolError,
    LocationValueError,
)


class TestPoolManager(unittest.TestCase):
    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)

    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)

    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.assertRaises(ClosedPoolError, conn_pool._get_conn)

        conn_pool._put_conn(conn)

        self.assertRaises(ClosedPoolError, conn_pool._get_conn)

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


    def test_nohost(self):
        p = PoolManager(5)
        self.assertRaises(LocationValueError, p.connection_from_url, 'http://@')
        self.assertRaises(LocationValueError, p.connection_from_url, None)

    def test_contextmanager(self):
        with PoolManager(1) as p:
            conn_pool = p.connection_from_url('http://google.com')
            self.assertEqual(len(p.pools), 1)
            conn = conn_pool._get_conn()

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

        self.assertRaises(ClosedPoolError, conn_pool._get_conn)

        conn_pool._put_conn(conn)

        self.assertRaises(ClosedPoolError, conn_pool._get_conn)

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


if __name__ == '__main__':
    unittest.main()

@@ -53,16 +53,30 @@
 lManager
+, SSL_KEYWORDS
 %0Afrom ur
@@ -2489,16 +2489,1779 @@
 s), 0)%0A%0A
+    def test_pools_keyed_on_ssl_arguments_from_host(self):%0A        ssl_kw = %7B%0A            'key_file': '/root/totally_legit.key',%0A            'cert_file': '/root/totally_legit.crt',%0A            'cert_reqs': 'CERT_REQUIRED',%0A            'ca_certs': '/root/path_to_pem',%0A            'ssl_version': 'SSLv23_METHOD',%0A        %7D%0A        p = PoolManager(5)%0A        conns = %5B%5D%0A        conns.append(%0A            p.connection_from_host(%0A                'example.com', 443, scheme='https', ssl_kwargs=ssl_kw%0A            )%0A        )%0A%0A        for k in ssl_kw:%0A            ssl_kw%5Bk%5D = 'newval'%0A            conns.append(%0A                p.connection_from_host(%0A                    'example.com', 443, scheme='https', ssl_kwargs=ssl_kw%0A                )%0A            )%0A%0A        self.assertTrue(%0A            all(%0A                x is not y%0A                for i, x in enumerate(conns)%0A                for j, y in enumerate(conns)%0A                if i != j%0A            )%0A        )%0A%0A    def test_pools_keyed_on_ssl_arguments_from_url(self):%0A        ssl_kw = %7B%0A            'key_file': '/root/totally_legit.key',%0A            'cert_file': '/root/totally_legit.crt',%0A            'cert_reqs': 'CERT_REQUIRED',%0A            'ca_certs': '/root/path_to_pem',%0A            'ssl_version': 'SSLv23_METHOD',%0A        %7D%0A        p = PoolManager(5, **ssl_kw)%0A        conns = %5B%5D%0A        conns.append(p.connection_from_url('https://example.com/'))%0A%0A        for k in ssl_kw:%0A            p.connection_pool_kw%5Bk%5D = 'newval'%0A            conns.append(p.connection_from_url('https://example.com/'))%0A%0A        self.assertTrue(%0A            all(%0A                x is not y%0A                for i, x in enumerate(conns)%0A                for j, y in enumerate(conns)%0A                if i != j%0A            )%0A        )%0A%0A
 %0Aif __na

Test that we key connection pools off SSL arguments.