Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Report json decoding errors with debug data, added unit tests

  • Loading branch information
simoncadman committed Feb 2, 2013
1 parent 9afc533 commit 0941c2100869b9d19d04e999086e6af960bc5953
Showing with 33 additions and 10 deletions.
  1. +20 −8 cloudprintrequestor.py
  2. +13 −2 test_cloudprintrequestor.py
@@ -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,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="")

0 comments on commit 0941c21

Please sign in to comment.
You can’t perform that action at this time.