stomp 1.1.10 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,251 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.unshift(File.dirname(__FILE__))
4
+
5
+ require 'test_helper'
6
+
7
+ class TestConnection1P < Test::Unit::TestCase
8
+ include TestBase
9
+
10
+ def setup
11
+ @conn = get_connection()
12
+ end
13
+
14
+ def teardown
15
+ @conn.disconnect if @conn.open? # allow tests to disconnect
16
+ end
17
+ #
18
+ def test_conn_1p_0000
19
+ assert @conn.open?
20
+ end
21
+ #
22
+ def test_conn_1p_0010
23
+ #
24
+ cha = {:host => "localhost"}
25
+ assert_raise Stomp::Error::ProtocolErrorConnect do
26
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
27
+ end
28
+ #
29
+ chb = {"accept-version" => "1.0"}
30
+ assert_raise Stomp::Error::ProtocolErrorConnect do
31
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, chb)
32
+ end
33
+ end
34
+ #
35
+ def test_conn_1p_0020
36
+ #
37
+ cha = {:host => "localhost", "accept-version" => "1.0"}
38
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
39
+ conn = nil
40
+ assert_nothing_raised do
41
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
42
+ conn.disconnect
43
+ end
44
+ assert_equal conn.protocol, Stomp::SPL_10
45
+ end
46
+ #
47
+ def test_conn_1p_0030
48
+ #
49
+ cha = {:host => "localhost", "accept-version" => "1.1"}
50
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
51
+ conn = nil
52
+ assert_nothing_raised do
53
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
54
+ conn.disconnect
55
+ end
56
+ assert_equal conn.protocol, Stomp::SPL_11
57
+ end
58
+ #
59
+ def test_conn_1p_0040
60
+ #
61
+ cha = {:host => "localhost", "accept-version" => "1.1"}
62
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
63
+ cha["heart-beat"] = "0,0" # No heartbeats
64
+ conn = nil
65
+ assert_nothing_raised do
66
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
67
+ conn.disconnect
68
+ end
69
+ assert_equal conn.protocol, Stomp::SPL_11
70
+ end
71
+ #
72
+
73
+ def test_conn_1p_0050
74
+ #
75
+ cha = {:host => "localhost", "accept-version" => "1.1"}
76
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
77
+ cha["heart-beat"] = "10,10,20" # Bad header Heartbeats
78
+ conn = nil
79
+ assert_raise Stomp::Error::InvalidHeartBeatHeaderError do
80
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
81
+ end
82
+ end
83
+ #
84
+ def test_conn_11h_0060
85
+ #
86
+ cha = {:host => "localhost", "accept-version" => "1.1"}
87
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
88
+ cha["heart-beat"] = "a,10" # Bad header Heartbeats
89
+ conn = nil
90
+ assert_raise Stomp::Error::InvalidHeartBeatHeaderError do
91
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
92
+ end
93
+ end
94
+ #
95
+ def test_conn_1p_0070
96
+ #
97
+ cha = {:host => "localhost", "accept-version" => "1.1"}
98
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
99
+ cha["heart-beat"] = "500,1000" # Valid heart beat headers
100
+ conn = nil
101
+ assert_nothing_raised do
102
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
103
+ conn.disconnect
104
+ end
105
+ end
106
+
107
+ #
108
+ def test_conn_1p_0080
109
+ #
110
+ cha = {:host => "localhost", "accept-version" => "1.1"}
111
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
112
+ cha["heart-beat"] = "5000,0" # Valid heart beat headers, send only
113
+ conn = nil
114
+ logger = Tlogger.new
115
+ assert_nothing_raised do
116
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
117
+ conn.set_logger(logger)
118
+ sleep 65
119
+ conn.set_logger(nil)
120
+ conn.disconnect
121
+ end
122
+ end if ENV['STOMP_HB11LONG']
123
+
124
+ #
125
+ def test_conn_1p_0090
126
+ #
127
+ cha = {:host => "localhost", "accept-version" => "1.1"}
128
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
129
+ cha["heart-beat"] = "0,10000" # Valid heart beat headers, receive only
130
+ conn = nil
131
+ logger = Tlogger.new
132
+ assert_nothing_raised do
133
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
134
+ # m = conn.receive # This will hang forever .....
135
+ conn.set_logger(logger)
136
+ sleep 65
137
+ conn.set_logger(nil)
138
+ conn.disconnect
139
+ end
140
+ end if ENV['STOMP_HB11LONG']
141
+
142
+ #
143
+ def test_conn_1p_0100
144
+ #
145
+ cha = {:host => "localhost", "accept-version" => "1.1"}
146
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
147
+ cha["heart-beat"] = "5000,10000" # Valid heart beat headers, send and receive
148
+ conn = nil
149
+ logger = Tlogger.new
150
+ assert_nothing_raised do
151
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
152
+ # m = conn.receive # This will hang forever .....
153
+ conn.set_logger(logger)
154
+ sleep 65
155
+ conn.set_logger(nil)
156
+ conn.disconnect
157
+ end
158
+ end if ENV['STOMP_HB11LONG']
159
+ #
160
+ def test_conn_1p_0110
161
+ #
162
+ cha = {:host => "localhost", "accept-version" => "1.1"}
163
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
164
+ cha["heart-beat"] = "0,0" # No heartbeats
165
+ conn = nil
166
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
167
+ good_data = [
168
+ "\x41\xc3\xb1\x42",
169
+ "\xc2\x80", # 2 byte characters
170
+ "\xc2\xbf",
171
+ "\xdf\x80",
172
+ "\xdf\xbf",
173
+ "\xe0\xa0\x80", # 3 byte characters
174
+ "\xe0\xbf\x80",
175
+ "\xe0\xa0\xbf",
176
+ "\xe0\xbf\xbf",
177
+ "\xf1\x80\x80\x80", # 4 byte characters
178
+ "\xf1\xbf\xbf\xbf",
179
+ "\xf2\x80\x80\x80",
180
+ "\xf2\xbf\xbf\xbf",
181
+ "\xf3\x80\x80\x80",
182
+ "\xf3\xbf\xbf\xbf",
183
+ ]
184
+ good_data.each do |string|
185
+ assert conn.valid_utf8?(string), "good unicode specs 01: #{string}"
186
+ end
187
+ conn.disconnect
188
+ end
189
+
190
+ #
191
+ def test_conn_1p_0120
192
+ #
193
+ cha = {:host => "localhost", "accept-version" => "1.1"}
194
+ cha[:host] = "/" if ENV['STOMP_RABBIT']
195
+ cha["heart-beat"] = "0,0" # No heartbeats
196
+ conn = nil
197
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, cha)
198
+ bad_data = [
199
+ "\x41\xc2\xc3\xb1\x42",
200
+ "\xed\xa0\x80", # UTF-16 surrogate halves
201
+ "\xed\xad\xbf",
202
+ "\xed\xae\x80",
203
+ "\xed\xaf\xbf",
204
+ "\xed\xb0\x80",
205
+ "\xed\xbe\x80",
206
+ "\xed\xbf\xbf",
207
+ "\xc0", # Single bytes
208
+ "\xc1",
209
+ "\xf5","\xf6","\xf7","\xf8","\xf9","\xfa","\xfb","\xfc",
210
+ "\xfd","\xfe","\xff",
211
+ "\xc0\x80", # Not shortest representation
212
+ "\xc1\x80",
213
+ "\xc0\x30",
214
+ "\xc1\x30",
215
+ "\xe0\x80\x80",
216
+ "\xf0\x80\x80\x80",
217
+ ]
218
+ bad_data.each do |string|
219
+ assert !conn.valid_utf8?(string), "bad unicode specs 01: #{string}"
220
+ end
221
+ conn.disconnect
222
+ end
223
+
224
+ # Repeated headers test. Currently:
225
+ # - Apollo emits repeated headers for a 1.1 connection only
226
+ # - RabbitMQ does not emit repeated headers under any circumstances
227
+ def test_conn_1p_0120
228
+ dest = make_destination
229
+ msg = "payload: #{Time.now.to_f}"
230
+ shdrs = { "key1" => "val1", "key2" => "val2",
231
+ "key3" => ["kv3", "kv2", "kv1"] }
232
+ assert_nothing_raised {
233
+ @conn.publish dest, msg, shdrs
234
+ }
235
+ #
236
+ sid = @conn.uuid()
237
+ @conn.subscribe dest, :id => sid
238
+ #
239
+ received = @conn.receive
240
+ assert_equal msg, received.body
241
+ if @conn.protocol != Stomp::SPL_10
242
+ assert_equal shdrs["key3"], received.headers["key3"] unless ENV['STOMP_RABBIT']
243
+ else
244
+ assert_equal "kv3", received.headers["key3"]
245
+ end
246
+ #
247
+ @conn.unsubscribe dest, :id => sid
248
+ end
249
+
250
+ end if ENV['STOMP_TEST11']
251
+
data/test/test_helper.rb CHANGED
@@ -1,8 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
4
 
3
5
  require 'test/unit'
4
6
  require 'timeout'
5
7
  require 'stomp'
8
+ require 'tlogger'
9
+
10
+ begin
11
+ dummy = RUBY_ENGINE
12
+ rescue NameError => ne
13
+ RUBY_ENGINE = "unknown"
14
+ end
6
15
 
7
16
  # Helper routines
8
17
  module TestBase
@@ -35,6 +44,45 @@ module TestBase
35
44
  end
36
45
  end
37
46
 
47
+ def get_connection()
48
+ ch = get_conn_headers()
49
+ conn = Stomp::Connection.open(user, passcode, host, port, false, 5, ch)
50
+ conn
51
+ end
52
+
53
+ def get_client()
54
+ hash = { :hosts => [
55
+ {:login => user, :passcode => passcode, :host => host, :port => port},
56
+ ],
57
+ :connect_headers => get_conn_headers()
58
+ }
59
+
60
+ client = Stomp::Client.new(hash)
61
+ client
62
+ end
63
+
64
+ def get_conn_headers()
65
+ ch = {}
66
+ if ENV['STOMP_TEST11']
67
+ #
68
+ if Stomp::SUPPORTED.index(ENV['STOMP_TEST11'])
69
+ ch['accept-version'] = ENV['STOMP_TEST11']
70
+ else
71
+ ch['accept-version'] = Stomp::SPL_11
72
+ end
73
+ #
74
+ ch['host'] = ENV['STOMP_RABBIT'] ? "/" : host
75
+ end
76
+ ch
77
+ end
78
+
79
+ def conn_subscribe(dest, headers = {})
80
+ if @conn.protocol >= Stomp::SPL_11
81
+ headers[:id] = @conn.uuid() unless headers[:id]
82
+ end
83
+ @conn.subscribe dest, headers
84
+ end
85
+
38
86
  # Test helper methods
39
87
 
40
88
  def make_destination
data/test/test_message.rb CHANGED
@@ -1,15 +1,23 @@
1
+ #
2
+ # !!!!
3
+ #
4
+ # This can *NOT* currently be marked as UTF-8 encoded. It uses invalid UTF-8
5
+ # sequences for testing. Tests will fail under 1.9.x+ if this file is marked
6
+ # as UTF-8 encoded.
7
+ #
8
+
1
9
  $:.unshift(File.dirname(__FILE__))
2
10
  #
3
11
  # Test Ruby 1.8 with $KCODE='U'
4
12
  #
5
13
  require 'test_helper'
6
14
  #
7
- class TestMessageKcode < Test::Unit::TestCase
15
+ class TestMessage < Test::Unit::TestCase
8
16
  include TestBase
9
17
  #
10
18
  def setup
11
19
  $KCODE = 'U' if RUBY_VERSION =~ /1\.8/
12
- @conn = Stomp::Connection.open(user, passcode, host, port)
20
+ @conn = get_connection()
13
21
  # Message body data
14
22
  @messages = [
15
23
  "normal text message",
@@ -24,10 +32,15 @@ class TestMessageKcode < Test::Unit::TestCase
24
32
  end
25
33
 
26
34
  # Various message bodies, including the failing test case reported
27
- def test_kcode_001
35
+ def test_0010_kcode
28
36
  #
29
37
  dest = make_destination
30
- @conn.subscribe dest
38
+ if @conn.protocol == Stomp::SPL_10
39
+ @conn.subscribe dest
40
+ else
41
+ sh = {}
42
+ @conn.subscribe dest, sh, @conn.uuid()
43
+ end
31
44
  @messages.each do |abody|
32
45
  @conn.publish dest, abody
33
46
  msg = @conn.receive
@@ -37,13 +50,18 @@ class TestMessageKcode < Test::Unit::TestCase
37
50
  end
38
51
 
39
52
  # All possible byte values
40
- def test_kcode_002
53
+ def test_0020_kcode
41
54
  #
42
55
  abody = ""
43
56
  "\000".upto("\377") {|abyte| abody << abyte }
44
57
  #
45
58
  dest = make_destination
46
- @conn.subscribe dest
59
+ if @conn.protocol == Stomp::SPL_10
60
+ @conn.subscribe dest
61
+ else
62
+ sh = {}
63
+ @conn.subscribe dest, sh, @conn.uuid()
64
+ end
47
65
  @conn.publish dest, abody
48
66
  msg = @conn.receive
49
67
  assert_instance_of Stomp::Message , msg, "type check for #{abody}"
@@ -51,10 +69,15 @@ class TestMessageKcode < Test::Unit::TestCase
51
69
  end
52
70
 
53
71
  # A single byte at a time
54
- def test_kcode_003
72
+ def test_0030_kcode
55
73
  #
56
74
  dest = make_destination
57
- @conn.subscribe dest
75
+ if @conn.protocol == Stomp::SPL_10
76
+ @conn.subscribe dest
77
+ else
78
+ sh = {:id => @conn.uuid()}
79
+ @conn.subscribe dest, sh
80
+ end
58
81
  #
59
82
  "\000".upto("\377") do |abody|
60
83
  @conn.publish dest, abody
@@ -65,49 +88,76 @@ class TestMessageKcode < Test::Unit::TestCase
65
88
  end
66
89
 
67
90
  #
68
- def test_kcode_004
91
+ def test_0040_msg_create
69
92
  #
70
93
  assert_raise(Stomp::Error::InvalidFormat) {
71
- aframe = Stomp::Message.new("junk")
94
+ aframe = Stomp::Message.new("junk", false)
72
95
  }
73
96
  #
74
97
  assert_raise(Stomp::Error::InvalidFormat) {
75
- aframe = Stomp::Message.new("command\njunk")
98
+ aframe = Stomp::Message.new("command\njunk", false)
76
99
  }
77
100
  #
78
101
  assert_raise(Stomp::Error::InvalidFormat) {
79
- aframe = Stomp::Message.new("command\nheaders\n\njunk")
102
+ aframe = Stomp::Message.new("command\nheaders\n\njunk", false)
80
103
  }
81
104
  #
82
105
  assert_raise(Stomp::Error::InvalidServerCommand) {
83
- aframe = Stomp::Message.new("junkcommand\nheaders\n\njunk\0\n\n")
106
+ aframe = Stomp::Message.new("junkcommand\nheaders\n\njunk\0\n\n", false)
84
107
  }
85
108
  #
86
109
  assert_raise(Stomp::Error::InvalidFormat) {
87
- aframe = Stomp::Message.new("ERROR\nbadheaders\n\njunk\0\n\n")
110
+ aframe = Stomp::Message.new("ERROR\nbadheaders\n\njunk\0\n\n", false)
88
111
  }
89
112
  #
90
113
  assert_nothing_raised {
91
- aframe = Stomp::Message.new("CONNECTED\nh1:val1\n\njunk\0\n")
114
+ aframe = Stomp::Message.new("CONNECTED\nh1:val1\n\njunk\0\n", false)
92
115
  }
93
116
  #
94
117
  assert_nothing_raised {
95
- aframe = Stomp::Message.new("MESSAGE\nh1:val1\n\njunk\0\n")
118
+ aframe = Stomp::Message.new("MESSAGE\nh1:val1\n\njunk\0\n", false)
96
119
  }
97
120
  #
98
121
  assert_nothing_raised {
99
- aframe = Stomp::Message.new("MESSAGE\nh2:val2\n\n\0")
122
+ aframe = Stomp::Message.new("MESSAGE\nh2:val2\n\n\0", false)
100
123
  }
101
124
  #
102
125
  assert_nothing_raised {
103
- aframe = Stomp::Message.new("RECEIPT\nh1:val1\n\njunk\0\n")
126
+ aframe = Stomp::Message.new("RECEIPT\nh1:val1\n\njunk\0\n", false)
104
127
  }
105
128
  #
106
129
  assert_nothing_raised {
107
- aframe = Stomp::Message.new("ERROR\nh1:val1\n\njunk\0\n")
130
+ aframe = Stomp::Message.new("ERROR\nh1:val1\n\njunk\0\n", false)
108
131
  }
109
132
 
110
133
  end
111
134
 
135
+ # Multiple headers with the same key
136
+ def test_0050_mh_msg_create
137
+ aframe = bframe = nil
138
+ assert_nothing_raised {
139
+ amsg = "MESSAGE\n" +
140
+ "h1:val1\n" +
141
+ "h2:val3\n" +
142
+ "h2:val2\n" +
143
+ "h2:val1\n" +
144
+ "h3:val1\n" +
145
+ "\n" +
146
+ "payload" +
147
+ "\0\n"
148
+ aframe = Stomp::Message.new(amsg, false)
149
+ bframe = Stomp::Message.new(amsg, true)
150
+ }
151
+ #
152
+ assert aframe.headers["h2"].is_a?(String), "Expected a String"
153
+ assert_equal "val3", aframe.headers["h2"], "Expected 1st value"
154
+ #
155
+ assert bframe.headers["h2"].is_a?(Array), "Expected an Array"
156
+ assert_equal 3, bframe.headers["h2"].length, "Expected 3 values"
157
+ assert_equal "val3", bframe.headers["h2"][0], "Expected val3"
158
+ assert_equal "val2", bframe.headers["h2"][1], "Expected val2"
159
+ assert_equal "val1", bframe.headers["h2"][2], "Expected val1"
160
+ end
161
+
112
162
  end
113
163