Skip to content

Commit a98add2

Browse files
Merge pull request googleapis#125 from alanbriolat/httpmock-bytes
Force HttpMock to read content from file as bytes.
2 parents b105fbe + 26b0100 commit a98add2

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Diff for: googleapiclient/http.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ def __init__(self, filename=None, headers=None):
14631463
if headers is None:
14641464
headers = {'status': '200'}
14651465
if filename:
1466-
f = open(filename, 'r')
1466+
f = open(filename, 'rb')
14671467
self.data = f.read()
14681468
f.close()
14691469
else:

Diff for: tests/data/bad_request.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"error": {
3+
"errors": [
4+
{
5+
"domain": "usageLimits",
6+
"reason": "keyInvalid",
7+
"message": "Bad Request"
8+
}
9+
],
10+
"code": 400,
11+
"message": "Bad Request"
12+
}
13+
}

Diff for: tests/test_http.py

+11
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,17 @@ def test_default_response_headers(self):
10101010
resp, content = http.request("http://example.com")
10111011
self.assertEqual(resp.status, 200)
10121012

1013+
def test_error_response(self):
1014+
http = HttpMock(datafile('bad_request.json'), {'status': '400'})
1015+
model = JsonModel()
1016+
request = HttpRequest(
1017+
http,
1018+
model.response,
1019+
'https://www.googleapis.com/someapi/v1/collection/?foo=bar',
1020+
method='GET',
1021+
headers={})
1022+
self.assertRaises(HttpError, request.execute)
1023+
10131024

10141025
if __name__ == '__main__':
10151026
logging.getLogger().setLevel(logging.ERROR)

0 commit comments

Comments
 (0)