Iimprovements for test client (#352)

* Json encoding only for not string text at HttpClient

* Decode received content if possible

* Check that content received

* Using json kwarg at receive method to decode message text content

* Wrap decorator function at channel_session_user_from_http

* Cleanup

* Arbitrary indent. sorry
This commit is contained in:
Krukov D 2016-09-12 13:28:12 +03:00 committed by Andrew Godwin
parent 075897d910
commit 5464cba742
3 changed files with 18 additions and 6 deletions

View File

@ -82,6 +82,7 @@ def channel_session_user_from_http(func):
"""
@http_session_user
@channel_session
@functools.wraps(func)
def inner(message, *args, **kwargs):
if message.http_session is not None:
transfer_user(message.http_session, message.channel_session)

View File

@ -135,7 +135,7 @@ class Client(object):
return self.consume(channel, fail_on_none=fail_on_none)
def receive(self):
"""self.get_next_message(self.reply_channel)
"""
Get content of next message for reply channel if message exists
"""
message = self.get_next_message(self.reply_channel)

View File

@ -1,12 +1,16 @@
import json
import copy
import six
from django.apps import apps
from django.conf import settings
from ..sessions import session_for_reply_channel
from .base import Client
json_module = json # alias for using at functions with json kwarg
class HttpClient(Client):
"""
@ -56,10 +60,14 @@ class HttpClient(Client):
self._session = session_for_reply_channel(self.reply_channel)
return self._session
def receive(self):
def receive(self, json=True):
"""
Return text content of a message for client channel and decoding it if json kwarg is set
"""
content = super(HttpClient, self).receive()
if content:
return json.loads(content['text'])
if content and json:
return json_module.loads(content['text'])
return content['text'] if content else None
def send(self, to, content={}, text=None, path='/'):
"""
@ -71,8 +79,11 @@ class HttpClient(Client):
content.setdefault('path', path)
content.setdefault('headers', self.headers)
text = text or content.get('text', None)
if text:
content['text'] = json.dumps(text)
if text is not None:
if not isinstance(text, six.string_types):
content['text'] = json.dumps(text)
else:
content['text'] = text
self.channel_layer.send(to, content)
def send_and_consume(self, channel, content={}, text=None, path='/', fail_on_none=True):