From 5c5e81fa31c5a55a06c334b7285a4f5c2dc28b75 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Mon, 6 Jan 2014 00:38:36 -0800 Subject: [PATCH] remove body parser and various cleanup moved to https://github.com/expressjs/body-parser --- Makefile | 22 +-- Readme.md | 4 + lib/connect.js | 12 -- lib/middleware/bodyParser.js | 63 -------- lib/middleware/json.js | 87 ----------- lib/middleware/static.js | 9 -- lib/middleware/urlencoded.js | 76 ---------- lib/utils.js | 29 ---- package.json | 7 +- test/bodyParser.js | 44 ------ test/csrf.js | 3 - test/exports.js | 26 ---- test/json.js | 279 ----------------------------------- test/methodOverride.js | 13 +- test/server.js | 23 --- test/shared/index.js | 26 ---- test/urlencoded.js | 39 ----- test/utils.js | 32 ---- 18 files changed, 9 insertions(+), 785 deletions(-) delete mode 100644 lib/middleware/bodyParser.js delete mode 100644 lib/middleware/json.js delete mode 100644 lib/middleware/urlencoded.js delete mode 100644 test/bodyParser.js delete mode 100644 test/exports.js delete mode 100644 test/json.js delete mode 100644 test/shared/index.js delete mode 100644 test/urlencoded.js delete mode 100644 test/utils.js diff --git a/Makefile b/Makefile index 198c0a75d..3bc81b016 100644 --- a/Makefile +++ b/Makefile @@ -1,39 +1,19 @@ TESTS = test/*.js REPORTER = dot -DOX = ./node_modules/.bin/dox - -SRC = $(shell find lib/*.js lib/middleware/*.js) -HTML = $(SRC:.js=.html) test: @NODE_ENV=test ./node_modules/.bin/mocha \ --reporter $(REPORTER) \ $(TESTS) -docs: $(HTML) - @mv $(HTML) docs - test-cov: lib-cov @CONNECT_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html lib-cov: @jscoverage lib $@ -%.html: %.js - $(DOX) < $< | node support/docs > $@ - -docclean: - rm -f $(HTML) - -site: docclean docs - rm -fr /tmp/docs \ - && cp -fr docs /tmp/docs \ - && git checkout gh-pages \ - && cp -fr /tmp/docs/* . \ - && echo "done" - benchmark: @./support/bench -.PHONY: test-cov site docs test docclean benchmark \ No newline at end of file +.PHONY: test-cov test benchmark \ No newline at end of file diff --git a/Readme.md b/Readme.md index 2f20623c5..cf81b8c34 100644 --- a/Readme.md +++ b/Readme.md @@ -32,6 +32,10 @@ If you would like to help maintain these middleware, please contact [@jongleberr These middleware are officially supported by the Connect/Express team: + - [body-parser](https://github.com/expressjs/body-parser) - previous `bodyParser`, `json`, and `urlencoded`. You may also be interested in: + - [body](https://github.com/raynos/body) + - [co-body](https://github.com/visionmedia/co-body) + - [raw-body](https://github.com/stream-utils/raw-body) - [compression](https://github.com/expressjs/compression) - previously `compress` These middleware previously included with Connect are no longer supported by the Connect/Express team. Use one of these alternatives intead: diff --git a/lib/connect.js b/lib/connect.js index 72961dca7..b55da4f16 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -24,18 +24,6 @@ require('./patch'); exports = module.exports = createServer; -/** - * Framework version. - */ - -exports.version = '2.7.11'; - -/** - * Expose mime module. - */ - -exports.mime = require('./middleware/static').mime; - /** * Expose the prototype. */ diff --git a/lib/middleware/bodyParser.js b/lib/middleware/bodyParser.js deleted file mode 100644 index 8cdee7dcd..000000000 --- a/lib/middleware/bodyParser.js +++ /dev/null @@ -1,63 +0,0 @@ - -/*! - * Connect - bodyParser - * Copyright(c) 2010 Sencha Inc. - * Copyright(c) 2011 TJ Holowaychuk - * MIT Licensed - */ - -/** - * Module dependencies. - */ - -var urlencoded = require('./urlencoded'); -var json = require('./json'); - -/** - * Body parser: - * - * Status: the multipart body parser will be removed in Connect 3. - * - * Parse request bodies, supports _application/json_, - * _application/x-www-form-urlencoded_, and _multipart/form-data_. - * - * This is equivalent to: - * - * app.use(connect.json()); - * app.use(connect.urlencoded()); - * app.use(connect.multipart()); - * - * Examples: - * - * connect() - * .use(connect.bodyParser()) - * .use(function(req, res) { - * res.end('viewing user ' + req.body.user.name); - * }); - * - * $ curl -d 'user[name]=tj' http://local/ - * $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://local/ - * - * View [json](json.html), [urlencoded](urlencoded.html), and [multipart](multipart.html) for more info. - * - * If you wish to create your own body parser, you may be interested in: - * - * - [raw-body](https://github.com/stream-utils/raw-body) - * - [body](https://github.com/raynos/body) - * - * @param {Object} options - * @return {Function} - * @api public - */ - -exports = module.exports = function bodyParser(options){ - var _urlencoded = urlencoded(options); - var _json = json(options); - - return function bodyParser(req, res, next) { - _json(req, res, function(err){ - if (err) return next(err); - _urlencoded(req, res, next); - }); - } -}; \ No newline at end of file diff --git a/lib/middleware/json.js b/lib/middleware/json.js deleted file mode 100644 index ef4898520..000000000 --- a/lib/middleware/json.js +++ /dev/null @@ -1,87 +0,0 @@ - -/*! - * Connect - json - * Copyright(c) 2010 Sencha Inc. - * Copyright(c) 2011 TJ Holowaychuk - * MIT Licensed - */ - -/** - * Module dependencies. - */ - -var utils = require('../utils'); -var getBody = require('raw-body'); - -/** - * JSON: - * - * Parse JSON request bodies, providing the - * parsed object as `req.body`. - * - * Options: - * - * - `strict` when `false` anything `JSON.parse()` accepts will be parsed - * - `reviver` used as the second "reviver" argument for JSON.parse - * - `limit` byte limit [1mb] - * - * @param {Object} options - * @return {Function} - * @api public - */ - -exports = module.exports = function(options){ - options = options || {}; - var strict = options.strict !== false; - var verify = typeof options.verify === 'function' && options.verify; - - return function json(req, res, next) { - if (req._body) return next(); - req.body = req.body || {}; - - if (!utils.hasBody(req)) return next(); - - // check Content-Type - if (!exports.regexp.test(utils.mime(req))) return next(); - - // flag as parsed - req._body = true; - - // parse - getBody(req, { - limit: options.limit || '1mb', - length: req.headers['content-length'], - encoding: 'utf8' - }, function (err, buf) { - if (err) return next(err); - - if (verify) { - try { - verify(req, res, buf) - } catch (err) { - if (!err.status) err.status = 403; - return next(err); - } - } - - var first = buf.trim()[0]; - - if (0 == buf.length) { - return next(utils.error(400, 'invalid json, empty body')); - } - - if (strict && '{' != first && '[' != first) return next(utils.error(400, 'invalid json')); - try { - req.body = JSON.parse(buf, options.reviver); - } catch (err){ - err.body = buf; - err.status = 400; - return next(err); - } - next(); - }) - }; -}; - -exports.regexp = /^application\/([\w!#\$%&\*`\-\.\^~]*\+)?json$/i; - diff --git a/lib/middleware/static.js b/lib/middleware/static.js index 936970f5b..5d213ef34 100644 --- a/lib/middleware/static.js +++ b/lib/middleware/static.js @@ -85,12 +85,3 @@ exports = module.exports = function(root, options){ .pipe(res); }; }; - -/** - * Expose mime module. - * - * If you wish to extend the mime table use this - * reference to the "mime" module in the npm registry. - */ - -exports.mime = send.mime; diff --git a/lib/middleware/urlencoded.js b/lib/middleware/urlencoded.js deleted file mode 100644 index b94868d86..000000000 --- a/lib/middleware/urlencoded.js +++ /dev/null @@ -1,76 +0,0 @@ -/*! - * Connect - urlencoded - * Copyright(c) 2010 Sencha Inc. - * Copyright(c) 2011 TJ Holowaychuk - * MIT Licensed - */ - -/** - * Module dependencies. - */ - -var utils = require('../utils'); -var getBody = require('raw-body'); -var qs = require('qs'); - -/** - * Urlencoded: - * - * Parse x-ww-form-urlencoded request bodies, - * providing the parsed object as `req.body` using - * [qs](https://github.com/visionmedia/node-querystring). - * - * Options: - * - * - `limit` byte limit [1mb] - * - * @param {Object} options - * @return {Function} - * @api public - */ - -exports = module.exports = function(options){ - options = options || {}; - var verify = typeof options.verify === 'function' && options.verify; - - return function urlencoded(req, res, next) { - if (req._body) return next(); - req.body = req.body || {}; - - if (!utils.hasBody(req)) return next(); - - // check Content-Type - if ('application/x-www-form-urlencoded' != utils.mime(req)) return next(); - - // flag as parsed - req._body = true; - - // parse - getBody(req, { - limit: options.limit || '1mb', - length: req.headers['content-length'], - encoding: 'utf8' - }, function (err, buf) { - if (err) return next(err); - - if (verify) { - try { - verify(req, res, buf) - } catch (err) { - if (!err.status) err.status = 403; - return next(err); - } - } - - try { - req.body = buf.length - ? qs.parse(buf) - : {}; - } catch (err){ - err.body = buf; - return next(err); - } - next(); - }) - } -}; diff --git a/lib/utils.js b/lib/utils.js index 6ff169918..3a6556522 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -16,35 +16,6 @@ var http = require('http') , sep = require('path').sep , signature = require('cookie-signature'); -/** - * Return `true` if the request has a body, otherwise return `false`. - * - * @param {IncomingMessage} req - * @return {Boolean} - * @api private - */ - -exports.hasBody = function(req) { - var encoding = 'transfer-encoding' in req.headers; - var length = 'content-length' in req.headers && req.headers['content-length'] !== '0'; - return encoding || length; -}; - -/** - * Extract the mime type from the given request's - * _Content-Type_ header. - * - * @param {IncomingMessage} req - * @return {String} - * @api private - */ - -exports.mime = function(req) { - var str = req.headers['content-type'] || '' - , i = str.indexOf(';'); - return ~i ? str.slice(0, i) : str; -}; - /** * Generate an `Error` from the given status `code` * and optional `msg`. diff --git a/package.json b/package.json index 874ed050d..060ce712f 100644 --- a/package.json +++ b/package.json @@ -23,14 +23,11 @@ "uid2": "0.0.3", "debug": ">= 0.7.3 < 1", "methods": "0.1.0", - "raw-body": "1.1.2", "negotiator": "0.3.0" }, "devDependencies": { "should": ">= 2.0.2 < 3", - "mocha": ">= 1.13.0 < 2", - "jade": ">= 0.35.0 < 1", - "dox": ">= 0.4.4 < 1" + "mocha": ">= 1.13.0 < 2" }, "licenses": [ { @@ -40,7 +37,7 @@ ], "main": "index", "engines": { - "node": ">= 0.8.0" + "node": ">= 0.10.0" }, "scripts": { "test": "make" diff --git a/test/bodyParser.js b/test/bodyParser.js deleted file mode 100644 index beb4f868a..000000000 --- a/test/bodyParser.js +++ /dev/null @@ -1,44 +0,0 @@ - -var connect = require('..'); -var assert = require('assert'); - -var app = connect(); - -app.use(connect.bodyParser()); - -app.use(function(req, res){ - res.end(JSON.stringify(req.body)); -}); - -describe('connect.bodyParser()', function(){ - it('should default to {}', function(done){ - app.request() - .post('/') - .end(function(res){ - res.body.should.equal('{}'); - done(); - }) - }) - - it('should parse JSON', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"user":"tobi"}') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) - - it('should parse x-www-form-urlencoded', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/x-www-form-urlencoded') - .write('user=tobi') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) -}) diff --git a/test/csrf.js b/test/csrf.js index aab839366..cdece2a5b 100644 --- a/test/csrf.js +++ b/test/csrf.js @@ -6,7 +6,6 @@ describe('csrf', function(){ app.use(connect.cookieParser()) app.use(connect.session({ secret: 'greg' })); - app.use(connect.bodyParser()); app.use(connect.csrf()); app.use(function(req, res){ res.end(req.csrfToken() || 'none'); @@ -33,7 +32,6 @@ describe('csrf', function(){ app.use(connect.cookieParser()); app.use(connect.session({ secret: 'greg' })); - app.use(connect.bodyParser()); app.use(connect.csrf()); app.use(function(req, res){ res.end(req.csrfToken() || 'none'); @@ -58,7 +56,6 @@ describe('csrf', function(){ app.use(connect.cookieParser()); app.use(connect.session({ secret: 'greg' })); - app.use(connect.bodyParser()); app.use(connect.csrf()); app.use(function(req, res){ res.end(req.csrfToken() || 'none'); diff --git a/test/exports.js b/test/exports.js deleted file mode 100644 index 378a6738c..000000000 --- a/test/exports.js +++ /dev/null @@ -1,26 +0,0 @@ - -var connect = require('../'); - -describe('exports', function(){ - describe('.version', function(){ - it('should be a string', function(){ - connect.version.should.be.a.String; - }) - }) - - describe('.middleware', function(){ - it('should lazy-load middleware', function(){ - connect.middleware.cookieParser.should.be.a.Function; - connect.middleware.bodyParser.should.be.a.Function; - connect.middleware.static.should.be.a.Function; - }) - }) - - describe('.NAME', function(){ - it('should lazy-load middleware', function(){ - connect.cookieParser.should.be.a.Function; - connect.bodyParser.should.be.a.Function; - connect.static.should.be.a.Function; - }) - }) -}) \ No newline at end of file diff --git a/test/json.js b/test/json.js deleted file mode 100644 index 7c6f9542b..000000000 --- a/test/json.js +++ /dev/null @@ -1,279 +0,0 @@ - -var connect = require('../') - , should = require('./shared'); - -var app = connect(); - -app.use(connect.json({ limit: '1mb' })); - -app.use(function(req, res){ - res.end(JSON.stringify(req.body)); -}); - -app.use(function(err, req, res, next){ - res.statusCode = err.status; - res.end(err.message); -}); - -describe('connect.json()', function(){ - should['default request body'](app); - should['limit body to']('1mb', 'application/json', app); - - it('should parse JSON', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"user":"tobi"}') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) - - it('should fail gracefully', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"user"') - .end(function(res){ - res.body.should.equal('Unexpected end of input'); - done(); - }); - }) - - it('should handle Content-Length: 0', function(done){ - var app = connect(); - app.use(connect.json()); - - app.use(function(req, res){ - res.end(Object.keys(req.body).length ? '' : 'empty'); - }); - - app.request() - .get('/') - .set('Content-Type', 'application/json') - .set('Content-Length', '0') - .end(function(res){ - res.should.have.status(200); - res.body.should.equal('empty'); - done(); - }); - }) - - it('should 400 on malformed JSON', function(done){ - var app = connect(); - app.use(connect.json()); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"foo') - .expect(400, done); - }) - - it('should 400 when no body is given', function(done){ - var app = connect(); - app.use(connect.json()); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .end(function(res) { - res.should.have.status(400); - res.body.should.include("invalid json, empty body"); - done(); - }) - }) - - it('should support all http methods', function(done){ - var app = connect(); - app.use(connect.json()); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .get('/') - .set('Content-Type', 'application/json') - .set('Content-Length', '["foo"]'.length) - .write('["foo"]') - .expect('["foo"]', done); - }) - - describe('when strict is false', function(){ - it('should parse primitives', function(done){ - var app = connect(); - app.use(connect.json({ strict: false })); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('true') - .expect('true', done); - }) - }) - - describe('when strict is true', function(){ - it('should not parse primitives', function(done){ - var app = connect(); - app.use(connect.json({ strict: true })); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('true') - .end(function(res){ - res.should.have.status(400); - res.body.should.include('invalid json'); - done(); - }); - }) - - it('should allow leading whitespaces in JSON', function(done){ - var app = connect(); - app.use(connect.json({ strict: true })); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write(' { "user": "tobi" }') - .end(function(res){ - res.should.have.status(200); - res.body.should.include('{"user":"tobi"}'); - done(); - }); - }) - }) - - describe('by default', function(){ - it('should 400 on primitives', function(done){ - var app = connect(); - app.use(connect.json()); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('true') - .expect(400, done); - }) - }) - - it('should support utf-8', function(done){ - var app = connect(); - - app.use(connect.json()); - - app.use(function(req, res, next){ - res.end(req.body.name); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json; charset=utf-8') - .write('{"name":"论"}') - .expect('论', done); - }) - - it('should support {"test":"å"}', function (done) { - // https://github.com/visionmedia/express/issues/1816 - - var app = connect(); - app.use(connect.json()); - app.use(function(req, res, next){ - res.end(req.body.test); - }) - - app.request() - .post('/') - .set('Content-Type', 'application/json; charset=utf-8') - .set('Content-Length', '13') - .write('{"test":"å"}') - .expect('å', done); - }) - - describe('the default json mime type regular expression', function() { - var mimeRegExp = connect.json.regexp; - it('should support the basic JSON mime type', function(){ - mimeRegExp.test('application/json').should.eql(true); - }) - - it('should not match incorrect mime type', function(){ - mimeRegExp.test('zapplication/json').should.eql(false); - }) - - it('should be case insensitive', function(){ - mimeRegExp.test('Application/JSON').should.eql(true); - }) - - it('should support suffix notation', function(){ - mimeRegExp.test('application/vnd.organization.prog-type.org+json').should.eql(true); - }) - - it('should support specific special characters on mime subtype', function(){ - mimeRegExp.test('application/vnd.organization.special_!#$%*`-^~.org+json').should.eql(true); - }) - - it('should not support other special characters on mime subtype', function(){ - mimeRegExp.test('application/vnd.organization.special_()<>@,;:\\"/[]?={} \t.org+json').should.eql(false); - }) - - it('should not match malformed mime subtype suffix', function(){ - mimeRegExp.test('application/vnd.test.org+json+xml').should.eql(false); - }) - }); - - if (!connect.utils.brokenPause) { - it('should parse JSON with limit and after next tick', function(done){ - var app = connect(); - - app.use(function(req, res, next) { - setTimeout(next, 10); - }); - - app.use(connect.json({ limit: '1mb' })); - - app.use(function(req, res){ - res.end(JSON.stringify(req.body)); - }); - - app.use(function(err, req, res, next){ - res.statusCode = err.status; - res.end(err.message); - }); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"user":"tobi"}') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) - } -}) diff --git a/test/methodOverride.js b/test/methodOverride.js index f1f05f9c6..28e401914 100644 --- a/test/methodOverride.js +++ b/test/methodOverride.js @@ -3,7 +3,6 @@ var connect = require('../'); var app = connect(); -app.use(connect.bodyParser()); app.use(connect.methodOverride()); app.use(function(req, res){ @@ -17,19 +16,11 @@ describe('connect.methodOverride()', function(){ .expect('GET', done); }) - it('should support req.body._method', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/x-www-form-urlencoded') - .write('_method=DELETE') - .expect('DELETE', done); - }) - it('should be case in-sensitive', function(done){ app.request() .post('/') .set('Content-Type', 'application/x-www-form-urlencoded') - .write('_method=delete') + .set('X-HTTP-Method-Override', 'DELETE') .expect('DELETE', done); }) @@ -37,7 +28,7 @@ describe('connect.methodOverride()', function(){ app.request() .post('/') .set('Content-Type', 'application/x-www-form-urlencoded') - .write('_method=') + .set('X-HTTP-Method-Override', 'POST') .expect('POST', done); }) }) diff --git a/test/server.js b/test/server.js index bf19b1889..3d1899a99 100644 --- a/test/server.js +++ b/test/server.js @@ -20,29 +20,6 @@ describe('app', function(){ .expect('http://example.com/foo', done); }) - it('should allow old-style constructor middleware', function(done){ - var app = connect( - connect.json() - , connect.urlencoded() - , function(req, res){ res.end(JSON.stringify(req.body)) }); - - app.stack.should.have.length(3); - - app.request() - .post('/') - .set('Content-Type', 'application/json') - .write('{"foo":"bar"}') - .expect('{"foo":"bar"}', done); - }) - - it('should allow old-style .createServer()', function(){ - var app = connect.createServer( - connect.json() - , connect.urlencoded()); - - app.stack.should.have.length(2); - }) - it('should work as middlware', function(done){ var http = require('http'); diff --git a/test/shared/index.js b/test/shared/index.js deleted file mode 100644 index 3958366e6..000000000 --- a/test/shared/index.js +++ /dev/null @@ -1,26 +0,0 @@ - -var bytes = require('bytes'); - -exports['default request body'] = function(app){ - it('should default to {}', function(done){ - app.request() - .post('/') - .end(function(res){ - res.body.should.equal('{}'); - done(); - }) - }) -}; - -exports['limit body to'] = function(size, type, app){ - it('should accept a limit option', function(done){ - app.request() - .post('/') - .set('Content-Length', bytes(size) + 1) - .set('Content-Type', type) - .end(function(res){ - res.should.have.status(413); - done(); - }) - }) -} \ No newline at end of file diff --git a/test/urlencoded.js b/test/urlencoded.js deleted file mode 100644 index 605d22e26..000000000 --- a/test/urlencoded.js +++ /dev/null @@ -1,39 +0,0 @@ - -var connect = require('../') - , should = require('./shared'); - -var app = connect(); - -app.use(connect.urlencoded({ limit: '1mb' })); - -app.use(function(req, res){ - res.end(JSON.stringify(req.body)); -}); - -describe('connect.urlencoded()', function(){ - should['default request body'](app); - should['limit body to']('1mb', 'application/x-www-form-urlencoded', app); - - it('should support all http methods', function(done){ - app.request() - .get('/') - .set('Content-Type', 'application/x-www-form-urlencoded') - .set('Content-Length', 'user=tobi'.length) - .write('user=tobi') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) - - it('should parse x-www-form-urlencoded', function(done){ - app.request() - .post('/') - .set('Content-Type', 'application/x-www-form-urlencoded') - .write('user=tobi') - .end(function(res){ - res.body.should.equal('{"user":"tobi"}'); - done(); - }); - }) -}) \ No newline at end of file diff --git a/test/utils.js b/test/utils.js deleted file mode 100644 index 13f93313b..000000000 --- a/test/utils.js +++ /dev/null @@ -1,32 +0,0 @@ - -var connect = require('../') - , utils = connect.utils; - -describe('utils.parseCacheControl(str)', function(){ - it('should parse Cache-Control', function(){ - var parse = utils.parseCacheControl; - parse('no-cache').should.eql({ 'no-cache': true }); - parse('no-store').should.eql({ 'no-store': true }); - parse('no-transform').should.eql({ 'no-transform': true }); - parse('only-if-cached').should.eql({ 'only-if-cached': true }); - parse('max-age=0').should.eql({ 'max-age': 0 }); - parse('max-age=60').should.eql({ 'max-age': 60 }); - parse('max-stale=60').should.eql({ 'max-stale': 60 }); - parse('min-fresh=60').should.eql({ 'min-fresh': 60 }); - parse('public, max-age=60').should.eql({ 'public': true, 'max-age': 60 }); - parse('must-revalidate, max-age=60').should.eql({ 'must-revalidate': true, 'max-age': 60 }); - }) -}) - -describe('utils.mime(req)', function(){ - it('should return the mime-type from Content-Type', function(){ - utils.mime({ headers: { 'content-type': 'text/html; charset=utf8' }}) - .should.equal('text/html'); - - utils.mime({ headers: { 'content-type': 'text/html; charset=utf8' }}) - .should.equal('text/html'); - - utils.mime({ headers: { 'content-type': 'text/html' }}) - .should.equal('text/html'); - }) -})