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(); |
| 21 parser.setCallbacks(onStanza, onError); |
| 22 } |
| 23 }); |
| 24 |
| 25 |
| 26 test('should parse XMPP stream', function() { |
| 27 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>')); |
| 28 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 29 }); |
| 30 |
| 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>'); |
| 35 }); |
| 36 |
| 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>'); |
| 40 }); |
| 41 |
| 42 test('should assemble messages from small chunks', function() { |
| 43 parser.appendData(base.encodeUtf8('<stream><i')); |
| 44 parser.appendData(base.encodeUtf8('q>')); |
| 45 |
| 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)); |
| 50 |
| 51 parser.appendData(base.encodeUtf8('</iq>')); |
| 52 |
| 53 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>'); |
| 54 }); |
| 55 |
| 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); |
| 60 }); |
| 61 |
| 62 test('should fail on invalid stream header', function() { |
| 63 parser.appendData(base.encodeUtf8('<stream p=\'>')); |
| 64 sinon.assert.calledWith(onError); |
| 65 }); |
| 66 |
| 67 test('should fail on loose text', function() { |
| 68 parser.appendData(base.encodeUtf8('stream')); |
| 69 sinon.assert.calledWith(onError); |
| 70 }); |
| 71 |
| 72 test('should fail on loose text with incomplete UTF-8 sequences', function() { |
| 73 var buffer = base.encodeUtf8('<stream>Ñ„') |
| 74 // Crop last byte. |
| 75 buffer = buffer.slice(0, buffer.byteLength - 1); |
| 76 parser.appendData(buffer); |
| 77 sinon.assert.calledWith(onError); |
| 78 }); |
| 79 |
| 80 test('should fail on incomplete UTF-8 sequences', function() { |
| 81 var buffer = base.encodeUtf8('<stream><iq>Ñ„') |
| 82 // Crop last byte. |
| 83 buffer = buffer.slice(0, buffer.byteLength - 1); |
| 84 parser.appendData(buffer); |
| 85 parser.appendData(base.encodeUtf8('</iq>')); |
| 86 sinon.assert.calledWith(onError); |
| 87 }); |
| 88 |
| 89 })(); |
OLD | NEW |