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.assertEquals(frame, commands.begin(transaction, receipt='bla')) self.assertEquals(frame, StompFrame(StompSpec.BEGIN, headers)) headers.pop(StompSpec.RECEIPT_HEADER) self.assertRaises(StompProtocolError, session.begin, transaction) frame = session.abort(transaction) self.assertEquals(frame, commands.abort(transaction)) self.assertEquals(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.assertEquals(frame, commands.begin(transaction)) self.assertEquals(frame, StompFrame(StompSpec.BEGIN, headers)) frame = session.commit(transaction) self.assertEquals(frame, commands.commit(transaction)) self.assertEquals(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_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_ack_writes_correct_frame(self): id_ = '12345' stomp = self._get_transport_mock() stomp.ack( StompFrame(StompSpec.MESSAGE, {StompSpec.MESSAGE_ID_HEADER: id_}, 'blah')) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEquals( StompFrame(StompSpec.ACK, {StompSpec.MESSAGE_ID_HEADER: id_}), sentFrame)
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, 'test message 1') client.send(self.DESTINATION, '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, 'test message 3', receipt='4711') self.assertTrue(client.canRead(self.TIMEOUT)) self.assertEquals( 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.assertEquals( client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712'})) self.assertRaises(StompConnectionError, client.receiveFrame) client.connect(host=VIRTUALHOST) client.disconnect(receipt='4711') self.assertEquals( client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'})) client.close() self.assertRaises(StompConnectionError, client.canRead, 0)
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_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.assertEquals( StompFrame(StompSpec.CONNECT, { StompSpec.LOGIN_HEADER: login, StompSpec.PASSCODE_HEADER: passcode }), sentFrame)
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_add_throws_FrameError_on_invalid_command(self): parser = StompParser() self.assertRaises(StompFrameError, parser.add, 'HELLO\n') self.assertFalse(parser.canRead()) parser.add('%s\n\n\x00' % StompSpec.DISCONNECT) self.assertEquals(StompFrame(StompSpec.DISCONNECT), parser.get()) self.assertFalse(parser.canRead())
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_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_duplicate_headers(self): rawHeaders = (('foo', 'bar1'), ('foo', 'bar2')) headers = dict(reversed(rawHeaders)) message = { 'command': 'SEND', 'body': 'some stuff\nand more', 'rawHeaders': rawHeaders } frame = StompFrame(**message) self.assertEquals(frame.headers, headers) self.assertEquals(frame.rawHeaders, rawHeaders) rawFrame = 'SEND\nfoo:bar1\nfoo:bar2\n\nsome stuff\nand more\x00' self.assertEquals(str(frame), rawFrame) frame.unraw() self.assertEquals(frame.headers, headers) self.assertEquals(frame.rawHeaders, None) rawFrame = 'SEND\nfoo:bar1\n\nsome stuff\nand more\x00' self.assertEquals(str(frame), rawFrame)
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_invalid_command(self): messages = [ 'RECEIPT\nreceipt-id:message-12345\n\n\x00', 'NACK\nsubscription:0\nmessage-id:007\n\n\x00' ] parser = StompParser('1.0') parser.add(messages[0]) self.assertRaises(StompFrameError, parser.add, messages[1]) self.assertEquals( parser.get(), StompFrame(StompSpec.RECEIPT, rawHeaders=((u'receipt-id', u'message-12345'), ))) self.assertFalse(parser.canRead()) self.assertEquals(parser.get(), None) parser = StompParser('1.1') parser.add(messages[1]) self.assertEquals( parser.get(), StompFrame(command=u'NACK', rawHeaders=((u'subscription', u'0'), (u'message-id', u'007'))))
def test_no_newline(self): headers = {'x': 'y'} body = 'testing 1 2 3' frameBytes = str(StompFrame(StompSpec.MESSAGE, headers, body)) self.assertTrue(frameBytes.endswith('\x00')) parser = StompParser() parser.add(self._generate_bytes(frameBytes)) frame = parser.get() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body, frame.body) self.assertEquals(parser.get(), None)
def test_frame(self): message = {'command': StompSpec.SEND, 'headers': {StompSpec.DESTINATION_HEADER: '/queue/world'}, 'body': 'two\nlines'} frame = StompFrame(**message) self.assertEquals(message['headers'], frame.headers) self.assertEquals(dict(frame), message) self.assertEquals(str(frame), """\ %s %s:/queue/world two lines\x00""" % (StompSpec.SEND, StompSpec.DESTINATION_HEADER)) self.assertEquals(eval(repr(frame)), frame)
def test_receive_binary(self): body = binascii.a2b_hex('f0000a09') headers = {StompSpec.CONTENT_LENGTH_HEADER: str(len(body))} frame = StompFrame(StompSpec.MESSAGE, headers, body) transport = self._get_receive_mock(str(frame)) frame_ = transport.receive() self.assertEquals(frame, frame_) self.assertEquals(1, transport._socket.recv.call_count) self.assertRaises(StompConnectionError, transport.receive) self.assertEquals(transport._socket, None)
def test_receive(self): headers = {'x': 'y'} body = 'testing 1 2 3' frame = StompFrame(StompSpec.MESSAGE, headers, body) transport = self._get_receive_mock(str(frame)) frame_ = transport.receive() self.assertEquals(frame, frame_) self.assertEquals(1, transport._socket.recv.call_count) self.assertRaises(StompConnectionError, transport.receive) self.assertEquals(transport._socket, None)
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.assertEquals( StompFrame(command, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame) with stomp.transaction(transaction): args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEquals( StompFrame(StompSpec.BEGIN, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame) args, _ = stomp._transport.send.call_args sentFrame = args[0] self.assertEquals( 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.assertEquals( StompFrame(StompSpec.ABORT, {StompSpec.TRANSACTION_HEADER: transaction}), sentFrame)
def test_binary_body(self): body = binascii.a2b_hex('f0000a09') headers = {'content-length': str(len(body))} frameBytes = str(StompFrame(StompSpec.MESSAGE, headers, body)) self.assertTrue(frameBytes.endswith('\x00')) parser = StompParser() parser.add(frameBytes) frame = parser.get() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body, frame.body) self.assertEquals(parser.get(), None)
def test_non_string_arguments(self): message = {'command': 0, 'headers': {123: 456}, 'body': 789} frame = StompFrame(**message) self.assertEquals(frame.command, 0) self.assertEquals(frame.headers, {123: 456}) self.assertEquals(frame.body, 789) self.assertEquals(dict(frame), message) self.assertEquals(str(frame), """\ 0 123:456 789\x00""") self.assertEquals(eval(repr(frame)), frame)
def test_frame_parse_succeeds(self): frame = StompFrame( StompSpec.SEND, { 'foo': 'bar', 'hello ': 'there-world with space ', 'empty-value': '', '': 'empty-header', StompSpec.DESTINATION_HEADER: '/queue/blah' }, 'some stuff\nand more') parser = StompParser() parser.add(str(frame)) self.assertEqual(parser.get(), frame) self.assertEqual(parser.get(), None)
def test_decode(self): headers = {u'fen\xeatre': u'\xbfqu\xe9 tal?, s\xfc\xdf'} frameBytes = str( StompFrame(command=StompSpec.DISCONNECT, headers=headers, version=StompSpec.VERSION_1_1)) parser = StompParser(version=StompSpec.VERSION_1_1) parser.add(frameBytes) frame = parser.get() self.assertEquals(frame.headers, headers) parser = StompParser(version=StompSpec.VERSION_1_0) self.assertRaises(UnicodeDecodeError, parser.add, frameBytes)
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.assertEquals( StompFrame( StompSpec.SUBSCRIBE, { StompSpec.DESTINATION_HEADER: destination, 'foo': 'bar', 'fuzz': 'ball' }, ''), sentFrame)
def test_receive_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)) transport = self._get_receive_mock(frameBytes) frame = transport.receive() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body1, frame.body) self.assertEquals(1, transport._socket.recv.call_count) frame = transport.receive() self.assertEquals(StompSpec.MESSAGE, frame.command) self.assertEquals(headers, frame.headers) self.assertEquals(body2, frame.body) self.assertEquals(1, transport._socket.recv.call_count) self.assertRaises(StompConnectionError, transport.receive) self.assertEquals(transport._socket, None)
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() 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.assertEquals(commands.nack(frame, receipt='123'), 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.assertEquals(token, (StompSpec.DESTINATION_HEADER, destination)) self.assertEquals( 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_send_writes_correct_frame(self): destination = '/queue/foo' message = '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.assertEquals( StompFrame( 'SEND', { StompSpec.DESTINATION_HEADER: destination, 'foo': 'bar', 'fuzz': 'ball' }, message), sentFrame)
def test_session_disconnect(self): session = StompSession(StompSpec.VERSION_1_1) session.connect(login='', passcode='') session.connected(StompFrame(StompSpec.CONNECTED, {StompSpec.SESSION_HEADER: 'hi'})) headers = {StompSpec.ID_HEADER: 4711} session.subscribe('bla', headers) frame = session.disconnect(receipt='4711') self.assertEquals(frame, commands.disconnect(receipt='4711')) self.assertEquals(session.state, session.DISCONNECTING) session.close(flush=False) self.assertEquals(session.state, session.DISCONNECTED) self.assertEquals(list(session.replay()), [('bla', headers, None, None)]) self.assertEquals(list(session.replay()), []) self.assertRaises(StompProtocolError, session.disconnect)
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)