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 /** @type {Function} */
10 var onStanzaStr = null;
12 /** @type {function(string):void} */
13 var onError = function(msg) {};
15 /** @type {remoting.XmppStreamParser} */
18 QUnit.module('XmppStreamParser', {
19 beforeEach: function() {
20 onStanzaStr = sinon.spy();
21 onError = /** @type {function(string):void} */ (sinon.spy());
22 /** @param {Element} stanza */
23 function onStanza(stanza) {
24 onStanzaStr(new XMLSerializer().serializeToString(stanza));
26 parser = new remoting.XmppStreamParser();
27 parser.setCallbacks(onStanza, onError);
32 QUnit.test('should parse XMPP stream', function() {
33 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>'));
34 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
37 QUnit.test('should handle multiple incoming stanzas', function() {
38 parser.appendData(base.encodeUtf8('<stream><iq>text</iq><iq>more text</iq>'));
39 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
40 sinon.assert.calledWith(onStanzaStr, '<iq>more text</iq>');
43 QUnit.test('should ignore whitespace between stanzas', function() {
44 parser.appendData(base.encodeUtf8('<stream> <iq>text</iq>'));
45 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>');
48 QUnit.test('should assemble messages from small chunks', function() {
49 parser.appendData(base.encodeUtf8('<stream><i'));
50 parser.appendData(base.encodeUtf8('q>'));
52 // Split one UTF-8 sequence into two chunks
53 var data = base.encodeUtf8('😃');
54 parser.appendData(data.slice(0, 2));
55 parser.appendData(data.slice(2));
57 parser.appendData(base.encodeUtf8('</iq>'));
59 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>');
62 QUnit.test('should stop parsing on errors', function() {
63 parser.appendData(base.encodeUtf8('<stream>error<iq>text</iq>'));
64 sinon.assert.calledWith(onError);
65 sinon.assert.notCalled(onStanzaStr);
68 QUnit.test('should fail on invalid stream header', function() {
69 parser.appendData(base.encodeUtf8('<stream p=\'>'));
70 sinon.assert.calledWith(onError);
73 QUnit.test('should fail on loose text', function() {
74 parser.appendData(base.encodeUtf8('stream'));
75 sinon.assert.calledWith(onError);
78 QUnit.test('should fail on loose text with incomplete UTF-8 sequences',
80 var buffer = base.encodeUtf8('<stream>Ñ„')
82 buffer = buffer.slice(0, buffer.byteLength - 1);
83 parser.appendData(buffer);
84 sinon.assert.calledWith(onError);
87 QUnit.test('should fail on incomplete UTF-8 sequences', function() {
88 var buffer = base.encodeUtf8('<stream><iq>Ñ„')
90 buffer = buffer.slice(0, buffer.byteLength - 1);
91 parser.appendData(buffer);
92 parser.appendData(base.encodeUtf8('</iq>'));
93 sinon.assert.calledWith(onError);