Skip to content
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
15 changes: 13 additions & 2 deletions canned.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions spec/canned.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Empty file.
1 change: 1 addition & 0 deletions spec/test_responses/_empty_with_headers.get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! statusCode: 420