OLD | NEW |
| (Empty) |
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. | |
4 | |
5 (function() { | |
6 | |
7 'use strict'; | |
8 | |
9 /** @type {Function} */ | |
10 var onStanzaStr = null; | |
11 | |
12 /** @type {function(string):void} */ | |
13 var onError = function(msg) {}; | |
14 | |
15 /** @type {remoting.XmppStreamParser} */ | |
16 var parser = null; | |
17 | |
18 module('XmppStreamParser', { | |
19 setup: 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)); | |
25 } | |
26 parser = new remoting.XmppStreamParser(); | |
27 parser.setCallbacks(onStanza, onError); | |
28 } | |
29 }); | |
30 | |
31 | |
32 test('should parse XMPP stream', function() { | |
33 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>')); | |
34 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); | |
35 }); | |
36 | |
37 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 }); | |
42 | |
43 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 }); | |
47 | |
48 test('should assemble messages from small chunks', function() { | |
49 parser.appendData(base.encodeUtf8('<stream><i')); | |
50 parser.appendData(base.encodeUtf8('q>')); | |
51 | |
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)); | |
56 | |
57 parser.appendData(base.encodeUtf8('</iq>')); | |
58 | |
59 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>'); | |
60 }); | |
61 | |
62 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 }); | |
67 | |
68 test('should fail on invalid stream header', function() { | |
69 parser.appendData(base.encodeUtf8('<stream p=\'>')); | |
70 sinon.assert.calledWith(onError); | |
71 }); | |
72 | |
73 test('should fail on loose text', function() { | |
74 parser.appendData(base.encodeUtf8('stream')); | |
75 sinon.assert.calledWith(onError); | |
76 }); | |
77 | |
78 test('should fail on loose text with incomplete UTF-8 sequences', function() { | |
79 var buffer = base.encodeUtf8('<stream>Ñ„') | |
80 // Crop last byte. | |
81 buffer = buffer.slice(0, buffer.byteLength - 1); | |
82 parser.appendData(buffer); | |
83 sinon.assert.calledWith(onError); | |
84 }); | |
85 | |
86 test('should fail on incomplete UTF-8 sequences', function() { | |
87 var buffer = base.encodeUtf8('<stream><iq>Ñ„') | |
88 // Crop last byte. | |
89 buffer = buffer.slice(0, buffer.byteLength - 1); | |
90 parser.appendData(buffer); | |
91 parser.appendData(base.encodeUtf8('</iq>')); | |
92 sinon.assert.calledWith(onError); | |
93 }); | |
94 | |
95 })(); | |
OLD | NEW |