middleware.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var http = require('http');
  2. var express = require('express');
  3. var request = require('supertest');
  4. var debug = require('debug')('tinylr:test');
  5. var Server = require('..').Server;
  6. var port = parseInt(process.env.npm_package_config_test_port || 0, 10);
  7. describe('Express Middleware', () => {
  8. before(function () {
  9. this.app = express();
  10. this.lr = new Server();
  11. this.app.use(this.lr.handler.bind(this.lr));
  12. this.server = http.createServer(this.app);
  13. debug('Start %s suite, listen on %d', 'Express', port);
  14. this.server.listen(port);
  15. });
  16. after(function (done) {
  17. this.server.close(done);
  18. });
  19. describe('GET /', function () {
  20. it('respond with nothing, but respond', function (done) {
  21. request(this.server)
  22. .get('/')
  23. .expect('Content-Type', /json/)
  24. .expect(/\{"tinylr":"Welcome","version":"[\d].[\d].[\d]+"\}/)
  25. .expect(200, done);
  26. });
  27. it('unknown route are noop with middlewares, next-ing', function (done) {
  28. request(this.server)
  29. .get('/whatev')
  30. .expect('Content-Type', /text\/html/)
  31. .expect(/Cannot GET \/whatev/)
  32. .expect(404, done);
  33. });
  34. });
  35. describe('GET /changed', function () {
  36. it('with no clients, no files', function (done) {
  37. request(this.server)
  38. .get('/changed')
  39. .expect('Content-Type', /json/)
  40. .expect(/"clients":\[\]/)
  41. .expect(/"files":\[\]/)
  42. .expect(200, done);
  43. });
  44. it('with no clients, some files', function (done) {
  45. request(this.server)
  46. .get('/changed?files=gonna.css,test.css,it.css')
  47. .expect('Content-Type', /json/)
  48. .expect('{"clients":[],"files":["gonna.css","test.css","it.css"]}')
  49. .expect(200, done);
  50. });
  51. });
  52. describe('POST /changed', function () {
  53. it('with no clients, no files', function (done) {
  54. request(this.server)
  55. .post('/changed')
  56. .expect('Content-Type', /json/)
  57. .expect(/"clients":\[\]/)
  58. .expect(/"files":\[\]/)
  59. .expect(200, done);
  60. });
  61. it('with no clients, some files', function (done) {
  62. var data = { clients: [], files: ['cat.css', 'sed.css', 'ack.js'] };
  63. request(this.server)
  64. .post('/changed')
  65. .send({ files: data.files })
  66. .expect('Content-Type', /json/)
  67. // .expect(JSON.stringify(data))
  68. .expect(200, done);
  69. });
  70. });
  71. describe('POST /alert', function () {
  72. it('with no clients, no message', function (done) {
  73. var data = { clients: [] };
  74. request(this.server)
  75. .post('/alert')
  76. .expect('Content-Type', /json/)
  77. .expect(JSON.stringify(data))
  78. .expect(200, done);
  79. });
  80. it('with no clients, some message', function (done) {
  81. var message = 'Hello Client!';
  82. var data = { clients: [], message: message };
  83. request(this.server)
  84. .post('/alert')
  85. .send({ message: message })
  86. .expect('Content-Type', /json/)
  87. .expect(JSON.stringify(data))
  88. .expect(200, done);
  89. });
  90. });
  91. describe('GET /livereload.js', function () {
  92. it('respond with livereload script', function (done) {
  93. request(this.server)
  94. .get('/livereload.js')
  95. .expect(/LiveReload/)
  96. .expect(200, done);
  97. });
  98. });
  99. describe('GET /kill', function () {
  100. it('shutdown the server', function (done) {
  101. var server = this.server;
  102. request(server)
  103. .get('/kill')
  104. .expect(200, done);
  105. });
  106. });
  107. });