Skip to content

Commit

Permalink
Improve missing Content-Type header error message
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
gr0uch authored and dougwilson committed Feb 13, 2015
1 parent 3126696 commit d7d6424
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Improve missing `Content-Type` header error message

1.0.0 / 2015-02-01
==================

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):
- `parameters`: An object of the parameters in the media type (name of parameter
always lower case). Example: `{charset: 'utf-8'}`

Throws a `TypeError` if the string is missing or invalid.

### contentType.parse(req)

```js
Expand All @@ -44,6 +46,8 @@ var obj = contentType.parse(req)
Parse the `content-type` header from the given `req`. Short-cut for
`contentType.parse(req.headers['content-type'])`.

Throws a `TypeError` if the `Content-Type` header is missing or invalid.

### contentType.parse(res)

```js
Expand All @@ -53,6 +57,8 @@ var obj = contentType.parse(res)
Parse the `content-type` header set on the given `res`. Short-cut for
`contentType.parse(res.getHeader('content-type'))`.

Throws a `TypeError` if the `Content-Type` header is missing or invalid.

### contentType.format(obj)

```js
Expand All @@ -68,6 +74,8 @@ shown that produce the string `'image/svg+xml; charset=utf-8'`):
- `parameters`: An object of the parameters in the media type (name of the
parameter will be lower-cased). Example: `{charset: 'utf-8'}`

Throws a `TypeError` if the object contains an invalid type or parameter names.

## License

[MIT](LICENSE)
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ function parse(string) {
throw new TypeError('argument string is required')
}

// support req/res-like objects as argument
if (typeof string === 'object') {
// support req/res-like objects as argument
string = getcontenttype(string)

if (typeof string !== 'string') {
throw new TypeError('content-type header is missing from object');
}
}

if (typeof string !== 'string') {
Expand Down
8 changes: 4 additions & 4 deletions test/contentType_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ describe('contentType.parse(req)', function () {
})

it('should reject objects without headers property', function () {
assert.throws(contentType.parse.bind(null, {}), /argument string is required/)
assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/)
})

it('should reject missing content-type', function () {
var req = {headers: {}}
assert.throws(contentType.parse.bind(null, req), /argument string is required/)
assert.throws(contentType.parse.bind(null, req), /content-type header is missing/)
})
})

Expand All @@ -134,11 +134,11 @@ describe('contentType.parse(res)', function () {
})

it('should reject objects without getHeader method', function () {
assert.throws(contentType.parse.bind(null, {}), /argument string is required/)
assert.throws(contentType.parse.bind(null, {}), /content-type header is missing/)
})

it('should reject missing content-type', function () {
var res = {getHeader: function(){}}
assert.throws(contentType.parse.bind(null, res), /argument string is required/)
assert.throws(contentType.parse.bind(null, res), /content-type header is missing/)
})
})

0 comments on commit d7d6424

Please sign in to comment.