| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Unit tests for goog.labs.net.webChannel.WireV8. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.labs.net.webChannel.WireV8Test'); |
| 22 |
|
| 23 |
goog.require('goog.labs.net.webChannel.WireV8'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
|
| 26 |
goog.setTestOnly('goog.labs.net.webChannel.WireV8Test'); |
| 27 |
|
| 28 |
|
| 29 |
var wireCodec; |
| 30 |
|
| 31 |
|
| 32 |
function setUp() { |
| 33 |
wireCodec = new goog.labs.net.webChannel.WireV8(); |
| 34 |
} |
| 35 |
|
| 36 |
function tearDown() {} |
| 37 |
|
| 38 |
|
| 39 |
function testEncodeSimpleMessage() { |
| 40 |
// scalar types only |
| 41 |
var message = {a: 'a', b: 'b'}; |
| 42 |
var buff = []; |
| 43 |
wireCodec.encodeMessage(message, buff, 'prefix_'); |
| 44 |
assertEquals(2, buff.length); |
| 45 |
assertEquals('prefix_a=a', buff[0]); |
| 46 |
assertEquals('prefix_b=b', buff[1]); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
function testEncodeComplexMessage() { |
| 51 |
var message = {a: 'a', b: {x: 1, y: 2}}; |
| 52 |
var buff = []; |
| 53 |
wireCodec.encodeMessage(message, buff, 'prefix_'); |
| 54 |
assertEquals(2, buff.length); |
| 55 |
assertEquals('prefix_a=a', buff[0]); |
| 56 |
// a round-trip URI codec |
| 57 |
assertEquals('prefix_b={\"x\":1,\"y\":2}', decodeURIComponent(buff[1])); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
function testEncodeMessageQueue() { |
| 62 |
var message1 = {a: 'a'}; |
| 63 |
var queuedMessage1 = {map: message1, mapId: 3}; |
| 64 |
var message2 = {b: 'b'}; |
| 65 |
var queuedMessage2 = {map: message2, mapId: 4}; |
| 66 |
var queue = [queuedMessage1, queuedMessage2]; |
| 67 |
var result = wireCodec.encodeMessageQueue(queue, 2, null); |
| 68 |
assertEquals('count=2&ofs=3&req0_a=a&req1_b=b', result); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
function testDecodeMessage() { |
| 73 |
var message = wireCodec.decodeMessage('[{"a":"a", "x":1}, {"b":"b"}]'); |
| 74 |
assertTrue(goog.isArray(message)); |
| 75 |
assertEquals(2, message.length); |
| 76 |
assertEquals('a', message[0].a); |
| 77 |
assertEquals(1, message[0].x); |
| 78 |
assertEquals('b', message[1].b); |
| 79 |
} |