def test_table_multi(self): val = { 'foo': 7, 'bar': Decimal('123345.1234'), 'baz': 'this is some random string I typed', 'ubaz': u'And something in unicode', 'dday_aniv': datetime(1994, 6, 6), 'more': { 'abc': -123, 'def': 'hello world', 'now': datetime(2007, 11, 11, 21, 14, 31), 'qty': Decimal('-123.45'), 'blank': {}, 'extra': { 'deeper': 'more strings', 'nums': -12345678, }, } } w = AMQPWriter() w.write_table(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_shortstr_unicode(self): w = AMQPWriter() w.write_shortstr(u'hello') s = w.getvalue() self.assertEqualBinary(s, '\x05hello') r = AMQPReader(s) self.assertEqual(r.read_shortstr(), u'hello')
def test_empty_longstr(self): w = AMQPWriter() w.write_longstr('') s = w.getvalue() self.assertEqualBinary(s, '\x00\x00\x00\x00') r = AMQPReader(s) self.assertEqual(r.read_longstr(), '')
def test_longlong(self): for i in range(256): val = randint(0, (2**64) - 1) w = AMQPWriter() w.write_longlong(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_longlong(), val)
def test_short(self): for i in range(256): val = randint(0, 65535) w = AMQPWriter() w.write_short(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_short(), val)
def test_octet(self): for val in range(256): w = AMQPWriter() w.write_octet(val) s = w.getvalue() self.assertEqualBinary(s, chr(val)) r = AMQPReader(s) self.assertEqual(r.read_octet(), val)
def test_single_bit(self): for val, check in [(True, '\x01'), (False, '\x00')]: w = AMQPWriter() w.write_bit(val) s = w.getvalue() self.assertEqualBinary(s, check) r = AMQPReader(s) self.assertEqual(r.read_bit(), val)
def test_table(self): val = {'foo': 7} w = AMQPWriter() w.write_table(val) s = w.getvalue() self.assertEqualBinary(s, '\x00\x00\x00\x09\x03fooI\x00\x00\x00\x07') r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_table_empty(self): val = {} w = AMQPWriter() w.write_table(val) s = w.getvalue() self.assertEqualBinary(s, '\x00\x00\x00\x00') r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_longstr_unicode(self): val = u'a' * 512 w = AMQPWriter() w.write_longstr(val) s = w.getvalue() self.assertEqualBinary(s, '\x00\x00\x02\x00' + ('a' * 512)) r = AMQPReader(s) self.assertEqual(r.read_longstr(), val)
def test_multiple_bits(self): w = AMQPWriter() w.write_bit(True) w.write_bit(True) w.write_bit(False) w.write_bit(True) s = w.getvalue() self.assertEqualBinary(s, '\x0b') r = AMQPReader(s) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False) self.assertEqual(r.read_bit(), True)
def __init__(self, sock): self.channels = {} super(amqp.Connection, self).__init__(self, 0) self.out = AMQPWriter(sock.makefile('w')) self.input = AMQPReader(sock.makefile('r')) self.method_reader = _MethodReader(self.input)
def test_multiple_bits3(self): """ Check bit groups that span multiple bytes """ w = AMQPWriter() # Spit out 20 bits for i in range(10): w.write_bit(True) w.write_bit(False) s = w.getvalue() self.assertEqualBinary(s, '\x55\x55\x05') r = AMQPReader(s) for i in range(10): self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False)
def test_multiple_bits2(self): """ Check bits mixed with non-bits """ w = AMQPWriter() w.write_bit(True) w.write_bit(True) w.write_bit(False) w.write_octet(10) w.write_bit(True) s = w.getvalue() self.assertEqualBinary(s, '\x03\x0a\x01') r = AMQPReader(s) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False) self.assertEqual(r.read_octet(), 10) self.assertEqual(r.read_bit(), True)
class FakeRedirectConnection(amqp.Connection): def __init__(self, sock): self.channels = {} super(amqp.Connection, self).__init__(self, 0) self.out = AMQPWriter(sock.makefile('w')) self.input = AMQPReader(sock.makefile('r')) self.method_reader = _MethodReader(self.input) def do_redirect(self, dest): if self.input.read(8) != AMQP_PROTOCOL_HEADER: print "Didn't receive AMQP 0-8 header" return # major, minor seems backwards, but that's what RabbitMQ sends self.start(8, 0, {'product': 'fake_redirect_0_8.py'}, ['AMQPLAIN'], ['en_US']) self.wait(allowed_methods=[ (10, 11), # start_ok ]) self.tune(0, 0, 0) self.wait(allowed_methods=[ (10, 31), # tune_ok ]) self.wait(allowed_methods=[ (10, 40), # open ]) if self.insist: self.close(reply_text="Can't redirect, insist was set to True") else: self.redirect(dest, '') try: self.wait(allowed_methods=[ (10, 60), # close ]) except amqp.AMQPConnectionError: pass print 'Redirect finished' def fake_op(self, args): """ We're not really much interested in what the client sends for start_ok, tune_ok """ pass ############## def _open(self, args): virtual_host = args.read_shortstr() capabilities = args.read_shortstr() self.insist = args.read_bit() def redirect(self, host, known_hosts): args = AMQPWriter() args.write_shortstr(host) args.write_shortstr(known_hosts) self._send_channel_method_frame(0, (10, 50), args) def start(self, version_major, version_minor, server_properties, mechanisms, locales): args = AMQPWriter() args.write_octet(version_major) args.write_octet(version_minor) args.write_table(server_properties) args.write_longstr(' '.join(mechanisms)) args.write_longstr(' '.join(locales)) self._send_channel_method_frame(0, (10, 10), args) def tune(self, channel_max, frame_max, heartbeat): args = AMQPWriter() args.write_short(channel_max) args.write_long(frame_max) args.write_short(heartbeat) self._send_channel_method_frame(0, (10, 30), args)