Пример #1
0
    def test_websockets_consumer(self):
        #  Create a channels test client
        client = Client()

        #  Send a websocket.connect message, passing in the path so it maps to the right consumer via the routing in routing.py
        #  Consume portion maps that message to a consumer and runs and returns an instance of the consumer
        connect_consumer = client.send_and_consume('websocket.connect',
                                                   {'path': '/game/123/'})
        connect_reply = client.receive(
        )  # receive() grabs the content of the next message off of the client's reply_channel
        self.assertEqual(
            connect_reply,
            {'accept': True
             })  # websocket.connect should return acceptance of connection

        receive_consumer = client.send_and_consume('websocket.receive', {
            'path': '/game/123/',
            'text': 'text'
        })
        receive_reply = client.receive(
        )  # receive() grabs the content of the next message off of the client's reply_channel
        self.assertEqual(receive_reply, {'text': 'textreply'})

        receive_consumer.group_send(
            '123', text='grouptext'
        )  # This sends a message out to a group - shortcut off of the Websocket Consumer
        group_reply = client.receive(
        )  # receive() grabs the content of the next message off of the client's reply_channel
        self.assertEqual(group_reply, {'text': 'grouptext'})

        disconnect_consumer = client.send_and_consume('websocket.disconnect',
                                                      {'path': '/game/123/'})
        disconnect_consumer.close()
Пример #2
0
    def test_as_route_method(self):
        class WebsocketConsumer(BaseConsumer):
            trigger = 'new'

            def test(self, message, **kwargs):
                self.message.reply_channel.send({'trigger': self.trigger})

        method_mapping = {'mychannel': 'test'}

        with apply_routes([
            WebsocketConsumer.as_route(
                {'method_mapping': method_mapping, 'trigger': 'from_as_route'},
                name='filter',
            ),
        ]):
            client = Client()

            client.send_and_consume('mychannel', {'name': 'filter'})
            self.assertEqual(client.receive(), {'trigger': 'from_as_route'})
Пример #3
0
    def test_as_route_method(self):
        class WebsocketConsumer(BaseConsumer):
            trigger = 'new'

            def test(self, message, **kwargs):
                self.message.reply_channel.send({'trigger': self.trigger})

        method_mapping = {'mychannel': 'test'}

        with apply_routes([
            WebsocketConsumer.as_route(
                {'method_mapping': method_mapping, 'trigger': 'from_as_route'},
                name='filter',
            ),
        ]):
            client = Client()

            client.send_and_consume('mychannel', {'name': 'filter'})
            self.assertEqual(client.receive(), {'trigger': 'from_as_route'})