server.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import request from 'supertest';
  2. import assert from 'assert';
  3. import listen from './helpers/listen';
  4. function testRoutes ({ prefix = '' } = {}) {
  5. const buildUrl = url => prefix ? `/${prefix}${url}` : url;
  6. describe('GET /', function () {
  7. it('respond with nothing, but respond', function (done) {
  8. request(this.server)
  9. .get(buildUrl('/'))
  10. .expect('Content-Type', /json/)
  11. .expect(/\{"tinylr":"Welcome","version":"[\d].[\d].[\d]+"\}/)
  12. .expect(200, done);
  13. });
  14. it('unknown route respond with proper 404 and error message', function (done) {
  15. request(this.server)
  16. .get(buildUrl('/whatev'))
  17. .expect('Content-Type', /json/)
  18. .expect('{"error":"not_found","reason":"no such route"}')
  19. .expect(404, done);
  20. });
  21. });
  22. describe('GET /changed', function () {
  23. it('with no clients, no files', function (done) {
  24. request(this.server)
  25. .get(buildUrl('/changed'))
  26. .expect('Content-Type', /json/)
  27. .expect(/"clients":\[\]/)
  28. .expect(/"files":\[\]/)
  29. .expect(200, done);
  30. });
  31. it('with no clients, some files', function (done) {
  32. request(this.server)
  33. .get(buildUrl('/changed?files=gonna.css,test.css,it.css'))
  34. .expect('Content-Type', /json/)
  35. .expect('{"clients":[],"files":["gonna.css","test.css","it.css"]}')
  36. .expect(200, done);
  37. });
  38. });
  39. describe('POST /changed', function () {
  40. it('with no clients, no files', function (done) {
  41. request(this.server)
  42. .post(buildUrl('/changed'))
  43. .expect('Content-Type', /json/)
  44. .expect(/"clients":\[\]/)
  45. .expect(/"files":\[\]/)
  46. .expect(200, done);
  47. });
  48. it('with no clients, some files', function (done) {
  49. const data = { clients: [], files: ['cat.css', 'sed.css', 'ack.js'] };
  50. request(this.server)
  51. .post(buildUrl('/changed'))
  52. // .type('json')
  53. .send({ files: data.files })
  54. .expect('Content-Type', /json/)
  55. .expect(JSON.stringify(data))
  56. .expect(200, done);
  57. });
  58. });
  59. describe('POST /alert', function () {
  60. it('with no clients, no message', function (done) {
  61. const data = { clients: [] };
  62. request(this.server)
  63. .post(buildUrl('/alert'))
  64. .expect('Content-Type', /json/)
  65. .expect(JSON.stringify(data))
  66. .expect(200, done);
  67. });
  68. it('with no clients, some message', function (done) {
  69. const message = 'Hello Client!';
  70. const data = { clients: [], message: message };
  71. request(this.server)
  72. .post(buildUrl('/alert'))
  73. .send({ message: message })
  74. .expect('Content-Type', /json/)
  75. .expect(JSON.stringify(data))
  76. .expect(200, done);
  77. });
  78. });
  79. describe('GET /livereload.js', function () {
  80. it('respond with livereload script', function (done) {
  81. request(this.server)
  82. .get(buildUrl('/livereload.js'))
  83. .expect(/LiveReload/)
  84. .expect(200, done);
  85. });
  86. });
  87. describe('GET /kill', function () {
  88. it('shutdown the server', function (done) {
  89. const srv = this.server;
  90. request(srv)
  91. .get(buildUrl('/kill'))
  92. .expect(200, err => {
  93. if (err) return done(err);
  94. assert.ok(!srv._handle);
  95. done();
  96. });
  97. });
  98. });
  99. }
  100. describe('Server', () => {
  101. context('with no options', function () {
  102. before(listen());
  103. testRoutes();
  104. });
  105. context('with prefix option', function () {
  106. const options = {
  107. prefix: 'tiny-lr'
  108. };
  109. before(listen(options));
  110. testRoutes(options);
  111. });
  112. });