[tests] adapt events tests to recent changes
[leap_pycommon.git] / src / leap / common / tests / test_http.py
1 # -*- coding: utf-8 -*-
2 # test_http.py
3 # Copyright (C) 2013 LEAP
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 """
18 Tests for:
19     * leap/common/http.py
20 """
21 import os
22 try:
23     import unittest2 as unittest
24 except ImportError:
25     import unittest
26
27 from leap.common import http
28 from leap.common.testing.basetest import BaseLeapTest
29
30 TEST_CERT_PEM = os.path.join(
31     os.path.split(__file__)[0],
32     '..', 'testing', "leaptest_combined_keycert.pem")
33
34
35 class HTTPClientTest(BaseLeapTest):
36
37     def setUp(self):
38         pass
39
40     def tearDown(self):
41         pass
42
43     def test_agents_sharing_pool_by_default(self):
44         client = http.HTTPClient()
45         client2 = http.HTTPClient(TEST_CERT_PEM)
46         self.assertNotEquals(
47             client._agent, client2._agent, "Expected dedicated agents")
48         self.assertEquals(
49             client._agent._pool, client2._agent._pool,
50             "Pool was not reused by default")
51
52     def test_agent_can_have_dedicated_custom_pool(self):
53         custom_pool = http._HTTPConnectionPool(
54             None,
55             timeout=10,
56             maxPersistentPerHost=42,
57             persistent=False
58         )
59         self.assertEquals(custom_pool.maxPersistentPerHost, 42,
60                           "Custom persistent connections "
61                           "limit is not being respected")
62         self.assertFalse(custom_pool.persistent,
63                          "Custom persistence is not being respected")
64         default_client = http.HTTPClient()
65         custom_client = http.HTTPClient(pool=custom_pool)
66
67         self.assertNotEquals(
68             default_client._agent, custom_client._agent,
69             "No agent reuse is expected")
70         self.assertEquals(
71             custom_pool, custom_client._agent._pool,
72             "Custom pool usage was not respected")
73
74 if __name__ == "__main__":
75     unittest.main()