Force HttpMock to read content from file as bytes
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index fdce194..5fcd7a1 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -1463,7 +1463,7 @@
if headers is None:
headers = {'status': '200'}
if filename:
- f = open(filename, 'r')
+ f = open(filename, 'rb')
self.data = f.read()
f.close()
else:
diff --git a/tests/data/bad_request.json b/tests/data/bad_request.json
new file mode 100644
index 0000000..5a3dc89
--- /dev/null
+++ b/tests/data/bad_request.json
@@ -0,0 +1,13 @@
+{
+ "error": {
+ "errors": [
+ {
+ "domain": "usageLimits",
+ "reason": "keyInvalid",
+ "message": "Bad Request"
+ }
+ ],
+ "code": 400,
+ "message": "Bad Request"
+ }
+}
diff --git a/tests/test_http.py b/tests/test_http.py
index 6d296b6..82369fc 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -1010,6 +1010,17 @@
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)