shingara-blather 0.4.9 → 0.4.14

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.
@@ -1,21 +1,28 @@
1
1
  require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
2
2
 
3
+ def message_xml
4
+ <<-XML
5
+ <message
6
+ to='romeo@example.net'
7
+ from='juliet@example.com/balcony'
8
+ type='chat'
9
+ xml:lang='en'>
10
+ <body>Wherefore art thou, Romeo?</body>
11
+ <x xmlns='jabber:x:data' type='form'>
12
+ <field var='field-name' type='text-single' label='description' />
13
+ </x>
14
+ <paused xmlns="http://jabber.org/protocol/chatstates"/>
15
+ </message>
16
+ XML
17
+ end
18
+
3
19
  describe Blather::Stanza::Message do
4
20
  it 'registers itself' do
5
21
  Blather::XMPPNode.class_from_registration(:message, nil).must_equal Blather::Stanza::Message
6
22
  end
7
23
 
8
24
  it 'must be importable' do
9
- doc = parse_stanza <<-XML
10
- <message
11
- to='romeo@example.net'
12
- from='juliet@example.com/balcony'
13
- type='chat'
14
- xml:lang='en'>
15
- <body>Wherefore art thou, Romeo?</body>
16
- </message>
17
- XML
18
- Blather::XMPPNode.import(doc.root).must_be_instance_of Blather::Stanza::Message
25
+ Blather::XMPPNode.import(parse_stanza(message_xml).root).must_be_instance_of Blather::Stanza::Message
19
26
  end
20
27
 
21
28
  it 'provides "attr_accessor" for body' do
@@ -103,8 +110,9 @@ describe Blather::Stanza::Message do
103
110
  msg = Blather::Stanza::Message.new
104
111
  msg << (h = Blather::XMPPNode.new('html', msg.document))
105
112
  h.namespace = Blather::Stanza::Message::HTML_NS
106
- h << (b = Blather::XMPPNode.new('body', msg.document))
113
+ b = Blather::XMPPNode.new('body', msg.document)
107
114
  b.namespace = Blather::Stanza::Message::HTML_BODY_NS
115
+ h << b
108
116
 
109
117
  msg.xhtml_node.must_equal(b)
110
118
  end
@@ -113,14 +121,21 @@ describe Blather::Stanza::Message do
113
121
  msg = Blather::Stanza::Message.new
114
122
  xhtml = "<some>xhtml</some>"
115
123
  msg.xhtml = xhtml
116
- msg.xhtml_node.content.strip.must_equal(xhtml)
124
+ msg.xhtml_node.inner_html.strip.must_equal(xhtml)
117
125
  end
118
126
 
119
127
  it 'sets valid xhtml even if the input is not valid' do
120
128
  msg = Blather::Stanza::Message.new
121
129
  xhtml = "<some>xhtml"
122
130
  msg.xhtml = xhtml
123
- msg.xhtml_node.content.strip.must_equal("<some>xhtml</some>")
131
+ msg.xhtml_node.inner_html.strip.must_equal("<some>xhtml</some>")
132
+ end
133
+
134
+ it 'sets xhtml with more than one root node' do
135
+ msg = Blather::Stanza::Message.new
136
+ xhtml = "<i>xhtml</i> more xhtml"
137
+ msg.xhtml = xhtml
138
+ msg.xhtml_node.inner_html.strip.must_equal("<i>xhtml</i> more xhtml")
124
139
  end
125
140
 
126
141
  it 'has an xhtml getter' do
@@ -129,4 +144,64 @@ describe Blather::Stanza::Message do
129
144
  msg.xhtml = xhtml
130
145
  msg.xhtml.must_equal(xhtml)
131
146
  end
132
- end
147
+
148
+ it 'has a chat state setter' do
149
+ msg = Blather::Stanza::Message.new
150
+ msg.chat_state = :composing
151
+ msg.xpath('ns:composing', :ns => Blather::Stanza::Message::CHAT_STATE_NS).wont_be_empty
152
+ end
153
+
154
+ it 'will only add one chat state at a time' do
155
+ msg = Blather::Stanza::Message.new
156
+ msg.chat_state = :composing
157
+ msg.chat_state = :paused
158
+
159
+ msg.xpath('ns:*', :ns => Blather::Stanza::Message::CHAT_STATE_NS).size.must_equal(1)
160
+ end
161
+
162
+ it 'ensures chat state setter accepts strings' do
163
+ msg = Blather::Stanza::Message.new
164
+ msg.chat_state = "gone"
165
+ msg.xpath('ns:gone', :ns => Blather::Stanza::Message::CHAT_STATE_NS).wont_be_empty
166
+ end
167
+
168
+ it 'ensures chat state is one of Blather::Stanza::Message::VALID_CHAT_STATES' do
169
+ lambda do
170
+ msg = Blather::Stanza::Message.new
171
+ msg.chat_state = :invalid_chat_state
172
+ end.must_raise(Blather::ArgumentError)
173
+
174
+ Blather::Stanza::Message::VALID_CHAT_STATES.each do |valid_chat_state|
175
+ msg = Blather::Stanza::Message.new
176
+ msg.chat_state = valid_chat_state
177
+ msg.chat_state.must_equal valid_chat_state
178
+ end
179
+ end
180
+
181
+ it 'has a chat state getter' do
182
+ msg = Blather::Stanza::Message.new
183
+ msg.chat_state = :paused
184
+ msg.chat_state.must_equal(:paused)
185
+ end
186
+
187
+ it 'imports correct chat state' do
188
+ Blather::XMPPNode.import(parse_stanza(message_xml).root).chat_state.must_equal :paused
189
+ end
190
+
191
+ it 'makes a form child available' do
192
+ n = Blather::XMPPNode.import(parse_stanza(message_xml).root)
193
+ n.form.fields.size.must_equal 1
194
+ n.form.fields.map { |f| f.class }.uniq.must_equal [Blather::Stanza::X::Field]
195
+ n.form.must_be_instance_of Blather::Stanza::X
196
+
197
+ r = Blather::Stanza::Message.new
198
+ r.form.type = :form
199
+ r.form.type.must_equal :form
200
+ end
201
+
202
+ it 'ensures the form child is a direct child' do
203
+ r = Blather::Stanza::Message.new
204
+ r.form
205
+ r.xpath('ns:x', :ns => Blather::Stanza::X.registered_ns).wont_be_empty
206
+ end
207
+ end
@@ -69,7 +69,7 @@ describe Blather::Stanza::PubSub::Retract do
69
69
 
70
70
  it 'will iterate over each retraction' do
71
71
  Blather::XMPPNode.import(parse_stanza(retract_xml).root).each do |i|
72
- i.must_include %w[ae890ac52d0df67ed7cfdf51b644e901]
72
+ i.must_include "ae890ac52d0df67ed7cfdf51b644e901"
73
73
  end
74
74
  end
75
75
  end
@@ -59,9 +59,4 @@ describe Blather::Stanza::PubSub::PubSubItem do
59
59
  item.payload = nil
60
60
  item.payload.must_be_nil
61
61
  end
62
-
63
- it 'must have an entry child to item' do
64
- item = Blather::Stanza::PubSub::Items::PubSubItem.new 'foo', 'payload'
65
- item.find_first('ns:entry', :ns => 'http://www.w3.org/2005/Atom').wont_be_nil
66
- end
67
62
  end
@@ -0,0 +1,235 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
2
+
3
+ def x_xml
4
+ <<-XML
5
+ <x xmlns='jabber:x:data'
6
+ type='form'>
7
+ <title/>
8
+ <instructions/>
9
+ <field var='field-name'
10
+ type='text-single'
11
+ label='description' />
12
+ <field var='field-name2'
13
+ type='text-single'
14
+ label='description' />
15
+ <field var='field-name3'
16
+ type='text-single'
17
+ label='description' />
18
+ <field var='field-name4'
19
+ type='list-multi'
20
+ label='description'>
21
+ <desc/>
22
+ <required/>
23
+ <value>field-value4</value>
24
+ <option label='option-label'><value>option-value</value></option>
25
+ <option label='option-label'><value>option-value</value></option>
26
+ </field>
27
+ </x>
28
+ XML
29
+ end
30
+
31
+ describe Blather::Stanza::X do
32
+
33
+ it 'can be created from an XML string' do
34
+ x = Blather::Stanza::X.new parse_stanza(x_xml).root
35
+ x.type.must_equal :form
36
+ x.must_be_instance_of Blather::Stanza::X
37
+ end
38
+
39
+ [:cancel, :form, :result, :submit].each do |type|
40
+ it "type can be set as \"#{type}\"" do
41
+ x = Blather::Stanza::X.new type
42
+ x.type.must_equal type
43
+ end
44
+ end
45
+
46
+ it 'is constructed properly' do
47
+ n = Blather::Stanza::X.new :form
48
+ n.find("/ns:x[@type='form']", :ns => Blather::Stanza::X.registered_ns).wont_be_empty
49
+ end
50
+
51
+ it 'has an action attribute' do
52
+ n = Blather::Stanza::X.new :form
53
+ n.type.must_equal :form
54
+ n.type = :submit
55
+ n.type.must_equal :submit
56
+ end
57
+
58
+ it 'has a title attribute' do
59
+ n = Blather::Stanza::X.new :form
60
+ n.title.must_equal nil
61
+ n.title = "Hello World!"
62
+ n.title.must_equal "Hello World!"
63
+ n.title = "goodbye"
64
+ n.title.must_equal "goodbye"
65
+ end
66
+
67
+ it 'has an instructions attribute' do
68
+ n = Blather::Stanza::X.new :form
69
+ n.instructions.must_equal nil
70
+ n.instructions = "Please fill in this form"
71
+ n.instructions.must_equal "Please fill in this form"
72
+ n.instructions = "goodbye"
73
+ n.instructions.must_equal "goodbye"
74
+ end
75
+
76
+ it 'inherits a list of fields' do
77
+ n = Blather::Stanza::Iq::Command.new
78
+ n.command << parse_stanza(x_xml).root
79
+ r = Blather::Stanza::X.new.inherit n.form
80
+ r.fields.size.must_equal 4
81
+ r.fields.map { |f| f.class }.uniq.must_equal [Blather::Stanza::X::Field]
82
+ end
83
+
84
+ it 'returns a field object for a particular var' do
85
+ x = Blather::Stanza::X.new parse_stanza(x_xml).root
86
+ f = x.field 'field-name4'
87
+ f.must_be_instance_of Blather::Stanza::X::Field
88
+ f.value.must_equal 'field-value4'
89
+ end
90
+
91
+ it 'takes a list of hashes for fields' do
92
+ fields = [
93
+ {:label => 'label', :type => 'text-single', :var => 'var'},
94
+ {:label => 'label1', :type => 'text-single', :var => 'var1'},
95
+ ]
96
+
97
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label]),
98
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
99
+
100
+ di = Blather::Stanza::X.new nil, fields
101
+ di.fields.size.must_equal 2
102
+ di.fields.each { |f| control.include?(f).must_equal true }
103
+ end
104
+
105
+ it 'takes a list of Field objects as fields' do
106
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label1]),
107
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
108
+
109
+ di = Blather::Stanza::X.new nil, control
110
+ di.fields.size.must_equal 2
111
+ di.fields.each { |f| control.include?(f).must_equal true }
112
+ end
113
+
114
+ it 'takes a mix of hashes and field objects as fields' do
115
+ fields = [
116
+ {:label => 'label', :type => 'text-single', :var => 'var'},
117
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1]),
118
+ ]
119
+
120
+ control = [ Blather::Stanza::X::Field.new(*%w[var text-single label]),
121
+ Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
122
+
123
+ di = Blather::Stanza::X.new nil, fields
124
+ di.fields.size.must_equal 2
125
+ di.fields.each { |f| control.include?(f).must_equal true }
126
+ end
127
+
128
+ it 'allows adding of fields' do
129
+ di = Blather::Stanza::X.new nil
130
+ di.fields.size.must_equal 0
131
+ di.fields = [{:label => 'label', :type => 'text-single', :var => 'var', :required => true}]
132
+ di.fields.size.must_equal 1
133
+ di.fields += [Blather::Stanza::X::Field.new(*%w[var1 text-single label1])]
134
+ di.fields.size.must_equal 2
135
+ end
136
+
137
+ end
138
+
139
+ describe Blather::Stanza::X::Field do
140
+ it 'will auto-inherit nodes' do
141
+ n = parse_stanza "<field type='text-single' var='music' label='Music from the time of Shakespeare' />"
142
+ i = Blather::Stanza::X::Field.new n.root
143
+ i.type.must_equal 'text-single'
144
+ i.var.must_equal 'music'
145
+ i.label.must_equal 'Music from the time of Shakespeare'
146
+ end
147
+
148
+ it 'has a type attribute' do
149
+ n = Blather::Stanza::X::Field.new 'var', 'text-single'
150
+ n.type.must_equal 'text-single'
151
+ n.type = 'hidden'
152
+ n.type.must_equal 'hidden'
153
+ end
154
+
155
+ it 'has a var attribute' do
156
+ n = Blather::Stanza::X::Field.new 'name', 'text-single'
157
+ n.var.must_equal 'name'
158
+ n.var = 'email'
159
+ n.var.must_equal 'email'
160
+ end
161
+
162
+ it 'has a label attribute' do
163
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
164
+ n.label.must_equal 'Music from the time of Shakespeare'
165
+ n.label = 'Books by and about Shakespeare'
166
+ n.label.must_equal 'Books by and about Shakespeare'
167
+ end
168
+
169
+ it 'has a desc attribute' do
170
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
171
+ n.desc.must_equal nil
172
+ n.desc = 'Books by and about Shakespeare'
173
+ n.desc.must_equal 'Books by and about Shakespeare'
174
+ n.desc = 'goodbye'
175
+ n.desc.must_equal 'goodbye'
176
+ end
177
+
178
+ it 'has a required? attribute' do
179
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
180
+ n.required?.must_equal false
181
+ n.required = true
182
+ n.required?.must_equal true
183
+ n.required = false
184
+ n.required?.must_equal false
185
+ end
186
+
187
+ it 'has a value attribute' do
188
+ n = Blather::Stanza::X::Field.new 'subject', 'text-single', 'Music from the time of Shakespeare'
189
+ n.value.must_equal nil
190
+ n.value = 'book1'
191
+ n.value.must_equal 'book1'
192
+ n.value = 'book2'
193
+ n.value.must_equal 'book2'
194
+ end
195
+
196
+ # Option child elements
197
+ it 'allows adding of options' do
198
+ di = Blather::Stanza::X::Field.new nil
199
+ di.options.size.must_equal 0
200
+ di.options += [{:label => 'Person', :value => 'person'}]
201
+ di.options.size.must_equal 1
202
+ di.options += [Blather::Stanza::X::Field::Option.new(*%w[person1 Person1])]
203
+ di.options.size.must_equal 2
204
+ end
205
+
206
+ it 'raises an error when compared against a non X::Field' do
207
+ a = Blather::Stanza::X::Field.new('secret_message', 'hidden')
208
+ lambda { a == 'test' }.must_raise RuntimeError
209
+ end
210
+
211
+ it 'can determine equality' do
212
+ a = Blather::Stanza::X::Field.new('subject', 'text-single')
213
+ a.must_equal Blather::Stanza::X::Field.new('subject', 'text-single')
214
+ a.wont_equal Blather::Stanza::X::Field.new('subject1', 'text-single')
215
+ end
216
+ end
217
+
218
+ describe Blather::Stanza::X::Field::Option do
219
+
220
+ it 'has a value attribute' do
221
+ n = Blather::Stanza::X::Field::Option.new 'person1', 'Person 1'
222
+ n.value.must_equal 'person1'
223
+ n.value = 'book1'
224
+ n.value.must_equal 'book1'
225
+ end
226
+
227
+ it 'has a label attribute' do
228
+ n = Blather::Stanza::X::Field::Option.new 'person1', 'Person 1'
229
+ n.label.must_equal 'Person 1'
230
+ n.label = 'Book 1'
231
+ n.label.must_equal 'Book 1'
232
+ n.label = 'Book 2'
233
+ n.label.must_equal 'Book 2'
234
+ end
235
+ end
@@ -45,7 +45,7 @@ describe Blather::Stream::Client do
45
45
 
46
46
  it 'attempts to find the SRV record if a host is not provided' do
47
47
  dns = mock(:sort! => nil, :empty? => false)
48
- dns.expects(:each).yields(mock({
48
+ dns.expects(:detect).yields(mock({
49
49
  :target => 'd',
50
50
  :port => 5222
51
51
  }))
@@ -1008,4 +1008,17 @@ describe Blather::Stream::Client do
1008
1008
  comp.expects(:send_data).with { |s| s.wont_match(/^<message[^>]*from=/); true }
1009
1009
  comp.send msg
1010
1010
  end
1011
+
1012
+ it 'sends xml without formatting' do
1013
+ client = mock()
1014
+ client.stubs(:jid)
1015
+ client.stubs(:jid=)
1016
+
1017
+ msg = Blather::Stanza::Message.new 'to@jid.com', 'body'
1018
+ msg.xhtml = '<i>xhtml</i> body'
1019
+
1020
+ comp = Blather::Stream::Client.new nil, client, 'node@jid.com/resource', 'pass'
1021
+ comp.expects(:send_data).with { |s| s.wont_match(/\n/); true }
1022
+ comp.send msg
1023
+ end
1011
1024
  end
@@ -89,6 +89,12 @@ describe Blather::Stream::Parser do
89
89
  @client.data[0].xpath('//*[namespace-uri()="urn:ietf:params:xml:ns:xmpp-stanzas"]').size.must_equal 2
90
90
  end
91
91
 
92
+ it 'handles not absolute namespaces' do
93
+ assert_nothing_raised do
94
+ @parser.receive_data '<iq type="result" id="blather0007" to="n@d/r"><vCard xmlns="vcard-temp"/></iq>'
95
+ end
96
+ end
97
+
92
98
  it 'responds with stream:stream as a separate response' do
93
99
  data = [
94
100
  '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" to="example.com" version="1.0">',
@@ -135,10 +135,11 @@ describe Blather::XMPPNode do
135
135
  c.namespace = 'foo:bar'
136
136
  n << c
137
137
 
138
- n.find(:bar).size.must_equal 2
139
- n.remove_child 'bar', 'foo:bar'
140
138
  n.find(:bar).size.must_equal 1
141
- n.find(:bar).first.namespace.must_be_nil
139
+ n.find('//xmlns:bar', :xmlns => 'foo:bar').size.must_equal 1
140
+ n.remove_child '//xmlns:bar', :xmlns => 'foo:bar'
141
+ n.find(:bar).size.must_equal 1
142
+ n.find('//xmlns:bar', :xmlns => 'foo:bar').size.must_equal 0
142
143
  end
143
144
 
144
145
  it 'will remove all child elements' do