issue-2.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 app,
  9. corsOptions;
  10. /* -------------------------------------------------------------------------- */
  11. app = express();
  12. corsOptions = {
  13. origin: true,
  14. methods: ['POST'],
  15. credentials: true,
  16. maxAge: 3600
  17. };
  18. app.options('/api/login', cors(corsOptions));
  19. app.post('/api/login', cors(corsOptions), function (req, res) {
  20. res.send('LOGIN');
  21. });
  22. /* -------------------------------------------------------------------------- */
  23. describe('issue #2', function () {
  24. it('OPTIONS works', function (done) {
  25. supertest(app)
  26. .options('/api/login')
  27. .expect(204)
  28. .set('Origin', 'http://example.com')
  29. .end(function (err, res) {
  30. should.not.exist(err);
  31. res.headers['access-control-allow-origin'].should.eql('http://example.com');
  32. done();
  33. });
  34. });
  35. it('POST works', function (done) {
  36. supertest(app)
  37. .post('/api/login')
  38. .expect(200)
  39. .set('Origin', 'http://example.com')
  40. .end(function (err, res) {
  41. should.not.exist(err);
  42. res.headers['access-control-allow-origin'].should.eql('http://example.com');
  43. res.text.should.eql('LOGIN');
  44. done();
  45. });
  46. });
  47. });
  48. }());