Permalink
Browse files

Merge pull request #315 from tjsingleton/master

Stomp content-length header should be in bytes
  • Loading branch information...
2 parents 19a8b2a + 1ac5ccb commit 7a44d7cb1dcba33bfaa89eedfeda70460902ac21 @tmm1 tmm1 committed Feb 28, 2013
Showing with 42 additions and 2 deletions.
  1. +5 −2 lib/em/protocols/stomp.rb
  2. +37 −0 tests/test_stomp.rb
View
7 lib/em/protocols/stomp.rb
@@ -104,12 +104,15 @@ def consume_line line
# @private
def send_frame verb, headers={}, body=""
+ body = body.to_s
ary = [verb, "\n"]
+ body_bytesize = body.bytesize if body.respond_to? :bytesize
+ body_bytesize ||= body.size
headers.each {|k,v| ary << "#{k}:#{v}\n" }
- ary << "content-length: #{body.to_s.length}\n"
+ ary << "content-length: #{body_bytesize}\n"
ary << "content-type: text/plain; charset=UTF-8\n" unless headers.has_key? 'content-type'
ary << "\n"
- ary << body.to_s
+ ary << body
ary << "\0"
send_data ary.join
end
View
37 tests/test_stomp.rb
@@ -0,0 +1,37 @@
+require 'em_test_helper'
+
+class TestStomp < Test::Unit::TestCase
+ CONTENT_LENGTH_REGEX = /^content-length: (\d+)$/
+
+ def bytesize(str)
+ str = str.to_s
+ size = str.bytesize if str.respond_to?(:bytesize) # bytesize added in 1.9
+ size || str.size
+ end
+
+ def test_content_length_in_bytes
+ connection = Object.new
+ connection.instance_eval do
+ extend EM::P::Stomp
+
+ def last_sent_content_length
+ @sent && Integer(@sent[CONTENT_LENGTH_REGEX, 1])
+ end
+
+ def send_data(string)
+ @sent = string
+ end
+ end
+
+ queue = "queue"
+ failure_message = "header content-length is not the byte size of last sent body"
+
+ body = "test"
+ connection.send queue, body
+ assert_equal bytesize(body), connection.last_sent_content_length, failure_message
+
+ body = "test\u221A"
+ connection.send queue, body
+ assert_equal bytesize(body), connection.last_sent_content_length, failure_message
+ end
+end

0 comments on commit 7a44d7c

Please sign in to comment.