require 'spec_helper' describe Blather::Stream::Parser do let :client do Class.new do attr_reader :data attr_accessor :latch def stopped?; false; end def receive(node) @data ||= [] @data << node latch.countdown! end end.new end subject { Blather::Stream::Parser.new client } def process(*data) client.latch = CountDownLatch.new 1 data.each { |d| subject.receive_data d } client.latch.wait(2).should be_true end def check_parse(data) process data client.data.size.should == 1 client.data[0].to_s.gsub(/\n\s*/,'').should == data end it 'handles fragmented parsing' do process '', '', '' client.data.size.should == 1 client.data[0].to_s.gsub(/\n\s*/,'').should == '' 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...', '', "", "" ] process *data client.data.size.should == 1 Nokogiri::XML(client.data[0].to_s.gsub(/\n\s*/, '')).to_s.should == Nokogiri::XML(data.join).to_s client.data[0].xpath('//*[namespace-uri()="urn:ietf:params:xml:ns:xmpp-stanzas"]').size.should == 2 end it 'handles not absolute namespaces' do lambda do process '' end.should_not raise_error end it 'responds with stream:stream as a separate response' do process '', '' client.data.size.should == 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.should == 1 client.data[1].to_s.should == '' end it 'response with stream:end when receiving ' do process '' client.data.size.should == 2 client.data[1].to_s.should == '' end it 'raises ParseError when an error is sent' do lambda { process "" }.should raise_error(Blather::ParseError) end it 'handles stream stanzas without an issue' do process '', '' client.data.size.should == 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.should == 1 client.data[1].to_s.should == '' end it 'ignores the client namespace on stanzas' do process "", "exit", "exit", "" client.data.size.should == 1 client.data[0].document.xpath('/message/body[.="exit"]').should_not 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').should_not be_empty end it 'ignores the component namespace on stanzas' do process "", "exit", "exit", "" client.data.size.should == 1 client.data[0].document.xpath('/message/body[.="exit"]').should_not 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').should_not be_empty end end