Skip to content

Commit

Permalink
[api] allow a custom respond method to be set when using restful as a…
Browse files Browse the repository at this point in the history
… flatiron plugin.
  • Loading branch information
nrw committed Oct 23, 2012
1 parent b7006a7 commit 3643a06
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/restful.js
Expand Up @@ -143,7 +143,8 @@ exports.attach = function (options) {
exports.extendRouter(
app.router,
app.resources[resource],
_options
_options,
_options.respond
);
}
});
Expand Down
112 changes: 112 additions & 0 deletions test/flatiron-plugin-custom-respond-test.js
@@ -0,0 +1,112 @@
/*
* flatiron-plugin-custom-respond-test.js: Integration test with Flatiron as
* plugin and a custom respond method
*
* (C) 2012, Nodejitsu Inc.
*
*/

var vows = require('vows'),
assert = require('assert'),
APIeasy = require('api-easy');

var suite = APIeasy.describe('restful/flatiron-plugin-custom-respond-test');

var flatiron = require('flatiron'),
fixtures = require('./fixtures'),
macros = require('./macros'),
restful = require('../lib/restful');

var app = flatiron.app;
app.resources = {};
app.resources.Creature = fixtures.Creature;

app.use(flatiron.plugins.http);
app.use(restful, {
respond: function (req, res, status, key, value) {
var result;
res.writeHead(status);

if (arguments.length === 5) {
result = value;
}
else {
result = key;
}

res.end(result ? JSON.stringify(result) : '');
}
});

app.start(8005)

suite.use('localhost', 8005)
.setHeader('Content-Type', 'application/json')
.followRedirect(false)
.post('/creature/new', { id: 1 })
.expect(201)
.next()
.get('/creature/1')
.expect(200)
.next()
.post('/creature/1/update', { 'type' : 'dragon' })
.expect(204)
.next()
.post('/creature/1/destroy')
.expect(204)
.next()
.get('/creature/1')
.expect(404)
.next()
.post('/creature/new', { id: "bob" })
.expect(201)
.next()
.get('/creature/bob')
.expect(200)
.next()
.post('/creature', { id: 2, 'type': 'Dragon' })
.expect(201)
.next()
.get('/creature/2')
.expect(200)
.expect('should have correct type', function (err, res, body) {
var creature = JSON.parse(body);
assert.isObject(creature)
assert.equal(creature.type, 'Dragon');
})
.next()
.get('/creature/find')
.expect(200)
.expect('should return all creatures', function (err, res, body) {
var creature = JSON.parse(body);
assert.isArray(creature);
})
.next()
.post('/creature/find')
.expect(200)
.expect('should return all creatures', function (err, res, body) {
var creature = JSON.parse(body);
assert.isArray(creature);
})
.next()
.post('/creature/find', { 'type': 'Dragon'})
.expect(200)
.expect('should return only Dragons', function (err, res, body) {
var creature = JSON.parse(body);
assert.isArray(creature);
creature.forEach(function(creature){
assert.equal(creature.type, 'Dragon');
});
})
.next()
.get('/creature/find?type=Dragon')
.expect(200)
.expect('should return only Dragons', function (err, res, body) {
var creature = JSON.parse(body);
assert.isArray(creature);
creature.forEach(function(creature){
assert.equal(creature.type, 'Dragon');
});
})

.export(module);
Expand Up @@ -28,7 +28,7 @@ suite.use('localhost', 8004)
TODO:
Uncomment these tests and figure out why they are not passing on travis
.addBatch({
'With a flatiron app ' : {
'starting the app': {
Expand Down

0 comments on commit 3643a06

Please sign in to comment.