example-app.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (function () {
  2. /*global describe, it*/
  3. 'use strict';
  4. var should = require('should'),
  5. express = require('express'),
  6. supertest = require('supertest'),
  7. cors = require('../lib');
  8. var simpleApp,
  9. complexApp;
  10. /* -------------------------------------------------------------------------- */
  11. simpleApp = express();
  12. simpleApp.head('/', cors(), function (req, res) {
  13. res.status(204).send();
  14. });
  15. simpleApp.get('/', cors(), function (req, res) {
  16. res.send('Hello World (Get)');
  17. });
  18. simpleApp.post('/', cors(), function (req, res) {
  19. res.send('Hello World (Post)');
  20. });
  21. /* -------------------------------------------------------------------------- */
  22. complexApp = express();
  23. complexApp.options('/', cors());
  24. complexApp.delete('/', cors(), function (req, res) {
  25. res.send('Hello World (Delete)');
  26. });
  27. /* -------------------------------------------------------------------------- */
  28. describe('example app(s)', function () {
  29. describe('simple methods', function () {
  30. it('GET works', function (done) {
  31. supertest(simpleApp)
  32. .get('/')
  33. .expect(200)
  34. .end(function (err, res) {
  35. should.not.exist(err);
  36. res.headers['access-control-allow-origin'].should.eql('*');
  37. res.text.should.eql('Hello World (Get)');
  38. done();
  39. });
  40. });
  41. it('HEAD works', function (done) {
  42. supertest(simpleApp)
  43. .head('/')
  44. .expect(204)
  45. .end(function (err, res) {
  46. should.not.exist(err);
  47. res.headers['access-control-allow-origin'].should.eql('*');
  48. done();
  49. });
  50. });
  51. it('POST works', function (done) {
  52. supertest(simpleApp)
  53. .post('/')
  54. .expect(200)
  55. .end(function (err, res) {
  56. should.not.exist(err);
  57. res.headers['access-control-allow-origin'].should.eql('*');
  58. res.text.should.eql('Hello World (Post)');
  59. done();
  60. });
  61. });
  62. });
  63. describe('complex methods', function () {
  64. it('OPTIONS works', function (done) {
  65. supertest(complexApp)
  66. .options('/')
  67. .expect(204)
  68. .end(function (err, res) {
  69. should.not.exist(err);
  70. res.headers['access-control-allow-origin'].should.eql('*');
  71. done();
  72. });
  73. });
  74. it('DELETE works', function (done) {
  75. supertest(complexApp)
  76. .del('/')
  77. .expect(200)
  78. .end(function (err, res) {
  79. should.not.exist(err);
  80. res.headers['access-control-allow-origin'].should.eql('*');
  81. res.text.should.eql('Hello World (Delete)');
  82. done();
  83. });
  84. });
  85. });
  86. });
  87. }());