Skip to content

Commit

Permalink
docs: document type option taking arrays
Browse files Browse the repository at this point in the history
closes #163
closes #268
  • Loading branch information
shawninder authored and dougwilson committed Sep 27, 2017
1 parent b2659a7 commit 090a92b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 18 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ accept anything `JSON.parse` accepts. Defaults to `true`.
##### type

The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `json`), a mime type (like
`application/json`), or a mime type with a wildcard (like `*/*` or `*/json`).
If a function, the `type` option is called as `fn(req)` and the request is
parsed if it returns a truthy value. Defaults to `application/json`.
parse. This option can be a string, array of strings, or a function. If not a
function, `type` option is passed directly to the
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
be an extension name (like `json`), a mime type (like `application/json`), or
a mime type with a wildcard (like `*/*` or `*/json`). If a function, the `type`
option is called as `fn(req)` and the request is parsed if it returns a truthy
value. Defaults to `application/json`.

##### verify

Expand Down Expand Up @@ -143,9 +144,10 @@ to `'100kb'`.
##### type

The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `bin`), a mime type (like
parse. This option can be a string, array of strings, or a function.
If not a function, `type` option is passed directly to the
[type-is](https://www.npmjs.org/package/type-is#readme) library and this
can be an extension name (like `bin`), a mime type (like
`application/octet-stream`), or a mime type with a wildcard (like `*/*` or
`application/*`). If a function, the `type` option is called as `fn(req)`
and the request is parsed if it returns a truthy value. Defaults to
Expand Down Expand Up @@ -192,12 +194,13 @@ to `'100kb'`.
##### type

The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `txt`), a mime type (like
`text/plain`), or a mime type with a wildcard (like `*/*` or `text/*`).
If a function, the `type` option is called as `fn(req)` and the request is
parsed if it returns a truthy value. Defaults to `text/plain`.
parse. This option can be a string, array of strings, or a function. If not
a function, `type` option is passed directly to the
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
be an extension name (like `txt`), a mime type (like `text/plain`), or a mime
type with a wildcard (like `*/*` or `text/*`). If a function, the `type`
option is called as `fn(req)` and the request is parsed if it returns a
truthy value. Defaults to `text/plain`.

##### verify

Expand Down Expand Up @@ -256,9 +259,10 @@ than this value, a 413 will be returned to the client. Defaults to `1000`.
##### type

The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `urlencoded`), a mime type (like
parse. This option can be a string, array of strings, or a function. If not
a function, `type` option is passed directly to the
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
be an extension name (like `urlencoded`), a mime type (like
`application/x-www-form-urlencoded`), or a mime type with a wildcard (like
`*/x-www-form-urlencoded`). If a function, the `type` option is called as
`fn(req)` and the request is parsed if it returns a truthy value. Defaults
Expand Down
32 changes: 32 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,38 @@ describe('bodyParser.json()', function () {
})
})

describe('when ["application/json", "application/vnd.api+json"]', function () {
before(function () {
this.server = createServer({
type: ['application/json', 'application/vnd.api+json']
})
})

it('should parse JSON for "application/json"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse JSON for "application/vnd.api+json"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/vnd.api+json')
.send('{"user":"tobi"}')
.expect(200, '{"user":"tobi"}', done)
})

it('should ignore "application/x-json"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
})
})

describe('when a function', function () {
it('should parse when truthy value returned', function (done) {
var server = createServer({ type: accept })
Expand Down
29 changes: 29 additions & 0 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,35 @@ describe('bodyParser.raw()', function () {
})
})

describe('when ["application/octet-stream", "application/vnd+octets"]', function () {
before(function () {
this.server = createServer({
type: ['application/octet-stream', 'application/vnd+octets']
})
})

it('should parse "application/octet-stream"', function (done) {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/octet-stream')
test.write(new Buffer('000102', 'hex'))
test.expect(200, 'buf:000102', done)
})

it('should parse "application/vnd+octets"', function (done) {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/vnd+octets')
test.write(new Buffer('000102', 'hex'))
test.expect(200, 'buf:000102', done)
})

it('should ignore "application/x-foo"', function (done) {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/x-foo')
test.write(new Buffer('000102', 'hex'))
test.expect(200, '{}', done)
})
})

describe('when a function', function () {
it('should parse when truthy value returned', function (done) {
var server = createServer({ type: accept })
Expand Down
30 changes: 30 additions & 0 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ describe('bodyParser.text()', function () {
})
})

describe('when ["text/html", "text/plain"]', function () {
before(function () {
this.server = createServer({ type: ['text/html', 'text/plain'] })
})

it('should parse "text/html"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'text/html')
.send('<b>tobi</b>')
.expect(200, '"<b>tobi</b>"', done)
})

it('should parse "text/plain"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'text/plain')
.send('tobi')
.expect(200, '"tobi"', done)
})

it('should ignore "text/xml"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'text/xml')
.send('<user>tobi</user>')
.expect(200, '{}', done)
})
})

describe('when a function', function () {
it('should parse when truthy value returned', function (done) {
var server = createServer({ type: accept })
Expand Down
32 changes: 32 additions & 0 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,38 @@ describe('bodyParser.urlencoded()', function () {
})
})

describe('when ["urlencoded", "application/x-pairs"]', function () {
before(function () {
this.server = createServer({
type: ['urlencoded', 'application/x-pairs']
})
})

it('should parse "application/x-www-form-urlencoded"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user=tobi')
.expect(200, '{"user":"tobi"}', done)
})

it('should parse "application/x-pairs"', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-pairs')
.send('user=tobi')
.expect(200, '{"user":"tobi"}', done)
})

it('should ignore application/x-foo', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-foo')
.send('user=tobi')
.expect(200, '{}', done)
})
})

describe('when a function', function () {
it('should parse when truthy value returned', function (done) {
var server = createServer({ type: accept })
Expand Down

0 comments on commit 090a92b

Please sign in to comment.