frame.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module("Stomp Frame");
  2. test("marshall a CONNECT frame", function() {
  3. var out = Stomp.Frame.marshall("CONNECT", {login: 'jmesnil', passcode: 'wombats'});
  4. equals(out, "CONNECT\nlogin:jmesnil\npasscode:wombats\n\n\0");
  5. });
  6. test("marshall a SEND frame", function() {
  7. var out = Stomp.Frame.marshall("SEND", {destination: '/queue/test'}, "hello, world!");
  8. equals(out, "SEND\ndestination:/queue/test\ncontent-length:13\n\nhello, world!\0");
  9. });
  10. test("marshall a SEND frame without content-length", function() {
  11. var out = Stomp.Frame.marshall("SEND", {destination: '/queue/test', 'content-length': false}, "hello, world!");
  12. equals(out, "SEND\ndestination:/queue/test\n\nhello, world!\0");
  13. });
  14. test("unmarshall a CONNECTED frame", function() {
  15. var data = "CONNECTED\nsession-id: 1234\n\n\0";
  16. var frame = Stomp.Frame.unmarshall(data)[0];
  17. equals(frame.command, "CONNECTED");
  18. same(frame.headers, {'session-id': "1234"});
  19. equals(frame.body, '');
  20. });
  21. test("unmarshall a RECEIVE frame", function() {
  22. var data = "RECEIVE\nfoo: abc\nbar: 1234\n\nhello, world!\0";
  23. var frame = Stomp.Frame.unmarshall(data)[0];
  24. equals(frame.command, "RECEIVE");
  25. same(frame.headers, {foo: 'abc', bar: "1234"});
  26. equals(frame.body, "hello, world!");
  27. });
  28. test("unmarshall should not include the null byte in the body", function() {
  29. var body1 = 'Just the text please.',
  30. body2 = 'And the newline\n',
  31. msg = "MESSAGE\ndestination: /queue/test\nmessage-id: 123\n\n";
  32. equals(Stomp.Frame.unmarshall(msg + body1 + '\0')[0].body, body1);
  33. equals(Stomp.Frame.unmarshall(msg + body2 + '\0')[0].body, body2);
  34. });
  35. test("unmarshall should support colons (:) in header values", function() {
  36. var dest = 'foo:bar:baz',
  37. msg = "MESSAGE\ndestination: " + dest + "\nmessage-id: 456\n\n\0";
  38. equals(Stomp.Frame.unmarshall(msg)[0].headers.destination, dest);
  39. });
  40. test("only the 1st value of repeated headers is used", function() {
  41. var msg = "MESSAGE\ndestination: /queue/test\nfoo:World\nfoo:Hello\n\n\0";
  42. equals(Stomp.Frame.unmarshall(msg)[0].headers['foo'], 'World');
  43. });
  44. test("Content length of UTF-8 strings", function() {
  45. equals(0, Stomp.Frame.sizeOfUTF8());
  46. equals(0, Stomp.Frame.sizeOfUTF8(""));
  47. equals(1, Stomp.Frame.sizeOfUTF8("a"));
  48. equals(2, Stomp.Frame.sizeOfUTF8("ф"));
  49. equals(3, Stomp.Frame.sizeOfUTF8("№"));
  50. equals(15, Stomp.Frame.sizeOfUTF8("1 a ф № @ ®"));
  51. });