Comparing version 2.0.1 to 2.0.3
@@ -11,2 +11,3 @@ module.exports = createApi | ||
, logger: console | ||
, maxBodySize: '100kb' | ||
} | ||
@@ -13,0 +14,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "A pluggable JSON API server", | ||
"version": "2.0.1", | ||
"version": "2.0.3", | ||
"tags": [], | ||
@@ -30,4 +30,4 @@ "repository": { | ||
"jshint-full-path": "0", | ||
"supertest": "balaclark/supertest" | ||
"supertest": "1" | ||
} | ||
} |
@@ -37,3 +37,4 @@ # cf-api | ||
- `checkOrigin` - a function with the signature `function (url, cb) {}` to check `req.headers.origin`. `cb(null, true)` to allow and `origin`, `cb(null, false)` to deny an origin. Defaults to `cb(null, true)` for all requests, meaning all cross-domain requests are allowed. It is up to the user to implement their whitelist/blacklist. | ||
- `logger` - a logger object with methods `debug()`, `info()`, `warn()` and `error()` (default: `console`) | ||
- `logger` - a logger object with methods `debug()`, `info()`, `warn()` and `error()` (default: `console`). | ||
- `maxBodySize` - an option to be passed along to the [body-parser json middleware](https://github.com/expressjs/body-parser#limit) function. If this is a number it will be the number of bytes, otherwise it will be parsed by the [bytes module](https://github.com/visionmedia/bytes.js) (default: `undefined` which falls back to the body parser default of `'100kB'`). | ||
@@ -40,0 +41,0 @@ *For backwards compatibility, the `allowedDomains` option still works and generates a `checkOrigin` function for you.* |
@@ -43,3 +43,3 @@ module.exports = createServer | ||
// Body parse API for JSON content type | ||
.use(bodyParser.json()) | ||
.use(bodyParser.json({ limit: options.maxBodySize })) | ||
@@ -53,4 +53,8 @@ // Server only speaks JSON | ||
// Allow routes to be added before the error handler. | ||
// When routes have finished being added `.emit('preBoot')` | ||
app.on('preBoot', function () { | ||
// Handle and log server error | ||
.use(errorHandler(options.logger)) | ||
app.use(errorHandler(options.logger)) | ||
}) | ||
@@ -57,0 +61,0 @@ return app |
var request = require('supertest') | ||
, createServer = require('../server') | ||
, noopLogger = { debug: noop, info: noop, warn: noop, error: noop } | ||
, assert = require('assert') | ||
@@ -8,11 +10,5 @@ function noop() {} | ||
var app | ||
before(function () { | ||
var noopLogger = { debug: noop, info: noop, warn: noop, error: noop } | ||
app = createServer({ logger: noopLogger, properties: { allowedDomains: [] } }) | ||
}) | ||
it('should start up and respond to a request', function (done) { | ||
var app = createServer({ logger: noopLogger, properties: { allowedDomains: [] } }) | ||
request(app) | ||
@@ -22,5 +18,59 @@ .get('/') | ||
.expect(404, done) | ||
}) | ||
it('should send a 413 response when request body is larger than `opts.maxBodySize`', function (done) { | ||
var buf = new Buffer(2) | ||
, app = createServer({ maxBodySize: '1b', logger: noopLogger, properties: { allowedDomains: [] } }) | ||
buf.fill('.') | ||
request(app) | ||
.post('/') | ||
.set('Accept', 'application/json') | ||
.set('Content-Type', 'application/json') | ||
.set('Content-Length', '12') | ||
.send(JSON.stringify({ str: buf.toString() })) | ||
.expect(413, done) | ||
}) | ||
}) | ||
it('should send a 200 response when request body is smaller than `opts.maxBodySize`', function (done) { | ||
var buf = new Buffer(2) | ||
, app = createServer({ maxBodySize: '100b', logger: noopLogger, properties: { allowedDomains: [] } }) | ||
app.post('/test', function (req, res) { res.end() }) | ||
buf.fill('.') | ||
request(app) | ||
.post('/test') | ||
.set('Accept', 'application/json') | ||
.set('Content-Type', 'application/json') | ||
.set('Content-Length', '12') | ||
.send(JSON.stringify({ str: buf.toString() })) | ||
.expect(200, done) | ||
}) | ||
it('shouldn’t add error middleware until "preBoot" event is emitted', function (done) { | ||
var app = createServer({ logger: noopLogger, properties: { allowedDomains: [] } }) | ||
app.use(function (req, res, next) { next(new Error('hi from test')) }) | ||
request(app) | ||
.get('/dshkdsfhk') | ||
.set('Accept', 'application/json') | ||
.expect(500) | ||
.end(function (err, res) { | ||
if (err) return done(err) | ||
app.emit('preBoot') | ||
assert(/Error: hi from test/.test(res.text)) | ||
request(app) | ||
.get('/dshkdsfhk') | ||
.set('Accept', 'application/json') | ||
.expect(500) | ||
.end(function (err, res) { | ||
if (err) return done(err) | ||
assert.deepEqual(res.body, { error: 'hi from test' }) | ||
}) | ||
done() | ||
}) | ||
}) | ||
}) |
31089
797
69