Permalink
Browse files

Reject publishes with routing key longer than shortstr (255 characters)

References #193.
  • Loading branch information...
1 parent eafa033 commit 487373cd2f3a0e1c47d10d42012a91bb2fb5330d Michael Klishin committed Mar 7, 2014
Showing with 76 additions and 0 deletions.
  1. +2 −0 lib/bunny/channel.rb
  2. +74 −0 spec/higher_level_api/integration/basic_publish_spec.rb
View
2 lib/bunny/channel.rb
@@ -158,6 +158,7 @@ class Channel
attr_reader :consumers
DEFAULT_CONTENT_TYPE = "application/octet-stream".freeze
+ SHORTSTR_LIMIT = 255
# @param [Bunny::Session] connection AMQP 0.9.1 connection
# @param [Integer] id Channel id, pass nil to make Bunny automatically allocate it
@@ -516,6 +517,7 @@ def nack(delivery_tag, multiple = false, requeue = false)
# @api public
def basic_publish(payload, exchange, routing_key, opts = {})
raise_if_no_longer_open!
+ raise ArgumentError, "routing key cannot be longer than #{SHORTSTR_LIMIT} characters" if routing_key.size > SHORTSTR_LIMIT
exchange_name = if exchange.respond_to?(:name)
exchange.name
View
74 spec/higher_level_api/integration/basic_publish_spec.rb
@@ -87,3 +87,77 @@
end
end
end
+
+
+describe "Published message" do
+ let(:connection) do
+ c = Bunny.new(:user => "bunny_gem", :password => "bunny_password", :vhost => "bunny_testbed")
+ c.start
+ c
+ end
+
+ after :each do
+ connection.close if connection.open?
+ end
+
+ context "with all default delivery and a 254 character long routing key" do
+ it "routes the messages" do
+ ch = connection.create_channel
+
+ q = ch.queue("", :exclusive => true)
+ x = ch.fanout("amq.fanout")
+ q.bind(x)
+
+ rk = "a" * 254
+ x.publish("xyzzy", :routing_key => rk, :persistent => true)
+
+ sleep(1)
+ q.message_count.should == 1
+
+ _, _, payload = q.pop
+
+ payload.should == "xyzzy"
+
+ ch.close
+ end
+ end
+
+ context "with all default delivery and a 255 character long routing key" do
+ it "routes the messages" do
+ ch = connection.create_channel
+
+ q = ch.queue("", :exclusive => true)
+ x = ch.fanout("amq.fanout")
+ q.bind(x)
+
+ rk = "a" * 255
+ x.publish("xyzzy", :routing_key => rk, :persistent => true)
+
+ sleep(1)
+ q.message_count.should == 1
+
+ _, _, payload = q.pop
+
+ payload.should == "xyzzy"
+
+ ch.close
+ end
+ end
+
+ context "with all default delivery and a 256 character long routing key" do
+ it "fails with a connection exception" do
+ ch = connection.create_channel
+
+ q = ch.queue("", :exclusive => true)
+ x = ch.fanout("amq.fanout")
+ q.bind(x)
+
+ rk = "a" * 256
+ expect do
+ x.publish("xyzzy", :routing_key => rk, :persistent => true)
+ end.to raise_error(ArgumentError)
+
+ ch.close
+ end
+ end
+end

0 comments on commit 487373c

Please sign in to comment.