From 8c9e7f987e2a7b410f263aae3ab1ce1e7005472a Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Fri, 12 Jun 2015 23:20:07 -0400 Subject: [PATCH] Improve error message when req argument missing --- HISTORY.md | 1 + index.js | 4 ++++ test/index.js | 10 +++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 016efff..9dab2a8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== + * Improve error message when `req` argument missing * perf: enable strict mode * perf: hoist regular expression * perf: parse with regular expressions diff --git a/index.js b/index.js index 1e74955..ca0be71 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,10 @@ var userPassRegExp = /^([^:]*):(.*)$/ */ function auth(req) { + if (!req) { + throw new TypeError('argument req is required') + } + // get header var header = (req.req || req).headers.authorization diff --git a/test/index.js b/test/index.js index 65f791f..4375388 100644 --- a/test/index.js +++ b/test/index.js @@ -9,7 +9,15 @@ function request(authorization) { } } -describe('auth(req)', function(){ +describe('auth(req)', function () { + describe('arguments', function () { + describe('req', function () { + it('should be required', function () { + assert.throws(auth, /argument req is required/) + }) + }) + }) + describe('with no Authorization field', function(){ it('should return null', function(){ var req = request();