Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
improved handling of unused routes; see #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Schulze committed May 28, 2016
1 parent 9373fa6 commit a3384a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
49 changes: 24 additions & 25 deletions api/v1/experiment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@ module.exports = function(io) {
'../../..',
'/models/Experiment')).Experiment;

/**
* Answers the request for an undefined action route pair with a Bad Request
* error.
*
* @param res the response object as given by the express router
*/
function respondWithNoSuchRoute(res) {
res.status(400).json({
'message': 'No action defined for this method and path'
});
}

/* unused routes */
app.get('/api/v1/experiment', function(req, res) {
return respondWithNoSuchRoute(res);
});

app.put('/api/v1/experiment', function(req, res) {
return respondWithNoSuchRoute(res);
});

app.delete('/api/v1/experiment', function(req, res) {
return respondWithNoSuchRoute(res);
});

app.post('/api/v1/experiment/', function(req, res) {
var exp = new Experiment({
'name': req.body.name || haikunate(),
Expand Down Expand Up @@ -398,5 +373,29 @@ module.exports = function(io) {
});
});

app.get('/api/v1/*', function(req, res) {
return res.status(404).json({
'message': 'No such route'
});
});

app.put('/api/v1/*', function(req, res) {
return res.status(404).json({
'message': 'No such route'
});
});

app.post('/api/v1/*', function(req, res) {
return res.status(404).json({
'message': 'No such route'
});
});

app.delete('/api/v1/*', function(req, res) {
return res.status(404).json({
'message': 'No such route'
});
});

return app;
};
6 changes: 3 additions & 3 deletions tests/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
it('should return bad request for get /experiment', function(done) {
request(app)
.get('/api/v1/experiment')
.expect(400)
.expect(404)
.send({'name': 'does not matter'})
.end(function(err, doc) {
done(err);
Expand All @@ -179,7 +179,7 @@
it('should return bad request for put /experiment', function(done) {
request(app)
.put('/api/v1/experiment')
.expect(400)
.expect(404)
.send({'name': 'does not matter'})
.end(function(err, doc) {
done(err);
Expand All @@ -189,7 +189,7 @@
it('should return bad request for delete /experiment', function(done) {
request(app)
.del('/api/v1/experiment')
.expect(400)
.expect(404)
.send({'name': 'does not matter'})
.end(function(err, doc) {
done(err);
Expand Down

0 comments on commit a3384a2

Please sign in to comment.