def test_channel_can_have_sockets_added_to_it(mk):
    mk(('echo.sock.spt', 'channel.send(channel.recv())'))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)

    expected = [socket]
    actual = list(channel)
    assert actual == expected
def test_channel_can_have_sockets_added_to_it():
    mk(('echo.sock', 'channel.send(channel.recv())'))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)

    expected = [socket]
    actual = list(channel)
    assert actual == expected, actual
def test_channel_can_have_sockets_added_to_it(harness):
    harness.fs.www.mk(('echo.sock.spt', 'channel.send(channel.recv())'))
    socket = harness.make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)

    expected = [socket]
    actual = list(channel)
    assert actual == expected
def test_channel_passes_send_on_to_four_sockets():
    mk(('echo.sock', 'channel.send(channel.recv())'))
    channel = Channel('foo', ThreadedBuffer)
    sockets = [make_socket(channel=channel) for i in range(4)]
    channel.send('foo')

    for socket in sockets:
        expected = deque([Message.from_bytes('3::/echo.sock:foo')])
        actual = socket.outgoing.queue
        assert actual == expected, actual
def test_channel_passes_send_on_to_one_socket():
    mk(('echo.sock', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    channel.send('foo')

    expected = deque([Message.from_bytes('3::/echo.sock:foo')])
    actual = socket.outgoing.queue
    assert actual == expected, actual
def test_channel_passes_send_on_to_four_sockets(mk):
    mk(('echo.sock.spt', 'channel.send(channel.recv())'))
    channel = Channel('foo', ThreadedBuffer)
    sockets = [make_socket(channel=channel) for i in range(4)]
    channel.send('foo')

    for socket in sockets:
        expected = deque([Message.from_bytes('3::/echo.sock:foo')])
        actual = socket.outgoing.queue
        assert actual == expected
def test_channel_passes_send_on_to_one_socket(mk):
    mk(('echo.sock.spt', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    channel.send('foo')

    expected = deque([Message.from_bytes('3::/echo.sock:foo')])
    actual = socket.outgoing.queue
    assert actual == expected
示例#8
0
def make_socket(filename='echo.sock', channel=None):
    request = make_request(filename='echo.sock')
    if channel is None:
        channel = Channel(request.line.uri.path.raw, ThreadedBuffer)
    socket = Socket(request, channel)
    return socket
def test_channel_raises_AssertionError_on_double_add():
    mk(('echo.sock', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    assert_raises(AssertionError, channel.add, socket)
def test_channel_raises_AssertionError_on_double_add(mk):
    mk(('echo.sock.spt', ''))
    socket = make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    raises(AssertionError, channel.add, socket)
def test_channel_is_instantiable():
    expected = Channel
    actual = Channel('/foo.sock', ThreadedBuffer).__class__
    assert actual is expected
示例#12
0
def get(request):
    """Takes a Request object and returns a Response or Transport object.

    When we get the request it has socket set to a string, the path part after
    *.sock, which is something like 1/websocket/43ef6fe7?foo=bar.

        1           protocol (we only support 1)
        websocket   transport
        43ef6fe7    socket id (sid)
        ?foo=bar    querystring

    The Socket.IO handshake is a GET request to 1/. We return Response for the
    handshake. After the handshake, subsequent messages are to the full URL as
    above. We return a Transported instance for actual messages.

    """

    # Exit early.
    # ===========

    if request.socket is None:
        return None

    # Parse and validate the socket URL.
    # ==================================

    parts = request.socket.split('/')
    nparts = len(parts)
    if nparts not in (2, 3):
        msg = "Expected 2 or 3 path parts for Socket.IO socket, got %d."
        raise Response(400, msg % nparts)

    protocol = parts[0]
    if protocol != '1':
        msg = "Expected Socket.IO protocol version 1, got %s."
        raise Response(400, msg % protocol)

    # Handshake
    # =========

    if len(parts) == 2:
        path = request.line.uri.path.raw
        if path in __channels__:
            channel = __channels__[path]
        else:
            channel = Channel(path, request.website.network_engine.Buffer)
            __channels__[path] = channel

        socket = Socket(request, channel)
        assert socket.sid not in __sockets__  # sanity check
        __sockets__[socket.sid] = socket
        socket.loop.start()

        return socket.shake_hands()  # a Response

    # More than a handshake.
    # ======================

    transport = parts[1]
    sid = parts[2]

    if transport not in TRANSPORTS:
        msg = "Expected transport in {%s}, got %s."
        msg %= (",".join(TRANSPORTS), transport)
        raise Response(400, msg)

    if sid not in __sockets__:
        msg = "Expected %s in cache, didn't find it"
        raise Response(400, msg % sid)

    if type(__sockets__[sid]) is Socket:
        # This is the first request after a handshake. It's not until this
        # point that we know what transport the client wants to use.
        Transport = XHRPollingTransport  # XXX derp
        __sockets__[sid] = Transport(__sockets__[sid])

    transport = __sockets__[sid]
    return transport
def test_channel_raises_AssertionError_on_double_add(harness):
    harness.fs.www.mk(('echo.sock.spt', ''))
    socket = harness.make_socket()
    channel = Channel('foo', ThreadedBuffer)
    channel.add(socket)
    raises(AssertionError, channel.add, socket)