Skip to content

Commit

Permalink
Spec out Frame.as_string
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminws committed Jan 3, 2012
1 parent b03aa96 commit 3e7bed5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/frame.js
Expand Up @@ -19,4 +19,25 @@ Frame.prototype.build_frame = function(frame_args, want_receipt) {
return this;
};

Frame.prototype.as_string = function() {
var frame = '',
header_strings = [];

frame += this.command + '\n';

for (var header in this.headers) {
header_strings.push(header + ':' + this.headers[header]);
}

frame += header_strings.join('\n') + '\n\n';

if (this.body) {
frame += this.body;
}

frame += '\x00';

return frame;
};

module.exports.Frame = Frame;
39 changes: 39 additions & 0 deletions spec/FrameSpec.js
Expand Up @@ -99,3 +99,42 @@ describe('When asking for a receipt', function() {
});

});

describe('When asking for the frame as a string', function() {
var frame;
var receipt;
var frame_args;
var new_frame;

beforeEach(function() {
receipt = false;
frame_args = {
'body': 'test',
'headers': {'test': 'test'},
'command': 'test'
}
frame = new Frame();
new_frame = frame.build_frame(frame_args, receipt);
});

it('should return a string', function() {
expect(typeof(new_frame.as_string())).toBe('string');
});

it('should have the command, followed by a newline', function() {
expect(new_frame.as_string()).toMatch(/^test\n/);
});

it('should have one header, followed by two newlines', function() {
expect(new_frame.as_string()).toMatch(/^test\ntest:test\n\n/);
});

it('should have a body', function() {
expect(new_frame.as_string()).toMatch(/^test\ntest:test\n\ntest/);
});

it('should end with a null byte', function() {
expect(new_frame.as_string()).toMatch(/^test\ntest:test\n\ntest\0/);
});

});

0 comments on commit 3e7bed5

Please sign in to comment.