Skip to content

Commit

Permalink
Revamped the way of introducing "original_message" parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
millerf committed Apr 4, 2017
1 parent 2397ef1 commit be3c10d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
23 changes: 12 additions & 11 deletions channels_jsonrpc/jsonrpcwebsocketconsumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,30 @@ def __process(cls, data, original_msg):
if not isinstance(params, (list, dict)):
raise JsonRpcException(data.get('id'), cls.INVALID_PARAMS)

args = []
kwargs = {}
if isinstance(params, list):
args = params
elif isinstance(params, dict):
kwargs.update(params)

if sys.version_info < (3, 5):
func_args, _, _, _ = getargspec(method)
else:
func_args, _, _, _, _, _, _ = getfullargspec(method)

if 'original_message' in func_args:
kwargs['original_message'] = original_msg
result = method(*args, **kwargs)
if isinstance(params, list):

# we make sure that it has the right size
args = params
if 'original_message' in func_args:
args.insert(func_args.index('original_message'), original_msg)
result = method(*args)
elif isinstance(params, dict):
kwargs = params
if 'original_message' in func_args:
kwargs['original_message'] = original_msg
result = method(**kwargs)

return {
'id': data.get('id'),
'jsonrpc': '2.0',
'result': result,
}


class JsonRpcWebsocketConsumerTest(JsonRpcWebsocketConsumer):

TEST_MODE = True
Expand Down
20 changes: 20 additions & 0 deletions example/django_example/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,24 @@ def thread_test():
{"id": 1, "jsonrpc": "2.0", "method": "ping2", "params": []}, "test%s" % i)
self.assertEqual(res['result'], "test%s" % i)

def test_original_message_position_safe(self):

@MyJsonRpcWebsocketConsumerTest.rpc_method()
def ping_set_session(name, original_message, value):
original_message.channel_session["test"] = True
return ["pong_set_session", value, name]

@MyJsonRpcWebsocketConsumerTest.rpc_method()
def ping_get_session(original_message, value2, name2):
self.assertEqual(original_message.channel_session["test"], True)
return ["pong_get_session", value2, name2]

client = HttpClient()
client.send_and_consume(u'websocket.receive',
text='{"id":1, "jsonrpc":"2.0", "method":"ping_set_session", "params":["name_of_function", "value_of_function"]}')
msg = client.receive()
self.assertEqual(msg['result'], ["pong_set_session", "value_of_function", "name_of_function"])
client.send_and_consume(u'websocket.receive',
text='{"id":1, "jsonrpc":"2.0", "method":"ping_get_session", "params":{"name2": "name2_of_function", "value2": "value2_of_function"}}')
msg = client.receive()
self.assertEqual(msg['result'], ["pong_get_session", "value2_of_function", "name2_of_function"])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-channels-jsonrpc',
version='1.1.5',
version='1.1.6',
packages=find_packages(),
install_requires=[
'channels',
Expand Down

0 comments on commit be3c10d

Please sign in to comment.