Example #1
0
    def test11(self):
        conn = stomp.Connection(get_default_host())
        tl = TestListener('123')
        conn.set_listener('', tl)
        conn.start()
        conn.connect(get_default_user(), get_default_password(), wait=True)
        conn.subscribe(destination='/queue/test', ack='auto', id=1)

        conn.send(body='this is a test', destination='/queue/test', receipt='123')

        tl.wait_for_message()

        self.assertTrue(tl.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(tl.messages >= 1, 'should have received at least 1 message')
        self.assertTrue(tl.errors == 0, 'should not have received any errors')

        conn.unsubscribe(destination='/queue/test', id=1)

        wl = WaitingListener('DISCONNECT1')
        conn.set_listener('waiting', wl)

        # stomp1.1 disconnect with receipt
        conn.disconnect(receipt='DISCONNECT1')

        # wait for the receipt
        wl.wait_on_receipt()
Example #2
0
    def start(self, block=True, testing=False):
        logger.info(f"Starting listener with name: {self._listener_id}")
        logger.info(f"Subscribe/Listener auto-generated ID: {self._subscription_id}")

        if testing:
            self.test_listener = TestListener()
            self._connection.set_listener(self._listener_id, self.test_listener)
        else:
            self._connection.set_listener(self._listener_id, TestListener() if testing else self)

        self._connection.start()
        self._connection.connect(**self._connection_configuration)
        self._connection.subscribe(id=self._subscription_id, **self._subscription_configuration)

        if block:
            while True:
                if not self.is_open():
                    logger.info("It is not open. Starting...")
                    self.start(block=False)
                time.sleep(1)

        if block:
            while True:
                # https://stackoverflow.com/a/529052/3899136
                time.sleep(1)
Example #3
0
    def testbasic(self):
        conn = stomp.Connection11(get_rabbitmq_host())
        listener = TestListener('123', print_to_log=True)
        listener2 = WaitingListener('456')
        conn.set_listener('123', listener)
        conn.set_listener('456', listener2)
        conn.connect(get_rabbitmq_user(), get_rabbitmq_password(), wait=True)
        conn.subscribe(destination='/queue/test', id=1, ack='auto')

        conn.send(body='this is a test',
                  destination='/queue/test',
                  receipt='123')
        listener.wait_on_receipt()

        conn.disconnect(receipt='456')
        listener2.wait_on_receipt()

        self.assertTrue(listener.connections == 1,
                        'should have received 1 connection acknowledgement')
        self.assertTrue(listener.messages == 1,
                        'should have received 1 message')
        self.assertTrue(listener.errors == 0,
                        'should not have received any errors')
        self.assertTrue(
            listener.disconnects == 1,
            'should have received 1 disconnect, was %s' % listener.disconnects)
Example #4
0
    def testheartbeat(self):
        conn = stomp.Connection(get_default_host(), heartbeats=(2000, 3000))
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)
        self.assertTrue(conn.heartbeats[0] > 0)
        conn.subscribe(destination='/queue/test', ack='auto', id=1)

        conn.send(body='this is a test',
                  destination='/queue/test',
                  receipt='123')

        listener.wait_for_message()
        conn.disconnect(receipt=None)

        self.assertTrue(
            listener.connections >= 1,
            'should have received 1 connection acknowledgement, was %s' %
            listener.connections)
        self.assertTrue(
            listener.messages >= 1,
            'should have received 1 message, was %s' % listener.messages)
        self.assertTrue(
            listener.errors == 0,
            'should not have received any errors, was %s' % listener.errors)
        self.assertTrue(
            listener.heartbeat_timeouts == 0,
            'should not have received a heartbeat timeout, was %s' %
            listener.heartbeat_timeouts)
    def send_test_message(self, conn):
        listener = TestListener("123", print_to_log=True)
        conn.set_listener("testlistener", listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)

        queuename = "/queue/test1-%s" % self.timestamp
        conn.subscribe(destination=queuename, id=1, ack="auto")
        conn.send(body="this is a test", destination=queuename, receipt="123")
        listener.wait_for_message()
Example #6
0
    def send_test_message(self, conn):
        self.listener = TestListener('123', print_to_log=True)
        conn.set_listener('testlistener', self.listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)

        queuename = '/queue/test1-%s' % self.timestamp
        conn.subscribe(destination=queuename, id=1, ack='auto')
        conn.send(body='this is a test', destination=queuename, receipt='123')
        self.listener.wait_for_message()
Example #7
0
    def test_with_context(self):
        with stomp.Connection(get_default_host()) as conn:
            self.listener = TestListener('123')
            conn.set_listener('', self.listener)
            conn.connect(get_default_user(), get_default_password(), wait=True)

            queuename = '/queue/test1-%s' % self.timestamp
            conn.subscribe(destination=queuename, id=1, ack='auto')
            conn.send(body='this is a test', destination=queuename, receipt='123')
            self.listener.wait_for_message()

        self.assertTrue(self.listener.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(self.listener.disconnects >= 1, 'should have received 1 disconnect')
Example #8
0
    def testbasic(self):
        conn = stomp.Connection11(get_rabbitmq_host())
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.connect(get_rabbitmq_user(), get_rabbitmq_password(), wait=True)
        conn.subscribe(destination='/queue/test', id=1, ack='auto')

        conn.send(body='this is a test', destination='/queue/test', receipt='123')

        listener.wait_on_receipt()
        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(listener.messages == 1, 'should have received 1 message')
        self.assertTrue(listener.errors == 0, 'should not have received any errors')
Example #9
0
    def testheartbeat_timeout(self):
        server = TestStompServer('127.0.0.1', 60000)
        server.start()
        try:
            server.add_frame('''CONNECTED
version: 1.1
session: 1
server: test
heart-beat: 1000,1000\x00''')

            conn = stomp.Connection([('127.0.0.1', 60000)], heartbeats=(1000, 1000))
            listener = TestListener()
            conn.set_listener('', listener)
            conn.start()
            conn.connect()

            time.sleep(5)

            server.running = False
        except Exception:
            _, e, _ = sys.exc_info()
            log.error("Error: %s", e)
        finally:
            server.stop()

        self.assertTrue(listener.heartbeat_timeouts >= 1, 'should have received a heartbeat timeout')
Example #10
0
def connutf16():
    conn = MulticastConnection(encoding="utf-16")
    listener = TestListener("123", print_to_log=True)
    conn.set_listener("testlistener", listener)
    conn.connect()
    yield conn
    conn.disconnect(receipt=None)
Example #11
0
def conn():
    conn = stomp.Connection12(get_default_host())
    listener = TestListener("123", print_to_log=True)
    conn.set_listener("testlistener", listener)
    conn.connect(get_default_user(), get_default_password(), wait=True)
    yield conn
    conn.disconnect(receipt=None)
Example #12
0
        def testheartbeat_shutdown(self):
            server = TestStompServer('127.0.0.1', 60000)
            server.start()
            conn = None
            try:
                server.add_frame('''CONNECTED
    version:1.1
    session:1
    server:test
    heart-beat:1000,1000

    \x00''')

                conn = stomp.Connection([('127.0.0.1', 60000)], heartbeats=(10000, 10000))
                listener = TestListener()
                conn.set_listener('', listener)
                conn.connect()

                start_time = time.time()
                time.sleep(0.5)
                # shutdown connection
                server.stop()
                while conn.heartbeat_thread is not None:
                    time.sleep(0.5)
                end_time = time.time()

                server.running = False
            except Exception:
                _, e, _ = sys.exc_info()
                log.error("Error: %s", e)

            self.assertLessEqual(end_time - start_time, 2, 'should stop immediately and not after heartbeat timeout')
            self.assertIsNone(conn.heartbeat_thread, 'heartbeat thread should have finished')
Example #13
0
    def testbasic(self):
        conn = stomp.Connection11(get_rabbitmq_host())
        listener = TestListener("123")
        conn.set_listener("", listener)
        conn.start()
        conn.connect(get_rabbitmq_user(), get_rabbitmq_password(), wait=True)
        conn.subscribe(destination="/queue/test", id=1, ack="auto")

        conn.send(body="this is a test", destination="/queue/test", receipt="123")

        listener.wait_on_receipt()
        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections == 1, "should have received 1 connection acknowledgement")
        self.assertTrue(listener.messages == 1, "should have received 1 message")
        self.assertTrue(listener.errors == 0, "should not have received any errors")
Example #14
0
 def setUp(self):
     conn = MulticastConnection()
     listener = TestListener('123')
     conn.set_listener('', listener)
     conn.connect()
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #15
0
class TestContext(unittest.TestCase):
    def setUp(self):
        self.timestamp = time.strftime('%Y%m%d%H%M%S')

    def test_with_context(self):
        with stomp.Connection(get_default_host()) as conn:
            self.listener = TestListener('123')
            conn.set_listener('', self.listener)
            conn.connect(get_default_user(), get_default_password(), wait=True)

            queuename = '/queue/test1-%s' % self.timestamp
            conn.subscribe(destination=queuename, id=1, ack='auto')
            conn.send(body='this is a test', destination=queuename, receipt='123')
            self.listener.wait_for_message()

        self.assertTrue(self.listener.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(self.listener.disconnects >= 1, 'should have received 1 disconnect')
Example #16
0
def conn():
    conn = stomp.Connection11(get_rabbitmq_host())
    listener = TestListener("123", print_to_log=True)
    listener2 = WaitingListener("456")
    conn.set_listener("123", listener)
    conn.set_listener("456", listener2)
    conn.connect(get_rabbitmq_user(), get_rabbitmq_password(), wait=True)
    yield conn
Example #17
0
 def setUp(self):
     conn = WebsocketConnection()
     listener = TestListener('123')
     conn.set_listener('', listener)
     conn.connect(get_rabbitmq_user(), get_rabbitmq_password())
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #18
0
 def setUp(self):
     conn = MulticastConnection(encoding='utf-16')
     listener = TestListener('123')
     conn.set_listener('', listener)
     conn.connect(wait=True)
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #19
0
 def test_default_to_localhost(self):
     conn = stomp.Connection()
     listener = TestListener("123", print_to_log=True)
     queuename = "/queue/test1-%s" % listener.timestamp
     conn.set_listener("testlistener", listener)
     conn.connect(get_rabbitmq_user(), get_rabbitmq_password(), wait=True)
     conn.send(body="this is a test", destination=queuename, receipt="123")
     conn.disconnect(receipt=None)
Example #20
0
 def setUp(self):
     conn = stomp.Connection11(get_ipv6_host())
     listener = TestListener('123')
     conn.set_listener('', listener)
     conn.connect('admin', 'password', wait=True)
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #21
0
 def setUp(self):
     conn = stomp.Connection(get_default_host(), auto_decode=True)
     listener = TestListener('123')
     conn.set_listener('', listener)
     conn.connect(get_default_user(), get_default_password(), wait=True)
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #22
0
def conn():
    if not is_inside_travis():
        conn = stomp.Connection11(get_ipv6_host())
        conn.set_listener("testlistener", TestListener("123", print_to_log=True))
        conn.connect("admin", "password", wait=True)
        yield conn
        conn.disconnect(receipt=None)
    else:
        yield None
Example #23
0
 def setUp(self):
     conn = stomp.Connection12(get_default_host(),
                               vhost=get_default_vhost())
     listener = TestListener('123', print_to_log=True)
     conn.set_listener('', listener)
     conn.connect(get_default_user(), get_default_password(), wait=True)
     self.conn = conn
     self.listener = listener
     self.timestamp = time.strftime('%Y%m%d%H%M%S')
Example #24
0
    def testheartbeat(self):
        conn = stomp.Connection(get_default_host(), heartbeats=(2000, 3000))
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.start()
        conn.connect(get_default_user(), get_default_password(), wait=True)
        self.assertTrue(conn.heartbeats[0] > 0)
        conn.subscribe(destination='/queue/test', ack='auto', id=1)

        conn.send(body='this is a test', destination='/queue/test', receipt='123')

        listener.wait_for_message()
        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections >= 1, 'should have received 1 connection acknowledgement, was %s' % listener.connections)
        self.assertTrue(listener.messages >= 1, 'should have received 1 message, was %s' % listener.messages)
        self.assertTrue(listener.errors == 0, 'should not have received any errors, was %s' % listener.errors)
        self.assertTrue(listener.heartbeat_timeouts == 0, 'should not have received a heartbeat timeout, was %s' % listener.heartbeat_timeouts)
Example #25
0
class TestContext(unittest.TestCase):
    def setUp(self):
        self.timestamp = time.strftime('%Y%m%d%H%M%S')

    def send_test_message(self, conn):
        self.listener = TestListener('123', print_to_log=True)
        conn.set_listener('testlistener', self.listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)

        queuename = '/queue/test1-%s' % self.timestamp
        conn.subscribe(destination=queuename, id=1, ack='auto')
        conn.send(body='this is a test', destination=queuename, receipt='123')
        self.listener.wait_for_message()

    def post_test_message(self, conn):
        conn.remove_listener('testlistener')
        pass

    def check_asserts(self):
        self.assertTrue(self.listener.connections == 1,
                        'should have received 1 connection acknowledgement')
        self.assertTrue(self.listener.disconnects >= 1,
                        'should have received 1 disconnect')

    def test_with_context_stomp11(self):
        with stomp.Connection11(get_default_host()) as conn:
            self.send_test_message(conn)
        self.check_asserts()
        self.post_test_message(conn)

    def test_with_context_stomp10(self):
        with stomp.Connection10(get_default_host()) as conn:
            self.send_test_message(conn)
        self.check_asserts()
        self.post_test_message(conn)

    def test_with_context_stomp12(self):
        with stomp.Connection12(get_default_host()) as conn:
            self.send_test_message(conn)
        self.check_asserts()
        self.post_test_message(conn)
Example #26
0
    def testconnect(self):
        conn = stomp.Connection11(get_sni_ssl_host())
        conn.set_ssl(get_sni_ssl_host())
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)
        conn.subscribe(destination='/queue/test', id=1, ack='auto')

        conn.send(body='this is a test',
                  destination='/queue/test',
                  receipt='123')

        listener.wait_on_receipt()
        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections == 1,
                        'should have received 1 connection acknowledgement')
        self.assertTrue(listener.messages == 1,
                        'should have received 1 message')
        self.assertTrue(listener.errors == 0,
                        'should not have received any errors')
Example #27
0
    def testconnect(self):
        conn = stomp.Connection11(get_sni_ssl_host())
        conn.set_ssl(get_sni_ssl_host())
        listener = TestListener(self.receipt_id, print_to_log=True)
        conn.set_listener('', listener)
        conn.connect(get_default_user(), get_default_password(), wait=True)
        conn.subscribe(destination='/queue/test', id=1, ack='auto')

        print('.....sending message with receipt %s' % self.receipt_id)
        conn.send(body='this is a test',
                  destination='/queue/test',
                  receipt=self.receipt_id)

        listener.wait_for_message()
        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections == 1,
                        'should have received 1 connection acknowledgement')
        self.assertTrue(listener.messages >= 1,
                        'should have received 1 message')
        self.assertTrue(listener.errors == 0,
                        'should not have received any errors')
Example #28
0
    def testconnect(self):
        if not is_inside_travis():
            logging.info("Running ipv6 test")
            receipt_id = str(uuid.uuid4())
            conn = stomp.Connection11(get_sni_ssl_host())
            conn.set_ssl(get_sni_ssl_host())
            listener = TestListener(receipt_id, print_to_log=True)
            conn.set_listener('', listener)
            conn.connect(get_default_user(), get_default_password(), wait=True)
            conn.subscribe(destination="/queue/test", id=1, ack="auto")

            logging.info("sending message with receipt %s" % receipt_id)
            conn.send(body="this is a test",
                      destination="/queue/test",
                      receipt=receipt_id)

            listener.wait_for_message()
            conn.disconnect(receipt=None)

            assert listener.connections == 1, "should have received 1 connection acknowledgement"
            assert listener.messages >= 1, "should have received 1 message"
            assert listener.errors == 0, "should not have received any errors"
Example #29
0
    def testbasic(self):
        conn = stomp.Connection10(get_stompserver_host())
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.connect(wait=True)
        conn.subscribe(destination='/queue/test', ack='auto')

        conn.send(body='this is a test',
                  destination='/queue/test',
                  receipt='123')

        listener.wait_on_receipt()

        conn.unsubscribe('/queue/test')

        conn.disconnect(receipt=None)

        self.assertTrue(listener.connections == 1,
                        'should have received 1 connection acknowledgement')
        self.assertTrue(listener.messages == 1,
                        'should have received 1 message')
        self.assertTrue(listener.errors == 0,
                        'should not have received any errors')
Example #30
0
    def test_timeout(self):
        server = TestStompServer('127.0.0.1', 60000)
        try:
            server.start()

            server.add_frame('''ERROR
message: connection failed\x00''')

            conn = stomp.Connection12([('127.0.0.1', 60000)])
            listener = TestListener()
            conn.set_listener('', listener)
            try:
                conn.connect(wait=True)
                self.fail("shouldn't happen")
            except exception.ConnectFailedException:
                pass
        finally:
            server.stop()
Example #31
0
    def test_ssl_connection(self):
        listener = TestListener("123", print_to_log=True)
        try:
            import ssl
            queuename = "/queue/test4-%s" % listener.timestamp
            conn = stomp.Connection(get_default_ssl_host())
            conn.set_ssl(get_default_ssl_host())
            conn.set_listener("testlistener", listener)
            conn.connect(get_default_user(), get_default_password(), wait=True)
            conn.subscribe(destination=queuename, id=1, ack="auto")

            conn.send(body="this is a test", destination=queuename, receipt="123")

            listener.wait_on_receipt()
            listener.wait_for_message()

            conn.disconnect(receipt=None)

            assert conn.get_ssl() is not None
            assert listener.connections == 1, "should have received 1 connection acknowledgement"
            assert listener.messages == 1, "should have received 1 message"
            assert listener.errors == 0, "should not have received any errors"
        except ImportError:
            pass
 def __init__(self, conn):
     TestListener.__init__(self, '123')
     self.conn = conn
Example #33
0
    def test_parsing(self):
        def pump(n):
            # pump; test server gives us one frame per received something
            for _ in range(n):
                conn.transport.send(b'\n')
                time.sleep(0.01)

        # Trailing optional EOLs in a frame

        self.server.add_frame('''CONNECTED
version:1.1
session:1
server:test
heart-beat:1000,1000

\x00''' + '\n\n\n')
        expected_heartbeat_count = 0

        conn = stomp.Connection([('127.0.0.1', 60000)])
        listener = TestListener()
        conn.set_listener('', listener)
        conn.connect()

        time.sleep(2)

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)

        # No trailing EOLs, separate heartbeat

        message_body = 'Hello\n...world!'
        message_frame = '''MESSAGE
content-type:text/plain

%s\x00''' % message_body

        self.server.add_frame(message_frame)
        self.server.add_frame('\n')
        expected_heartbeat_count += 1

        pump(2)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # Trailing EOL, separate heartbeat, another message

        self.server.add_frame(message_frame + '\n')
        self.server.add_frame('\n')
        self.server.add_frame(message_frame + '\n')
        expected_heartbeat_count += 1

        pump(3)

        listener.wait_for_heartbeat()
        listener.wait_for_message()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # Torture tests: return content one byte at a time

        self.server.add_frame('\n')
        for c in message_frame:
            self.server.add_frame(c)
        self.server.add_frame('\n')
        expected_heartbeat_count += 2

        pump(len(message_frame) + 2)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # ...and a similar one with content-length and null bytes in body

        message_body = '%s\x00\x00%s' % (message_body, message_body)
        message_frame = '''MESSAGE
content-type:text/plain
content-length:%s

%s\x00''' % (len(message_body), message_body)

        self.server.add_frame('\n')
        self.server.add_frame('\n')
        for c in message_frame:
            self.server.add_frame(c)
        self.server.add_frame('\n')
        expected_heartbeat_count += 3

        pump(len(message_frame) + 3)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual(
            {
                "content-type": "text/plain",
                "content-length": str(len(message_body)),
            }, headers)
        self.assertEqual(message_body, body)
Example #34
0
    def test_disconnect(self):
        self.server.add_frame('''CONNECTED
version:1.1
session:1
server:test
heart-beat:1000,1000

\x00''')

        conn = stomp.Connection([('127.0.0.1', 60000)])
        listener = TestListener()
        conn.set_listener('', listener)
        conn.connect()

        time.sleep(2)

        self.server.stop()

        for _ in range(100):
            if self.server.stopped:
                break
            time.sleep(0.1)
        else:
            assert False, 'server never disconnected'

        time.sleep(1)

        try:
            conn.send(body='test disconnect',
                      destination='/test/disconnectqueue')
            self.fail(
                'Should not have successfully sent a message at this point')
        except Exception:
            _, e, _ = sys.exc_info()
            if e.__class__ == AssertionError:
                self.fail(str(e))
            log.debug('stopping conn after expected exception %s', e)
            # lost connection, now restart the server
            try:
                conn.disconnect(receipt=None)
            except:
                pass

            time.sleep(2)

            self.server.add_frame('''CONNECTED
version:1.1
session:1
server:test
heart-beat:1000,1000

\x00''')

            self.server.start()

            conn.connect()

            time.sleep(5)

        self.assertTrue(listener.connections >= 2,
                        'should have received 2 connection acknowledgements')
Example #35
0
    def test_parsing(self):

        def pump(n):
            # pump; test server gives us one frame per received something
            for _ in range(n):
                conn.transport.send(b'\n')
                time.sleep(0.01)

        # Trailing optional EOLs in a frame

        self.server.add_frame('''CONNECTED
version:1.1
session:1
server:test
heart-beat:1000,1000

\x00''' + '\n\n\n')
        expected_heartbeat_count = 0

        conn = stomp.Connection([('127.0.0.1', 60000)])
        listener = TestListener()
        conn.set_listener('', listener)
        conn.start()
        conn.connect()

        time.sleep(2)

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)

        # No trailing EOLs, separate heartbeat

        message_body = 'Hello\n...world!'
        message_frame = '''MESSAGE
content-type:text/plain

%s\x00''' % message_body

        self.server.add_frame(message_frame)
        self.server.add_frame('\n')
        expected_heartbeat_count += 1

        pump(2)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # Trailing EOL, separate heartbeat, another message

        self.server.add_frame(message_frame + '\n')
        self.server.add_frame('\n')
        self.server.add_frame(message_frame + '\n')
        expected_heartbeat_count += 1

        pump(3)

        listener.wait_for_heartbeat()
        listener.wait_for_message()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # Torture tests: return content one byte at a time

        self.server.add_frame('\n')
        for c in message_frame:
            self.server.add_frame(c)
        self.server.add_frame('\n')
        expected_heartbeat_count += 2

        pump(len(message_frame) + 2)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({"content-type": "text/plain"}, headers)
        self.assertEqual(message_body, body)

        # ...and a similar one with content-length and null bytes in body

        message_body = '%s\x00\x00%s' % (message_body, message_body)
        message_frame = '''MESSAGE
content-type:text/plain
content-length:%s

%s\x00''' % (len(message_body), message_body)

        self.server.add_frame('\n')
        self.server.add_frame('\n')
        for c in message_frame:
            self.server.add_frame(c)
        self.server.add_frame('\n')
        expected_heartbeat_count += 3

        pump(len(message_frame) + 3)

        listener.wait_for_heartbeat()
        headers, body = listener.get_latest_message()

        self.assertEqual(expected_heartbeat_count, listener.heartbeat_count)
        self.assertEqual({
            "content-type": "text/plain",
            "content-length": str(len(message_body)),
        }, headers)
        self.assertEqual(message_body, body)
 def __init__(self, conn):
     TestListener.__init__(self, '123')
     self.conn = conn