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 var onStanzaStr = null; |
| 10 var onError = null; |
| 11 var parser = null; |
| 12 |
| 13 module('XmppStreamParser', { |
| 14 setup: function() { |
| 15 onStanzaStr = sinon.spy(); |
| 16 onError = sinon.spy(); |
| 17 function onStanza(stanza) { |
| 18 onStanzaStr(new XMLSerializer().serializeToString(stanza)); |
| 19 } |
| 20 parser = new remoting.XmppStreamParser(onStanza, onError); |
| 21 } |
| 22 }); |
| 23 |
| 24 |
| 25 test('should parse XMPP stream', function() { |
| 26 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>')); |
| 27 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 28 }); |
| 29 |
| 30 test('should handle multiple incoming stanzas', function() { |
| 31 parser.appendData(base.encodeUtf8('<stream><iq>text</iq><iq>more text</iq>')); |
| 32 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 33 sinon.assert.calledWith(onStanzaStr, '<iq>more text</iq>'); |
| 34 }); |
| 35 |
| 36 test('should ignore whitespace between stanzas', function() { |
| 37 parser.appendData(base.encodeUtf8('<stream> <iq>text</iq>')); |
| 38 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 39 }); |
| 40 |
| 41 test('should assemble messages from small chunks', function() { |
| 42 parser.appendData(base.encodeUtf8('<stream><i')); |
| 43 parser.appendData(base.encodeUtf8('q>')); |
| 44 |
| 45 // Split one UTF-8 sequence into two chunks |
| 46 var data = base.encodeUtf8('😃'); |
| 47 parser.appendData(data.slice(0, 2)); |
| 48 parser.appendData(data.slice(2)); |
| 49 |
| 50 parser.appendData(base.encodeUtf8('</iq>')); |
| 51 |
| 52 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>'); |
| 53 }); |
| 54 |
| 55 test('should stop parsing on errors', function() { |
| 56 parser.appendData(base.encodeUtf8('<stream>error<iq>text</iq>')); |
| 57 sinon.assert.calledWith(onError); |
| 58 sinon.assert.notCalled(onStanzaStr); |
| 59 }); |
| 60 |
| 61 test('should fail on invalid stream header', function() { |
| 62 parser.appendData(base.encodeUtf8('<stream p=\'>')); |
| 63 sinon.assert.calledWith(onError); |
| 64 }); |
| 65 |
| 66 test('should fail on loose text', function() { |
| 67 parser.appendData(base.encodeUtf8('stream')); |
| 68 sinon.assert.calledWith(onError); |
| 69 }); |
| 70 |
| 71 })(); |
OLD | NEW |