From 3e7bed5dbe2418003c13db99b328b5e9c7275c9a Mon Sep 17 00:00:00 2001 From: "Benjamin W. Smith" Date: Mon, 2 Jan 2012 23:19:17 -0500 Subject: [PATCH] Spec out Frame.as_string --- lib/frame.js | 21 +++++++++++++++++++++ spec/FrameSpec.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/frame.js b/lib/frame.js index fdbe29f..19608bd 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -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; diff --git a/spec/FrameSpec.js b/spec/FrameSpec.js index 5f2bb90..52d8215 100644 --- a/spec/FrameSpec.js +++ b/spec/FrameSpec.js @@ -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/); + }); + +});