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


Python tests.HttpClient類代碼示例

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


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

示例1: test_trigger_outbound_delete

    def test_trigger_outbound_delete(self):
        class TestBinding(WebsocketBinding):
            model = User
            stream = 'test'
            fields = ['username']

            def group_names(self, instance, action):
                return ["users3"]

            def has_permission(self, user, action, pk):
                return True

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

        with apply_routes([route('test', TestBinding.consumer)]):
            client = HttpClient()
            client.join_group('users3')

            user.delete()

            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('model' in received['payload'])
            self.assertTrue('pk' in received['payload'])

            self.assertEqual(received['payload']['action'], 'delete')
            self.assertEqual(received['payload']['model'], 'auth.user')
            self.assertEqual(received['payload']['pk'], 1)
            self.assertEqual(received['payload']['data']['username'], 'test')

            received = client.receive()
            self.assertIsNone(received)
開發者ID:KIKUYA-Takumi,項目名稱:channels,代碼行數:35,代碼來源:test_binding.py

示例2: test_demultiplexer_without_payload_and_steam

    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,代碼行數:31,代碼來源:test_binding.py

示例3: test_inbound_delete

    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,代碼行數:34,代碼來源:test_binding.py

示例4: test_trigger_outbound_update

    def test_trigger_outbound_update(self):
        class TestBinding(WebsocketBinding):
            model = User
            stream = 'test'
            fields = ['__all__']

            @classmethod
            def group_names(cls, instance, action):
                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='[email protected]')
        consumer_finished.send(sender=None)

        with apply_routes([route('test', TestBinding.consumer)]):
            client = HttpClient()
            client.join_group('users2')

            user.username = 'test_new'
            user.save()

            consumer_finished.send(sender=None)
            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'])

            self.assertEqual(received['payload']['action'], 'update')
            self.assertEqual(received['payload']['model'], 'auth.user')
            self.assertEqual(received['payload']['pk'], user.pk)

            self.assertEqual(received['payload']['data']['email'], '[email protected]')
            self.assertEqual(received['payload']['data']['username'], 'test_new')
            self.assertEqual(received['payload']['data']['password'], '')
            self.assertEqual(received['payload']['data']['last_name'], '')

            received = client.receive()
            self.assertIsNone(received)
開發者ID:linuxlewis,項目名稱:channels,代碼行數:47,代碼來源:test_binding.py

示例5: test_model_sub_with_fields

    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,代碼行數:19,代碼來源:test_model_consumers.py

示例6: test_websockets_demultiplexer

    def test_websockets_demultiplexer(self):

        class MyWebsocketConsumer(websockets.JsonWebsocketConsumer):
            def connect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def disconnect(self, message, multiplexer=None, **kwargs):
                multiplexer.send(kwargs)

            def receive(self, content, multiplexer=None, **kwargs):
                multiplexer.send(content)

        class Demultiplexer(websockets.WebsocketDemultiplexer):

            consumers = {
                "mystream": MyWebsocketConsumer
            }

        with apply_routes([route_class(Demultiplexer, path='/path/(?P<id>\d+)')]):
            client = HttpClient()

            client.send_and_consume('websocket.connect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })

            client.send_and_consume('websocket.receive', text={
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            }, path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"text_field": "mytext"},
            })

            client.send_and_consume('websocket.disconnect', path='/path/1')
            self.assertEqual(client.receive(), {
                "stream": "mystream",
                "payload": {"id": "1"},
            })
開發者ID:heshiyou,項目名稱:channels,代碼行數:41,代碼來源:test_generic.py

示例7: test_model_sub

    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,代碼行數:35,代碼來源:test_model_consumers.py

示例8: test_websocket_demultiplexer_send

    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<id>\d+)')]):
            client = HttpClient()

            with self.assertRaises(SendNotAvailableOnDemultiplexer):
                client.send_and_consume('websocket.receive', path='/path/1', text={
                    "stream": "mystream",
                    "payload": {"text_field": "mytext"},
                })

                client.receive()
開發者ID:heshiyou,項目名稱:channels,代碼行數:22,代碼來源:test_generic.py

示例9: test_create_mixin

    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,代碼行數:12,代碼來源:test_model_consumers.py

示例10: test_delete_mixin

    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,代碼行數:12,代碼來源:test_model_consumers.py

示例11: test_dynamic_channels_names

    def test_dynamic_channels_names(self):

        class Test(Consumers):

            channel_name = 'test1'

            @consumer
            def test(self, message):
                return self.channel_name

        test = Test.as_routes()
        test_2 = Test.as_routes(channel_name='test')
        client = HttpClient()

        with apply_routes([test]):
            client.send_and_consume(u'websocket.receive')

        with apply_routes([test_2]):
            client.send_and_consume(u'websocket.receive')
        channel_layer = channel_layers[DEFAULT_CHANNEL_LAYER]

        self.assertEqual(len(channel_layer._channels), 3, channel_layer._channels.keys())
        self.assertIn('test', channel_layer._channels.keys())
開發者ID:Krukov,項目名稱:cbchennels,代碼行數:23,代碼來源:test_base.py

示例12: test_object_sub_with_subs_first

    def test_object_sub_with_subs_first(self):
        # 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('1')})

            # create object
            User.objects.create_user(username='test', email='[email protected]')
            res = json.loads(client.receive()['text'])
            self.assertTrue('data' in res.keys())
            self.assertTrue('action' in res.keys())
            self.assertTrue(res['action'] == 'created')
            data = res['data']
            self.assertEqual(data['username'], 'test')
            self.assertEqual(data['is_active'], True)
            self.assertEqual(data['email'], '[email protected]')
            self.assertEqual(data['is_staff'], False)

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

示例13: test_get_mixin

    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,代碼行數:16,代碼來源:test_model_consumers.py

示例14: test_object_sub_with_fields

    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,代碼行數:51,代碼來源:test_model_consumers.py

示例15: test_list_consumers

    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,代碼行數:18,代碼來源:test_model_consumers.py


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