UI for Zipcoin Blue

middleware.js 3.1KB

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