diff --git a/README.md b/README.md index 62221e47..365c81cb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/json.js b/test/json.js index 0be9b20f..5cc10cf3 100644 --- a/test/json.js +++ b/test/json.js @@ -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 }) diff --git a/test/raw.js b/test/raw.js index 9329fa2d..cde33224 100644 --- a/test/raw.js +++ b/test/raw.js @@ -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 }) diff --git a/test/text.js b/test/text.js index 24a40a58..fd4f0aab 100644 --- a/test/text.js +++ b/test/text.js @@ -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('tobi') + .expect(200, '"tobi"', 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('tobi') + .expect(200, '{}', done) + }) + }) + describe('when a function', function () { it('should parse when truthy value returned', function (done) { var server = createServer({ type: accept }) diff --git a/test/urlencoded.js b/test/urlencoded.js index b475cd3f..38f85d7e 100644 --- a/test/urlencoded.js +++ b/test/urlencoded.js @@ -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 })