def test_disconnect(self): self.assertEqual(commands.disconnect(), StompFrame(StompSpec.DISCONNECT)) self.assertEqual( commands.disconnect(receipt='4711'), StompFrame(StompSpec.DISCONNECT, {StompSpec.RECEIPT_HEADER: '4711'}))
def test_encoding(self): key = u'fen\xeatre' value = u'\xbfqu\xe9 tal?, s\xfc\xdf' command = StompSpec.DISCONNECT message = { 'command': command, 'headers': { key: value }, 'version': StompSpec.VERSION_1_1 } frame = StompFrame(**message) self.assertEquals(message['headers'], frame.headers) self.assertEquals(dict(frame), message) self.assertEquals(eval(repr(frame)), frame) frame.version = StompSpec.VERSION_1_1 self.assertEquals(eval(repr(frame)), frame) self.assertEquals( str(frame), codecs.lookup('utf-8').encode(command + u'\n' + key + u':' + value + u'\n\n\x00')[0]) otherFrame = StompFrame(**message) self.assertEquals(frame, otherFrame) frame.version = StompSpec.VERSION_1_0 self.assertRaises(UnicodeEncodeError, frame.__str__)
def test_encoding(self): key = b'fen\xc3\xaatre'.decode('utf-8') value = b'\xc2\xbfqu\xc3\xa9 tal?'.decode('utf-8') command = StompSpec.DISCONNECT message = { 'command': command, 'headers': { key: value }, 'version': StompSpec.VERSION_1_1 } frame = StompFrame(**message) self.assertEqual(message['headers'], frame.headers) self.assertEqual(dict(frame), message) self.assertEqual(eval(repr(frame)), frame) frame.version = StompSpec.VERSION_1_1 self.assertEqual(eval(repr(frame)), frame) expectedResult = (command + '\n' + key + ':' + value + '\n\n\x00').encode('utf-8') self.assertEqual(binaryType(frame), expectedResult) otherFrame = StompFrame(**message) self.assertEqual(frame, otherFrame) frame.version = StompSpec.VERSION_1_0 self.assertRaises(UnicodeEncodeError, binaryType, frame)
def _test_4_integration_stomp(self, version): client = Stomp(self.getConfig(version)) try: client.connect(host=VIRTUALHOST, versions=[version]) except StompProtocolError as e: print('Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version)) return client.send(self.DESTINATION, b'test message 1') client.send(self.DESTINATION, b'test message 2') self.assertFalse(client.canRead(self.TIMEOUT)) token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}) self.assertTrue(client.canRead(self.TIMEOUT)) client.ack(client.receiveFrame()) self.assertTrue(client.canRead(self.TIMEOUT)) client.ack(client.receiveFrame()) self.assertFalse(client.canRead(self.TIMEOUT)) client.unsubscribe(token) client.send(self.DESTINATION, b'test message 3', receipt='4711') self.assertTrue(client.canRead(self.TIMEOUT)) self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'})) self.assertFalse(client.canRead(self.TIMEOUT)) client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}) self.assertTrue(client.canRead(self.TIMEOUT)) client.ack(client.receiveFrame()) self.assertFalse(client.canRead(self.TIMEOUT)) client.disconnect(receipt='4712') self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712'})) self.assertRaises(StompConnectionError, client.receiveFrame) client.connect(host=VIRTUALHOST) client.disconnect(receipt='4711') self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'})) client.close() self.assertRaises(StompConnectionError, client.canRead, 0)
def test_session_transaction(self): session = StompSession(check=False) transaction = session.transaction() headers = { StompSpec.TRANSACTION_HEADER: transaction, StompSpec.RECEIPT_HEADER: 'bla' } frame = session.begin(transaction, receipt='bla') self.assertEqual(frame, commands.begin(transaction, receipt='bla')) self.assertEqual(frame, StompFrame(StompSpec.BEGIN, headers)) headers.pop(StompSpec.RECEIPT_HEADER) self.assertRaises(StompProtocolError, session.begin, transaction) frame = session.abort(transaction) self.assertEqual(frame, commands.abort(transaction)) self.assertEqual(frame, StompFrame(StompSpec.ABORT, headers)) self.assertRaises(StompProtocolError, session.abort, transaction) self.assertRaises(StompProtocolError, session.commit, transaction) transaction = session.transaction(4711) headers = {StompSpec.TRANSACTION_HEADER: '4711'} frame = session.begin(transaction) self.assertEqual(frame, commands.begin(transaction)) self.assertEqual(frame, StompFrame(StompSpec.BEGIN, headers)) frame = session.commit(transaction) self.assertEqual(frame, commands.commit(transaction)) self.assertEqual(frame, StompFrame(StompSpec.COMMIT, headers)) self.assertRaises(StompProtocolError, session.commit, transaction) self.assertRaises(StompProtocolError, session.abort, transaction) session = StompSession() self.assertRaises(StompProtocolError, session.begin, 4711) self.assertRaises(StompProtocolError, session.abort, None) self.assertRaises(StompProtocolError, session.commit, None)
def test_3_socket_failure_and_replay(self): client = Stomp(self.getConfig(StompSpec.VERSION_1_0)) client.connect(host=VIRTUALHOST) headers = {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL} token = client.subscribe(self.DESTINATION, headers) client.sendFrame( StompFrame(StompSpec.DISCONNECT) ) # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect self.assertRaises(StompConnectionError, client.receiveFrame) client.connect(host=VIRTUALHOST) client.send(self.DESTINATION, 'test message 1') client.ack(client.receiveFrame()) client.unsubscribe(token) headers = { StompSpec.ID_HEADER: 'bla', StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL } client.subscribe(self.DESTINATION, headers) headers[StompSpec.DESTINATION_HEADER] = self.DESTINATION client.sendFrame( StompFrame(StompSpec.DISCONNECT) ) # DISCONNECT frame is out-of-band, as far as the session is concerned -> unexpected disconnect self.assertRaises(StompConnectionError, client.receiveFrame) client.connect(host=VIRTUALHOST) client.send(self.DESTINATION, 'test message 2') client.ack(client.receiveFrame()) client.unsubscribe((StompSpec.ID_HEADER, 'bla')) client.disconnect()
def test_transaction_writes_correct_frames(self): transaction = '4711' stomp = self._get_transport_mock() for (method, command) in [ (stomp.begin, StompSpec.BEGIN), (stomp.commit, StompSpec.COMMIT), (stomp.begin, StompSpec.BEGIN), (stomp.abort, StompSpec.ABORT) ]: method(transaction) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(command, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame) with stomp.transaction(transaction): args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.BEGIN, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.COMMIT, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame) try: with stomp.transaction(transaction): raise except: args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.ABORT, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame)
def test_ack_writes_correct_frame(self): id_ = '12345' stomp = self._get_transport_mock() stomp.ack(StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: id_}, b'blah')) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.ACK, {StompSpec.MESSAGE_ID_HEADER: id_}), sentFrame)
def test_subscribe_matching_and_corner_cases(self): destination = '/queue/foo' headers = {'foo': 'bar', 'fuzz': 'ball'} stomp = self._get_transport_mock() token = stomp.subscribe(destination, headers) self.assertEqual(token, (StompSpec.DESTINATION_HEADER, destination)) self.assertEqual(stomp.message(StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.DESTINATION_HEADER: destination})), token) self.assertRaises(StompProtocolError, stomp.message, StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.DESTINATION_HEADER: 'unknown'})) self.assertRaises(StompProtocolError, stomp.message, StompFrame(StompSpec.MESSAGE, {StompSpec.DESTINATION_HEADER: destination}))
def test_2_transaction(self): config = self.getConfig(StompSpec.VERSION_1_0) client = Stomp(config) client.connect(host=VIRTUALHOST) client.subscribe( self.DESTINATION, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}) self.assertFalse(client.canRead(self.TIMEOUT)) with client.transaction(4711) as transaction: self.assertEquals(transaction, '4711') client.send(self.DESTINATION, 'test message', {StompSpec.TRANSACTION_HEADER: transaction}) self.assertFalse(client.canRead(0)) self.assertTrue(client.canRead(self.TIMEOUT)) frame = client.receiveFrame() self.assertEquals(frame.body, 'test message') client.ack(frame) with client.transaction(4713, receipt='4712') as transaction: self.assertEquals(transaction, '4713') self.assertEquals( client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712-begin'})) client.send(self.DESTINATION, 'test message', {StompSpec.TRANSACTION_HEADER: transaction}) client.send(self.DESTINATION, 'test message without transaction') self.assertTrue(client.canRead(self.TIMEOUT)) frame = client.receiveFrame() self.assertEquals(frame.body, 'test message without transaction') client.ack(frame) self.assertFalse(client.canRead(0)) frames = [client.receiveFrame() for _ in xrange(2)] frames = list(sorted(frames, key=lambda f: f.command)) frame = frames[0] client.ack(frame) self.assertEquals(frame.body, 'test message') frame = frames[1] self.assertEquals( frame, StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712-commit'})) try: with client.transaction(4714) as transaction: self.assertEquals(transaction, '4714') client.send(self.DESTINATION, 'test message', {StompSpec.TRANSACTION_HEADER: transaction}) raise RuntimeError('poof') except RuntimeError as e: self.assertEquals(str(e), 'poof') else: raise self.assertFalse(client.canRead(self.TIMEOUT)) client.disconnect()
def test_disconnect(self): self.assertEquals(commands.disconnect(), StompFrame(StompSpec.DISCONNECT)) self.assertEquals( commands.disconnect(receipt='4711'), StompFrame(StompSpec.DISCONNECT, {StompSpec.RECEIPT_HEADER: '4711'})) self.assertRaises(StompProtocolError, commands.disconnect, receipt=4711)
def test_connect_writes_correct_frame(self): login = '******' passcode = 'george' stomp = self._get_connect_mock(StompFrame(StompSpec.CONNECTED, {StompSpec.SESSION_HEADER: '4711'})) stomp._config.login = login stomp._config.passcode = passcode stomp.connect() args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.CONNECT, {StompSpec.LOGIN_HEADER: login, StompSpec.PASSCODE_HEADER: passcode}), sentFrame)
def test_unescape(self): frameBytes = """%s \\n\\\\:\\c\t\\n \x00""" % StompSpec.DISCONNECT frame = StompFrame(command=StompSpec.DISCONNECT, headers={'\n\\': ':\t\n'}, version=StompSpec.VERSION_1_1) self.assertEquals(str(frame), frameBytes) frameBytes = """%s \\n\\\\:\\c\t\\r \x00""" % StompSpec.DISCONNECT frame = StompFrame(command=StompSpec.DISCONNECT, headers={'\n\\': ':\t\r'}, version=StompSpec.VERSION_1_2) self.assertEquals(str(frame), frameBytes) frameBytes = """%s \\n\\\\:\\c\t\r \x00""" % StompSpec.DISCONNECT frame = StompFrame(command=StompSpec.DISCONNECT, headers={'\n\\': ':\t\r'}, version=StompSpec.VERSION_1_1) self.assertEquals(str(frame), frameBytes) frameBytes = """%s \\::\t\r \x00""" % StompSpec.DISCONNECT frame = StompFrame(command=StompSpec.DISCONNECT, headers={'\n\\': ':\t\r\n'}, version=StompSpec.VERSION_1_0) self.assertEquals(str(frame), frameBytes) frameBytes = """%s \\::\t\r \x00""" % StompSpec.CONNECT frame = StompFrame(command=StompSpec.CONNECT, headers={'\n\\': ':\t\r\n'}) for version in StompSpec.VERSIONS: frame.version = version self.assertEquals(str(frame), frameBytes)
def test_stomp_version_1_1(self): destination = '/queue/foo' stomp = self._get_transport_mock(config=StompConfig('tcp://%s:%s' % (HOST, PORT), version=StompSpec.VERSION_1_1, check=False)) stomp._transport = mock.Mock() frame = StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.DESTINATION_HEADER: destination}) self.assertRaises(StompProtocolError, stomp.nack, frame) frame = StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.DESTINATION_HEADER: destination, StompSpec.SUBSCRIPTION_HEADER: '0815'}, version=StompSpec.VERSION_1_1) stomp.nack(frame, receipt='123') args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(commands.nack(frame, receipt='123'), sentFrame)
def test_binary_body(self): body = binascii.a2b_hex('f0000a09') headers = {'content-length': str(len(body))} frame = StompFrame('MESSAGE', headers, body) self.assertEquals(frame.body, body) self.assertEquals(str(frame), 'MESSAGE\ncontent-length:4\n\n\xf0\x00\n\t\x00')
def test_binary_body(self): body = b'\xf0\x00\x0a\x09' headers = {'content-length': str(len(body))} frame = StompFrame(StompSpec.MESSAGE, headers, body) self.assertEqual(frame.body, body) self.assertEqual(binaryType(frame), b'MESSAGE\ncontent-length:4\n\n\xf0\x00\n\t\x00')
def test_send_not_connected_raises(self): frame = StompFrame(StompSpec.MESSAGE) transport = self._get_send_mock() transport._connected.return_value = False self.assertRaises(StompConnectionError, transport.send, frame) self.assertEquals(0, transport._socket.sendall.call_count)
def test_session_nack(self): session = StompSession(version=StompSpec.VERSION_1_1, check=False) frame_ = lambda h: StompFrame( StompSpec.MESSAGE, h, version=StompSpec.VERSION_1_1) for headers in [{ StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.SUBSCRIPTION_HEADER: 'bla' }, { StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.SUBSCRIPTION_HEADER: 'bla', 'foo': 'bar' }]: self.assertEqual(session.nack(frame_(headers)), commands.nack(frame_(headers))) headers = { StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.SUBSCRIPTION_HEADER: 'bla' } self.assertEqual(session.nack(frame_(headers), receipt='4711'), commands.nack(frame_(headers), receipt='4711')) self.assertRaises(StompProtocolError, session.nack, frame_({})) self.assertRaises(StompProtocolError, session.nack, frame_({StompSpec.MESSAGE_ID_HEADER: '4711'})) self.assertRaises(StompProtocolError, session.nack, frame_({StompSpec.SUBSCRIPTION_HEADER: 'bla'})) session = StompSession(version=StompSpec.VERSION_1_1) self.assertRaises( StompProtocolError, lambda: session.nack( frame_({ StompSpec.MESSAGE_ID_HEADER: '4711', StompSpec.SUBSCRIPTION_HEADER: 'bla' })))
def test_binary_body(self): body = b'\xf0\x00\x0a\x09' headers = {StompSpec.CONTENT_LENGTH_HEADER: textType(len(body))} frame = StompFrame(StompSpec.MESSAGE, body=body) frame.setContentLength() frameBytes = binaryType(frame) self.assertTrue(frameBytes.endswith(b'\x00')) parser = StompParser() for _ in range(2): for (j, _) in enumerate(frameBytes): parser.add(frameBytes[j:j + 1]) frame = parser.get() self.assertEqual(StompSpec.MESSAGE, frame.command) self.assertEqual(headers, frame.headers) self.assertEqual(body, frame.body) self.assertEqual(parser.get(), None) frames = 2 * frameBytes split = len(frameBytes) - 1 chunks = [frames[:split], frames[split:]] parser.add(chunks.pop(0)) self.assertEqual(parser.get(), None) parser.add(chunks.pop(0)) self.assertEqual(parser.get(), frame) self.assertEqual(parser.get(), frame) self.assertEqual(parser.get(), None) split = len(frameBytes) + 1 chunks = [frames[:split], frames[split:]] parser.add(chunks.pop(0)) self.assertEqual(parser.get(), frame) self.assertEqual(parser.get(), None) parser.add(chunks.pop(0)) self.assertEqual(parser.get(), frame) self.assertEqual(parser.get(), None)
def test_send(self): frame = StompFrame(StompSpec.MESSAGE) transport = self._get_send_mock() transport.send(frame) self.assertEquals(1, transport._socket.sendall.call_count) args, _ = transport._socket.sendall.call_args self.assertEquals(str(frame), args[0])
def test_non_string_headers(self): message = {'command': 'MESSAGE', 'headers': {123: 456}} frame = StompFrame(**message) self.assertEqual(frame.command, 'MESSAGE') self.assertEqual(frame.headers, {123: 456}) self.assertEqual(dict(frame), { 'command': 'MESSAGE', 'headers': { 123: 456 } }) self.assertEqual(binaryType(frame), b'MESSAGE\n123:456\n\n\x00') message = {'command': 'MESSAGE', 'headers': {123: 456}} frame = StompFrame(**message) self.assertEqual(binaryType(frame), b'MESSAGE\n123:456\n\n\x00') self.assertEqual(eval(repr(frame)), frame)
def test_add_throws_FrameError_on_invalid_command(self): parser = StompParser() self.assertRaises(StompFrameError, parser.add, b'HELLO\n\n\x00') self.assertFalse(parser.canRead()) parser.add(('%s\n\n\x00' % StompSpec.DISCONNECT).encode()) self.assertEqual(StompFrame(StompSpec.DISCONNECT), parser.get()) self.assertFalse(parser.canRead())
def test_subscribe_writes_correct_frame(self): destination = '/queue/foo' headers = {'foo': 'bar', 'fuzz': 'ball'} stomp = self._get_transport_mock() stomp.subscribe(destination, headers) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame(StompSpec.SUBSCRIBE, {StompSpec.DESTINATION_HEADER: destination, 'foo': 'bar', 'fuzz': 'ball'}), sentFrame)
def test_frame_info(self): frame = StompFrame(StompSpec.MESSAGE, headers={'a': 'c'}, body=b'More text than fits a short info.', version=StompSpec.VERSION_1_1) self.assertEqual( frame.info().replace("b'", "'").replace("u'", "'"), "MESSAGE frame [headers={'a': 'c'}, body='More text than fits ...', version=1.1]" )
def test_send_writes_correct_frame(self): destination = '/queue/foo' message = b'test message' headers = {'foo': 'bar', 'fuzz': 'ball'} stomp = self._get_transport_mock() stomp.send(destination, message, headers) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEqual(StompFrame('SEND', {StompSpec.DESTINATION_HEADER: destination, 'foo': 'bar', 'fuzz': 'ball'}, message), sentFrame)
def test_unescape(self): frameBytes = ("""%s \\n\\\\:\\c\t\\n \x00""" % StompSpec.DISCONNECT).encode() for version in (StompSpec.VERSION_1_1, StompSpec.VERSION_1_2): parser = StompParser(version=version) parser.add(frameBytes) frame = parser.get() self.assertEqual(frame.headers, {'\n\\': ':\t\n'}) parser = StompParser(version=StompSpec.VERSION_1_0) parser.add(frameBytes) self.assertEqual( parser.get(), StompFrame(command='DISCONNECT', rawHeaders=[('\\n\\\\', '\\c\t\\n')])) frameBytes = ("""%s \\n\\\\:\\c\\t \x00""" % StompSpec.DISCONNECT).encode() for version in (StompSpec.VERSION_1_1, StompSpec.VERSION_1_2): self.assertRaises(StompFrameError, StompParser(version=version).add, frameBytes) parser = StompParser(version=StompSpec.VERSION_1_0) parser.add(frameBytes) self.assertEqual( parser.get(), StompFrame(command='DISCONNECT', rawHeaders=[('\\n\\\\', '\\c\\t')])) frameBytes = ("""%s \\n\\\\:\\c\t\\r \x00""" % StompSpec.DISCONNECT).encode() parser = StompParser(version=StompSpec.VERSION_1_2) parser.add(frameBytes) frame = parser.get() self.assertEqual(frame.headers, {'\n\\': ':\t\r'})
def test_frame_without_headers_and_body(self): message = {'command': StompSpec.DISCONNECT} frame = StompFrame(**message) self.assertEqual(frame.headers, {}) self.assertEqual(dict(frame), message) self.assertEqual(binaryType(frame), ("""\ %s \x00""" % StompSpec.DISCONNECT).encode()) self.assertEqual(eval(repr(frame)), frame)
def test_frame_without_headers_and_body(self): message = {'command': StompSpec.DISCONNECT} frame = StompFrame(**message) self.assertEquals(frame.headers, {}) self.assertEquals(dict(frame), message) self.assertEquals(str(frame), """\ %s \x00""" % StompSpec.DISCONNECT) self.assertEquals(eval(repr(frame)), frame)
def test_add_multiple_frames_per_read(self): body1 = 'boo' body2 = 'hoo' headers = {'x': 'y'} frameBytes = str(StompFrame(StompSpec.MESSAGE, headers, body1)) + str( StompFrame(StompSpec.MESSAGE, headers, body2)) parser = StompParser() parser.add(frameBytes) frame = parser.get() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body1, frame.body) frame = parser.get() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body2, frame.body) self.assertEquals(parser.get(), None)
def test_receive(self): headers = {'x': 'y'} body = b'testing 1 2 3' frame = StompFrame(StompSpec.MESSAGE, headers, body) transport = self._get_receive_mock(binaryType(frame)) frame_ = transport.receive() self.assertEqual(frame, frame_) self.assertEqual(1, transport._socket.recv.call_count) self.assertRaises(StompConnectionError, transport.receive) self.assertEqual(transport._socket, None)