Skip to content

Commit

Permalink
Added tests for router changes in balderdashy@42a0aaf
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Aug 6, 2014
1 parent 8960ccd commit a84e125
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 4 deletions.
5 changes: 3 additions & 2 deletions test/fixtures/middleware.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Stub middleware/handlers for use in tests.
*
*
* @type {Object}
*/
module.exports = {
HELLO: function (req, res) { res.send('hello world!'); },
GOODBYE: function (req, res) { res.send('goodbye world!'); },
HELLO_500: function (req, res) { res.send(500, 'hello world!'); },
JSON_HELLO: function (req, res) { res.json({ hello: 'world' }); },
SOMETHING_THAT_THROWS: function (req, res) { throw 'oops'; },
};
};
31 changes: 29 additions & 2 deletions test/integration/hook.cors_csrf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('CORS and CSRF ::', function() {
'POST /test': {controller: 'TestController', action: 'create', cors: 'http://www.different.com'},
'DELETE /test': {controller: 'TestController', action: 'delete', cors: false},
'POST /test2': {controller: 'TestController', action: 'create', cors: true},
'OPTIONS /test2': {controller: 'TestController', action: 'index'},
'PUT /test2': {controller: 'TestController', action: 'update'},
'GET /test/patch': {controller: 'TestController', action: 'update', cors: 'http://www.example.com:1338'},
'GET /test/create': {controller: 'TestController', action: 'create', cors: 'http://www.different.com'},
Expand Down Expand Up @@ -77,6 +78,32 @@ describe('CORS and CSRF ::', function() {

});

it('for a route without a custom OPTIONS handler should use the default Express handler', function(done) {

httpHelper.testRoute('options', {
url: 'test',
}, function(err, response) {
if (err) return done(new Error(err));
assert.equal(response.statusCode, 200);
assert.equal(response.body, "GET,POST,PUT,HEAD,DELETE,TRACE,COPY,LOCK,MKCOL,MOVE,PROPFIND,PROPPATCH,UNLOCK,REPORT,MKACTIVITY,CHECKOUT,MERGE,M-SEARCH,NOTIFY,SUBSCRIBE,UNSUBSCRIBE,PATCH");
done();
});

});

it('for a route with a custom OPTIONS handler should use the custom handler', function(done) {

httpHelper.testRoute('options', {
url: 'test2',
}, function(err, response) {
if (err) return done(new Error(err));
assert.equal(response.statusCode, 200);
assert.equal(response.body, "index");
done();
});

});

it('for a POST route with {cors: http://www.different.com} with an Access-Control-Request-Method header set to "POST" should respond with blank Access-Control-Allow-Origin and correct Access-Control-Allow-Method headers', function(done) {

httpHelper.testRoute('options', {
Expand Down Expand Up @@ -116,7 +143,7 @@ describe('CORS and CSRF ::', function() {
it('for a POST route with {cors: true} and an Access-Control-Request-Method header set to "POST" should respond with correct Access-Control-Allow-Origin and Access-Control-Allow-Method headers', function(done) {

httpHelper.testRoute('options', {
url: 'test/2',
url: 'test2',
headers: {
'Access-Control-Request-Method': 'POST',
'Origin': 'http://www.example.com'
Expand All @@ -134,7 +161,7 @@ describe('CORS and CSRF ::', function() {
it('for a PUT route with no CORS settings and an Access-Control-Request-Method header set to "PUT" should respond with correct Access-Control-Allow-Origin and Access-Control-Allow-Method headers', function(done) {

httpHelper.testRoute('options', {
url: 'test/2',
url: 'test2',
headers: {
'Access-Control-Request-Method': 'PUT',
'Origin': 'http://www.example.com'
Expand Down
170 changes: 170 additions & 0 deletions test/unit/router.bind.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,176 @@ describe('Router.bind', function() {
});
});

$Router.bind('/bar', RESPOND.HELLO)
.expectBoundRoute({
path: '/bar',
method: 'get'
})
.expectBoundRoute({
path: '/bar',
method: 'put'
})
.expectBoundRoute({
path: '/bar',
method: 'post'
})
.expectBoundRoute({
path: '/bar',
method: 'patch'
})
.expectBoundRoute({
path: '/bar',
method: 'delete'
})
.test(function() {
it('should send expected response (get /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.get('/bar')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (post /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.post('/bar')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (put /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.put('/bar')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (delete /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.del('/bar')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (patch /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.patch('/bar')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (options /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.options('/bar')
.expect(200, 'GET,POST,PUT,DELETE,PATCH')
.end(done);
});
})
.test(function() {
it('should send a 404 response (copy /bar)', function(done) {
supertest(this.sails.router._privateRouter)
.copy('/bar')
.expect(404)
.end(done);
});
});

$Router.bind('all /boop', RESPOND.HELLO)
.expectBoundRoute({
path: '/boop',
method: 'get'
})
.expectBoundRoute({
path: '/boop',
method: 'put'
})
.expectBoundRoute({
path: '/boop',
method: 'post'
})
.expectBoundRoute({
path: '/boop',
method: 'patch'
})
.expectBoundRoute({
path: '/boop',
method: 'delete'
})
.test(function() {
it('should send expected response (get /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.get('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (post /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.post('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (put /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.put('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (delete /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.del('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (patch /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.patch('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (options /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.options('/boop')
.expect(200, 'hello world!')
.end(done);
});
})
.test(function() {
it('should send expected response (options /boop)', function(done) {
supertest(this.sails.router._privateRouter)
.copy('/boop')
.expect(200, 'hello world!')
.end(done);
});
});


$Router.bind('options /blap', RESPOND.GOODBYE)
.expectBoundRoute({
path: '/boop',
method: 'options'
})
.test(function() {
it('should send expected response (options /blap)', function(done) {
supertest(this.sails.router._privateRouter)
.options('/blap')
.expect(200, 'goodbye world!')
.end(done);
});
});


$Router.bind('post /bar_baz_beezzz', RESPOND.HELLO_500)
Expand Down

0 comments on commit a84e125

Please sign in to comment.