Added app.is() to register req.is() assertions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent bd65ecf · Browse files · 3 files·+71 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+71 added−7 removed
- tests:+32 added−6 removed
- other:+39 added−1 removed
Provenance
- Commit
0cb4677- Tree
73cb6d2- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
bd65ecf
3 files changed+71 added−7 removed
modified:lib/express/request.js+23 added-1 removed
213 unmodified lines214 * // => true215 *216 * req.is('html');217 * // => false218- *218+ * 219+ * Ad-hoc callbacks can also be registered with Express, to perform220+ * assertions again the request, for example if we need an expressive221+ * way to check if our incoming request is an image, we can register "an image"222+ * callback:223+ * 224+ * app.is('an image', function(req){225+ * return 0 == req.headers['content-type'].indexOf('image');226+ * });227+ * 228+ * Now within our route callbacks, we can use to to assert content types229+ * such as "image/jpeg", "image/png", etc.230+ * 231+ * app.post('/image/upload', function(req, res, next){232+ * if (req.is('an image')) {233+ * // do something234+ * } else {235+ * next();236+ * }237+ * });238+ * 239 * @param {String} type240 * @return {Boolean}241 * @api public242 */243 244 http.IncomingMessage.prototype.is = function(type){245+ var fn = this.app.is(type);246+ if (fn) return fn(this);247 var contentType = this.headers['content-type'];248 if (!contentType) return;249 if (!~type.indexOf('/')) type = mime.type('.' + type);250 // TODO: remove when connect no longer appends charset...19 unmodified linesmodified:lib/express/server.js+16 added
26 unmodified lines27 var self = this;28 this.config = {};29 this.settings = {};30 this.redirects = {};31+ this.isCallbacks = {};32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 this.errorHandlers = [];35 connect.Server.call(this, middleware || []);196 unmodified lines232 return this;233 };234 235 /**236+ * Register the given callback `fn` for the given `type`.237+ *238+ * @param {String} type239+ * @param {Function} fn240+ * @return {Server} for chaining241+ * @api public242+ */243+244+Server.prototype.is = function(type, fn){245+ if (!fn) return this.isCallbacks[type];246+ this.isCallbacks[type] = fn;247+ return this;248+};249+250+/**251 * Assign `setting` to `val`, or return `setting`'s value.252 * Mounted servers inherit their parent server's settings.253 *254 * @param {String} setting111 unmodified linesmodified:test/request.test.js+32 added-6 removed
155 unmodified lines156 },157 158 'test #is()': function(assert){159 var app = express.createServer();160+161+ app.is('an image', function(req){162+ return 0 == req.headers['content-type'].indexOf('image');163+ });164 165+ app.is('text', function(req){166+ return 0 == req.headers['content-type'].indexOf('text');167+ });168+169 app.get('/', function(req, res, next){162- if (req.is('html')) return res.send('html');163- res.send('not sure');170+ if (req.is('html')) return res.send('html');171+ res.send('not sure');172 });173 174 app.post('/', function(req, res, next){167- if (req.is('html')) return res.send('/ html');168- if (req.is('json')) return res.send('/ json');169- if (req.is('png')) return res.send('/ png');170- res.send('/ not sure');175+ if (req.is('html')) return res.send('/ html');176+ if (req.is('json')) return res.send('/ json');177+ if (req.is('png')) return res.send('/ png');178+ res.send('/ not sure');179 });180 181 app.post('/long', function(req, res, next){182 if (req.is('text/html')) return res.send('/long html');183 if (req.is('application/json')) return res.send('/long json');184 if (req.is('image/png')) return res.send('/long png');185 res.send('/long not sure');186 });187+ 188+ app.put('/custom', function(req, res, next){189+ if (req.is('an image')) return res.send('/custom an image');190+ if (req.is('text')) return res.send('/custom text');191+ });192 193 assert.response(app,194 { url: '/' },195 { body: 'not sure' });22 unmodified lines218 { body: '/long png' });219 assert.response(app,220 { url: '/long', method: 'POST' },221 { body: '/long not sure' });222+ 223+ assert.response(app,224+ { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }},225+ { body: '/custom text' });226+ assert.response(app,227+ { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }},228+ { body: '/custom text' });229+ assert.response(app,230+ { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }},231+ { body: '/custom an image' });232+ assert.response(app,233+ { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }},234+ { body: '/custom an image' });235 }236 };