Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / xmpp_stream_parser_unittest.js
blob564d0e3223dcff4fc7ca8c10fd9528e695363e55
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.
5 (function() {
7 'use strict';
9 /** @type {Function} */
10 var onStanzaStr = null;
12 /** @type {function(string):void} */
13 var onError = function(msg) {};
15 /** @type {remoting.XmppStreamParser} */
16 var parser = null;
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);
29 });
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>');
35 });
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>');
41 });
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>');
46 });
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>');
60 });
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);
66 });
68 QUnit.test('should fail on invalid stream header', function() {
69 parser.appendData(base.encodeUtf8('<stream p=\'>'));
70 sinon.assert.calledWith(onError);
71 });
73 QUnit.test('should fail on loose text', function() {
74 parser.appendData(base.encodeUtf8('stream'));
75 sinon.assert.calledWith(onError);
76 });
78 QUnit.test('should fail on loose text with incomplete UTF-8 sequences',
79 function() {
80 var buffer = base.encodeUtf8('<stream>Ñ„')
81 // Crop last byte.
82 buffer = buffer.slice(0, buffer.byteLength - 1);
83 parser.appendData(buffer);
84 sinon.assert.calledWith(onError);
85 });
87 QUnit.test('should fail on incomplete UTF-8 sequences', function() {
88 var buffer = base.encodeUtf8('<stream><iq>Ñ„')
89 // Crop last byte.
90 buffer = buffer.slice(0, buffer.byteLength - 1);
91 parser.appendData(buffer);
92 parser.appendData(base.encodeUtf8('</iq>'));
93 sinon.assert.calledWith(onError);
94 });
96 })();