Exemple #1
0
 def test_defaults_none(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'), None)
     self.assertIs(proxy.auth_username, None)
Exemple #2
0
def websocket_connect(url,
                      io_loop=None,
                      callback=None,
                      connect_timeout=None,
                      on_message_callback=None,
                      compression_options=None,
                      ping_interval=None,
                      ping_timeout=None,
                      max_message_size=None):
    """Client-side websocket support.

    Takes a url and returns a Future whose result is a
    `WebSocketClientConnection`.

    ``compression_options`` is interpreted in the same way as the
    return value of `.WebSocketHandler.get_compression_options`.

    The connection supports two styles of operation. In the coroutine
    style, the application typically calls
    `~.WebSocketClientConnection.read_message` in a loop::

        conn = yield websocket_connect(url)
        while True:
            msg = yield conn.read_message()
            if msg is None: break
            # Do something with msg

    In the callback style, pass an ``on_message_callback`` to
    ``websocket_connect``. In both styles, a message of ``None``
    indicates that the connection has been closed.

    .. versionchanged:: 3.2
       Also accepts ``HTTPRequest`` objects in place of urls.

    .. versionchanged:: 4.1
       Added ``compression_options`` and ``on_message_callback``.
       The ``io_loop`` argument is deprecated.

    .. versionchanged:: 4.5
       Added the ``ping_interval``, ``ping_timeout``, and ``max_message_size``
       arguments, which have the same meaning as in `WebSocketHandler`.
    """
    if io_loop is None:
        io_loop = IOLoop.current()
    if isinstance(url, httpclient.HTTPRequest):
        assert connect_timeout is None
        request = url
        # Copy and convert the headers dict/object (see comments in
        # AsyncHTTPClient.fetch)
        request.headers = httputil.HTTPHeaders(request.headers)
    else:
        request = httpclient.HTTPRequest(url, connect_timeout=connect_timeout)
    request = httpclient._RequestProxy(request,
                                       httpclient.HTTPRequest._DEFAULTS)
    conn = WebSocketClientConnection(io_loop,
                                     request,
                                     on_message_callback=on_message_callback,
                                     compression_options=compression_options,
                                     ping_interval=ping_interval,
                                     ping_timeout=ping_timeout,
                                     max_message_size=max_message_size)
    if callback is not None:
        io_loop.add_future(conn.connect_future, callback)
    return conn.connect_future
Exemple #3
0
 def test_bad_attribute(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'), dict())
     with self.assertRaises(AttributeError):
         proxy.foo
Exemple #4
0
 def test_neither_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'), dict())
     self.assertIs(proxy.auth_username, None)
Exemple #5
0
 def test_both_set(self):
     proxy = _RequestProxy(
         HTTPRequest('http://example.com/', proxy_host='foo'),
         dict(proxy_host='bar'))
     self.assertEqual(proxy.proxy_host, 'foo')
Exemple #6
0
 def test_default_set(self):
     proxy = _RequestProxy(HTTPRequest('http://example.com/'),
                           dict(network_interface='foo'))
     self.assertEqual(proxy.network_interface, 'foo')
Exemple #7
0
 def test_request_set(self):
     proxy = _RequestProxy(
         HTTPRequest('http://example.com/', user_agent='foo'), dict())
     self.assertEqual(proxy.user_agent, 'foo')