Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
.vscode
9 changes: 5 additions & 4 deletions lib/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ StompFrame.prototype.send = function(stream) {
for (var key in this.headers) {
frame += key + ':' + this.headers[key] + '\n';
}
if (this.body.length > 0) {
if (!this.headers.hasOwnProperty('suppress-content-length')) {
frame += 'content-length:' + Buffer.byteLength(this.body) + '\n';
}
// User content-length for non-strings and if suppress-content-length header doesn't exist
var addContentLength = !(typeof this.body === 'string') && !this.headers.hasOwnProperty('suppress-content-length');
if (addContentLength && this.body.length > 0) {
// this.body is Buffer or ArrayBuffer
frame += 'content-length:' + this.body.length + '\n';
}
frame += '\n';
if (this.body.length > 0) {
Expand Down
73 changes: 65 additions & 8 deletions test/frame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ connectionObserver.write = function(data) {
this.writeBuffer.push(data);
};

// To support old and new nodejs Buffer api
var bufferFromString = function(str) {
return Buffer.from ? Buffer.from(str) : new Buffer(str);
}

module.exports = testCase({

setUp: function(callback) {
Expand Down Expand Up @@ -61,7 +66,7 @@ module.exports = testCase({
'header1': 'value1',
'header2': 'value2'
},
'body': 'wewp de doo'
'body': bufferFromString('wewp de doo')
});

// Command before headers, content-length auto-inserted, and terminating with null char (line feed chars for each line too)
Expand Down Expand Up @@ -125,7 +130,7 @@ module.exports = testCase({
'test content-length header is present when suppress-content-length is not': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'body' : 'Content length is 20'
'body' : bufferFromString('Content length is 20')
});
frame.send(connectionObserver);

Expand All @@ -152,10 +157,24 @@ module.exports = testCase({
test.equal(containsContentLengthHeader, false, "Content length header should not exist since we are suppressing it");
test.done();
},
'test stream write correctly handles single-byte UTF-8 characters': function(test) {
'test content-length is not present when frame.body is string': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'headers': {},
'body' : 'Content length is 20'
});
frame.send(connectionObserver);

//Check the headers for the content-length header
var writtenString = connectionObserver.writeBuffer.join('');
var containsContentLengthHeader = (writtenString.split("\n").indexOf("content-length:20") == -1 ? false : true);
test.equal(containsContentLengthHeader, false, "Content length header should not exist since we are suppressing it");
test.done();
},
'test stream write correctly handles single-byte UTF-8 characters as a buffer': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'body' : 'Welcome!'
'body' : bufferFromString('Welcome!')
});
frame.send(connectionObserver);

Expand All @@ -164,14 +183,33 @@ module.exports = testCase({
var contentLengthHeaderLine = writtenString.split("\n")[1];
var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim();

test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server");
test.equal(frame.body.length, contentLengthValue, "We should be truthful about how much data we plan to send to the server");

test.done();
},
'test stream write correctly handles multi-byte UTF-8 characters': function(test) {
'test stream write correctly handles single-byte UTF-8 characters as a string': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'body' : 'Ẇḗḽḉớḿẽ☃'
'body' : 'Welcome!'
});
frame.send(connectionObserver);

var expectedStream = [
'SEND\n\n',
'Welcome!',
'\0'
];

var writtenString = connectionObserver.writeBuffer.join('');

test.equal(expectedStream.join(''), writtenString, 'Sent content should be eqaul to expected content');

test.done();
},
'test stream write correctly handles multi-byte UTF-8 characters as a buffer': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'body' : bufferFromString('Ẇḗḽḉớḿẽ☃')
});
frame.send(connectionObserver);

Expand All @@ -180,7 +218,26 @@ module.exports = testCase({
var contentLengthHeaderLine = writtenString.split("\n")[1];
var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim();

test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server");
test.equal(frame.body.length, contentLengthValue, "We should be truthful about how much data we plan to send to the server");

test.done();
},
'test stream write correctly handles multi-byte UTF-8 characters as a string': function(test) {
var frame = new StompFrame({
'command': 'SEND',
'body' : 'Ẇḗḽḉớḿẽ☃'
});
frame.send(connectionObserver);

var expectedStream = [
'SEND\n\n',
'Ẇḗḽḉớḿẽ☃',
'\0'
];

var writtenString = connectionObserver.writeBuffer.join('');

test.equal(expectedStream.join(''), writtenString, 'Sent content should be eqaul to expected content');

test.done();
}
Expand Down