2013-12-06 14:23:02 +00:00
|
|
|
assert = require 'assert'
|
2015-05-29 15:54:00 +00:00
|
|
|
http = require 'http'
|
2013-12-06 14:23:02 +00:00
|
|
|
path = require 'path'
|
2015-11-12 10:28:04 +00:00
|
|
|
|
|
|
|
{remote} = require 'electron'
|
|
|
|
{protocol} = remote.require 'electron'
|
2013-08-24 08:38:19 +00:00
|
|
|
|
2013-12-06 14:23:02 +00:00
|
|
|
describe 'protocol module', ->
|
2015-08-13 13:24:27 +00:00
|
|
|
protocolName = 'sp'
|
|
|
|
text = 'valar morghulis'
|
2013-08-24 12:18:12 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
afterEach (done) ->
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.unregisterProtocol protocolName, ->
|
|
|
|
protocol.uninterceptProtocol 'http', -> done()
|
2015-08-13 13:24:27 +00:00
|
|
|
|
|
|
|
describe 'protocol.register(Any)Protocol', ->
|
|
|
|
emptyHandler = (request, callback) -> callback()
|
|
|
|
it 'throws error when scheme is already registered', (done) ->
|
|
|
|
protocol.registerStringProtocol protocolName, emptyHandler, (error) ->
|
|
|
|
assert.equal error, null
|
|
|
|
protocol.registerBufferProtocol protocolName, emptyHandler, (error) ->
|
|
|
|
assert.notEqual error, null
|
2015-07-09 09:18:45 +00:00
|
|
|
done()
|
2013-08-25 04:36:06 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'does not crash when handler is called twice', (done) ->
|
|
|
|
doubleHandler = (request, callback) ->
|
2015-10-28 12:44:46 +00:00
|
|
|
try
|
|
|
|
callback(text)
|
|
|
|
callback()
|
|
|
|
catch
|
2015-08-13 13:24:27 +00:00
|
|
|
protocol.registerStringProtocol protocolName, doubleHandler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2013-08-25 12:45:34 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends error when callback is called with nothing', (done) ->
|
|
|
|
protocol.registerBufferProtocol protocolName, emptyHandler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2013-08-25 07:07:07 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'does not crash when callback is called in next tick', (done) ->
|
|
|
|
handler = (request, callback) ->
|
|
|
|
setImmediate -> callback(text)
|
|
|
|
protocol.registerStringProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2013-08-25 12:45:34 +00:00
|
|
|
|
2015-08-13 14:39:11 +00:00
|
|
|
describe 'protocol.unregisterProtocol', ->
|
|
|
|
it 'returns error when scheme does not exist', (done) ->
|
|
|
|
protocol.unregisterProtocol 'not-exist', (error) ->
|
|
|
|
assert.notEqual error, null
|
|
|
|
done()
|
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
describe 'protocol.registerStringProtocol', ->
|
|
|
|
it 'sends string as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(text)
|
|
|
|
protocol.registerStringProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2013-08-25 08:06:29 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends object as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(data: text, mimeType: 'text/html')
|
|
|
|
protocol.registerStringProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data, statux, request) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2015-05-07 07:42:35 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending object other than string', (done) ->
|
|
|
|
handler = (request, callback) -> callback(new Date)
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2015-05-07 07:42:35 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
describe 'protocol.registerBufferProtocol', ->
|
|
|
|
buffer = new Buffer(text)
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends Buffer as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(buffer)
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-05-29 15:54:00 +00:00
|
|
|
$.ajax
|
2015-08-13 13:24:27 +00:00
|
|
|
url: "#{protocolName}://fake-host"
|
2015-05-29 15:54:00 +00:00
|
|
|
success: (data) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal data, text
|
2015-05-29 15:54:00 +00:00
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
done(error)
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends object as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(data: buffer, mimeType: 'text/html')
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data, statux, request) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2015-03-16 12:53:45 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending string', (done) ->
|
|
|
|
handler = (request, callback) -> callback(text)
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2013-08-25 12:45:34 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
describe 'protocol.registerFileProtocol', ->
|
|
|
|
filePath = path.join __dirname, 'fixtures', 'asar', 'a.asar', 'file1'
|
|
|
|
fileContent = require('fs').readFileSync(filePath)
|
2013-08-29 12:57:09 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
normalPath = path.join __dirname, 'fixtures', 'pages', 'a.html'
|
|
|
|
normalContent = require('fs').readFileSync(normalPath)
|
2015-03-04 05:22:51 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends file path as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(filePath)
|
|
|
|
protocol.registerFileProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, String(fileContent)
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2015-03-04 05:22:51 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'sends object as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(path: filePath)
|
|
|
|
protocol.registerFileProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data, statux, request) ->
|
|
|
|
assert.equal data, String(fileContent)
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2015-03-20 11:12:59 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'can send normal file', (done) ->
|
|
|
|
handler = (request, callback) -> callback(normalPath)
|
|
|
|
protocol.registerFileProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, String(normalContent)
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2015-03-20 11:12:59 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending unexist-file', (done) ->
|
|
|
|
fakeFilePath = path.join __dirname, 'fixtures', 'asar', 'a.asar', 'not-exist'
|
|
|
|
handler = (request, callback) -> callback(fakeFilePath)
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending unsupported content', (done) ->
|
|
|
|
handler = (request, callback) -> callback(new Date)
|
|
|
|
protocol.registerBufferProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
describe 'protocol.registerHttpProtocol', ->
|
|
|
|
it 'sends url as response', (done) ->
|
|
|
|
server = http.createServer (req, res) ->
|
|
|
|
assert.notEqual req.headers.accept, ''
|
|
|
|
res.end(text)
|
|
|
|
server.close()
|
|
|
|
server.listen 0, '127.0.0.1', ->
|
|
|
|
{port} = server.address()
|
|
|
|
url = "http://127.0.0.1:#{port}"
|
|
|
|
handler = (request, callback) -> callback({url})
|
|
|
|
protocol.registerHttpProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 13:24:27 +00:00
|
|
|
$.ajax
|
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending invalid url', (done) ->
|
|
|
|
handler = (request, callback) -> callback({url: 'url'})
|
|
|
|
protocol.registerHttpProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2013-09-03 08:50:10 +00:00
|
|
|
$.ajax
|
2015-08-13 13:24:27 +00:00
|
|
|
url: "#{protocolName}://fake-host"
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
2013-09-03 08:50:10 +00:00
|
|
|
error: (xhr, errorType, error) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2013-09-03 08:50:10 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'fails when sending unsupported content', (done) ->
|
|
|
|
handler = (request, callback) -> callback(new Date)
|
|
|
|
protocol.registerHttpProtocol protocolName, handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2013-09-03 08:50:10 +00:00
|
|
|
$.ajax
|
2015-08-13 13:24:27 +00:00
|
|
|
url: "#{protocolName}://fake-host"
|
2013-09-03 08:50:10 +00:00
|
|
|
success: (data) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
done('request succeeded but it should not')
|
2013-09-03 08:50:10 +00:00
|
|
|
error: (xhr, errorType, error) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
2015-05-06 20:15:56 +00:00
|
|
|
|
2015-08-13 14:39:11 +00:00
|
|
|
describe 'protocol.isProtocolHandled', ->
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'returns true for file:', (done) ->
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.isProtocolHandled 'file', (result) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal result, true
|
2015-05-06 20:15:56 +00:00
|
|
|
done()
|
2015-05-11 07:26:36 +00:00
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'returns true for http:', (done) ->
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.isProtocolHandled 'http', (result) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal result, true
|
2015-05-11 07:26:36 +00:00
|
|
|
done()
|
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'returns true for https:', (done) ->
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.isProtocolHandled 'https', (result) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal result, true
|
2015-05-11 07:26:36 +00:00
|
|
|
done()
|
|
|
|
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'returns false when scheme is not registred', (done) ->
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.isProtocolHandled 'no-exist', (result) ->
|
2015-08-13 13:24:27 +00:00
|
|
|
assert.equal result, false
|
2015-05-11 07:26:36 +00:00
|
|
|
done()
|
2015-08-13 14:39:11 +00:00
|
|
|
|
|
|
|
it 'returns true for custom protocol', (done) ->
|
|
|
|
emptyHandler = (request, callback) -> callback()
|
|
|
|
protocol.registerStringProtocol protocolName, emptyHandler, (error) ->
|
|
|
|
assert.equal error, null
|
|
|
|
protocol.isProtocolHandled protocolName, (result) ->
|
|
|
|
assert.equal result, true
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'returns true for intercepted protocol', (done) ->
|
|
|
|
emptyHandler = (request, callback) -> callback()
|
|
|
|
protocol.interceptStringProtocol 'http', emptyHandler, (error) ->
|
|
|
|
assert.equal error, null
|
|
|
|
protocol.isProtocolHandled 'http', (result) ->
|
|
|
|
assert.equal result, true
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'protocol.intercept(Any)Protocol', ->
|
|
|
|
emptyHandler = (request, callback) -> callback()
|
|
|
|
|
|
|
|
it 'throws error when scheme is already intercepted', (done) ->
|
|
|
|
protocol.interceptStringProtocol 'http', emptyHandler, (error) ->
|
|
|
|
assert.equal error, null
|
|
|
|
protocol.interceptBufferProtocol 'http', emptyHandler, (error) ->
|
|
|
|
assert.notEqual error, null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'does not crash when handler is called twice', (done) ->
|
|
|
|
doubleHandler = (request, callback) ->
|
2015-10-28 12:44:46 +00:00
|
|
|
try
|
|
|
|
callback(text)
|
|
|
|
callback()
|
|
|
|
catch
|
2015-08-13 14:39:11 +00:00
|
|
|
protocol.interceptStringProtocol 'http', doubleHandler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 14:39:11 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'http://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
|
|
|
|
|
|
|
it 'sends error when callback is called with nothing', (done) ->
|
|
|
|
protocol.interceptBufferProtocol 'http', emptyHandler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 14:39:11 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'http://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
done('request succeeded but it should not')
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert.equal errorType, 'error'
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'protocol.interceptStringProtocol', ->
|
|
|
|
it 'can intercept http protocol', (done) ->
|
|
|
|
handler = (request, callback) -> callback(text)
|
|
|
|
protocol.interceptStringProtocol 'http', handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 14:39:11 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'http://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
|
|
|
|
2015-09-15 14:24:16 +00:00
|
|
|
it 'can set content-type', (done) ->
|
|
|
|
handler = (request, callback) ->
|
|
|
|
callback({mimeType: 'application/json', data: '{"value": 1}'})
|
|
|
|
protocol.interceptStringProtocol 'http', handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-09-15 14:24:16 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'http://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal typeof(data), 'object'
|
|
|
|
assert.equal data.value, 1
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
|
|
|
|
2015-08-13 14:39:11 +00:00
|
|
|
describe 'protocol.interceptBufferProtocol', ->
|
|
|
|
it 'can intercept http protocol', (done) ->
|
|
|
|
handler = (request, callback) -> callback(new Buffer(text))
|
|
|
|
protocol.interceptBufferProtocol 'http', handler, (error) ->
|
2015-10-28 10:19:36 +00:00
|
|
|
return done(error) if error
|
2015-08-13 14:39:11 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'http://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, text
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
done(error)
|
|
|
|
|
|
|
|
describe 'protocol.uninterceptProtocol', ->
|
|
|
|
it 'returns error when scheme does not exist', (done) ->
|
|
|
|
protocol.uninterceptProtocol 'not-exist', (error) ->
|
|
|
|
assert.notEqual error, null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'returns error when scheme is not intercepted', (done) ->
|
|
|
|
protocol.uninterceptProtocol 'http', (error) ->
|
|
|
|
assert.notEqual error, null
|
|
|
|
done()
|