Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Report json decoding errors with debug data, added unit tests

  • Loading branch information...
commit 0941c2100869b9d19d04e999086e6af960bc5953 1 parent 9afc533
Simon Cadman authored
Showing with 33 additions and 10 deletions.
  1. +20 −8 cloudprintrequestor.py
  2. +13 −2 test_cloudprintrequestor.py
28 cloudprintrequestor.py
View
@@ -13,7 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import httplib2, json
+import httplib2, json, sys
class cloudprintrequestor(httplib2.Http):
@@ -36,7 +36,7 @@ def getAccount ( self ):
"""
return self.account
- def doRequest ( self, path, headers = None, data = None , boundary = None ): # pragma: no cover
+ def doRequest ( self, path, headers = None, data = None , boundary = None, testResponse=None ): # pragma: no cover
"""Sends a request to Google Cloud Print
Args:
@@ -53,10 +53,22 @@ def doRequest ( self, path, headers = None, data = None , boundary = None ): # p
headers['user-agent'] = "CUPS Cloud Print"
url = '%s/%s' % (self.CLOUDPRINT_URL, path)
- if data == None:
- headers, response = self.request(url, "GET", headers=headers)
+
+ # use test response for testing
+ if testResponse == None:
+ if data == None:
+ headers, response = self.request(url, "GET", headers=headers)
+ else:
+ headers['Content-Length'] = str(len(data))
+ headers['Content-Type'] = 'multipart/form-data;boundary=%s' % boundary
+ headers, response = self.request(url, "POST", body=data, headers=headers)
else:
- headers['Content-Length'] = str(len(data))
- headers['Content-Type'] = 'multipart/form-data;boundary=%s' % boundary
- headers, response = self.request(url, "POST", body=data, headers=headers)
- return json.loads(response)
+ response = testResponse
+
+ try:
+ decodedresponse = json.loads(response)
+ except ValueError as e:
+ print("ERROR: Failed to decode JSON, value was: " + response)
+ raise e
+
+ return decodedresponse
15 test_cloudprintrequestor.py
View
@@ -15,9 +15,20 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import json, pytest
from cloudprintrequestor import cloudprintrequestor
-def test_requestor():
+global requestor
+
+def setup_function(function):
+ global requestor
requestor = cloudprintrequestor()
+
+def test_requestor():
requestor.setAccount('testdetails')
- assert requestor.getAccount() == 'testdetails'
+ assert requestor.getAccount() == 'testdetails'
+
+def test_request():
+ assert requestor.doRequest(path="/test",testResponse=json.dumps("randomstring1233")) == "randomstring1233"
+ with pytest.raises(ValueError):
+ assert requestor.doRequest(path="/test",testResponse="")
Please sign in to comment.
Something went wrong with that request. Please try again.