Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
super(ResourceBindingTestCase, self).setUp()
self.client = WSClient()
def test_channel_socket_exception(self):
class MyChannelSocketException(ChannelSocketException):
def run(self, message):
message.reply_channel.send({'text': 'error'})
def consumer(message):
raise MyChannelSocketException
client = WSClient()
with apply_routes(route('websocket.receive', consumer)):
client.send_and_consume('websocket.receive')
self.assertEqual(client.receive(json=False), 'error')
def test_get_params(self):
client = WSClient()
content = client._get_content(path='/my/path?test=1&token=2')
self.assertTrue('path' in content)
self.assertTrue('query_string' in content)
self.assertEqual(content['path'], '/my/path')
self.assertEqual(content['query_string'], 'test=1&token=2')
class WebsocketConsumer(websockets.WebsocketConsumer):
def connect(self, message, **kwargs):
self.message.reply_channel.send({'accept': True})
self.send(text=message.get('order'))
routes = [
WebsocketConsumer.as_route(attrs={"strict_ordering": True}, path='^/path$'),
WebsocketConsumer.as_route(path='^/path/2$'),
]
self.assertIsNot(routes[0].consumer, WebsocketConsumer)
self.assertIs(routes[1].consumer, WebsocketConsumer)
with apply_routes(routes):
client = WSClient()
client.send('websocket.connect', {'path': '/path', 'order': 1})
client.send('websocket.connect', {'path': '/path', 'order': 0})
client.consume('websocket.connect', check_accept=False)
client.consume('websocket.connect')
self.assertEqual(client.receive(json=False), 0)
client.consume('websocket.connect')
self.assertEqual(client.receive(json=False), 1)
client.send_and_consume('websocket.connect', {'path': '/path/2', 'order': 'next'})
self.assertEqual(client.receive(json=False), 'next')
def test_ordering(self):
client = WSClient(ordered=True)
@enforce_ordering
def consumer(message):
message.reply_channel.send({'text': message['text']})
with apply_routes(route('websocket.receive', consumer)):
client.send_and_consume('websocket.receive', text='1') # order = 0
client.send_and_consume('websocket.receive', text='2') # order = 1
client.send_and_consume('websocket.receive', text='3') # order = 2
self.assertEqual(client.receive(), 1)
self.assertEqual(client.receive(), 2)
self.assertEqual(client.receive(), 3)
def test_cookies(self):
client = WSClient()
client.set_cookie('foo', 'not-bar')
client.set_cookie('foo', 'bar')
client.set_cookie('qux', 'qu;x')
# Django's interpretation of the serialized cookie.
cookie_dict = parse_cookie(client.headers['cookie'].decode('ascii'))
self.assertEqual(client.get_cookies(),
cookie_dict)
self.assertEqual({'foo': 'bar',
'qux': 'qu;x',
'sessionid': client.get_cookies()['sessionid']},
cookie_dict)
def test_websockets_http_session_and_channel_session(self):
class WebsocketConsumer(websockets.WebsocketConsumer):
http_user_and_session = True
user_model = get_user_model()
user = user_model.objects.create_user(username='test', email='test@test.com', password='123456')
client = WSClient()
client.force_login(user)
with apply_routes([route_class(WebsocketConsumer, path='/path')]):
connect = client.send_and_consume('websocket.connect', {'path': '/path'})
receive = client.send_and_consume('websocket.receive', {'path': '/path'}, text={'key': 'value'})
disconnect = client.send_and_consume('websocket.disconnect', {'path': '/path'})
self.assertEqual(
connect.message.http_session.session_key,
receive.message.http_session.session_key
)
self.assertEqual(
connect.message.http_session.session_key,
disconnect.message.http_session.session_key
)
class TestBinding(WebsocketBinding):
model = User
stream = 'test'
fields = ['__all__']
@classmethod
def group_names(cls, instance):
return ["users2"]
def has_permission(self, user, action, pk):
return True
# Make model and clear out pending sends
user = User.objects.create(username='test', email='test@test.com')
client = WSClient()
client.join_group('users2')
user.username = 'test_new'
user.save()
received = client.receive()
self.assertTrue('payload' in received)
self.assertTrue('action' in received['payload'])
self.assertTrue('data' in received['payload'])
self.assertTrue('username' in received['payload']['data'])
self.assertTrue('email' in received['payload']['data'])
self.assertTrue('password' in received['payload']['data'])
self.assertTrue('last_name' in received['payload']['data'])
self.assertTrue('model' in received['payload'])
self.assertTrue('pk' in received['payload'])
def test_websocket_demultiplexer_send(self):
class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
def receive(self, content, multiplexer=None, **kwargs):
self.send(content)
class Demultiplexer(websockets.WebsocketDemultiplexer):
consumers = {
"mystream": MyWebsocketConsumer
}
with apply_routes([route_class(Demultiplexer, path='/path/(?P\d+)')]):
client = WSClient()
with self.assertRaises(SendNotAvailableOnDemultiplexer):
client.send_and_consume('websocket.receive', path='/path/1', text={
"stream": "mystream",
"payload": {"text_field": "mytext"},
})
client.receive()
def test_simple_content(self):
client = WSClient()
content = client._get_content(text={'key': 'value'}, path='/my/path')
self.assertEqual(content['text'], '{"key": "value"}')
self.assertEqual(content['path'], '/my/path')
self.assertTrue('reply_channel' in content)
self.assertTrue('headers' in content)