Skip to content

Commit

Permalink
Allow empty 'error' to be in repsonse. (#2)
Browse files Browse the repository at this point in the history
* Allow empty 'error' to be in repsonse.

Reason: some servers send `'error': None`.

* Fix python3.6 testenv in tox.ini.
  • Loading branch information
tdivis authored and emlove committed Aug 22, 2018
1 parent 124e9f6 commit a2f2187
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jsonrpc_base/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def parse_response(self, data):

if not isinstance(data, dict):
raise ProtocolError('Response is not a dictionary')
if 'error' in data:
if data.get('error') is not None:
code = data['error'].get('code', '')
message = data['error'].get('message', '')
raise ProtocolError(code, message, data)
Expand Down
13 changes: 13 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ def test_request(self):
request = jsonrpc_base.Request('test_method', msg_id=1)
self.assertEqual(request.response_id, 1)

def test_jsonrpc_1_0_call(self):
# JSON-RPC 1.0 spec needs "error" to be present (with `null` value) when no error occured
def handler(message):
self.assertEqual(message.params, [42, 23])
return {
"result": 19,
"id": 1,
"error": None,
}

self.server._handler = handler
self.assertEqual(self.server.subtract(42, 23), 19)


if __name__ == '__main__':
unittest.main()
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist =
envlist =
py27,
py34,
py35,
Expand All @@ -8,7 +8,7 @@ envlist =
[testenv]
setenv =
PYTHONPATH = {toxinidir}:{toxinidir}/jsonrpc_base
commands =
commands =
coverage run tests.py
coverage report
deps =
Expand All @@ -29,7 +29,7 @@ basepython = python3.5
deps =
{[testenv]deps}

[testenv:py35]
[testenv:py36]
basepython = python3.6
deps =
{[testenv]deps}

0 comments on commit a2f2187

Please sign in to comment.