class TestSameOrigin(DragonTestCaseAsync):
    def test_same_origin(self):
        settings.DRAGON_URL = self.host
        settings.SWAMP_DRAGON_SAME_ORIGIN = True

        response = self.fetch("/settings.js")
        cookie = SimpleCookie(response.headers["Set-Cookie"])
        request = ConnectionInfo(
            "127.0.0.1", cookies=cookie, arguments={}, headers={}, path="/data/983/9cz4ridg/websocket"
        )
        session = TestSession()
        self.connection = SubscriberConnection(session)
        self.connection.on_open(request)

        self.assertFalse(self.connection.is_closed)
Exemple #2
0
    def test_same_origin(self):
        settings.DRAGON_URL = self.host
        settings.SWAMP_DRAGON_SAME_ORIGIN = True

        response = self.fetch('/settings.js')
        cookie = SimpleCookie(response.headers['Set-Cookie'])
        request = ConnectionInfo('127.0.0.1',
                                 cookies=cookie,
                                 arguments={},
                                 headers={},
                                 path='/data/983/9cz4ridg/websocket')
        session = TestSession()
        self.connection = SubscriberConnection(session)
        self.connection.on_open(request)

        self.assertFalse(self.connection.is_closed)
 def setUp(self):
     self.session = TestSession()
     self.connection = SubscriberConnection(self.session)
class TestSubscriberConnection(DragonTestCase):
    def setUp(self):
        self.session = TestSession()
        self.connection = SubscriberConnection(self.session)

    def test_on_close(self):
        """
        Closing a connection should automatically unsubscribe
        from all channels
        """
        self.connection.on_open(None)
        self.connection.pub_sub.subscribe(["test-channel"], self.connection)
        self.assertIn(self.connection, self.connection.pub_sub.publisher.subscribers["test-channel"])
        self.connection.on_close()
        self.assertNotIn("test-channel", self.connection.pub_sub.publisher.subscribers)

    def test_on_message(self):
        route_handler.register(TestRouter)
        data = {"verb": "say_hello", "route": TestRouter.get_name()}
        self.connection.on_message(data)
        self.assertTrue(self.connection.hello_said)

    def test_on_message_invalid_verb(self):
        route_handler.register(TestRouter)
        data = {"verb": "invalid_verb", "route": TestRouter.get_name()}
        with self.assertRaises(UnexpectedVerbException):
            self.connection.on_message(data)
        self.assertTrue(self.connection.is_closed)

    def test_string_to_json(self):
        data = '{"key": "val"}'
        self.assertDictEqual({"key": "val"}, self.connection.to_json(data))

    def test_to_json_plain_text(self):
        expected = {"message": "hello"}
        self.assertDictEqual(expected, self.connection.to_json("hello"))

    def test_dict_to_json(self):
        """
        If the message received is a dictionary simply return it
        """
        expected = {"key": "value"}
        actual = self.connection.to_json(expected)
        self.assertEqual(expected, actual)
class TestSubscriberConnection(DragonTestCase):
    def setUp(self):
        self.session = TestSession()
        self.connection = SubscriberConnection(self.session)

    def test_on_close(self):
        """
        Closing a connection should automatically unsubscribe
        from all channels
        """
        request = ConnectionInfo('127.0.0.1', cookies={}, arguments={}, headers={}, path='/data/983/9cz4ridg/websocket')
        self.connection.on_open(request)
        self.connection.pub_sub.subscribe(['test-channel'], self.connection)
        self.assertIn(self.connection, self.connection.pub_sub.publisher.subscribers['test-channel'])
        self.connection.on_close()
        self.assertNotIn('test-channel', self.connection.pub_sub.publisher.subscribers)

    def test_on_message(self):
        route_handler.register(TestRouter)
        data = {'verb': 'say_hello', 'route': TestRouter.get_name()}
        self.connection.on_message(data)
        self.assertTrue(self.connection.hello_said)

    def test_on_message_invalid_verb(self):
        route_handler.register(TestRouter)
        data = {'verb': 'invalid_verb', 'route': TestRouter.get_name()}
        with self.assertRaises(UnexpectedVerbException):
            self.connection.on_message(data)
        self.assertTrue(self.connection.is_closed)

    def test_string_to_json(self):
        data = '{"key": "val"}'
        self.assertDictEqual({'key': 'val'}, self.connection.to_json(data))

    def test_to_json_plain_text(self):
        expected = {'message': 'hello'}
        self.assertDictEqual(expected, self.connection.to_json('hello'))

    def test_dict_to_json(self):
        """
        If the message received is a dictionary simply return it
        """
        expected = {'key': 'value'}
        actual = self.connection.to_json(expected)
        self.assertEqual(expected, actual)
 def setUp(self):
     django_settings.SWAMP_DRAGON_MIDDLEWARE_CLASSES = ['swampdragon.mock_middleware.TestMiddleware']
     self.session = TestSession()
     self.connection = SubscriberConnection(self.session)
 def setUp(self):
     self.session = TestSession()
     self.connection = SubscriberConnection(self.session)
class TestSubscriberConnection(DragonTestCase):
    def setUp(self):
        self.session = TestSession()
        self.connection = SubscriberConnection(self.session)

    def test_on_close(self):
        """
        Closing a connection should automatically unsubscribe
        from all channels
        """
        request = ConnectionInfo('127.0.0.1',
                                 cookies={},
                                 arguments={},
                                 headers={},
                                 path='/data/983/9cz4ridg/websocket')
        self.connection.on_open(request)
        self.connection.pub_sub.subscribe(['test-channel'], self.connection)
        self.assertIn(
            self.connection,
            self.connection.pub_sub.publisher.subscribers['test-channel'])
        self.connection.on_close()
        self.assertNotIn('test-channel',
                         self.connection.pub_sub.publisher.subscribers)

    def test_on_message(self):
        route_handler.register(TestRouter)
        data = {'verb': 'say_hello', 'route': TestRouter.get_name()}
        self.connection.on_message(data)
        self.assertTrue(self.connection.hello_said)

    def test_on_message_invalid_verb(self):
        route_handler.register(TestRouter)
        data = {'verb': 'invalid_verb', 'route': TestRouter.get_name()}
        with self.assertRaises(UnexpectedVerbException):
            self.connection.on_message(data)
        self.assertTrue(self.connection.is_closed)

    def test_string_to_json(self):
        data = '{"key": "val"}'
        self.assertDictEqual({'key': 'val'}, self.connection.to_json(data))

    def test_to_json_plain_text(self):
        expected = {'message': 'hello'}
        self.assertDictEqual(expected, self.connection.to_json('hello'))

    def test_dict_to_json(self):
        """
        If the message received is a dictionary simply return it
        """
        expected = {'key': 'value'}
        actual = self.connection.to_json(expected)
        self.assertEqual(expected, actual)