Skip to content
Myr

Added `req.accepts()` comma-delimited string 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 86a9e08 · Browse files · 3 files+41 added−2 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+41 added−2 removed

  • tests:+40 added−1 removed
  • other:+1 added−1 removed

3 files+41 added−2 removed

modified:lib/utils.js+1 added-1 removed
113 unmodified lines
114 * @api private115 */116117exports.accepts = function(type, str){118 if (!Array.isArray(type)) type = [type];118 if ('string' == typeof type) type = type.split(/ *, */);119 return exports.acceptsArray(type, str);120};121122/**
93 unmodified lines
modified:test/req.accepts.js+13 added
modified:test/utils.js+27 added-1 removed
95 unmodified lines
96 utils.accepts('text/html', '')97 .should.equal('text/html');98 })99 })100 100101 describe('when */* is given', function(){102 it('should return the value', function(){103 utils.accepts('text/html', 'text/plain, */*')104 .should.equal('text/html');105 })106 })107108 describe('when an array is given', function(){109 it('should return the best match', function(){110 utils.accepts(['html', 'json'], 'text/plain, application/json')111 .should.equal('json');112113 utils.accepts(['html', 'application/json'], 'text/plain, application/json')114 .should.equal('application/json');115116 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')117 .should.equal('text/html');118 })119 })120121 describe('when a comma-delimited list is give', function(){122 it('should behave like an array', function(){123 utils.accepts('html, json', 'text/plain, application/json')124 .should.equal('json');125126 utils.accepts('html, application/json', 'text/plain, application/json')127 .should.equal('application/json');128129 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')130 .should.equal('text/html');131 })132 })133134 describe('when accepting type/subtype', function(){135 it('should return the value when present', function(){136 utils.accepts('text/html', 'text/plain, text/html')137 .should.equal('text/html');
48 unmodified lines