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 }
after { subject.finish }
def process(*data)
client.latch = CountDownLatch.new 1
data.each { |d| subject.receive_data d }
expect(client.latch.wait(2)).to be true
end
def check_parse(data)
process data
expect(client.data.size).to eq(1)
expect(client.data[0].to_s.gsub(/\n\s*/,'')).to eq(data)
end
it 'handles fragmented parsing' do
process '', '', ''
expect(client.data.size).to eq(1)
expect(client.data[0].to_s.gsub(/\n\s*/,'')).to eq('')
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
expect(client.data.size).to eq(1)
expect(Nokogiri::XML(client.data[0].to_s.gsub(/\n\s*/, '')).to_s).to eq(Nokogiri::XML(data.join).to_s)
expect(client.data[0].xpath('//*[namespace-uri()="urn:ietf:params:xml:ns:xmpp-stanzas"]').size).to eq(2)
end
it 'handles not absolute namespaces' do
expect do
process ''
end.not_to raise_error
end
it 'responds with stream:stream as a separate response' do
process '',
''
expect(client.data.size).to eq(2)
expect(client.data[0].document.xpath('/stream:stream[@to="example.com" and @version="1.0"]', 'xmlns' => 'jabber:client', 'stream' => 'http://etherx.jabber.org/streams').size).to eq(1)
expect(client.data[1].to_s).to eq('')
end
it 'response with stream:end when receiving ' do
process ''
expect(client.data.size).to eq(2)
expect(client.data[1].to_s).to eq('')
end
it 'raises ParseError when an error is sent' do
expect { process "" }.to raise_error(Blather::ParseError)
end
it 'handles stream stanzas without an issue' do
process '',
''
expect(client.data.size).to eq(2)
expect(client.data[0].document.xpath('/stream:stream[@to="example.com" and @version="1.0"]', 'xmlns' => 'jabber:client', 'stream' => 'http://etherx.jabber.org/streams').size).to eq(1)
expect(client.data[1].to_s).to eq('')
end
it 'ignores the client namespace on stanzas' do
process "",
"exit",
"exit",
""
expect(client.data.size).to eq(1)
expect(client.data[0].document.xpath('/message/body[.="exit"]')).not_to be_empty
expect(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')).not_to be_empty
end
it 'ignores the component namespace on stanzas' do
process "",
"exit",
"exit",
""
expect(client.data.size).to eq(1)
expect(client.data[0].document.xpath('/message/body[.="exit"]')).not_to be_empty
expect(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')).not_to be_empty
end
end