shingara-blather 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +162 -0
  3. data/examples/echo.rb +18 -0
  4. data/examples/execute.rb +16 -0
  5. data/examples/ping_pong.rb +37 -0
  6. data/examples/print_hierarchy.rb +76 -0
  7. data/examples/rosterprint.rb +14 -0
  8. data/examples/stream_only.rb +27 -0
  9. data/examples/xmpp4r/echo.rb +35 -0
  10. data/lib/blather/client/client.rb +310 -0
  11. data/lib/blather/client/dsl/pubsub.rb +170 -0
  12. data/lib/blather/client/dsl.rb +264 -0
  13. data/lib/blather/client.rb +87 -0
  14. data/lib/blather/core_ext/nokogiri.rb +40 -0
  15. data/lib/blather/errors/sasl_error.rb +43 -0
  16. data/lib/blather/errors/stanza_error.rb +107 -0
  17. data/lib/blather/errors/stream_error.rb +82 -0
  18. data/lib/blather/errors.rb +69 -0
  19. data/lib/blather/jid.rb +142 -0
  20. data/lib/blather/roster.rb +111 -0
  21. data/lib/blather/roster_item.rb +122 -0
  22. data/lib/blather/stanza/disco/disco_info.rb +176 -0
  23. data/lib/blather/stanza/disco/disco_items.rb +132 -0
  24. data/lib/blather/stanza/disco.rb +25 -0
  25. data/lib/blather/stanza/iq/query.rb +53 -0
  26. data/lib/blather/stanza/iq/roster.rb +179 -0
  27. data/lib/blather/stanza/iq.rb +138 -0
  28. data/lib/blather/stanza/message.rb +332 -0
  29. data/lib/blather/stanza/presence/status.rb +212 -0
  30. data/lib/blather/stanza/presence/subscription.rb +101 -0
  31. data/lib/blather/stanza/presence.rb +163 -0
  32. data/lib/blather/stanza/pubsub/affiliations.rb +79 -0
  33. data/lib/blather/stanza/pubsub/create.rb +65 -0
  34. data/lib/blather/stanza/pubsub/errors.rb +18 -0
  35. data/lib/blather/stanza/pubsub/event.rb +123 -0
  36. data/lib/blather/stanza/pubsub/items.rb +103 -0
  37. data/lib/blather/stanza/pubsub/publish.rb +103 -0
  38. data/lib/blather/stanza/pubsub/retract.rb +92 -0
  39. data/lib/blather/stanza/pubsub/subscribe.rb +68 -0
  40. data/lib/blather/stanza/pubsub/subscription.rb +134 -0
  41. data/lib/blather/stanza/pubsub/subscriptions.rb +81 -0
  42. data/lib/blather/stanza/pubsub/unsubscribe.rb +68 -0
  43. data/lib/blather/stanza/pubsub.rb +129 -0
  44. data/lib/blather/stanza/pubsub_owner/delete.rb +52 -0
  45. data/lib/blather/stanza/pubsub_owner/purge.rb +52 -0
  46. data/lib/blather/stanza/pubsub_owner.rb +51 -0
  47. data/lib/blather/stanza.rb +149 -0
  48. data/lib/blather/stream/client.rb +31 -0
  49. data/lib/blather/stream/component.rb +38 -0
  50. data/lib/blather/stream/features/resource.rb +63 -0
  51. data/lib/blather/stream/features/sasl.rb +187 -0
  52. data/lib/blather/stream/features/session.rb +44 -0
  53. data/lib/blather/stream/features/tls.rb +28 -0
  54. data/lib/blather/stream/features.rb +53 -0
  55. data/lib/blather/stream/parser.rb +102 -0
  56. data/lib/blather/stream.rb +231 -0
  57. data/lib/blather/xmpp_node.rb +218 -0
  58. data/lib/blather.rb +78 -0
  59. data/spec/blather/client/client_spec.rb +559 -0
  60. data/spec/blather/client/dsl/pubsub_spec.rb +462 -0
  61. data/spec/blather/client/dsl_spec.rb +143 -0
  62. data/spec/blather/core_ext/nokogiri_spec.rb +83 -0
  63. data/spec/blather/errors/sasl_error_spec.rb +33 -0
  64. data/spec/blather/errors/stanza_error_spec.rb +129 -0
  65. data/spec/blather/errors/stream_error_spec.rb +108 -0
  66. data/spec/blather/errors_spec.rb +33 -0
  67. data/spec/blather/jid_spec.rb +87 -0
  68. data/spec/blather/roster_item_spec.rb +96 -0
  69. data/spec/blather/roster_spec.rb +103 -0
  70. data/spec/blather/stanza/discos/disco_info_spec.rb +226 -0
  71. data/spec/blather/stanza/discos/disco_items_spec.rb +148 -0
  72. data/spec/blather/stanza/iq/query_spec.rb +64 -0
  73. data/spec/blather/stanza/iq/roster_spec.rb +140 -0
  74. data/spec/blather/stanza/iq_spec.rb +45 -0
  75. data/spec/blather/stanza/message_spec.rb +132 -0
  76. data/spec/blather/stanza/presence/status_spec.rb +132 -0
  77. data/spec/blather/stanza/presence/subscription_spec.rb +105 -0
  78. data/spec/blather/stanza/presence_spec.rb +66 -0
  79. data/spec/blather/stanza/pubsub/affiliations_spec.rb +57 -0
  80. data/spec/blather/stanza/pubsub/create_spec.rb +56 -0
  81. data/spec/blather/stanza/pubsub/event_spec.rb +84 -0
  82. data/spec/blather/stanza/pubsub/items_spec.rb +79 -0
  83. data/spec/blather/stanza/pubsub/publish_spec.rb +83 -0
  84. data/spec/blather/stanza/pubsub/retract_spec.rb +75 -0
  85. data/spec/blather/stanza/pubsub/subscribe_spec.rb +61 -0
  86. data/spec/blather/stanza/pubsub/subscription_spec.rb +97 -0
  87. data/spec/blather/stanza/pubsub/subscriptions_spec.rb +59 -0
  88. data/spec/blather/stanza/pubsub/unsubscribe_spec.rb +61 -0
  89. data/spec/blather/stanza/pubsub_owner/delete_spec.rb +50 -0
  90. data/spec/blather/stanza/pubsub_owner/purge_spec.rb +50 -0
  91. data/spec/blather/stanza/pubsub_owner_spec.rb +27 -0
  92. data/spec/blather/stanza/pubsub_spec.rb +67 -0
  93. data/spec/blather/stanza_spec.rb +116 -0
  94. data/spec/blather/stream/client_spec.rb +1011 -0
  95. data/spec/blather/stream/component_spec.rb +95 -0
  96. data/spec/blather/stream/parser_spec.rb +145 -0
  97. data/spec/blather/xmpp_node_spec.rb +231 -0
  98. data/spec/fixtures/pubsub.rb +311 -0
  99. data/spec/spec_helper.rb +43 -0
  100. metadata +249 -0
@@ -0,0 +1,462 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. .. .. spec_helper])
2
+ require File.join(File.dirname(__FILE__), *%w[.. .. .. fixtures pubsub])
3
+ require 'blather/client/dsl'
4
+
5
+ describe Blather::DSL::PubSub do
6
+ before do
7
+ @host = 'host.name'
8
+ @client = mock()
9
+ @client.stubs(:jid).returns Blather::JID.new('n@d/r')
10
+ @pubsub = Blather::DSL::PubSub.new @client, @host
11
+ end
12
+
13
+ it 'raises an error when trying to send a stanza without a host' do
14
+ @pubsub.host = nil
15
+ proc { @pubsub.affiliations }.must_raise RuntimeError
16
+ end
17
+
18
+ it 'requests affiliations' do
19
+ @client.expects(:write_with_handler).with do |n|
20
+ n.must_be_instance_of Blather::Stanza::PubSub::Affiliations
21
+ n.find('//ns:pubsub/ns:affiliations', :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
22
+ n.to.must_equal Blather::JID.new(@host)
23
+ n.type.must_equal :get
24
+ end
25
+ @pubsub.affiliations
26
+ end
27
+
28
+ it 'requests affiliations from a specified host' do
29
+ host = 'another.host'
30
+ @client.expects(:write_with_handler).with do |n|
31
+ n.must_be_instance_of Blather::Stanza::PubSub::Affiliations
32
+ n.find('//ns:pubsub/ns:affiliations', :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
33
+ n.to.must_equal Blather::JID.new(host)
34
+ n.type.must_equal :get
35
+ end
36
+ @pubsub.affiliations host
37
+ end
38
+
39
+ it 'requests subscriptions' do
40
+ @client.expects(:write_with_handler).with do |n|
41
+ n.must_be_instance_of Blather::Stanza::PubSub::Subscriptions
42
+ n.find('//ns:pubsub/ns:subscriptions', :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
43
+ n.to.must_equal Blather::JID.new(@host)
44
+ n.type.must_equal :get
45
+ end
46
+ @pubsub.subscriptions
47
+ end
48
+
49
+ it 'requests subscriptions from a specified host' do
50
+ host = 'another.host'
51
+ @client.expects(:write_with_handler).with do |n|
52
+ n.must_be_instance_of Blather::Stanza::PubSub::Subscriptions
53
+ n.find('//ns:pubsub/ns:subscriptions', :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
54
+ n.to.must_equal Blather::JID.new(host)
55
+ n.type.must_equal :get
56
+ end
57
+ @pubsub.subscriptions host
58
+ end
59
+
60
+ it 'requests nodes defaulting to "/"' do
61
+ @client.expects(:write_with_handler).with do |n|
62
+ n.must_be_instance_of Blather::Stanza::DiscoItems
63
+ n.find("/iq/ns:query[@node='/']", :ns => Blather::Stanza::DiscoItems.registered_ns).wont_be_empty
64
+ n.to.must_equal Blather::JID.new(@host)
65
+ n.type.must_equal :get
66
+ end
67
+ @pubsub.nodes nil
68
+ end
69
+
70
+ it 'requests nodes from a specified host' do
71
+ host = 'another.host'
72
+ @client.expects(:write_with_handler).with do |n|
73
+ n.must_be_instance_of Blather::Stanza::DiscoItems
74
+ n.find("/iq/ns:query[@node='/']", :ns => Blather::Stanza::DiscoItems.registered_ns).wont_be_empty
75
+ n.to.must_equal Blather::JID.new(host)
76
+ n.type.must_equal :get
77
+ end
78
+ @pubsub.nodes nil, host
79
+ end
80
+
81
+ it 'requests nodes under a specified path' do
82
+ @client.expects(:write_with_handler).with do |n|
83
+ n.must_be_instance_of Blather::Stanza::DiscoItems
84
+ n.find("/iq/ns:query[@node='/path/to/nodes']", :ns => Blather::Stanza::DiscoItems.registered_ns).wont_be_empty
85
+ n.to.must_equal Blather::JID.new(@host)
86
+ n.type.must_equal :get
87
+ end
88
+ @pubsub.nodes '/path/to/nodes'
89
+ end
90
+
91
+ it 'requests information on a node' do
92
+ @client.expects(:write_with_handler).with do |n|
93
+ n.must_be_instance_of Blather::Stanza::DiscoInfo
94
+ n.find("/iq/ns:query[@node='/path/to/node']", :ns => Blather::Stanza::DiscoInfo.registered_ns).wont_be_empty
95
+ n.to.must_equal Blather::JID.new(@host)
96
+ n.type.must_equal :get
97
+ end
98
+ @pubsub.node '/path/to/node'
99
+ end
100
+
101
+ it 'requests all items from a node' do
102
+ @client.expects(:write_with_handler).with do |n|
103
+ n.must_be_instance_of Blather::Stanza::PubSub::Items
104
+ n.find("/iq/ns:pubsub/ns:items[@node='/path/to/node']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
105
+ n.to.must_equal Blather::JID.new(@host)
106
+ n.type.must_equal :get
107
+ end
108
+ @pubsub.items '/path/to/node'
109
+ end
110
+
111
+ it 'requests specific items from a node' do
112
+ @client.expects(:write_with_handler).with do |n|
113
+ n.must_be_instance_of Blather::Stanza::PubSub::Items
114
+ n.find("/iq/ns:pubsub/ns:items[@node='/path/to/node'][ns:item[@id='item1']][ns:item[@id='item2']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
115
+ n.to.must_equal Blather::JID.new(@host)
116
+ n.type.must_equal :get
117
+ end
118
+ @pubsub.items '/path/to/node', %w[item1 item2]
119
+ end
120
+
121
+ it 'requests some items from a node' do
122
+ @client.expects(:write_with_handler).with do |n|
123
+ n.must_be_instance_of Blather::Stanza::PubSub::Items
124
+ n.find("/iq/ns:pubsub/ns:items[@node='/path/to/node' and @max_items='2']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
125
+ n.to.must_equal Blather::JID.new(@host)
126
+ n.type.must_equal :get
127
+ end
128
+ @pubsub.items '/path/to/node', nil, 2
129
+ end
130
+
131
+ it 'can publish items to a node with a hash' do
132
+ @client.expects(:write_with_handler).with do |n|
133
+ n.must_be_instance_of Blather::Stanza::PubSub::Publish
134
+ n.find("/iq[@type='set']/ns:pubsub/ns:publish[@node='/path/to/node' and ns:item[@id='id1' and .='payload1'] and ns:item[@id='id2' and .='payload2']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
135
+ n.to.must_equal Blather::JID.new(@host)
136
+ n.type.must_equal :set
137
+ end
138
+ @pubsub.publish '/path/to/node', {'id1' => 'payload1', 'id2' => 'payload2'}
139
+ end
140
+
141
+ it 'can publish items to a node with an array' do
142
+ @client.expects(:write_with_handler).with do |n|
143
+ n.must_be_instance_of Blather::Stanza::PubSub::Publish
144
+ n.find("/iq[@type='set']/ns:pubsub/ns:publish[@node='/path/to/node' and ns:item[.='payload1'] and ns:item[.='payload2']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
145
+ n.to.must_equal Blather::JID.new(@host)
146
+ n.type.must_equal :set
147
+ end
148
+ @pubsub.publish '/path/to/node', %w[payload1 payload2]
149
+ end
150
+
151
+ it 'can publish items to a node with a string' do
152
+ @client.expects(:write_with_handler).with do |n|
153
+ n.must_be_instance_of Blather::Stanza::PubSub::Publish
154
+ n.find("/iq[@type='set']/ns:pubsub/ns:publish[@node='/path/to/node' and ns:item[.='payload']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
155
+ n.to.must_equal Blather::JID.new(@host)
156
+ n.type.must_equal :set
157
+ end
158
+ @pubsub.publish '/path/to/node', 'payload'
159
+ end
160
+
161
+ it 'can retract an item with an array' do
162
+ @client.expects(:write_with_handler).with do |n|
163
+ n.must_be_instance_of Blather::Stanza::PubSub::Retract
164
+ n.find("/iq[@type='set']/ns:pubsub/ns:retract[@node='/path/to/node' and ns:item[@id='id1'] and ns:item[@id='id2']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
165
+ n.to.must_equal Blather::JID.new(@host)
166
+ n.type.must_equal :set
167
+ end
168
+ @pubsub.retract '/path/to/node', %w[id1 id2]
169
+ end
170
+
171
+ it 'can retract an item with a string' do
172
+ @client.expects(:write_with_handler).with do |n|
173
+ n.must_be_instance_of Blather::Stanza::PubSub::Retract
174
+ n.find("/iq[@type='set']/ns:pubsub/ns:retract[@node='/path/to/node' and ns:item[@id='id1']]", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
175
+ n.to.must_equal Blather::JID.new(@host)
176
+ n.type.must_equal :set
177
+ end
178
+ @pubsub.retract '/path/to/node', 'id1'
179
+ end
180
+
181
+ it 'can subscribe to a node with the default jid' do
182
+ @client.expects(:write_with_handler).with do |n|
183
+ n.must_be_instance_of Blather::Stanza::PubSub::Subscribe
184
+ n.find("/iq[@type='set']/ns:pubsub/ns:subscribe[@node='/path/to/node' and @jid='#{@client.jid.stripped}']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
185
+ n.to.must_equal Blather::JID.new(@host)
186
+ n.type.must_equal :set
187
+ end
188
+ @pubsub.subscribe '/path/to/node'
189
+ end
190
+
191
+ it 'can subscribe to a node with a specified jid as a string' do
192
+ @client.expects(:write_with_handler).with do |n|
193
+ n.must_be_instance_of Blather::Stanza::PubSub::Subscribe
194
+ n.find("/iq[@type='set']/ns:pubsub/ns:subscribe[@node='/path/to/node' and @jid='jid@d/r']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
195
+ n.to.must_equal Blather::JID.new(@host)
196
+ n.type.must_equal :set
197
+ end
198
+ @pubsub.subscribe '/path/to/node', 'jid@d/r'
199
+ end
200
+
201
+ it 'can subscribe to a node with a specified jid as a Blather::JID' do
202
+ @client.expects(:write_with_handler).with do |n|
203
+ n.must_be_instance_of Blather::Stanza::PubSub::Subscribe
204
+ n.find("/iq[@type='set']/ns:pubsub/ns:subscribe[@node='/path/to/node' and @jid='jid@d/r']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
205
+ n.to.must_equal Blather::JID.new(@host)
206
+ n.type.must_equal :set
207
+ end
208
+ @pubsub.subscribe '/path/to/node', Blather::JID.new('jid@d/r')
209
+ end
210
+
211
+ it 'can unsubscribe to a node with the default jid' do
212
+ @client.expects(:write_with_handler).with do |n|
213
+ n.must_be_instance_of Blather::Stanza::PubSub::Unsubscribe
214
+ n.find("/iq[@type='set']/ns:pubsub/ns:unsubscribe[@node='/path/to/node' and @jid='#{@client.jid.stripped}']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
215
+ n.to.must_equal Blather::JID.new(@host)
216
+ n.type.must_equal :set
217
+ end
218
+ @pubsub.unsubscribe '/path/to/node'
219
+ end
220
+
221
+ it 'can unsubscribe to a node with a specified jid as a string' do
222
+ @client.expects(:write_with_handler).with do |n|
223
+ n.must_be_instance_of Blather::Stanza::PubSub::Unsubscribe
224
+ n.find("/iq[@type='set']/ns:pubsub/ns:unsubscribe[@node='/path/to/node' and @jid='jid@d/r']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
225
+ n.to.must_equal Blather::JID.new(@host)
226
+ n.type.must_equal :set
227
+ end
228
+ @pubsub.unsubscribe '/path/to/node', 'jid@d/r'
229
+ end
230
+
231
+ it 'can unsubscribe to a node with a specified jid as a Blather::JID' do
232
+ @client.expects(:write_with_handler).with do |n|
233
+ n.must_be_instance_of Blather::Stanza::PubSub::Unsubscribe
234
+ n.find("/iq[@type='set']/ns:pubsub/ns:unsubscribe[@node='/path/to/node' and @jid='jid@d/r']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
235
+ n.to.must_equal Blather::JID.new(@host)
236
+ n.type.must_equal :set
237
+ end
238
+ @pubsub.unsubscribe '/path/to/node', Blather::JID.new('jid@d/r')
239
+ end
240
+
241
+ it 'can purge a node' do
242
+ @client.expects(:write_with_handler).with do |n|
243
+ n.must_be_instance_of Blather::Stanza::PubSubOwner::Purge
244
+ n.find("/iq[@type='set']/ns:pubsub/ns:purge[@node='/path/to/node']", :ns => Blather::Stanza::PubSubOwner.registered_ns).wont_be_empty
245
+ n.to.must_equal Blather::JID.new(@host)
246
+ n.type.must_equal :set
247
+ end
248
+ @pubsub.purge '/path/to/node'
249
+ end
250
+
251
+ it 'can create a node' do
252
+ @client.expects(:write_with_handler).with do |n|
253
+ n.must_be_instance_of Blather::Stanza::PubSub::Create
254
+ n.find("/iq[@type='set']/ns:pubsub/ns:create[@node='/path/to/node']", :ns => Blather::Stanza::PubSub.registered_ns).wont_be_empty
255
+ n.to.must_equal Blather::JID.new(@host)
256
+ n.type.must_equal :set
257
+ end
258
+ @pubsub.create '/path/to/node'
259
+ end
260
+
261
+ it 'can delete a node' do
262
+ @client.expects(:write_with_handler).with do |n|
263
+ n.must_be_instance_of Blather::Stanza::PubSubOwner::Delete
264
+ n.find("/iq[@type='set']/ns:pubsub/ns:delete[@node='/path/to/node']", :ns => Blather::Stanza::PubSubOwner.registered_ns).wont_be_empty
265
+ n.to.must_equal Blather::JID.new(@host)
266
+ n.type.must_equal :set
267
+ end
268
+ @pubsub.delete '/path/to/node'
269
+ end
270
+ end
271
+
272
+ describe 'Blather::DSL::PubSub callbacks' do
273
+ before do
274
+ @host = 'host.name'
275
+ @client = Blather::Client.setup Blather::JID.new('n@d/r'), 'pass'
276
+ @pubsub = Blather::DSL::PubSub.new @client, @host
277
+ end
278
+
279
+ it 'returns a list of affiliations when requesting affiliations' do
280
+ affiliations = Blather::XMPPNode.import(parse_stanza(affiliations_xml).root)
281
+ response = mock()
282
+ response.expects(:call).with { |n| n.must_equal affiliations.list }
283
+ @client.stubs(:write).with do |n|
284
+ affiliations.id = n.id
285
+ @client.receive_data affiliations
286
+ true
287
+ end
288
+ @pubsub.affiliations { |n| response.call n }
289
+ end
290
+
291
+ it 'returns a list of subscriptions when requesting subscriptions' do
292
+ subscriptions = Blather::XMPPNode.import(parse_stanza(subscriptions_xml).root)
293
+ response = mock()
294
+ response.expects(:call).with { |n| n.must_equal subscriptions.list }
295
+ @client.stubs(:write).with do |n|
296
+ subscriptions.id = n.id
297
+ @client.receive_data subscriptions
298
+ true
299
+ end
300
+ @pubsub.subscriptions { |n| response.call n }
301
+ end
302
+
303
+ it 'returns a list of node items when requesting nodes' do
304
+ nodes = Blather::XMPPNode.import(parse_stanza(<<-NODES).root)
305
+ <iq type='result'
306
+ from='pubsub.shakespeare.lit'
307
+ to='francisco@denmark.lit/barracks'
308
+ id='nodes1'>
309
+ <query xmlns='http://jabber.org/protocol/disco#items'>
310
+ <item jid='pubsub.shakespeare.lit'
311
+ node='blogs'
312
+ name='Weblog updates'/>
313
+ <item jid='pubsub.shakespeare.lit'
314
+ node='news'
315
+ name='News and announcements'/>
316
+ </query>
317
+ </iq>
318
+ NODES
319
+ response = mock()
320
+ response.expects(:call).with { |n| n.must_equal nodes.items }
321
+ @client.stubs(:write).with do |n|
322
+ nodes.id = n.id
323
+ @client.receive_data nodes
324
+ true
325
+ end
326
+ @pubsub.nodes { |n| response.call n }
327
+ end
328
+
329
+ it 'returns a DiscoInfo node when requesting a node' do
330
+ node = Blather::XMPPNode.import(parse_stanza(<<-NODES).root)
331
+ <iq type='result'
332
+ from='pubsub.shakespeare.lit'
333
+ to='francisco@denmark.lit/barracks'
334
+ id='meta1'>
335
+ <query xmlns='http://jabber.org/protocol/disco#info'
336
+ node='blogs'>
337
+ <identity category='pubsub' type='collection'/>
338
+ </query>
339
+ </iq>
340
+ NODES
341
+ response = mock()
342
+ response.expects(:call).with { |n| n.must_equal node }
343
+ @client.stubs(:write).with do |n|
344
+ node.id = n.id
345
+ @client.receive_data node
346
+ true
347
+ end
348
+ @pubsub.node('blogs') { |n| response.call n }
349
+ end
350
+
351
+ it 'returns a set of items when requesting items' do
352
+ items = Blather::XMPPNode.import(parse_stanza(items_all_nodes_xml).root)
353
+ response = mock()
354
+ response.expects(:call).with { |n| n.map{|i|i.to_s}.must_equal items.items.map{|i|i.to_s} }
355
+ @client.stubs(:write).with do |n|
356
+ items.id = n.id
357
+ @client.receive_data items
358
+ true
359
+ end
360
+ @pubsub.items('princely_musings') { |n| response.call n }
361
+ end
362
+
363
+ it 'returns aa subscription node when subscribing' do
364
+ subscription = Blather::XMPPNode.import(parse_stanza(subscription_xml).root)
365
+ response = mock()
366
+ response.expects(:call).with { |n| n.must_equal subscription }
367
+ @client.stubs(:write).with do |n|
368
+ subscription.id = n.id
369
+ @client.receive_data subscription
370
+ true
371
+ end
372
+ @pubsub.subscribe('princely_musings') { |n| response.call n }
373
+ end
374
+
375
+ it 'returns aa unsubscribe node when unsubscribing' do
376
+ unsubscribe = Blather::XMPPNode.import(parse_stanza(unsubscribe_xml).root)
377
+ response = mock()
378
+ response.expects(:call).with { |n| n.must_equal unsubscribe }
379
+ @client.stubs(:write).with do |n|
380
+ unsubscribe.id = n.id
381
+ @client.receive_data unsubscribe
382
+ true
383
+ end
384
+ @pubsub.unsubscribe('princely_musings') { |n| response.call n }
385
+ end
386
+
387
+ it 'returns a publish result when publishing to a node' do
388
+ result = Blather::XMPPNode.import(parse_stanza(<<-NODE).root)
389
+ <iq type='result'
390
+ from='pubsub.shakespeare.lit'
391
+ to='hamlet@denmark.lit/blogbot'
392
+ id='publish1'>
393
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
394
+ <publish node='princely_musings'>
395
+ <item id='ae890ac52d0df67ed7cfdf51b644e901'/>
396
+ </publish>
397
+ </pubsub>
398
+ </iq>
399
+ NODE
400
+ response = mock()
401
+ response.expects(:call).with { |n| n.must_equal result }
402
+ @client.stubs(:write).with do |n|
403
+ result.id = n.id
404
+ @client.receive_data result
405
+ true
406
+ end
407
+ @pubsub.publish('princely_musings', 'payload') { |n| response.call n }
408
+ end
409
+
410
+ it 'returns a create result when creating a node' do
411
+ result = Blather::XMPPNode.import(parse_stanza(<<-NODE).root)
412
+ <iq type='result'
413
+ from='pubsub.shakespeare.lit'
414
+ to='hamlet@denmark.lit/elsinore'
415
+ id='create2'>
416
+ <pubsub xmlns='http://jabber.org/protocol/pubsub'>
417
+ <create node='25e3d37dabbab9541f7523321421edc5bfeb2dae'/>
418
+ </pubsub>
419
+ </iq>
420
+ NODE
421
+ response = mock()
422
+ response.expects(:call).with { |n| n.must_equal result }
423
+ @client.stubs(:write).with do |n|
424
+ result.id = n.id
425
+ @client.receive_data result
426
+ true
427
+ end
428
+ @pubsub.create('princely_musings') { |n| response.call n }
429
+ end
430
+
431
+ it 'returns a purge result when purging a node' do
432
+ result = Blather::XMPPNode.import(parse_stanza(<<-NODE).root)
433
+ <iq type='result'
434
+ from='pubsub.shakespeare.lit'
435
+ id='purge1'/>
436
+ NODE
437
+ response = mock()
438
+ response.expects(:call).with { |n| n.must_equal result }
439
+ @client.stubs(:write).with do |n|
440
+ result.id = n.id
441
+ @client.receive_data result
442
+ true
443
+ end
444
+ @pubsub.purge('princely_musings') { |n| response.call n }
445
+ end
446
+
447
+ it 'returns a delete result when deleting a node' do
448
+ result = Blather::XMPPNode.import(parse_stanza(<<-NODE).root)
449
+ <iq type='result'
450
+ from='pubsub.shakespeare.lit'
451
+ id='delete1'/>
452
+ NODE
453
+ response = mock()
454
+ response.expects(:call).with { |n| n.must_equal result }
455
+ @client.stubs(:write).with do |n|
456
+ result.id = n.id
457
+ @client.receive_data result
458
+ true
459
+ end
460
+ @pubsub.delete('princely_musings') { |n| response.call n }
461
+ end
462
+ end
@@ -0,0 +1,143 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
2
+ require 'blather/client/dsl'
3
+
4
+ describe Blather::DSL do
5
+ before do
6
+ @client = mock()
7
+ @dsl = Class.new { include Blather::DSL }.new
8
+ Blather::Client.stubs(:new).returns(@client)
9
+ end
10
+
11
+ it 'wraps the setup' do
12
+ args = ['jid', 'pass', 'host', 0000]
13
+ @client.expects(:setup).with *args
14
+ @dsl.setup *args
15
+ end
16
+
17
+ it 'allows host to be nil in setup' do
18
+ args = ['jid', 'pass']
19
+ @client.expects(:setup).with *(args + [nil, nil])
20
+ @dsl.setup *args
21
+ end
22
+
23
+ it 'allows port to be nil in setup' do
24
+ args = ['jid', 'pass', 'host']
25
+ @client.expects(:setup).with *(args + [nil])
26
+ @dsl.setup *args
27
+ end
28
+
29
+ it 'stops when shutdown is called' do
30
+ @client.expects(:close)
31
+ @dsl.shutdown
32
+ end
33
+
34
+ it 'can throw a halt' do
35
+ catch(:halt) { @dsl.halt }
36
+ end
37
+
38
+ it 'can throw a pass' do
39
+ catch(:pass) { @dsl.pass }
40
+ end
41
+
42
+ it 'can setup before filters' do
43
+ guards = [:chat?, {:body => 'exit'}]
44
+ @client.expects(:register_filter).with :before, nil, *guards
45
+ @dsl.before nil, *guards
46
+ end
47
+
48
+ it 'can setup after filters' do
49
+ guards = [:chat?, {:body => 'exit'}]
50
+ @client.expects(:register_filter).with :after, nil, *guards
51
+ @dsl.after nil, *guards
52
+ end
53
+
54
+ it 'sets up handlers' do
55
+ type = :message
56
+ guards = [:chat?, {:body => 'exit'}]
57
+ @client.expects(:register_handler).with type, *guards
58
+ @dsl.handle type, *guards
59
+ end
60
+
61
+ it 'provides a helper for ready state' do
62
+ @client.expects(:register_handler).with :ready
63
+ @dsl.when_ready
64
+ end
65
+
66
+ it 'provides a helper for disconnected' do
67
+ @client.expects(:register_handler).with :disconnected
68
+ @dsl.disconnected
69
+ end
70
+
71
+ it 'sets the initial status' do
72
+ state = :away
73
+ msg = 'do not disturb'
74
+ @client.expects(:status=).with [state, msg]
75
+ @dsl.set_status state, msg
76
+ end
77
+
78
+ it 'provides a roster accessor' do
79
+ @client.expects :roster
80
+ @dsl.my_roster
81
+ end
82
+
83
+ it 'provides a << style writer that provides chaining' do
84
+ stanza = Blather::Stanza::Iq.new
85
+ @client.expects(:write).with stanza
86
+ (@dsl << stanza).must_equal @dsl
87
+ end
88
+
89
+ it 'provides a writer' do
90
+ stanza = Blather::Stanza::Iq.new
91
+ @client.expects(:write).with stanza
92
+ @dsl.write_to_stream stanza
93
+ end
94
+
95
+ it 'provides a "say" helper' do
96
+ to, msg = 'me@me.com', 'hello!'
97
+ Blather::Stanza::Message.stubs(:next_id).returns 0
98
+ @client.expects(:write).with { |n| n.to_s.must_equal Blather::Stanza::Message.new(to, msg).to_s }
99
+ @dsl.say to, msg
100
+ end
101
+
102
+ it 'provides a JID accessor' do
103
+ @client.expects :jid
104
+ @dsl.jid
105
+ end
106
+
107
+ it 'provides a disco helper for items' do
108
+ what, who, where = :items, 'me@me.com', 'my/node'
109
+ Blather::Stanza::Disco::DiscoItems.stubs(:next_id).returns 0
110
+ @client.expects(:register_tmp_handler).with '0'
111
+ expected_stanza = Blather::Stanza::Disco::DiscoItems.new
112
+ expected_stanza.to = who
113
+ expected_stanza.node = where
114
+ @client.expects(:write).with { |n| n.to_s.must_equal expected_stanza.to_s }
115
+ @dsl.discover what, who, where
116
+ end
117
+
118
+ it 'provides a disco helper for info' do
119
+ what, who, where = :info, 'me@me.com', 'my/node'
120
+ Blather::Stanza::Disco::DiscoInfo.stubs(:next_id).returns 0
121
+ @client.expects(:register_tmp_handler).with '0'
122
+ expected_stanza = Blather::Stanza::Disco::DiscoInfo.new
123
+ expected_stanza.to = who
124
+ expected_stanza.node = where
125
+ @client.expects(:write).with { |n| n.to_s.must_equal expected_stanza.to_s }
126
+ @dsl.discover what, who, where
127
+ end
128
+
129
+ Blather::Stanza.handler_list.each do |handler_method|
130
+ it "provides a helper method for #{handler_method}" do
131
+ guards = [:chat?, {:body => 'exit'}]
132
+ @client.expects(:register_handler).with handler_method, *guards
133
+ @dsl.__send__(handler_method, *guards)
134
+ end
135
+ end
136
+
137
+ it 'has a pubsub helper set to the jid domain' do
138
+ jid = Blather::JID.new('jid@domain/resource')
139
+ @client.stubs(:jid).returns jid
140
+ @dsl.pubsub.must_be_instance_of Blather::DSL::PubSub
141
+ @dsl.pubsub.host.must_equal jid.domain
142
+ end
143
+ end
@@ -0,0 +1,83 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
2
+
3
+ describe 'Nokogiri::XML::Node' do
4
+ before { @doc = Nokogiri::XML::Document.new }
5
+
6
+ it 'aliases #name to #element_name' do
7
+ node = Nokogiri::XML::Node.new 'foo', @doc
8
+ node.must_respond_to :element_name
9
+ node.element_name.must_equal node.name
10
+ end
11
+
12
+ it 'aliases #name= to #element_name=' do
13
+ node = Nokogiri::XML::Node.new 'foo', @doc
14
+ node.must_respond_to :element_name=
15
+ node.element_name.must_equal node.name
16
+ node.element_name = 'bar'
17
+ node.element_name.must_equal 'bar'
18
+ end
19
+
20
+ it 'allows symbols as hash keys for attributes' do
21
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
22
+ attrs['foo'] = 'bar'
23
+
24
+ attrs['foo'].must_equal 'bar'
25
+ attrs[:foo].must_equal 'bar'
26
+ end
27
+
28
+ it 'ensures a string is passed to the attribute setter' do
29
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
30
+ attrs[:foo] = 1
31
+ attrs[:foo].must_equal '1'
32
+
33
+ attrs[:jid] = Blather::JID.new('n@d/r')
34
+ attrs[:jid].must_equal 'n@d/r'
35
+ end
36
+
37
+ it 'removes an attribute when set to nil' do
38
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
39
+ attrs['foo'] = 'bar'
40
+
41
+ attrs['foo'].must_equal 'bar'
42
+ attrs['foo'] = nil
43
+ attrs['foo'].must_be_nil
44
+ end
45
+
46
+ it 'allows attribute values to change' do
47
+ attrs = Nokogiri::XML::Node.new('foo', @doc)
48
+ attrs['foo'] = 'bar'
49
+
50
+ attrs['foo'].must_equal 'bar'
51
+ attrs['foo'] = 'baz'
52
+ attrs['foo'].must_equal 'baz'
53
+ end
54
+
55
+ it 'allows symbols as the path in #xpath' do
56
+ node = Nokogiri::XML::Node.new('foo', @doc)
57
+ node.must_respond_to :find
58
+ @doc.root = node
59
+ @doc.xpath(:foo).first.wont_be_nil
60
+ @doc.xpath(:foo).first.must_equal @doc.xpath('/foo').first
61
+ end
62
+
63
+ it 'allows symbols as namespace names in #xpath' do
64
+ node = Nokogiri::XML::Node.new('foo', @doc)
65
+ node.namespace = node.add_namespace('bar', 'baz')
66
+ @doc.root = node
67
+ node.xpath('/bar:foo', :bar => 'baz').first.wont_be_nil
68
+ end
69
+
70
+ it 'aliases #xpath to #find' do
71
+ node = Nokogiri::XML::Node.new('foo', @doc)
72
+ node.must_respond_to :find
73
+ @doc.root = node
74
+ node.find('/foo').first.wont_be_nil
75
+ end
76
+
77
+ it 'has a helper function #find_first' do
78
+ node = Nokogiri::XML::Node.new('foo', @doc)
79
+ node.must_respond_to :find
80
+ @doc.root = node
81
+ node.find_first('/foo').must_equal node.find('/foo').first
82
+ end
83
+ end