frame.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var util = require('util'),
  2. Events = require('events').EventEmitter,
  3. nodeunit = require('nodeunit'),
  4. testCase = require('nodeunit').testCase,
  5. StompFrame = require('../lib/frame').StompFrame;
  6. // Mock net object so we never try to send any real data
  7. var connectionObserver = new Events();
  8. connectionObserver.writeBuffer = [];
  9. connectionObserver.write = function(data) {
  10. this.writeBuffer.push(data);
  11. };
  12. module.exports = testCase({
  13. setUp: function(callback) {
  14. callback();
  15. },
  16. tearDown: function(callback) {
  17. connectionObserver.writeBuffer = [];
  18. callback();
  19. },
  20. 'test StompFrame utility methods work correctly': function (test) {
  21. var frame = new StompFrame({
  22. 'command': 'HITME',
  23. 'headers': {
  24. 'header1': 'value1',
  25. 'header2': 'value2'
  26. },
  27. 'body': 'wewp de doo'
  28. });
  29. test.equal(frame.command, 'HITME');
  30. // setCommand
  31. frame.setCommand('SOMETHINGELSE');
  32. test.equal(frame.command, 'SOMETHINGELSE');
  33. // setHeader
  34. frame.setHeader('header2', 'newvalue');
  35. test.equal(frame.headers['header2'], 'newvalue');
  36. frame.setHeader('new-header', 'blah');
  37. test.equal(frame.headers['new-header'], 'blah');
  38. // TODO - Content-length assignment? Why is this done?
  39. // appendToBody
  40. frame.appendToBody('pip pop');
  41. test.equal(frame.body, 'wewp de doopip pop');
  42. test.done();
  43. },
  44. 'test stream writes are correct on arbitrary frame definition': function (test) {
  45. var frame = new StompFrame({
  46. 'command': 'HITME',
  47. 'headers': {
  48. 'header1': 'value1',
  49. 'header2': 'value2'
  50. },
  51. 'body': 'wewp de doo'
  52. });
  53. // Command before headers, content-length auto-inserted, and terminating with null char (line feed chars for each line too)
  54. var expectedStream = [
  55. 'HITME\n',
  56. 'header1:value1\n',
  57. 'header2:value2\n',
  58. 'content-length:11\n',
  59. '\n',
  60. 'wewp de doo',
  61. '\u0000'
  62. ];
  63. frame.send(connectionObserver);
  64. test.deepEqual(expectedStream.join(''), connectionObserver.writeBuffer.join(''), 'frame stream data is correctly output on the mocked wire');
  65. test.done();
  66. },
  67. 'check validation of arbitrary frame with arbitrary frame construct': function (test) {
  68. var validation,
  69. frameConstruct = {
  70. 'headers': {
  71. 'blah': { required: true },
  72. 'regexheader': { required: true, regex: /(wibble|wobble)/ }
  73. }
  74. };
  75. var frame = new StompFrame({
  76. 'command': 'COMMAND',
  77. 'headers': {},
  78. 'body': ''
  79. });
  80. // Invalid header (required)
  81. validation = frame.validate(frameConstruct);
  82. test.equal(validation.isValid, false);
  83. test.equal(validation.message, 'Header "blah" is required for COMMAND');
  84. test.equal(validation.details, 'Frame: {"command":"COMMAND","headers":{},"body":""}');
  85. frame.setHeader('blah', 'something or other'); // Set it now so it doesn't complain in later tests
  86. // Invalid header (regex)
  87. frame.setHeader('regexheader', 'not what it should be');
  88. validation = frame.validate(frameConstruct);
  89. test.equal(validation.isValid, false);
  90. test.equal(validation.message, 'Header "regexheader" has value "not what it should be" which does not match against the following regex: /(wibble|wobble)/ (Frame: {"command":"COMMAND","headers":{"blah":"something or other","regexheader":"not what it should be"},"body":""})');
  91. // Now make the header valid
  92. frame.setHeader('regexheader', 'wibble');
  93. validation = frame.validate(frameConstruct);
  94. test.equal(validation.isValid, true);
  95. frame.setHeader('regexheader', 'wobble');
  96. validation = frame.validate(frameConstruct);
  97. test.equal(validation.isValid, true, 'still valid!');
  98. test.done();
  99. },
  100. 'test content-length header is present when suppress-content-length is not': function(test) {
  101. var frame = new StompFrame({
  102. 'command': 'SEND',
  103. 'body' : 'Content length is 20'
  104. });
  105. frame.send(connectionObserver);
  106. //Check the headers for the content-length header
  107. var writtenString = connectionObserver.writeBuffer.join('');
  108. var containsContentLengthHeader = (writtenString.split("\n").indexOf("content-length:20") == -1 ? false : true);
  109. test.equal(containsContentLengthHeader, true, "Content length header should exist since we are not suppressing it");
  110. test.done();
  111. },
  112. 'test content-length is not present when suppress-content-length is provided': function(test) {
  113. var frame = new StompFrame({
  114. 'command': 'SEND',
  115. 'headers': {
  116. 'suppress-content-length': true
  117. },
  118. 'body' : 'Content length is 20'
  119. });
  120. frame.send(connectionObserver);
  121. //Check the headers for the content-length header
  122. var writtenString = connectionObserver.writeBuffer.join('');
  123. var containsContentLengthHeader = (writtenString.split("\n").indexOf("content-length:20") == -1 ? false : true);
  124. test.equal(containsContentLengthHeader, false, "Content length header should not exist since we are suppressing it");
  125. test.done();
  126. },
  127. 'test stream write correctly handles single-byte UTF-8 characters': function(test) {
  128. var frame = new StompFrame({
  129. 'command': 'SEND',
  130. 'body' : 'Welcome!'
  131. });
  132. frame.send(connectionObserver);
  133. var writtenString = connectionObserver.writeBuffer.join('');
  134. //Assume content-length header is second line
  135. var contentLengthHeaderLine = writtenString.split("\n")[1];
  136. var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim();
  137. test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server");
  138. test.done();
  139. },
  140. 'test stream write correctly handles multi-byte UTF-8 characters': function(test) {
  141. var frame = new StompFrame({
  142. 'command': 'SEND',
  143. 'body' : 'Ẇḗḽḉớḿẽ☃'
  144. });
  145. frame.send(connectionObserver);
  146. var writtenString = connectionObserver.writeBuffer.join('');
  147. //Assume content-length header is second line
  148. var contentLengthHeaderLine = writtenString.split("\n")[1];
  149. var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim();
  150. test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server");
  151. test.done();
  152. }
  153. });