Skip to content
Myr

Added req.is() wildcard support

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 071ea95 · Browse files · 2 files+32 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+32 added−4 removed

  • tests:+26 added−0 removed
  • other:+6 added−4 removed

2 files+32 added−4 removed

modified:lib/express/request.js+6 added-4 removed
199 unmodified lines
200 * Examples:201 *202 * // With Content-Type: text/html; charset=utf-8203 * req.is('html');204 * // => true205 *204 * req.is('text/html');205 * // => true206 *207 * // When Content-Type is application/json208 * req.is('json');211 * // => true212 *209 * req.is('application/json');210 * // => true211 *212 * req.is('html');
31 unmodified lines
244 if (!contentType) return;245 if (!~type.indexOf('/')) type = mime.type('.' + type);246 // TODO: remove when connect no longer appends charset...247 if (~type.indexOf(';')) type = type.split(';')[0];248 if (~type.indexOf('*')) {249 type = type.split('/')250 contentType = contentType.split('/');251 if ('*' == type[0] && type[1] == contentType[1]) return true;252 if ('*' == type[1] && type[0] == contentType[0]) return true;253 }254 return ~contentType.indexOf(type);255};256257// Callback for isXMLHttpRequest / xhr
14 unmodified lines
modified:test/request.test.js+26 added
230 unmodified lines
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 237 'test #is() wildcard': function(assert){238 var app = express.createServer();239240 app.post('/', function(req, res, next){241 if (req.is('image/*')) return res.send('image');242 if (req.is('*/json')) return res.send('json');243 res.send('not sure');244 });245246 assert.response(app,247 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/png' }},248 { body: 'image' });249 assert.response(app,250 { url: '/', method: 'POST', headers: { 'Content-Type': 'image/jpeg' }},251 { body: 'image' });252 assert.response(app,253 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/plain' }},254 { body: 'not sure' });255 assert.response(app,256 { url: '/', method: 'POST', headers: { 'Content-Type': 'text/json' }},257 { body: 'json' });258 assert.response(app,259 { url: '/', method: 'POST', headers: { 'Content-Type': 'application/json' }},260 { body: 'json' });261 }262};