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'
|
|
|
|
remote = require 'remote'
|
2013-08-25 12:45:34 +00:00
|
|
|
protocol = remote.require 'protocol'
|
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) ->
|
|
|
|
protocol.unregisterProtocol protocolName, -> done()
|
2013-08-24 08:38:19 +00:00
|
|
|
|
|
|
|
describe 'protocol.unregisterProtocol', ->
|
2015-08-13 13:24:27 +00:00
|
|
|
it 'returns error when scheme does not exist', (done) ->
|
|
|
|
protocol.unregisterProtocol 'not-exist', (error) ->
|
|
|
|
assert.notEqual error, null
|
|
|
|
done()
|
|
|
|
|
|
|
|
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) ->
|
|
|
|
callback(text)
|
|
|
|
callback()
|
|
|
|
protocol.registerStringProtocol protocolName, doubleHandler, (error) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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
|
|
|
describe 'protocol.registerStringProtocol', ->
|
|
|
|
it 'sends string as response', (done) ->
|
|
|
|
handler = (request, callback) -> callback(text)
|
|
|
|
protocol.registerStringProtocol protocolName, handler, (error) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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-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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
|
|
|
$.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) ->
|
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) ->
|
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 13:24:27 +00:00
|
|
|
describe 'protocol.isHandledProtocol', ->
|
|
|
|
it 'returns true for file:', (done) ->
|
|
|
|
protocol.isHandledProtocol 'file', (result) ->
|
|
|
|
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) ->
|
|
|
|
protocol.isHandledProtocol 'http', (result) ->
|
|
|
|
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) ->
|
|
|
|
protocol.isHandledProtocol 'https', (result) ->
|
|
|
|
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) ->
|
|
|
|
protocol.isHandledProtocol 'atom', (result) ->
|
|
|
|
assert.equal result, false
|
2015-05-11 07:26:36 +00:00
|
|
|
done()
|