[tests] implemented http feature according to test
[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(client._agent, client2._agent, "Expected dedicated agents")
47         self.assertEquals(client._agent._pool, client2._agent._pool, "Pool was not reused by default")
48
49     def test_agent_can_have_dedicated_custom_pool(self):
50         custom_pool = http._HTTPConnectionPool(
51             None,
52             timeout=10,
53             maxPersistentPerHost=42,
54             persistent=False
55         )
56         self.assertEquals(custom_pool.maxPersistentPerHost, 42,
57                           "Custom persistent connections limit is not being respected")
58         self.assertFalse(custom_pool.persistent,
59                          "Custom persistence is not being respected")
60         default_client = http.HTTPClient()
61         custom_client = http.HTTPClient(pool=custom_pool)
62
63         self.assertNotEquals(default_client._agent, custom_client._agent, "No agent reuse is expected")
64         self.assertEquals(custom_pool, custom_client._agent._pool, "Custom pool usage was not respected")
65
66 if __name__ == "__main__":
67     unittest.main()