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 b2b6484..5978b47 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