Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ function mimeMatch (expected, actual) {
*/
function normalizeType (value) {
if (!value) return null
var type = contentType.parse(value, { parameters: false }).type

return typer.test(type) ? type : null
try {
var type = contentType.parse(value, { parameters: false }).type
return typer.test(type) ? type : null
} catch (err) {
return null
}
}
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ describe('typeis(req, types)', function () {
assert.strictEqual(typeis(req, [undefined, null, true, function () {}]), false)
})

it('should not throw on a Content-Type with an empty type token', function () {
// A Content-Type header where the portion before the first ";" (after
// trimming optional whitespace) is empty - e.g. ";" or " " - causes
// content-type@2's lenient parse() to return type: "". media-typer's
// test() then throws on that falsy input instead of returning false,
// and normalizeType() no longer catches it (unlike 1.x's
// tryNormalizeType, which wrapped this in a try/catch).
var req = createRequest(';')
assert.strictEqual(typeis(req, ['urlencoded']), false)
})

describe('when no body is given', function () {
it('should return null', function () {
var req = { headers: {} }
Expand Down