Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
add requireParameters config option, return 400 bad request if requir…
Browse files Browse the repository at this point in the history
…ed parameter not found in query/body/paras
  • Loading branch information
29decibel committed Mar 21, 2014
1 parent 7856aa2 commit 88c2e03
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
_site
_site
node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This is where dyson comes in. Get a full fake server for your application up and
* Includes dummy image generator
* Use any external or local image service (included)
* Supports base64 encoded image strings
* Supports required parameter validation

[![Build Status](https://travis-ci.org/webpro/dyson.png)](https://travis-ci.org/webpro/dyson)

Expand Down
16 changes: 16 additions & 0 deletions dummy/get/require-parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var g = require('dyson-generators');

module.exports = {
path: '/requirepara',
requireParameters: ["client_id"],
template: {
id: g.id,
name: g.name,
address: {
city: g.address.city,
zipUS: g.address.zipUS
},
time: g.time.byQuarter,
lorem: g.lorem.short,
}
};
4 changes: 3 additions & 1 deletion lib/dyson.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var loader = require('./loader'),
defaults = require('./defaults'),
proxy = require('./proxy'),
util = require('./util'),
requireParameter = require('./parameter'),
cors = require('cors'),
express = require('express'),
path = require('path');
Expand Down Expand Up @@ -42,6 +43,7 @@ var initExpress = function(port) {
return app;
};


// Register middleware to Express as service for each config (as in: `app.get(config.path, config.callback);`)

var registerServices = function(app, options, configs) {
Expand All @@ -52,7 +54,7 @@ var registerServices = function(app, options, configs) {

util.log('Registering', method.toUpperCase(), 'service at', config.path);

app[method](config.path, config.callback.bind(config), config.render.bind(config));
app[method](config.path, requireParameter.bind(config), config.callback.bind(config), config.render.bind(config));
});
}

Expand Down
22 changes: 22 additions & 0 deletions lib/parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var _ = require('lodash');

module.exports = function(req, res, next){
var config = this,
missingParameters = [];

// check if it's need require parameters
if (!_.isEmpty(config.requireParameters)) {
_(config.requireParameters).forEach(function(p){
if (_.isEmpty(req.body[p]) && _.isEmpty(req.query[p])) {
missingParameters.push(p);
}
});

// check missing parameters
if (missingParameters.length > 0) {
res.status(400).send({"error": "Required parameters(" + missingParameters.join(',') + ") not found."});
}
}

next();
};
20 changes: 18 additions & 2 deletions test/dyson.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe('dyson', function() {
}, {
path: '/combined/:id',
template: {}
}, {
path: '/require-para',
requireParameters: ['name']
}]
};

Expand Down Expand Up @@ -109,16 +112,29 @@ describe('dyson', function() {

});


it('should respond with a 204 for an OPTIONS request', function(done) {

request(app).options('/').expect(204).end(function(err, res) {
res.headers['access-control-allow-methods'].should.equal('GET,HEAD,PUT,POST,DELETE');
res.headers['access-control-allow-credentials'].should.equal('true');
// The next actual value is 'undefined', should be req.header('Origin') (probably an issue with supertest)
// res.headers['access-control-allow-origin'].should.equal('*');
// The next actual value is 'undefined', should be req.header('Origin') (probably an issue with supertest)
// res.headers['access-control-allow-origin'].should.equal('*');
done();
});

});

it('should respond with 400 bad request if required parameter not found', function(done) {
request(app).get('/require-para').expect(400).end(function(err, res) {
res.body.error.should.include("(name) not found");

// with required parameter
request(app).get('/require-para?name=Tim').expect(200).end(function(err, res) {
done();
});
});

});
})
});

0 comments on commit 88c2e03

Please sign in to comment.