Skip to content

Commit

Permalink
Add test for parse errors of request
Browse files Browse the repository at this point in the history
* silpa/api/jsonrpc.py: Remove unwanted print statement and fix the
  error dict wrapping in error response.
* tests/api/jsonrpc_tests.py: Tests for parse errors

Signed-off-by: Vasudev Kamath <kamathvasudev@gmail.com>
  • Loading branch information
copyninja committed Apr 19, 2014
1 parent 3bd5d29 commit 4cc8a5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 3 additions & 6 deletions silpa/api/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,24 @@ def __init__(self, data):
error = JsonRpcError(code=INVALID_REQUEST,
message="Not a valid JSON-RPC request",
data='')
error_dict = dict(zip(error._fields, error))
self.error_response = JsonRpcErrorResponse(jsonrpc="2.0",
error=error, id='')
error=error_dict,
id='')

def __call__(self):
# process request here
print("call function")
module, method = self.request.method.split('.')
if module not in sys.modules:
# Module is not yet loaded? handle this
print("module not loaded")
pass
else:
# module is present in sys
print("module loaded")
mod = sys.modules[module]
if hasattr(mod, 'getInstance'):
print("method has getInstance")
instance = getattr(mod, 'getInstance')()
if not hasattr(instance, method):
result = getattr(instance, method)(*self.request.params)
print(result)
self.response = JsonRpcResultResponse(jsonrpc="2.0",
result=result,
id=self.request.id)
Expand Down
13 changes: 11 additions & 2 deletions tests/api/jsonrpc_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def assertJsonRpcMethodNotFound(self, response):
def assertJsonRpcInvalidRequest(self, response):
response_dict = json.loads(self.assertBadJson(response).data)
self.assertIn('error', response_dict)
error_obj = jsonrpc.JsonRpcErrorResponse(**response_dict['error'])
error_obj = jsonrpc.JsonRpcError(**response_dict['error'])
self.assertEquals(error_obj.code, jsonrpc.INVALID_REQUEST)

def assertJsonRpcParseErrors(self, response):
response_dict = json.loads(self.assertBadJson(response).data)
self.assertIn('error', response_dict)
error_obj = jsonrpc.JsonRpcErrorResponse(**response_dict['error'])
error_obj = jsonrpc.JsonRpcError(**response_dict['error'])
self.assertEquals(error_obj.code, jsonrpc.PARSE_ERRORS)

def assertJsonRpcResult(self, response):
Expand All @@ -42,3 +42,12 @@ def test_methodnot_found(self):
params=['Hello World!', 'kn_IN'],
id=random.randint(1, 1000))
self.assertJsonRpcMethodNotFound(self.jpost('/api/JSONRPC', data=data))

def test_request_parseerror(self):
# notice missing jsonrpc key? this is invalid request but our
# code will trigger parse error as we can't convert this to
# JsonRpcRequest object
data = dict(method='transliteration.transliterate',
params=['Hello World!', 'kn_IN'],
id=random.randint(1, 1000))
self.assertJsonRpcParseErrors(self.jpost('/api/JSONRPC', data=data))

0 comments on commit 4cc8a5e

Please sign in to comment.