1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 var onStanzaStr = null;
13 module('XmppStreamParser', {
15 onStanzaStr = sinon.spy();
16 onError = sinon.spy();
17 function onStanza(stanza) {
18 onStanzaStr(new XMLSerializer().serializeToString(stanza));
20 parser = new remoting.XmppStreamParser();
21 parser.setCallbacks(onStanza, onError);
26 test('should parse XMPP stream', function() {
27 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>'));
28 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
31 test('should handle multiple incoming stanzas', function() {
32 parser.appendData(base.encodeUtf8('<stream><iq>text</iq><iq>more text</iq>'));
33 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
34 sinon.assert.calledWith(onStanzaStr, '<iq>more text</iq>');
37 test('should ignore whitespace between stanzas', function() {
38 parser.appendData(base.encodeUtf8('<stream> <iq>text</iq>'));
39 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
42 test('should assemble messages from small chunks', function() {
43 parser.appendData(base.encodeUtf8('<stream><i'));
44 parser.appendData(base.encodeUtf8('q>'));
46 // Split one UTF-8 sequence into two chunks
47 var data = base.encodeUtf8('😃');
48 parser.appendData(data.slice(0, 2));
49 parser.appendData(data.slice(2));
51 parser.appendData(base.encodeUtf8('</iq>'));
53 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>');
56 test('should stop parsing on errors', function() {
57 parser.appendData(base.encodeUtf8('<stream>error<iq>text</iq>'));
58 sinon.assert.calledWith(onError);
59 sinon.assert.notCalled(onStanzaStr);
62 test('should fail on invalid stream header', function() {
63 parser.appendData(base.encodeUtf8('<stream p=\'>'));
64 sinon.assert.calledWith(onError);
67 test('should fail on loose text', function() {
68 parser.appendData(base.encodeUtf8('stream'));
69 sinon.assert.calledWith(onError);
72 test('should fail on loose text with incomplete UTF-8 sequences', function() {
73 var buffer = base.encodeUtf8('<stream>Ñ„')
75 buffer = buffer.slice(0, buffer.byteLength - 1);
76 parser.appendData(buffer);
77 sinon.assert.calledWith(onError);
80 test('should fail on incomplete UTF-8 sequences', function() {
81 var buffer = base.encodeUtf8('<stream><iq>Ñ„')
83 buffer = buffer.slice(0, buffer.byteLength - 1);
84 parser.appendData(buffer);
85 parser.appendData(base.encodeUtf8('</iq>'));
86 sinon.assert.calledWith(onError);