Finished req.accepts()
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 9750817 · Browse files · 2 files·+35 added−9 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+35 added−9 removed
- tests:+2 added−3 removed
- other:+33 added−6 removed
Provenance
- Commit
e58f468- Tree
5d5af83- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9750817
2 files changed+35 added−9 removed
modified:lib/express/request.js+33 added-6 removed
7 unmodified lines8910111212131415161723 unmodified lines414243444546474849505152535455565758596061626364656667686970717256577374757677787980818283848586878889616263909192939431 unmodified lines7 unmodified lines/** * Module dependencies. */
var http = require('http');var http = require('http'), mime = require('connect/utils').mime;
/** * Return request header or optional default. *23 unmodified lines
/** * Check if the _Accept_ header is present, and includes the given `type`. * * When the _Accept_ header is not present `true` is returned. Otherwise * the given `type` is matched by an exact match, and then subtypes. You * may pass the subtype such as "html" which is then converted internally * to "text/html" using the mime lookup table. * * Examples: * * // Accept: text/html * req.accepts('html'); * // => true * * // Accept: text/*; application/json * req.accepts('html'); * req.accepts('text/html'); * req.accepts('text/plain'); * req.accepts('application/json'); * // => true * * req.accepts('image/png'); * req.accepts('png'); * // => false * * @param {String} type * @return {Boolean} * @api public */
http.IncomingMessage.prototype.accepts = function(type){ var accept = this.header('Accept'), type = String(type).trim(); var accept = this.header('Accept'); if (!accept || accept === '*/*') { return true; } else if (type) { // Allow "html" vs "text/html" etc if (type.indexOf('/') < 0) { type = mime.types['.' + type]; } // Check if we have a direct match if (accept.indexOf(type) >= 0) { return true; // Check if we have type/* } else { type = type.split('/')[0] + '/*'; return accept.indexOf(type) >= 0; } } else { return type ? accept.indexOf(type) >= 0 : false; return false; }};
31 unmodified linesmodified:test/request.test.js+2 added-3 removed
55 unmodified lines56 assert.strictEqual(true, req.accepts('json'));57 assert.strictEqual(true, req.accepts('application/json'));58 assert.strictEqual(false, req.accepts('xml'));59 assert.strictEqual(false, req.accepts());60- assert.strictEqual(false, req.accepts(' '));60 res.send('ok');61 });62 63 app.get('/type', function(req, res){64 assert.strictEqual(true, req.accepts('html'));65 assert.strictEqual(true, req.accepts('text/html'));66 assert.strictEqual(true, req.accepts('json'));67 assert.strictEqual(true, req.accepts('application/json'));69- assert.strictEqual(false, req.accepts('svg'));70- assert.strictEqual(false, req.accepts('image/svg'));68+ assert.strictEqual(false, req.accepts('png'));69+ assert.strictEqual(false, req.accepts('image/png'));70 res.send('ok'); 71 });72 73 assert.response(app,36 unmodified lines