Skip to content

Commit

Permalink
Bear: Raise exception on problematic HTTP status
Browse files Browse the repository at this point in the history
requests.Response.raise_for_status raises an exception
when the HTTP status code is 400-599.

Fixes coala#3803
Related to coala/coala-bears#1461
  • Loading branch information
jayvdb committed Mar 1, 2017
1 parent dd1a7c7 commit 422193a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions coalib/bears/Bear.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ def download_cached_file(self, url, filename):
.format(filename=filename, bearname=self.name, url=url))

response = requests.get(url, stream=True, timeout=20)
response.raise_for_status()

with open(filename, 'wb') as file:
for chunk in response.iter_content(125):
file.write(chunk)
Expand Down
36 changes: 32 additions & 4 deletions tests/bears/BearTest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import multiprocessing
import unittest
from os.path import abspath, exists, isfile, join, getmtime
import requests_mock
import shutil
from time import sleep

import requests
import requests_mock

from coalib.bears.Bear import Bear
from coalib.bears.BEAR_KIND import BEAR_KIND
from coalib.bears.GlobalBear import GlobalBear
Expand Down Expand Up @@ -102,7 +104,7 @@ def run(self, y: int, w: float):
yield w


class BearTest(unittest.TestCase):
class BearTestBase(unittest.TestCase):

def setUp(self):
self.queue = multiprocessing.Queue()
Expand All @@ -113,6 +115,9 @@ def tearDown(self):
if exists(self.uut.data_dir):
shutil.rmtree(self.uut.data_dir)


class BearTest(BearTestBase):

def test_simple_api(self):
self.assertRaises(TypeError, TestBear, self.settings, 2)
self.assertRaises(TypeError, TestBear, None, self.queue)
Expand Down Expand Up @@ -262,13 +267,36 @@ def test_new_result(self):
expected = Result.from_values(bear, 'test message', '/tmp/testy')
self.assertEqual(result, expected)


class BearDownloadTest(BearTestBase):

def setUp(self):
super().setUp()
self.mock_url = 'https://test.com'
self.filename = 'test.html'
self.file_location = join(self.uut.data_dir, self.filename)

def test_connection_timeout_mocked(self):
exc = requests.exceptions.ConnectTimeout
with requests_mock.Mocker() as reqmock:
reqmock.get(self.mock_url, exc=exc)
with self.assertRaisesRegexp(exc, '^$'):
self.uut.download_cached_file(
self.mock_url, self.filename)

def test_status_code_error(self):
exc = requests.exceptions.HTTPError
with self.assertRaisesRegexp(exc, '418 Client Error'):
self.uut.download_cached_file(
'http://httpbin.org/status/418', self.filename)

def test_download_cached_file(self):
mock_url = 'https://test.com'
mock_text = """<html>
<p> lorem impsum dolor</p>
</hrml>"""
filename = 'test.html'
file_location = join(self.uut.data_dir, filename)
filename = self.filename
file_location = self.file_location

with requests_mock.Mocker() as reqmock:
reqmock.get(mock_url, text=mock_text)
Expand Down

0 comments on commit 422193a

Please sign in to comment.