Added tests for req.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 · parent a2a1aad · Browse files · 2 files·+61 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
2 files+61 added−0 removed
- tests:+53 added−0 removed
- other:+8 added−0 removed
Provenance
- Commit
9da2adf- Tree
540076a- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
a2a1aad
2 files changed+61 added−0 removed
modified:lib/express/request.js+8 added
191 unmodified lines192 return msgs;193 }194 };195 196+http.IncomingMessage.prototype.is = function(type){197+ var contentType = this.headers['content-type'];198+ if (!contentType) return;199+ if (!type.indexOf('/')) type = mime.type('.' + type);200+ return contentType.indexOf(type);201+};202+203+204 // Callback for isXMLHttpRequest / xhr205 206 function isxhr() {207 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';11 unmodified linesmodified:test/request.test.js+53 added
151 unmodified linesExpand all152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210151 unmodified linesExpand all assert.response(app, { url: '/' }, { body: 'ok' }); }, 'test #is()': function(assert){ var app = express.createServer(); 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'); });
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' }); }};No newline at end of fileNo newline at end of file