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