spec/blather/errors/stanza_error_spec.rb in sprsquish-blather-0.3.4 vs spec/blather/errors/stanza_error_spec.rb in sprsquish-blather-0.4.0
- old
+ new
@@ -1,60 +1,55 @@
require File.join(File.dirname(__FILE__), *%w[.. .. spec_helper])
def stanza_error_node(type = 'cancel', error = 'internal-server-error', msg = nil)
- node = Stanza::Message.new 'error@jabber.local', 'test message', :error
- XML::Document.new.root = node
+ node = Blather::Stanza::Message.new 'error@jabber.local', 'test message', :error
- error_node = XMPPNode.new('error')
+ node << (error_node = Blather::XMPPNode.new('error'))
error_node['type'] = type.to_s
- err = XMPPNode.new(error)
+ error_node << (err = Blather::XMPPNode.new(error, error_node.document))
err.namespace = 'urn:ietf:params:xml:ns:xmpp-stanzas'
- error_node << err
if msg
- text = XMPPNode.new('text')
+ error_node << (text = Blather::XMPPNode.new('text', error_node.document))
text.namespace = 'urn:ietf:params:xml:ns:xmpp-stanzas'
- text << msg
- error_node << text
+ text.content = msg
end
- extra = XMPPNode.new('extra-error')
+ error_node << (extra = Blather::XMPPNode.new('extra-error', error_node.document))
extra.namespace = 'blather:stanza:error'
- extra << 'Blather Error'
- error_node << extra
+ extra.content = 'Blather Error'
- node << error_node
node
end
-describe 'Blather::StanzaError' do
+describe Blather::StanzaError do
it 'can import a node' do
- StanzaError.must_respond_to :import
- e = StanzaError.import stanza_error_node
- e.must_be_kind_of StanzaError
+ Blather::StanzaError.must_respond_to :import
+ e = Blather::StanzaError.import stanza_error_node
+ e.must_be_kind_of Blather::StanzaError
end
describe 'valid types' do
- before { @original = Stanza::Message.new 'error@jabber.local', 'test message', :error }
+ before { @original = Blather::Stanza::Message.new 'error@jabber.local', 'test message', :error }
it 'ensures type is one of Stanza::Message::VALID_TYPES' do
- lambda { StanzaError.new @original, :gone, :invalid_type_name }.must_raise(Blather::ArgumentError)
+ lambda { Blather::StanzaError.new @original, :gone, :invalid_type_name }.must_raise(Blather::ArgumentError)
- StanzaError::VALID_TYPES.each do |valid_type|
- msg = StanzaError.new @original, :gone, valid_type
+ Blather::StanzaError::VALID_TYPES.each do |valid_type|
+ msg = Blather::StanzaError.new @original, :gone, valid_type
msg.type.must_equal valid_type
end
end
end
describe 'when instantiated' do
before do
@type = 'cancel'
@err_name = 'internal-server-error'
@msg = 'the server has experienced a misconfiguration'
- @err = StanzaError.import stanza_error_node(@type, @err_name, @msg)
+ @err = Blather::StanzaError.import stanza_error_node(@type, @err_name, @msg)
end
it 'provides a type attribute' do
@err.must_respond_to :type
@err.type.must_equal @type.to_sym
@@ -70,11 +65,11 @@
@err.text.must_equal @msg
end
it 'provides a reader to the original node' do
@err.must_respond_to :original
- @err.original.must_be_instance_of Stanza::Message
+ @err.original.must_be_instance_of Blather::Stanza::Message
end
it 'provides an extras attribute' do
@err.must_respond_to :extras
@err.extras.must_be_instance_of Array
@@ -89,18 +84,17 @@
@err.inspect.must_match(/#{@msg}/)
end
it 'can be turned into xml' do
@err.must_respond_to :to_xml
- control = "<body>test message</body>\n<error>\n<internal-server-error xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>\n<text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">the server has experienced a misconfiguration</text>\n<extra-error xmlns=\"blather:stanza:error\">Blather Error</extra-error>\n</error>\n</message>".split("\n")
- test = @err.to_xml.split("\n")
- test_msg = test.shift
- test.must_equal control
+ doc = parse_stanza @err.to_xml
- test_msg.must_match(/<message[^>]*id="#{@err.original.id}"/)
- test_msg.must_match(/<message[^>]*from="error@jabber\.local"/)
- test_msg.must_match(/<message[^>]*type="error"/)
+ doc.xpath("/message[@from='error@jabber.local' and @type='error']").wont_be_empty
+ doc.xpath("/message/error").wont_be_empty
+ doc.xpath("/message/error/err_ns:internal-server-error", :err_ns => Blather::StanzaError::STANZA_ERR_NS).wont_be_empty
+ doc.xpath("/message/error/err_ns:text[.='the server has experienced a misconfiguration']", :err_ns => Blather::StanzaError::STANZA_ERR_NS).wont_be_empty
+ doc.xpath("/message/error/extra_ns:extra-error[.='Blather Error']", :extra_ns => 'blather:stanza:error').wont_be_empty
end
end
describe 'each XMPP stanza error type' do
%w[ bad-request
@@ -125,13 +119,11 @@
subscription-required
undefined-condition
unexpected-request
].each do |error_type|
it "handles the name for #{error_type}" do
- e = StanzaError.import stanza_error_node(:cancel, error_type)
+ e = Blather::StanzaError.import stanza_error_node(:cancel, error_type)
e.name.must_equal error_type.gsub('-','_').to_sym
end
end
end
end
-
-