wirev8_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @fileoverview Unit tests for goog.labs.net.webChannel.WireV8.
  16. *
  17. */
  18. goog.provide('goog.labs.net.webChannel.WireV8Test');
  19. goog.require('goog.labs.net.webChannel.WireV8');
  20. goog.require('goog.testing.jsunit');
  21. goog.setTestOnly('goog.labs.net.webChannel.WireV8Test');
  22. var wireCodec;
  23. function setUp() {
  24. wireCodec = new goog.labs.net.webChannel.WireV8();
  25. }
  26. function tearDown() {}
  27. function testEncodeSimpleMessage() {
  28. // scalar types only
  29. var message = {a: 'a', b: 'b'};
  30. var buff = [];
  31. wireCodec.encodeMessage(message, buff, 'prefix_');
  32. assertEquals(2, buff.length);
  33. assertEquals('prefix_a=a', buff[0]);
  34. assertEquals('prefix_b=b', buff[1]);
  35. }
  36. function testEncodeComplexMessage() {
  37. var message = {a: 'a', b: {x: 1, y: 2}};
  38. var buff = [];
  39. wireCodec.encodeMessage(message, buff, 'prefix_');
  40. assertEquals(2, buff.length);
  41. assertEquals('prefix_a=a', buff[0]);
  42. // a round-trip URI codec
  43. assertEquals('prefix_b={\"x\":1,\"y\":2}', decodeURIComponent(buff[1]));
  44. }
  45. function testEncodeMessageQueue() {
  46. var message1 = {a: 'a'};
  47. var queuedMessage1 = {map: message1, mapId: 3};
  48. var message2 = {b: 'b'};
  49. var queuedMessage2 = {map: message2, mapId: 4};
  50. var queue = [queuedMessage1, queuedMessage2];
  51. var result = wireCodec.encodeMessageQueue(queue, 2, null);
  52. assertEquals('count=2&ofs=3&req0_a=a&req1_b=b', result);
  53. }
  54. function testDecodeMessage() {
  55. var message = wireCodec.decodeMessage('[{"a":"a", "x":1}, {"b":"b"}]');
  56. assertTrue(goog.isArray(message));
  57. assertEquals(2, message.length);
  58. assertEquals('a', message[0].a);
  59. assertEquals(1, message[0].x);
  60. assertEquals('b', message[1].b);
  61. }