express-mung
Advanced tools
Comparing version 0.2.0 to 0.3.0
21
index.js
@@ -16,3 +16,7 @@ 'use strict'; | ||
res.json = original; | ||
if (res.headersSent) | ||
return res; | ||
json = fn(json, req, res); | ||
if (res.headersSent) | ||
return res; | ||
@@ -46,4 +50,9 @@ // If no returned value from fn, then assume json has been mucked with. | ||
res.json = original; | ||
if (res.headersSent) | ||
return; | ||
fn(json, req, res) | ||
.then(json => { | ||
if (res.headersSent) | ||
return; | ||
// If null, then 204 No Content | ||
@@ -59,4 +68,4 @@ if (json === null) | ||
return original.call(this, json); | ||
}); | ||
return original.call(this, json); | ||
}); | ||
@@ -76,3 +85,9 @@ return faux_fin; | ||
res.end = original; | ||
if (res.headersSent) | ||
return; | ||
fn(req, res); | ||
if (res.headersSent) { | ||
console.error('sending response while in mung.headers is undefined behaviour'); | ||
return; | ||
} | ||
@@ -96,2 +111,4 @@ return original.apply(this, arguments); | ||
res.end = original; | ||
if (res.headersSent) | ||
return; | ||
original.apply(this, args); | ||
@@ -98,0 +115,0 @@ }); |
{ | ||
"name": "express-mung", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Transform an express response (or make until no good)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -55,2 +55,6 @@ # express-mung [![Build Status](https://travis-ci.org/richardschneider/express-prefer.svg)](https://travis-ci.org/richardschneider/express-mung) | ||
**NOTE** when `mung.json*` detects that a response has been sent, it will abort. | ||
**NOTE** sending a response while in `mung.headers*` is **undefined behaviour** and will most likely result in an error. | ||
# License | ||
@@ -57,0 +61,0 @@ The MIT license |
@@ -76,2 +76,19 @@ 'use strict'; | ||
it('should abort if a response is sent', done => { | ||
function error (json, req, res) { | ||
res.status(403).send('no permissions') | ||
} | ||
let server = express() | ||
.use(mung.json(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(403) | ||
.expect(res => { | ||
res.text.should.equal('no permissions'); | ||
res.headers.should.have.property('content-type', 'text/html; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
}) |
@@ -69,2 +69,20 @@ 'use strict'; | ||
it('should abort if a response is sent', done => { | ||
function error (json, req, res) { | ||
res.status(403).send('no permissions') | ||
return Promise.resolve(json); | ||
} | ||
let server = express() | ||
.use(mung.jsonAsync(error)) | ||
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end()); | ||
request(server) | ||
.get('/') | ||
.expect(403) | ||
.expect(res => { | ||
res.text.should.equal('no permissions'); | ||
res.headers.should.have.property('content-type', 'text/html; charset=utf-8'); | ||
}) | ||
.end(done); | ||
}); | ||
}) |
15539
298
63