From be3c10d13b676c2c54b3be8692aaf9bace70710c Mon Sep 17 00:00:00 2001 From: Fabien Millerand Date: Tue, 4 Apr 2017 16:34:22 +0200 Subject: [PATCH] Revamped the way of introducing "original_message" parameter --- channels_jsonrpc/jsonrpcwebsocketconsumer.py | 23 ++++++++++---------- example/django_example/tests.py | 20 +++++++++++++++++ setup.py | 2 +- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/channels_jsonrpc/jsonrpcwebsocketconsumer.py b/channels_jsonrpc/jsonrpcwebsocketconsumer.py index fac3b46..754f7a1 100644 --- a/channels_jsonrpc/jsonrpcwebsocketconsumer.py +++ b/channels_jsonrpc/jsonrpcwebsocketconsumer.py @@ -217,21 +217,23 @@ 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'), @@ -239,7 +241,6 @@ def __process(cls, data, original_msg): 'result': result, } - class JsonRpcWebsocketConsumerTest(JsonRpcWebsocketConsumer): TEST_MODE = True diff --git a/example/django_example/tests.py b/example/django_example/tests.py index 2bc1d0f..e4f5116 100644 --- a/example/django_example/tests.py +++ b/example/django_example/tests.py @@ -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"]) \ No newline at end of file diff --git a/setup.py b/setup.py index 90992a1..21c5923 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name='django-channels-jsonrpc', - version='1.1.5', + version='1.1.6', packages=find_packages(), install_requires=[ 'channels',