當前位置: 首頁>>代碼示例>>Python>>正文


Python HttpClient.send_and_consume方法代碼示例

本文整理匯總了Python中channels.tests.HttpClient.send_and_consume方法的典型用法代碼示例。如果您正苦於以下問題:Python HttpClient.send_and_consume方法的具體用法?Python HttpClient.send_and_consume怎麽用?Python HttpClient.send_and_consume使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在channels.tests.HttpClient的用法示例。


在下文中一共展示了HttpClient.send_and_consume方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_model_sub

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_model_sub(self):
        # define consumers
        routes = ModelSubscribeConsumers.as_routes(model=User)
        # create client
        client = HttpClient()

        # create object
        user = User.objects.create_user(username='test', email='[email protected]')

        with apply_routes([routes]):
            # subscribe for Models changes
            client.send_and_consume(u'websocket.connect')

            # change object
            user.username = 'new username'
            user.save()

            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'updated')
            self.assertEqual(res['data']['username'], 'new username')
            self.assertEqual(res['data']['email'], '[email protected]')

            # create new one
            to_del = User.objects.create_user(username='test2', email='[email protected]')
            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'created')
            self.assertEqual(res['data']['username'], 'test2')
            self.assertEqual(res['data']['email'], '[email protected]')

            # delete
            to_del.delete()
            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'deleted')
            self.assertEqual(res['data']['username'], 'test2')
            self.assertEqual(res['data']['email'], '[email protected]')
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:37,代碼來源:test_model_consumers.py

示例2: test_simple_as_route_method

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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')
開發者ID:heshiyou,項目名稱:channels,代碼行數:31,代碼來源:test_generic.py

示例3: test_inbound_delete

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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())
開發者ID:raiderrobert,項目名稱:channels,代碼行數:36,代碼來源:test_binding.py

示例4: test_delete_mixin

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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())
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:14,代碼來源:test_model_consumers.py

示例5: test_create_mixin

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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())
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:14,代碼來源:test_model_consumers.py

示例6: test_object_sub_with_fields

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_object_sub_with_fields(self):
        # create object for subscribe
        sub_object = User.objects.create_user(username='test', email='[email protected]')

        # define consumers
        routes = ObjectSubscribeConsumers.as_routes(path='/(?P<pk>\d+)/?', model=User,
                                                    serializer_kwargs={'fields': ['username', 'is_active']})

        # create client
        client = HttpClient()
        with apply_routes([routes]):
            # subscribe for object changes
            client.send_and_consume(u'websocket.connect', content={'path': '/{}'.format(sub_object.pk)})

            # change sub object
            sub_object.username = 'sub_object'
            sub_object.email = '[email protected]'
            sub_object.save()

            res = json.loads(client.receive()['text'])['data']
            self.assertEqual(res['username'], 'sub_object')
            self.assertEqual(res['is_active'], True)
            self.assertNotIn('email', res)
            self.assertNotIn('is_staff', res)

            sub_object.username = 'test'
            sub_object.is_active = False
            sub_object.save()

            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'updated')
            self.assertNotIn('email', res['data'])
            self.assertNotIn('is_staff', res['data'])

            self.assertEqual(res['data']['username'], 'test')
            self.assertEqual(res['data']['is_active'], False)

            sub_object.username = 'test_new'
            sub_object.save(update_fields=['username'])

            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'updated')
            self.assertEqual(res['data']['username'], 'test_new')
            self.assertNotIn('is_active', res['data'])
            self.assertNotIn('is_staff', res['data'])

            sub_object.email = '[email protected]'
            sub_object.save(update_fields=['email'])

            # check that nothing happened
            self.assertIsNone(client.receive())
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:53,代碼來源:test_model_consumers.py

示例7: test_get_mixin

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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)
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:18,代碼來源:test_model_consumers.py

示例8: test_update_mixin

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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')
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:19,代碼來源:test_model_consumers.py

示例9: test_filters_and_routing

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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())
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:35,代碼來源:test_base.py

示例10: test_list_consumers

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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)
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:20,代碼來源:test_model_consumers.py

示例11: test_passing_kwargs_and_reply_channel

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_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'})
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:21,代碼來源:test_base.py

示例12: test_model_sub_with_fields

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_model_sub_with_fields(self):
        # define consumers
        routes = ModelSubscribeConsumers.as_routes(model=User,
                                                   serializer_kwargs={'fields': ['username']})
        # create client
        client = HttpClient()

        with apply_routes([routes]):
            # subscribe for Models changes
            client.send_and_consume(u'websocket.connect')

            # create object
            User.objects.create_user(username='test', email='[email protected]')

            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'created')
            self.assertEqual(res['data']['username'], 'test')
            self.assertNotIn('is_active', res['data'])
            self.assertNotIn('email', res['data'])
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:21,代碼來源:test_model_consumers.py

示例13: test_object_sub

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_object_sub(self):

        # create object for subscribe
        sub_object = User.objects.create_user(username='test', email='[email protected]')

        # create object without subscribers
        just_object = User.objects.create_user(username='test2', email='[email protected]')

        # define consumers
        routes = ObjectSubscribeConsumers.as_routes(path='/(?P<pk>\d+)/?', model=User)

        # create client
        client = HttpClient()
        with apply_routes([routes]):
            # subscribe for object changes
            client.send_and_consume(u'websocket.connect', content={'path': '/{}'.format(sub_object.pk)})

            # change sub object
            sub_object.username = 'sub_object'
            sub_object.email = '[email protected]'
            sub_object.save()
            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'updated')
            self.assertEqual(res['data']['username'], 'sub_object')
            self.assertEqual(res['data']['email'], '[email protected]')
            self.assertEqual(res['data']['is_staff'], False)

            # change second object
            just_object.email = '[email protected]'
            just_object.save()

            # check that nothing happened
            self.assertIsNone(client.receive())

            # delete
            sub_object.delete()
            just_object.delete()
            res = json.loads(client.receive()['text'])
            self.assertEqual(res['action'], 'deleted')
            self.assertEqual(res['data']['username'], 'sub_object')
            self.assertEqual(res['data']['email'], '[email protected]')
            self.assertEqual(res['data']['is_staff'], False)
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:44,代碼來源:test_model_consumers.py

示例14: test_demultiplexer_with_wrong_payload

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_demultiplexer_with_wrong_payload(self):
        class Demultiplexer(WebsocketDemultiplexer):
            mapping = {
                'users': 'binding.users',
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')

            with self.assertRaises(ValueError) as value_error:
                client.send_and_consume('websocket.receive', path='/', text={
                    'stream': 'users', 'payload': 'test',
                })

            self.assertEqual(value_error.exception.args[0], 'Multiplexed frame payload is not a dict')

            message = client.get_next_message('binding.users')
            self.assertIsNone(message)
開發者ID:raiderrobert,項目名稱:channels,代碼行數:23,代碼來源:test_binding.py

示例15: test_demultiplexer_without_payload_and_steam

# 需要導入模塊: from channels.tests import HttpClient [as 別名]
# 或者: from channels.tests.HttpClient import send_and_consume [as 別名]
    def test_demultiplexer_without_payload_and_steam(self):
        class Demultiplexer(WebsocketDemultiplexer):
            mapping = {
                'users': 'binding.users',
            }

            groups = ['inbound']

        with apply_routes([Demultiplexer.as_route(path='/')]):
            client = HttpClient()
            client.send_and_consume('websocket.connect', path='/')

            with self.assertRaises(ValueError) as value_error:
                client.send_and_consume('websocket.receive', path='/', text={
                    'nostream': 'users', 'payload': 'test',
                })

            self.assertIn('no channel/payload key', value_error.exception.args[0])

            message = client.get_next_message('binding.users')
            self.assertIsNone(message)

            with self.assertRaises(ValueError) as value_error:
                client.send_and_consume('websocket.receive', path='/', text={
                    'stream': 'users',
                })

            self.assertIn('no channel/payload key', value_error.exception.args[0])

            message = client.get_next_message('binding.users')
            self.assertIsNone(message)
開發者ID:raiderrobert,項目名稱:channels,代碼行數:33,代碼來源:test_binding.py


注:本文中的channels.tests.HttpClient.send_and_consume方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。