require 'spec_helper' describe Blather::Stream::Parser do before do @client = Class.new do attr_reader :data def stopped?; false; end def receive(data) @data ||= [] @data << data end end.new @parser = Blather::Stream::Parser.new @client end after { @client = @parser = nil} def check_parse(data) @parser.receive_data data @client.data.size.must_equal 1 @client.data[0].to_s.gsub(/\n\s*/,'').must_equal data end it 'handles fragmented parsing' do @parser.receive_data '' @parser.receive_data '' @parser.receive_data '' @client.data.size.must_equal 1 @client.data[0].to_s.gsub(/\n\s*/,'').must_equal '' end it 'handles a basic example' do check_parse "" end it 'handles a basic namespace definition' do check_parse '' end it 'handles multiple namespace definitions' do check_parse '' end it 'handles prefix namespacing' do check_parse '' end it 'handles namespaces with children' do check_parse "" end it 'handles multiple namespaces with children' do check_parse "" end it 'handles prefix namespaces with children' do check_parse "" end it 'handles prefix namespaces with children in the namespace' do check_parse "" end it 'handles prefix namespaces with children in the namespace' do check_parse "" end it 'handles attributes' do check_parse '' end it 'handles attributes with entities properly' do check_parse 'example' end it 'handles character input' do check_parse 'bar baz fizbuz' end it 'handles a complex input' do data = [ '', '', '', 'Some special application diagnostic information...', '', "", "", ] data.each { |d| @parser.receive_data d } @client.data.size.must_equal 1 @client.data[0].to_s.split("\n").map{|n|n.strip}.must_equal data @client.data[0].xpath('//*[namespace-uri()="urn:ietf:params:xml:ns:xmpp-stanzas"]').size.must_equal 2 end it 'handles not absolute namespaces' do assert_nothing_raised do @parser.receive_data '' end end it 'responds with stream:stream as a separate response' do data = [ '', '' ] data.each { |d| @parser.receive_data d } @client.data.size.must_equal 2 @client.data[0].document.xpath('/stream:stream[@to="example.com" and @version="1.0"]', 'xmlns' => 'jabber:client', 'stream' => 'http://etherx.jabber.org/streams').size.must_equal 1 @client.data[1].to_s.must_equal '' end it 'response with stream:end when receiving ' do @parser.receive_data '' @client.data.size.must_equal 2 @client.data[1].to_s.must_equal '' end it 'raises ParseError when an error is sent' do lambda { @parser.receive_data "" }.must_raise(Blather::ParseError) end it 'handles stream stanzas without an issue' do data = [ '', '' ] data.each { |d| @parser.receive_data d } @client.data.size.must_equal 2 @client.data[0].document.xpath('/stream:stream[@to="example.com" and @version="1.0"]', 'xmlns' => 'jabber:client', 'stream' => 'http://etherx.jabber.org/streams').size.must_equal 1 @client.data[1].to_s.must_equal '' end it 'ignores the client namespace on stanzas' do [ "", "exit", "exit", "" ].each { |d| @parser.receive_data d } @client.data.size.must_equal 1 @client.data[0].document.xpath('/message/body[.="exit"]').wont_be_empty @client.data[0].document.xpath('/message/im:html/xhtml:body[.="exit"]', 'im' => 'http://jabber.org/protocol/xhtml-im', 'xhtml' => 'http://www.w3.org/1999/xhtml').wont_be_empty end it 'ignores the component namespace on stanzas' do [ "", "exit", "exit", "" ].each { |d| @parser.receive_data d } @client.data.size.must_equal 1 @client.data[0].document.xpath('/message/body[.="exit"]').wont_be_empty @client.data[0].document.xpath('/message/im:html/xhtml:body[.="exit"]', 'im' => 'http://jabber.org/protocol/xhtml-im', 'xhtml' => 'http://www.w3.org/1999/xhtml').wont_be_empty end end