Skip to content

Commit 3f93e2d

Browse files
authored
Auto close fetcher session (#64)
1 parent e256d2f commit 3f93e2d

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# UNRELEASED
2+
3+
- [FIXED] ComplianceFetcher session object is auto-closed now in tearDownClass.
4+
15
# 1.2.7
26

37
- [CHANGED] Removed PyYAML dependency to resolve downstream dependency issues.

compliance/fetch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
class ComplianceFetcher(unittest.TestCase):
2929
"""Compliance fetcher automation TestCase class."""
3030

31-
_multiprocess_can_split_ = True
31+
@classmethod
32+
def tearDownClass(cls):
33+
"""Perform clean up."""
34+
if hasattr(cls, '_session'):
35+
cls._session.close()
3236

3337
@classmethod
3438
def session(cls, url=None, creds=None, **headers):

test/t_compliance/t_fetch/test_base_fetcher.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
from compliance.config import ComplianceConfig
2020
from compliance.fetch import ComplianceFetcher
21+
from compliance.utils.http import BaseSession
22+
23+
import requests
2124

2225

2326
class ComplianceFetchTest(unittest.TestCase):
@@ -30,9 +33,31 @@ def setUp(self):
3033
# unittest.TestCase, we must pass a method in the
3134
# constructor (otherwise, we will get a ValueError). Since we
3235
# don't need this method, passing ``__doc__`` is enough for
33-
# building a ComplianceCheck object successfully.
34-
self.check = ComplianceFetcher('__doc__')
36+
# building a ComplianceFetcher object successfully.
37+
ComplianceFetcher.config = ComplianceConfig()
38+
self.fetcher = ComplianceFetcher('__doc__')
39+
self.fetcher.config.load()
3540

3641
def test_config(self):
3742
"""Check that the config property returns a ComplianceConfig object."""
38-
self.assertIsInstance(self.check.config, ComplianceConfig)
43+
self.assertIsInstance(self.fetcher.config, ComplianceConfig)
44+
45+
def test_session(self):
46+
"""Ensure that a session is constructed correctly."""
47+
# Create a requests.Session
48+
self.assertIsInstance(self.fetcher.session(), requests.Session)
49+
# Recycle session, create a BaseSession and persist it
50+
self.assertIsInstance(
51+
self.fetcher.session(
52+
'https://foo.bar.com', ('foo', 'bar'), foo='FOO', bar='BAR'
53+
),
54+
BaseSession
55+
)
56+
self.assertEqual(self.fetcher.session().baseurl, 'https://foo.bar.com')
57+
self.assertEqual(self.fetcher.session().auth, ('foo', 'bar'))
58+
self.assertEqual(
59+
self.fetcher.session().headers['User-Agent'],
60+
'your_org-compliance-checks'
61+
)
62+
self.assertEqual(self.fetcher.session().headers['foo'], 'FOO')
63+
self.assertEqual(self.fetcher.session().headers['bar'], 'BAR')

0 commit comments

Comments
 (0)