Skip to content

Commit 9f5a550

Browse files
author
Burak Yigit Kaya
committed
Add bad HTTP status (non 200) check for responses (fixes disqus#30)
1 parent 7d2c2ec commit 9f5a550

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

phabricator/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ def validate_kwarg(key, target):
293293
# TODO: Use HTTP "method" from interfaces.json
294294
conn.request('POST', path, body, headers)
295295
response = conn.getresponse()
296+
297+
# Make sure we got a 2xx response indicating success
298+
if not response.status >= 200 or not response.status < 300:
299+
raise httplib.HTTPException(
300+
'Bad response status: {0}'.format(response.status)
301+
)
302+
296303
data = self._parse_response(response.read())
297304

298305
return Result(data['result'])

phabricator/tests.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737

3838
class PhabricatorTest(unittest.TestCase):
3939
def setUp(self):
40-
self.api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
40+
self.api = phabricator.Phabricator(
41+
username='test',
42+
certificate='test',
43+
host='http://localhost'
44+
)
4145
self.api.certificate = CERTIFICATE
4246

4347
def test_generate_hash(self):
@@ -49,8 +53,14 @@ def test_generate_hash(self):
4953
def test_connect(self, mock_connection):
5054
mock_obj = mock_connection.return_value = mock.Mock()
5155
mock_obj.getresponse.return_value = StringIO(RESPONSES['conduit.connect'])
56+
mock_obj.getresponse.return_value.status = 200
57+
58+
api = phabricator.Phabricator(
59+
username='test',
60+
certificate='test',
61+
host='http://localhost'
62+
)
5263

53-
api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
5464
api.connect()
5565
keys = api._conduit.keys()
5666
self.assertIn('sessionKey', keys)
@@ -60,16 +70,39 @@ def test_connect(self, mock_connection):
6070
def test_user_whoami(self, mock_connection):
6171
mock_obj = mock_connection.return_value = mock.Mock()
6272
mock_obj.getresponse.return_value = StringIO(RESPONSES['user.whoami'])
73+
mock_obj.getresponse.return_value.status = 200
6374

64-
api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
75+
api = phabricator.Phabricator(
76+
username='test',
77+
certificate='test',
78+
host='http://localhost'
79+
)
6580
api._conduit = True
6681

6782
self.assertEqual(api.user.whoami()['userName'], 'testaccount')
6883

84+
@mock.patch('phabricator.httplib.HTTPConnection')
85+
def test_bad_status(self, mock_connection):
86+
mock_obj = mock_connection.return_value = mock.Mock()
87+
mock_obj.getresponse.return_value = mock.Mock()
88+
mock_obj.getresponse.return_value.status = 400
89+
90+
api = phabricator.Phabricator(
91+
username='test',
92+
certificate='test',
93+
host='http://localhost'
94+
)
95+
api._conduit = True
96+
97+
with self.assertRaises(phabricator.httplib.HTTPException):
98+
api.user.whoami()
99+
100+
69101
@mock.patch('phabricator.httplib.HTTPConnection')
70102
def test_maniphest_find(self, mock_connection):
71103
mock_obj = mock_connection.return_value = mock.Mock()
72104
mock_obj.getresponse.return_value = StringIO(RESPONSES['maniphest.find'])
105+
mock_obj.getresponse.return_value.status = 200
73106

74107
api = phabricator.Phabricator(username='test', certificate='test', host='http://localhost')
75108
api._conduit = True

0 commit comments

Comments
 (0)