Added exception around parsing the discovery doc.
This commit is contained in:
@@ -38,6 +38,7 @@ from http import HttpRequest
|
|||||||
from anyjson import simplejson
|
from anyjson import simplejson
|
||||||
from model import JsonModel
|
from model import JsonModel
|
||||||
from errors import UnknownLinkType
|
from errors import UnknownLinkType
|
||||||
|
from errors import HttpError
|
||||||
|
|
||||||
URITEMPLATE = re.compile('{[^}]*}')
|
URITEMPLATE = re.compile('{[^}]*}')
|
||||||
VARNAME = re.compile('[a-zA-Z0-9_-]+')
|
VARNAME = re.compile('[a-zA-Z0-9_-]+')
|
||||||
@@ -107,7 +108,10 @@ def build(serviceName, version,
|
|||||||
requested_url = uritemplate.expand(discoveryServiceUrl, params)
|
requested_url = uritemplate.expand(discoveryServiceUrl, params)
|
||||||
logging.info('URL being requested: %s' % requested_url)
|
logging.info('URL being requested: %s' % requested_url)
|
||||||
resp, content = http.request(requested_url)
|
resp, content = http.request(requested_url)
|
||||||
service = simplejson.loads(content)
|
try:
|
||||||
|
service = simplejson.loads(content)
|
||||||
|
except ValueError, e:
|
||||||
|
raise HttpError(resp, content)
|
||||||
|
|
||||||
fn = os.path.join(os.path.dirname(__file__), 'contrib',
|
fn = os.path.join(os.path.dirname(__file__), 'contrib',
|
||||||
serviceName, 'future.json')
|
serviceName, 'future.json')
|
||||||
|
|||||||
1
tests/data/malformed.json
Normal file
1
tests/data/malformed.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{
|
||||||
@@ -34,6 +34,7 @@ except ImportError:
|
|||||||
|
|
||||||
from apiclient.discovery import build, key2param
|
from apiclient.discovery import build, key2param
|
||||||
from apiclient.http import HttpMock
|
from apiclient.http import HttpMock
|
||||||
|
from apiclient.errors import HttpError
|
||||||
|
|
||||||
|
|
||||||
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
|
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
@@ -48,6 +49,17 @@ class Utilities(unittest.TestCase):
|
|||||||
self.assertEqual('x007_bond', key2param('007-bond'))
|
self.assertEqual('x007_bond', key2param('007-bond'))
|
||||||
|
|
||||||
|
|
||||||
|
class DiscoveryErrors(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_failed_to_parse_discovery_json(self):
|
||||||
|
self.http = HttpMock(datafile('malformed.json'), {'status': '200'})
|
||||||
|
try:
|
||||||
|
buzz = build('buzz', 'v1', self.http)
|
||||||
|
self.fail("should have raised an exception over malformed JSON.")
|
||||||
|
except HttpError, e:
|
||||||
|
self.assertEqual(e.content, "{\n")
|
||||||
|
|
||||||
|
|
||||||
class Discovery(unittest.TestCase):
|
class Discovery(unittest.TestCase):
|
||||||
|
|
||||||
def test_method_error_checking(self):
|
def test_method_error_checking(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user