Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_side_effects_from_message_marshal_error(self):
# Verify that frame buffer is empty on entry
self.assertEqual(b'', self.connection._frame_buffer)
# Use Basic.Public with invalid body to trigger marshalling error
method = spec.Basic.Publish()
properties = spec.BasicProperties()
# Verify that marshalling of method and header won't trigger error
frame.Method(1, method).marshal()
frame.Header(1, body_size=10, props=properties).marshal()
# Create bogus body that should trigger an error during marshalling
body = [1,2,3,4]
# Verify that frame body can be created using the bogus body, but
# that marshalling will fail
frame.Body(1, body)
with self.assertRaises(TypeError):
frame.Body(1, body).marshal()
# Now, attempt to send the method with the bogus body
with self.assertRaises(TypeError):
self.connection._send_method(channel_number=1,
method=method,
content=(properties, body))
def send_message(_result):
d = self.channel.basic_publish("testexch", "testrk", "testbody")
# Send the Basic.Return frame
method = spec.Basic.Return(
exchange="testexch", routing_key="testrk")
return_cb(self.channel, method,
spec.BasicProperties(), "testbody")
# Send the Basic.Ack frame
frame = Method(1, spec.Basic.Ack(delivery_tag=1))
self.channel._on_delivery_confirmation(frame)
return d
def test_on_getok_no_callback(self, error):
method_value = frame.Method(1, spec.Basic.GetOk('ctag0', 1))
header_value = frame.Header(1, 10, spec.BasicProperties())
body_value = b'0123456789'
self.obj._on_getok(method_value, header_value, body_value)
error.assert_called_with(
'Basic.GetOk received with no active callback')
def test_send_message_updates_frames_sent_and_bytes_sent(
self,
_adapter_emit_data):
self.connection._flush_outbound = mock.Mock()
self.connection._body_max_length = 10000
method = spec.Basic.Publish(
exchange='my-exchange', routing_key='my-route')
props = spec.BasicProperties()
body = b'b' * 1000000
self.connection._send_method(
channel_number=1, method=method, content=(props, body))
frames_sent = _adapter_emit_data.call_count
bytes_sent = sum(
len(call[0][0]) for call in _adapter_emit_data.call_args_list)
self.assertEqual(self.connection.frames_sent, frames_sent)
self.assertEqual(self.connection.bytes_sent, bytes_sent)
channel.queue_declare(queue = replyQueue, exclusive = True, auto_delete = True)
channel.queue_bind(exchange = exchangeName, queue = replyQueue, routing_key = replyKey)
channel.basic_consume(consumer_callback = onMessage, queue = replyQueue, no_ack = True)
channel.start_consuming()
try:
#connect
credentials = pika.PlainCredentials(username, password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host = server, port = port, virtual_host = vhost, credentials = credentials, heartbeat_interval = 60))
channel = connection.channel()
thread.start_new_thread(listen, ())
time.sleep(1) #give time for it to start consuming
#send request message
properties = pika.spec.BasicProperties(content_type = "text/plain", delivery_mode = 1, reply_to = replyKey)
channel.basic_publish(exchange = exchangeName, routing_key = requestKey, body = "Hello World!", properties = properties)
#block until receives reply message
while connection.is_open:
pass
except Exception, e:
print e
server = "hostname"
port = 5672
vhost = "yourvhost"
username = "username"
password = "password"
exchangeName = "testEx"
routingKey = "test"
try:
#connect
credentials = pika.PlainCredentials(username, password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host = server, port = port, virtual_host = vhost, credentials = credentials, heartbeat_interval = 60))
channel = connection.channel()
#send message
properties = pika.spec.BasicProperties(content_type = "text/plain", delivery_mode = 1)
channel.basic_publish(exchange = exchangeName, routing_key = routingKey, body = "Hello World!", properties = properties)
#disconnect
connection.close()
except Exception, e:
print e
def publish(self, message, route='*', correlation_id=None, **args):
"""
This method publishes a message over the amqp queue.
"""
# the properties for the message
message_properties = pika.spec.BasicProperties(
correlation_id=correlation_id
) if correlation_id else None
# publish the message
self._channel.basic_publish(
exchange=self.EXCHANGE,
body=message,
routing_key=route or self.ROUTING_KEY,
properties=message_properties,
**args
)
# create connection parameters
credentials = pika.PlainCredentials(username, password)
parameters = pika.ConnectionParameters(host, port, virtual_host,
credentials)
# create connection & channel
self.connection = pika.BlockingConnection(parameters)
self.channel = self.connection.channel()
# create an exchange, if needed
self.channel.exchange_declare(exchange=exchange,
exchange_type=exchange_type,
durable=durable)
# needed when publishing
self.spec = pika.spec.BasicProperties(delivery_mode=2)
self.routing_key = routing_key
self.exchange = exchange
def _amqp_basic_properties(self, content_type, content_encoding, delivery_mode, priority, expiration, user_id, app_id):
return BasicProperties(content_type=content_type, content_encoding=content_encoding,
delivery_mode=delivery_mode, priority=priority, expiration=expiration,
user_id=user_id, app_id=app_id)
vhost = "yourvhost"
username = "username"
password = "password"
exchangeName = "testEx"
routingKey = "test"
try:
#connect
credentials = pika.PlainCredentials(username, password)
sslOptions = {"cert_reqs": ssl.CERT_NONE}
parameters = pika.ConnectionParameters(host = server, port = port, virtual_host = vhost, credentials = credentials, heartbeat_interval = 60, ssl = True, ssl_options = sslOptions)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
#send message
properties = pika.spec.BasicProperties(content_type = "text/plain", delivery_mode = 1)
channel.basic_publish(exchange = exchangeName, routing_key = routingKey, body = "Hello World!", properties = properties)
#disconnect
connection.close()
except Exception, e:
print e