From a5eecd12f5959a66774cda88e06e1b6f810a2d21 Mon Sep 17 00:00:00 2001 From: Josh Kramer Date: Sat, 12 Jul 2014 08:54:30 -0400 Subject: [PATCH 1/2] Add support for empty files, returns status code 204 by default --- canned.js | 15 +++++++++++++-- spec/canned.spec.js | 18 ++++++++++++++++++ spec/test_responses/_empty.get.json | 0 .../_empty_with_headers.get.json | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 spec/test_responses/_empty.get.json create mode 100644 spec/test_responses/_empty_with_headers.get.json diff --git a/canned.js b/canned.js index b2b6484..28320b2 100644 --- a/canned.js +++ b/canned.js @@ -84,13 +84,24 @@ Canned.prototype._extractOptions = function (data) { } lines.splice(0, 1) } - opts.statusCode = opts.statusCode || 200 + var defaultStatusCode; + if (lines.length === 0 || lines[0].length === 0) { + defaultStatusCode = 204 + } else { + defaultStatusCode = 200 + } + opts.statusCode = opts.statusCode || defaultStatusCode opts.data = lines.join('\n') return opts } Canned.prototype.sanatizeContent = function (data, fileObject) { var sanatized + + if (data.length === 0) { + return data; + } + switch (fileObject.mimetype) { case 'json': // make sure we return valid JSON even so we support comments @@ -123,7 +134,7 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { data = _data.data var statusCode = _data.statusCode var content = that.sanatizeContent(data, fileObject) - if (content) { + if (content !== false) { response = new Response(_data.contentType || getContentType(fileObject.mimetype), content, statusCode, httpObj.res, that.response_opts) cb(null, response) } else { diff --git a/spec/canned.spec.js b/spec/canned.spec.js index 618c8ca..4f677ef 100644 --- a/spec/canned.spec.js +++ b/spec/canned.spec.js @@ -62,6 +62,24 @@ describe('canned', function () { } can(req, res) }) + + it('sets 204 for empty file', function (done) { + req.url = '/empty' + res.end = function () { + expect(res.statusCode).toBe(204) + done() + } + can(req, res) + }) + + it('sets specified status for empty file with headers set', function (done) { + req.url = '/empty_with_headers' + res.end = function () { + expect(res.statusCode).toBe(420) + done() + } + can(req, res) + }) }) describe('content type', function () { diff --git a/spec/test_responses/_empty.get.json b/spec/test_responses/_empty.get.json new file mode 100644 index 0000000..e69de29 diff --git a/spec/test_responses/_empty_with_headers.get.json b/spec/test_responses/_empty_with_headers.get.json new file mode 100644 index 0000000..f855d0f --- /dev/null +++ b/spec/test_responses/_empty_with_headers.get.json @@ -0,0 +1 @@ +//! statusCode: 420 \ No newline at end of file From 08baf0e3fe6cec6f7a40a4703ce354dec0695d6b Mon Sep 17 00:00:00 2001 From: Josh Kramer Date: Sat, 12 Jul 2014 15:38:22 -0400 Subject: [PATCH 2/2] Remove erroneous semicolons, and add jkjustjoshing to contributors list in README --- README.md | 1 + canned.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5720d9f..2694754 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ Contributors * [runemadsen](https://github.com/runemadsen) * [mulderp](https://github.com/mulderp) * [creynders](https://github.com/creynders) +* [jkjustjoshing](https://github.com/jkjustjoshing) License ------- diff --git a/canned.js b/canned.js index 28320b2..5978b47 100644 --- a/canned.js +++ b/canned.js @@ -84,7 +84,7 @@ Canned.prototype._extractOptions = function (data) { } lines.splice(0, 1) } - var defaultStatusCode; + var defaultStatusCode if (lines.length === 0 || lines[0].length === 0) { defaultStatusCode = 204 } else { @@ -99,7 +99,7 @@ Canned.prototype.sanatizeContent = function (data, fileObject) { var sanatized if (data.length === 0) { - return data; + return data } switch (fileObject.mimetype) {