1 | livereload = require '../lib/livereload'
|
2 | should = require 'should'
|
3 | request = require 'request'
|
4 | http = require 'http'
|
5 | url = require 'url'
|
6 | fs = require 'fs'
|
7 | path = require 'path'
|
8 | WebSocket = require 'ws'
|
9 | sinon = require 'sinon'
|
10 |
|
11 | describe 'livereload http file serving', ->
|
12 |
|
13 | it 'should serve up livereload.js', (done) ->
|
14 | server = livereload.createServer({port: 35729})
|
15 |
|
16 | fileContents = fs.readFileSync('./ext/livereload.js').toString()
|
17 |
|
18 | request 'http://localhost:35729/livereload.js?snipver=1', (error, response, body) ->
|
19 | should.not.exist error
|
20 | response.statusCode.should.equal 200
|
21 | fileContents.should.equal body
|
22 |
|
23 | server.config.server.close()
|
24 |
|
25 | done()
|
26 |
|
27 | it 'should connect to the websocket server', (done) ->
|
28 | server = livereload.createServer({port: 35729})
|
29 |
|
30 | ws = new WebSocket('ws://localhost:35729/livereload')
|
31 |
|
32 | ws.on 'open', () ->
|
33 | data = JSON.stringify {
|
34 | command: 'hello',
|
35 | protocols: [
|
36 | 'http://livereload.com/protocols/official-7',
|
37 | 'http://livereload.com/protocols/official-8',
|
38 | 'http://livereload.com/protocols/2.x-origin-version-negotiation']
|
39 | }
|
40 | ws.send data
|
41 | ws.on 'message', (data, flags) ->
|
42 | console.log "hello"
|
43 |
|
44 | data.should.equal JSON.stringify {
|
45 | command: 'hello',
|
46 | protocols: [
|
47 | 'http://livereload.com/protocols/official-7',
|
48 | 'http://livereload.com/protocols/official-8',
|
49 | 'http://livereload.com/protocols/official-9',
|
50 | 'http://livereload.com/protocols/2.x-origin-version-negotiation',
|
51 | 'http://livereload.com/protocols/2.x-remote-control'],
|
52 | serverName: 'node-livereload'
|
53 | }
|
54 |
|
55 | server.config.server.close()
|
56 |
|
57 | done()
|
58 |
|
59 | it 'should allow you to override the internal http server', (done) ->
|
60 | app = http.createServer (req, res) ->
|
61 | if url.parse(req.url).pathname is '/livereload.js'
|
62 | res.writeHead(200, {'Content-Type': 'text/javascript'})
|
63 | res.end '// nothing to see here'
|
64 |
|
65 | server = livereload.createServer({port: 35729, server: app})
|
66 |
|
67 | request 'http://localhost:35729/livereload.js?snipver=1', (error, response, body) ->
|
68 | should.not.exist error
|
69 | response.statusCode.should.equal 200
|
70 | body.should.equal '// nothing to see here'
|
71 |
|
72 | server.config.server.close()
|
73 |
|
74 | done()
|
75 |
|
76 | it 'should allow you to specify ssl certificates to run via https', (done)->
|
77 | server = livereload.createServer
|
78 | port: 35729
|
79 | https:
|
80 | cert: fs.readFileSync path.join __dirname, 'ssl/localhost.cert'
|
81 | key: fs.readFileSync path.join __dirname, 'ssl/localhost.key'
|
82 |
|
83 | fileContents = fs.readFileSync('./ext/livereload.js').toString()
|
84 |
|
85 |
|
86 | unsafeRequest = request.defaults
|
87 | strictSSL: false
|
88 | rejectUnauthorized: false
|
89 |
|
90 | unsafeRequest 'https://localhost:35729/livereload.js?snipver=1', (error, response, body) ->
|
91 | should.not.exist error
|
92 | response.statusCode.should.equal 200
|
93 | fileContents.should.equal body
|
94 |
|
95 | server.config.server.close()
|
96 |
|
97 | done()
|
98 |
|
99 | it 'should support passing a callback to the websocket server', (done) ->
|
100 | server = livereload.createServer {port: 35729}, ->
|
101 | server.config.server.close()
|
102 | done()
|
103 |
|
104 | describe 'livereload file watching', ->
|
105 | describe "config.delay", ->
|
106 | tmpFile = tmpFile2 = clock = server = refresh = undefined
|
107 |
|
108 | beforeEach (done) ->
|
109 | tmpFile = path.join(__dirname, "tmp.js")
|
110 | tmpFile2 = path.join(__dirname, "tmp2.js")
|
111 | fs.writeFileSync(tmpFile, "use strict;", "utf-8")
|
112 | fs.writeFileSync(tmpFile2, "use strict;", "utf-8")
|
113 |
|
114 | setTimeout(done, 1000)
|
115 |
|
116 | afterEach (done) ->
|
117 | server.close()
|
118 | server = undefined
|
119 |
|
120 | setTimeout(done, 1000)
|
121 |
|
122 | after ->
|
123 | fs.unlinkSync(tmpFile)
|
124 | fs.unlinkSync(tmpFile2)
|
125 |
|
126 | describe 'when set', ->
|
127 | beforeEach (done) ->
|
128 | server = livereload.createServer({delay: 2000, port: 12345})
|
129 | refresh = sinon.spy(server, "refresh")
|
130 | server.watch(__dirname)
|
131 | server.watcher.on('ready', done)
|
132 |
|
133 | it 'should send a refresh message after `config.delay` milliseconds', (done) ->
|
134 | refresh.callCount.should.be.exactly(0)
|
135 | fs.writeFileSync(tmpFile, "use strict; var a = 1;", "utf-8")
|
136 |
|
137 |
|
138 | setTimeout(->
|
139 | refresh.callCount.should.be.exactly(0)
|
140 | , 1500)
|
141 |
|
142 |
|
143 | setTimeout(->
|
144 | refresh.callCount.should.be.exactly(1)
|
145 | done()
|
146 | , 3000)
|
147 |
|
148 | it 'should only set the timeout/refresh for files that have been changed', (done) ->
|
149 | refresh.callCount.should.be.exactly(0)
|
150 | fs.writeFileSync(tmpFile2, "use strict; var a = 2;", "utf-8")
|
151 |
|
152 | setTimeout(->
|
153 | refresh.callCount.should.be.exactly(1)
|
154 | done()
|
155 | , 3000)
|
156 |
|
157 | describe 'when not set or set to 0', ->
|
158 | beforeEach (done) ->
|
159 | server = livereload.createServer({delay: 0, port: 22345})
|
160 | refresh = sinon.spy(server, "refresh")
|
161 | server.watch(__dirname)
|
162 | server.watcher.on('ready', done)
|
163 |
|
164 | it 'should send a refresh message near immediately if `config.delay` is falsey`', (done) ->
|
165 | refresh.callCount.should.be.exactly(0)
|
166 | fs.writeFileSync(tmpFile, "use strict; var a = 1;", "utf-8")
|
167 |
|
168 |
|
169 | setTimeout(->
|
170 | refresh.callCount.should.be.exactly(1)
|
171 | done()
|
172 | , 500)
|
173 |
|
174 |
|
175 | it 'should correctly watch common files', ->
|
176 |
|
177 |
|
178 | it 'should correctly ignore common exclusions', ->
|
179 |
|
180 |
|
181 | it 'should not exclude a dir named git', ->
|
182 |
|