# Path: sio/protocol/rpc.py
# class TimeoutError(Exception):
# class ProtocolError(Exception):
# class RemoteError(Exception):
# class NoSuchMethodError(RemoteError):
# class WorkerRPC(NetstringReceiver):
#     def __init__(self, err, tb=None, uid=None):
#     def __init__(self, err=None, uid=None):
# def makeRemoteException(msg, uid=None):
#     def __init__(self, server=False, timeout=DEFAULT_TIMEOUT):
#     def connectionMade(self):
#     def connectionLost(self, reason):
#     def _processMessage(self, msg):
#     def getHelloData(self):
#     def stringReceived(self, string):
#     def _reply(self, value, request=None):
#     def _replyError(self, err, request=None):
#     def _timeout(self, rid):
#     def sendMsg(self, msg_type, **kwargs):
#     def call(self, cmd, *args, **kwargs):
#         def cb(ignore):
#     MAX_LENGTH = 2 ** 20  # 1MB should be enough
#     DEFAULT_TIMEOUT = 30
# 
# Path: sio/workers/util.py
# def json_dumps(obj, **kwargs):
#     """Python 3 and 2 compatible json.dump."""
#     kwargs.setdefault('cls', CompatibleJSONEncoder)
#     return json.dumps(obj, **kwargs)


from twisted.trial import unittest
from twisted.test import proto_helpers
from twisted.internet import protocol, reactor
from sio.protocol import rpc
from sio.workers.util import json_dumps
import json
    protocol = TestServer


def encode(x):
    x = json_dumps(x).encode('utf-8')
    return b''.join([str(len(x)).encode('utf-8'), b':', x, b','])


def decode(x):
    # This is a bit hacky, but we only care about the data part -
    # encoding netstrings is handled by Twisted and should work
    data = x.partition(b':')[2][:-1].decode('utf-8')
    return json.loads(data)


hello_msg = {'type': 'hello', 'data': {}}

hello_ack_msg = {'type': 'hello_ack'}


class ServerTestCase(unittest.TestCase):
    def setUp(self):
        factory = TestServerFactory()
        self.proto = factory.buildProtocol(('127.0.0.1', 0))
        self.tr = proto_helpers.StringTransport()
        self.proto.makeConnection(self.tr)

    def _hello(self):
        self.proto.dataReceived(encode(hello_msg))
        self.assertEqual(decode(self.tr.value()), hello_ack_msg)

# Path: sio/protocol/rpc.py
# class TimeoutError(Exception):
# class ProtocolError(Exception):
# class RemoteError(Exception):
# class NoSuchMethodError(RemoteError):
# class WorkerRPC(NetstringReceiver):
#     def __init__(self, err, tb=None, uid=None):
#     def __init__(self, err=None, uid=None):
# def makeRemoteException(msg, uid=None):
#     def __init__(self, server=False, timeout=DEFAULT_TIMEOUT):
#     def connectionMade(self):
#     def connectionLost(self, reason):
#     def _processMessage(self, msg):
#     def getHelloData(self):
#     def stringReceived(self, string):
#     def _reply(self, value, request=None):
#     def _replyError(self, err, request=None):
#     def _timeout(self, rid):
#     def sendMsg(self, msg_type, **kwargs):
#     def call(self, cmd, *args, **kwargs):
#         def cb(ignore):
#     MAX_LENGTH = 2 ** 20  # 1MB should be enough
#     DEFAULT_TIMEOUT = 30
# 
# Path: sio/workers/util.py
# def json_dumps(obj, **kwargs):
#     """Python 3 and 2 compatible json.dump."""
#     kwargs.setdefault('cls', CompatibleJSONEncoder)
#     return json.dumps(obj, **kwargs)


# Path: sio/protocol/tests.py
from twisted.trial import unittest
from twisted.test import proto_helpers
from twisted.internet import protocol, reactor
from sio.protocol import rpc
from sio.workers.util import json_dumps
import json

    protocol = TestServer


def encode(x):
    x = json_dumps(x).encode('utf-8')
    return b''.join([str(len(x)).encode('utf-8'), b':', x, b','])


def decode(x):
    # This is a bit hacky, but we only care about the data part -
    # encoding netstrings is handled by Twisted and should work
    data = x.partition(b':')[2][:-1].decode('utf-8')
    return json.loads(data)


hello_msg = {'type': 'hello', 'data': {}}

hello_ack_msg = {'type': 'hello_ack'}


class ServerTestCase(unittest.TestCase):
    def setUp(self):
        factory = TestServerFactory()
        self.proto = factory.buildProtocol(('127.0.0.1', 0))
        self.tr = proto_helpers.StringTransport()
        self.proto.makeConnection(self.tr)

    def _hello(self):
        self.proto.dataReceived(encode(hello_msg))
        self.assertEqual(decode(self.tr.value()), hello_ack_msg)

        self.assertEqual(self.proto.state, rpc.State.established)