Merge branch 'is'
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 · parents a2a1aad, 0cb4677 · Browse files · 3 files·+154 added−0 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+154 added−0 removed
- tests:+79 added−0 removed
- other:+75 added−0 removed
Provenance
- Commit
5b7d231- Tree
73cb6d2- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
a2a1aad- Parent 2
0cb4677
3 files changed+154 added−0 removed
modified:lib/express/request.js+59 added
191 unmodified lines192 return msgs;193 }194 };195 196+/**197+ * Check if the incoming request contains the "Content-Type" 198+ * header field, and it contains the give mime `type`.199+ *200+ * Examples:201+ *202+ * // With Content-Type: text/html; charset=utf-8203+ * req.is('html');204+ * // => true205+ *206+ * req.is('text/html');207+ * // => true208+ *209+ * // When Content-Type is application/json210+ * req.is('json');211+ * // => true212+ *213+ * req.is('application/json');214+ * // => true215+ *216+ * req.is('html');217+ * // => false218+ * 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...251+ if (~type.indexOf(';')) type = type.split(';')[0];252+ return ~contentType.indexOf(type);253+};254+255 // Callback for isXMLHttpRequest / xhr256 257 function isxhr() {258 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';11 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+79 added
151 unmodified linesExpand all152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236151 unmodified linesExpand all assert.response(app, { url: '/' }, { body: 'ok' }); }, 'test #is()': function(assert){ var app = express.createServer();
app.is('an image', function(req){ return 0 == req.headers['content-type'].indexOf('image'); }); app.is('text', function(req){ return 0 == req.headers['content-type'].indexOf('text'); });
app.get('/', function(req, res, next){ if (req.is('html')) return res.send('html'); res.send('not sure'); }); app.post('/', function(req, res, next){ if (req.is('html')) return res.send('/ html'); if (req.is('json')) return res.send('/ json'); if (req.is('png')) return res.send('/ png'); res.send('/ not sure'); }); app.post('/long', function(req, res, next){ if (req.is('text/html')) return res.send('/long html'); if (req.is('application/json')) return res.send('/long json'); if (req.is('image/png')) return res.send('/long png'); res.send('/long not sure'); }); app.put('/custom', function(req, res, next){ if (req.is('an image')) return res.send('/custom an image'); if (req.is('text')) return res.send('/custom text'); });
assert.response(app, { url: '/' }, { body: 'not sure' });
assert.response(app, { url: '/', method: 'POST', headers: { 'Content-Type': 'text/html' }}, { body: '/ html' }); assert.response(app, { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }}, { body: '/ json' }); assert.response(app, { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }}, { body: '/ png' }); assert.response(app, { url: '/', method: 'POST' }, { body: '/ not sure' });
assert.response(app, { url: '/long', method: 'POST', headers: { 'Content-Type': 'text/html' }}, { body: '/long html' }); assert.response(app, { url: '/long', method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8' }}, { body: '/long json' }); assert.response(app, { url: '/long', method: 'POST', headers: { 'Content-Type': 'image/png' }}, { body: '/long png' }); assert.response(app, { url: '/long', method: 'POST' }, { body: '/long not sure' }); assert.response(app, { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/plain' }}, { body: '/custom text' }); assert.response(app, { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'text/html' }}, { body: '/custom text' }); assert.response(app, { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/jpeg' }}, { body: '/custom an image' }); assert.response(app, { url: '/custom', method: 'PUT', headers: { 'Content-Type': 'image/png' }}, { body: '/custom an image' }); }};No newline at end of fileNo newline at end of file