Skip to content

Commit

Permalink
Merge pull request #125 from alanbriolat/httpmock-bytes
Browse files Browse the repository at this point in the history
Force HttpMock to read content from file as bytes.
  • Loading branch information
nathanielmanistaatgoogle committed Aug 13, 2015
2 parents b105fbe + 26b0100 commit a98add2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ def __init__(self, filename=None, headers=None):
if headers is None:
headers = {'status': '200'}
if filename:
f = open(filename, 'r')
f = open(filename, 'rb')
self.data = f.read()
f.close()
else:
Expand Down
13 changes: 13 additions & 0 deletions tests/data/bad_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "keyInvalid",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
11 changes: 11 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,17 @@ def test_default_response_headers(self):
resp, content = http.request("http://example.com")
self.assertEqual(resp.status, 200)

def test_error_response(self):
http = HttpMock(datafile('bad_request.json'), {'status': '400'})
model = JsonModel()
request = HttpRequest(
http,
model.response,
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
method='GET',
headers={})
self.assertRaises(HttpError, request.execute)


if __name__ == '__main__':
logging.getLogger().setLevel(logging.ERROR)
Expand Down

0 comments on commit a98add2

Please sign in to comment.