Browse Source

steamid: fix exception handling and http_timeout

pull/34/head
Rossen Georgiev 9 years ago
parent
commit
45f41314fa
  1. 6
      steam/steamid.py
  2. 12
      tests/test_steamid.py

6
steam/steamid.py

@ -362,12 +362,12 @@ def steam64_from_url(url, http_timeout=30):
try: try:
if match.group('type') in ('id', 'profiles'): if match.group('type') in ('id', 'profiles'):
xml = web.get("%s/?xml=1&nocache=%d" % (url, nocache)).text xml = web.get("%s/?xml=1&nocache=%d" % (url, nocache), timeout=http_timeout).text
match = re.findall('<steamID64>(\d+)</steamID64>', xml) match = re.findall('<steamID64>(\d+)</steamID64>', xml)
else: else:
xml = web.get("%s/memberslistxml/?xml=1&nocache=%d" % (url, nocache)).text xml = web.get("%s/memberslistxml/?xml=1&nocache=%d" % (url, nocache), timeout=http_timeout).text
match = re.findall('<groupID64>(\d+)</groupID64>', xml) match = re.findall('<groupID64>(\d+)</groupID64>', xml)
except requests.exceptions.BaseHTTPError: except requests.exceptions.RequestException:
return None return None
if not match: if not match:

12
tests/test_steamid.py

@ -244,13 +244,15 @@ class steamid_functions(unittest.TestCase):
self.assertIsInstance(test_instance, SteamID) self.assertIsInstance(test_instance, SteamID)
self.assertEqual(test_instance.as_64, 76580280500085312) self.assertEqual(test_instance.as_64, 76580280500085312)
@mock.patch('requests.get') @mock.patch('steam.steamid.make_requests_session')
def test_steam64_from_url_timeout(self, get_mock): def test_steam64_from_url_timeout(self, mrs_mock):
get_mock.return_value = requests.exceptions.ConnectTimeout('test') mm = mrs_mock.return_value = mock.MagicMock()
mm.get.side_effect = requests.exceptions.ConnectTimeout('test')
self.assertIsNone(steamid.steam64_from_url("https://steamcommunity.com/id/timeout_me")) self.assertIsNone(steamid.steam64_from_url("https://steamcommunity.com/id/timeout_me"))
get_mock.reset_mock() mm.get.reset_mock()
get_mock.return_value = requests.exceptions.ReadTimeout('test') mm.get.side_effect = requests.exceptions.ReadTimeout('test')
self.assertIsNone(steamid.steam64_from_url("https://steamcommunity.com/id/timeout_me")) self.assertIsNone(steamid.steam64_from_url("https://steamcommunity.com/id/timeout_me"))
@vcr.use_cassette('vcr/steamid_community_urls.yaml', mode='once', serializer='yaml', filter_query_parameters=['nocache']) @vcr.use_cassette('vcr/steamid_community_urls.yaml', mode='once', serializer='yaml', filter_query_parameters=['nocache'])

Loading…
Cancel
Save