本文整理汇总了Python中channels.tests.HttpClient.consume方法的典型用法代码示例。如果您正苦于以下问题:Python HttpClient.consume方法的具体用法?Python HttpClient.consume怎么用?Python HttpClient.consume使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类channels.tests.HttpClient
的用法示例。
在下文中一共展示了HttpClient.consume方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_inbound_delete
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_inbound_delete(self):
user = User.objects.create(username='test', email='[email protected]')
class Demultiplexer(WebsocketDemultiplexer):
mapping = {
'users': 'binding.users',
}
groups = ['inbound']
class UserBinding(WebsocketBinding):
model = User
stream = 'users'
fields = ['username', ]
@classmethod
def group_names(cls, instance, action):
return ['users_outbound']
def has_permission(self, user, action, pk):
return True
with apply_routes([Demultiplexer.as_route(path='/'), route('binding.users', UserBinding.consumer)]):
client = HttpClient()
client.send_and_consume('websocket.connect', path='/')
client.send_and_consume('websocket.receive', path='/', text={
'stream': 'users',
'payload': {'action': DELETE, 'pk': user.pk}
})
# our Demultiplexer route message to the inbound consumer, so call Demultiplexer consumer
client.consume('binding.users')
self.assertIsNone(User.objects.filter(pk=user.pk).first())
self.assertIsNone(client.receive())
示例2: test_delete_mixin
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_delete_mixin(self):
# create object
obj = User.objects.create_user(username='test', email='[email protected]')
# create client
client = HttpClient()
with apply_routes([DeleteConsumers.as_routes(model=User, path='/(?P<pk>\d+)/?', channel_name='test')]):
client.send_and_consume(u'websocket.connect', {'path': '/{}'.format(obj.pk)})
client.send_and_consume(u'websocket.receive', {'path': '/{}'.format(obj.pk), 'action': 'delete'})
client.consume('test')
self.assertFalse(User.objects.filter(pk=obj.pk).exists())
示例3: test_create_mixin
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_create_mixin(self):
# create client
client = HttpClient()
data = {'username': 'test', 'email': '[email protected]'}
with apply_routes([CreateConsumers.as_routes(model=User, path='/', channel_name='test')]):
client.send_and_consume(u'websocket.connect', {'path': '/'})
client.send_and_consume(u'websocket.receive', {'path': '/', 'action': 'create',
'data': json.dumps(data)})
client.consume(u'test')
self.assertTrue(User.objects.filter(username='test', email='[email protected]').exists())
示例4: test_get_mixin
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_get_mixin(self):
# create object
obj = User.objects.create_user(username='test', email='[email protected]')
# create client
client = HttpClient()
with apply_routes([ReadOnlyConsumers.as_routes(model=User, path='/(?P<pk>\d+)/?', channel_name='test')]):
client.send_and_consume(u'websocket.connect', {'path': '/{}'.format(obj.pk)})
client.send_and_consume(u'websocket.receive', {'path': '/{}'.format(obj.pk), 'action': 'get'})
client.consume('test')
res = json.loads(json.loads(client.receive()['text'])['response'])
self.assertEqual(res['username'], 'test')
self.assertEqual(res['email'], '[email protected]')
self.assertEqual(res['is_active'], True)
示例5: test_update_mixin
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_update_mixin(self):
# create object
obj = User.objects.create_user(username='test', email='[email protected]')
# create client
client = HttpClient()
data = {'username': 'new_name'}
with apply_routes([UpdateConsumers.as_routes(model=User, path='/(?P<pk>\d+)/?', channel_name='test')]):
client.send_and_consume('websocket.connect', {'path': '/{}'.format(obj.pk)})
client.send_and_consume('websocket.receive', {'path': '/{}'.format(obj.pk), 'action': 'update',
'data': json.dumps(data)})
client.consume('test')
user = User.objects.filter(pk=obj.pk).first()
self.assertTrue(user)
self.assertEqual(user.username, 'new_name')
示例6: test_inbound_update
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_inbound_update(self):
user = User.objects.create(username='test', email='[email protected]')
class Demultiplexer(WebsocketDemultiplexer):
mapping = {
'users': 'binding.users',
}
groups = ['inbound']
class UserBinding(WebsocketBinding):
model = User
stream = 'users'
fields = ['username', ]
@classmethod
def group_names(cls, instance, action):
return ['users_outbound']
def has_permission(self, user, action, pk):
return True
with apply_routes([Demultiplexer.as_route(path='/'), route('binding.users', UserBinding.consumer)]):
client = HttpClient()
client.send_and_consume('websocket.connect', path='/')
client.send_and_consume('websocket.receive', path='/', text={
'stream': 'users',
'payload': {'action': UPDATE, 'pk': user.pk, 'data': {'username': 'test_inbound'}}
})
# our Demultiplexer route message to the inbound consumer, so call Demultiplexer consumer
client.consume('binding.users')
user = User.objects.get(pk=user.pk)
self.assertEqual(user.username, 'test_inbound')
self.assertEqual(user.email, '[email protected]')
# trying change field that not in binding fields
client.send_and_consume('websocket.receive', path='/', text={
'stream': 'users',
'payload': {'action': UPDATE, 'pk': user.pk, 'data': {'email': '[email protected]'}}
})
client.consume('binding.users')
user = User.objects.get(pk=user.pk)
self.assertEqual(user.username, 'test_inbound')
self.assertEqual(user.email, '[email protected]')
示例7: test_list_consumers
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_list_consumers(self):
# create object
for i in range(20):
User.objects.create_user(username='test' + str(i), email='[email protected]')
# create client
client = HttpClient()
with apply_routes([ListConsumers.as_routes(model=User, path='/', channel_name='test', paginate_by=10)]):
client.send_and_consume(u'websocket.connect', {'path': '/'})
client.send_and_consume(u'websocket.receive', {'path': '/', 'action': 'list', 'page': 2})
client.consume('test')
rec = client.receive()
res = json.loads(json.loads(rec['text'])['response'])
self.assertEqual(len(res), 10)
self.assertEqual(res[0]['username'], 'test10')
self.assertEqual(res[0]['email'], '[email protected]')
self.assertEqual(res[0]['is_active'], True)
示例8: test_passing_kwargs_and_reply_channel
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_passing_kwargs_and_reply_channel(self):
class Test(Consumers):
path = '^/(?P<slug>[^/]+)/(?P<pk>\d+)/?'
channel_name = 'test'
@consumer(tag='(?P<test>[^/]+)')
def test(this, message, test):
this.reply_channel.send({'test': test, 'kwargs': message.content['_kwargs']['slug'],
'slug': this.kwargs.get('slug', None)})
with apply_routes([Test.as_routes()]):
client = HttpClient()
client.send_and_consume(u'websocket.connect', content={'path': '/name/123/'})
client.send_and_consume(u'websocket.receive', content={'path': '/name/123', 'tag': 'tag'})
client.consume(u'test')
content = client.receive()
self.assertDictEqual(content, {'test': 'tag', 'slug': 'name', 'kwargs': 'name'})
示例9: test_filters_and_routing
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_filters_and_routing(self):
class Test(Consumers):
channel_name = 'test'
mark = 'default'
@consumer(tag='test')
def test(this, message):
this.reply_channel.send({'status': 'ok'})
@consumer('test2', tag='test')
def test2(this, message):
this.reply_channel.send({'status': 'ok', 'mark': this.mark})
with apply_routes([Test.as_routes(), Test.as_routes(channel_name='test3', mark='new')]):
client = HttpClient()
self.assertIsNone(client.send_and_consume(u'test', content={'tag': 'tag'}, fail_on_none=False))
client.send_and_consume(u'test', content={'tag': 'test'})
self.assertDictEqual(client.receive(), {'status': 'ok'})
client.consume('test', fail_on_none=False)
self.assertIsNone(client.receive())
client.send_and_consume(u'test3', content={'tag': 'test'})
self.assertDictEqual(client.receive(), {'status': 'ok'})
client.consume('test3', fail_on_none=False)
self.assertIsNone(client.receive())
client.send_and_consume(u'test2', content={'tag': 'test'})
self.assertDictEqual(client.receive(), {'status': 'ok', 'mark': 'default'})
client.consume('test2', fail_on_none=False)
self.assertIsNone(client.receive())
示例10: test_crud_consumers
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_crud_consumers(self):
# create object
for i in range(20):
User.objects.create_user(username='test' + str(i), email='[email protected]')
# create client
client = HttpClient()
with apply_routes([CRUDConsumers.as_routes(model=User, path='/', channel_name='test', paginate_by=10)]):
client.send_and_consume(u'websocket.connect', {'path': '/'})
client.send_and_consume(u'websocket.receive', {'path': '/', 'action': 'list', 'page': 2})
client.consume('test')
rec = client.receive()
res = json.loads(json.loads(rec['text'])['response'])
self.assertEqual(len(res), 10)
self.assertEqual(res[0]['username'], 'test10')
self.assertEqual(res[0]['email'], '[email protected]')
self.assertEqual(res[0]['is_active'], True)
client.send_and_consume(u'websocket.connect', {'path': '/{}'.format(10)})
client.send_and_consume(u'websocket.receive', {'path': '/{}'.format(10), 'action': 'delete'})
client.consume('test')
self.assertFalse(User.objects.filter(pk=10).exists())
data = {'username': 'new_name'}
client.send_and_consume('websocket.connect', {'path': '/{}'.format(11)})
client.send_and_consume('websocket.receive', {'path': '/{}'.format(11), 'action': 'update',
'data': json.dumps(data)})
client.consume('test')
user = User.objects.filter(pk=11).first()
self.assertTrue(user)
self.assertEqual(user.username, 'new_name')
示例11: test_simple_as_route_method
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_simple_as_route_method(self):
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 = HttpClient()
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')
示例12: test_inbound_create
# 需要导入模块: from channels.tests import HttpClient [as 别名]
# 或者: from channels.tests.HttpClient import consume [as 别名]
def test_inbound_create(self):
self.assertEqual(User.objects.all().count(), 0)
class Demultiplexer(WebsocketDemultiplexer):
mapping = {
'users': 'binding.users',
}
groups = ['inbound']
class UserBinding(WebsocketBinding):
model = User
stream = 'users'
fields = ['username', 'email', 'password', 'last_name']
@classmethod
def group_names(cls, instance, action):
return ['users_outbound']
def has_permission(self, user, action, pk):
return True
with apply_routes([Demultiplexer.as_route(path='/'), route('binding.users', UserBinding.consumer)]):
client = HttpClient()
client.send_and_consume('websocket.connect', path='/')
client.send_and_consume('websocket.receive', path='/', text={
'stream': 'users',
'payload': {'action': CREATE, 'data': {'username': 'test_inbound', 'email': '[email protected]_steam.com'}}
})
# our Demultiplexer route message to the inbound consumer, so call Demultiplexer consumer
client.consume('binding.users')
self.assertEqual(User.objects.all().count(), 1)
user = User.objects.all().first()
self.assertEqual(user.username, 'test_inbound')
self.assertEqual(user.email, '[email protected]_steam.com')
self.assertIsNone(client.receive())