2013-12-06 14:23:02 +00:00
|
|
|
assert = require 'assert'
|
|
|
|
ipc = require 'ipc'
|
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', ->
|
2013-08-24 08:38:19 +00:00
|
|
|
describe 'protocol.registerProtocol', ->
|
2015-07-05 17:53:07 +00:00
|
|
|
it 'error when scheme is already registered', (done) ->
|
|
|
|
register = ->
|
|
|
|
protocol.registerProtocol 'test1', (error, request) ->
|
|
|
|
assert error instanceof Error
|
|
|
|
protocol.unregisterProtocol 'test1'
|
|
|
|
done()
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.once 'registered', (event, scheme) ->
|
2013-08-30 05:04:02 +00:00
|
|
|
assert.equal scheme, 'test1'
|
2015-07-05 17:53:07 +00:00
|
|
|
register()
|
2013-08-24 08:38:19 +00:00
|
|
|
register()
|
2013-08-24 12:18:12 +00:00
|
|
|
|
|
|
|
it 'calls the callback when scheme is visited', (done) ->
|
2015-07-05 17:53:07 +00:00
|
|
|
protocol.registerProtocol 'test2', (error, request) ->
|
2013-08-29 13:12:48 +00:00
|
|
|
assert.equal request.url, 'test2://test2'
|
2013-08-25 08:06:29 +00:00
|
|
|
protocol.unregisterProtocol 'test2'
|
2013-08-25 07:07:07 +00:00
|
|
|
done()
|
2013-08-24 12:18:12 +00:00
|
|
|
$.get 'test2://test2', ->
|
2013-08-24 08:38:19 +00:00
|
|
|
|
|
|
|
describe 'protocol.unregisterProtocol', ->
|
|
|
|
it 'throws error when scheme does not exist', ->
|
2013-08-24 12:18:12 +00:00
|
|
|
unregister = -> protocol.unregisterProtocol 'test3'
|
2013-08-24 08:38:19 +00:00
|
|
|
assert.throws unregister, /The scheme has not been registered/
|
2013-08-25 04:36:06 +00:00
|
|
|
|
|
|
|
describe 'registered protocol callback', ->
|
|
|
|
it 'returns string should send the string as request content', (done) ->
|
2013-08-25 12:45:34 +00:00
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.registerProtocol 'atom-string', handler
|
|
|
|
|
2013-08-25 04:36:06 +00:00
|
|
|
$.ajax
|
2013-08-25 12:45:34 +00:00
|
|
|
url: 'atom-string://fake-host'
|
2013-08-25 04:36:06 +00:00
|
|
|
success: (data) ->
|
2013-08-25 12:45:34 +00:00
|
|
|
assert.equal data, handler()
|
|
|
|
protocol.unregisterProtocol 'atom-string'
|
2013-08-25 04:36:06 +00:00
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
2013-08-25 07:07:07 +00:00
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
2013-08-25 12:45:34 +00:00
|
|
|
protocol.unregisterProtocol 'atom-string'
|
2013-08-25 07:07:07 +00:00
|
|
|
|
|
|
|
it 'returns RequestStringJob should send string', (done) ->
|
2013-08-25 12:45:34 +00:00
|
|
|
data = 'valar morghulis'
|
|
|
|
job = new protocol.RequestStringJob(mimeType: 'text/html', data: data)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-string-job', handler
|
|
|
|
|
2013-08-25 07:07:07 +00:00
|
|
|
$.ajax
|
2013-08-25 12:45:34 +00:00
|
|
|
url: 'atom-string-job://fake-host'
|
|
|
|
success: (response) ->
|
|
|
|
assert.equal response, data
|
|
|
|
protocol.unregisterProtocol 'atom-string-job'
|
2013-08-25 07:07:07 +00:00
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
2013-08-25 12:45:34 +00:00
|
|
|
protocol.unregisterProtocol 'atom-string-job'
|
2013-08-25 08:06:29 +00:00
|
|
|
|
2015-05-07 07:42:35 +00:00
|
|
|
it 'returns RequestErrorJob should send error', (done) ->
|
|
|
|
data = 'valar morghulis'
|
|
|
|
job = new protocol.RequestErrorJob(-6)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-error-job', handler
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: 'atom-error-job://fake-host'
|
|
|
|
success: (response) ->
|
|
|
|
assert false, 'should not reach here'
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert errorType, 'error'
|
|
|
|
protocol.unregisterProtocol 'atom-error-job'
|
|
|
|
done()
|
|
|
|
|
2015-05-29 15:54:00 +00:00
|
|
|
it 'returns RequestHttpJob should send respone', (done) ->
|
|
|
|
server = http.createServer (req, res) ->
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'})
|
|
|
|
res.end('hello')
|
|
|
|
server.close()
|
|
|
|
server.listen 0, '127.0.0.1', ->
|
|
|
|
{port} = server.address()
|
|
|
|
url = "http://127.0.0.1:#{port}"
|
2015-06-17 02:19:58 +00:00
|
|
|
job = new protocol.RequestHttpJob({url})
|
2015-05-29 15:54:00 +00:00
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-http-job', handler
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: 'atom-http-job://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
assert.equal data, 'hello'
|
|
|
|
protocol.unregisterProtocol 'atom-http-job'
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
protocol.unregisterProtocol 'atom-http-job'
|
|
|
|
|
2015-03-16 12:53:45 +00:00
|
|
|
it 'returns RequestBufferJob should send buffer', (done) ->
|
|
|
|
data = new Buffer("hello")
|
|
|
|
job = new protocol.RequestBufferJob(data: data)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-buffer-job', handler
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: 'atom-buffer-job://fake-host'
|
|
|
|
success: (response) ->
|
|
|
|
assert.equal response.length, data.length
|
|
|
|
buf = new Buffer(response.length)
|
|
|
|
buf.write(response)
|
|
|
|
assert buf.equals(data)
|
|
|
|
protocol.unregisterProtocol 'atom-buffer-job'
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
protocol.unregisterProtocol 'atom-buffer-job'
|
|
|
|
|
2013-08-25 08:06:29 +00:00
|
|
|
it 'returns RequestFileJob should send file', (done) ->
|
2013-08-25 12:45:34 +00:00
|
|
|
job = new protocol.RequestFileJob(__filename)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-file-job', handler
|
|
|
|
|
2013-08-25 08:06:29 +00:00
|
|
|
$.ajax
|
|
|
|
url: 'atom-file-job://' + __filename
|
|
|
|
success: (data) ->
|
2013-08-25 08:49:54 +00:00
|
|
|
content = require('fs').readFileSync __filename
|
|
|
|
assert.equal data, String(content)
|
2013-08-25 12:45:34 +00:00
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
2013-08-25 08:06:29 +00:00
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
2013-08-25 12:45:34 +00:00
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
2013-08-29 12:57:09 +00:00
|
|
|
|
2015-03-04 05:22:51 +00:00
|
|
|
it 'returns RequestFileJob should send file from asar archive', (done) ->
|
|
|
|
p = path.join __dirname, 'fixtures', 'asar', 'a.asar', 'file1'
|
|
|
|
job = new protocol.RequestFileJob(p)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-file-job', handler
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: 'atom-file-job://' + p
|
|
|
|
success: (data) ->
|
|
|
|
content = require('fs').readFileSync(p)
|
|
|
|
assert.equal data, String(content)
|
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
|
|
|
|
2015-03-20 11:12:59 +00:00
|
|
|
it 'returns RequestFileJob should send file from asar archive with unpacked file', (done) ->
|
|
|
|
p = path.join __dirname, 'fixtures', 'asar', 'unpack.asar', 'a.txt'
|
|
|
|
job = new protocol.RequestFileJob(p)
|
|
|
|
handler = remote.createFunctionWithReturnValue job
|
|
|
|
protocol.registerProtocol 'atom-file-job', handler
|
|
|
|
|
|
|
|
$.ajax
|
|
|
|
url: 'atom-file-job://' + p
|
|
|
|
success: (response) ->
|
|
|
|
data = require('fs').readFileSync(p)
|
|
|
|
assert.equal response.length, data.length
|
|
|
|
buf = new Buffer(response.length)
|
|
|
|
buf.write(response)
|
|
|
|
assert buf.equals(data)
|
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
|
|
|
done()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
protocol.unregisterProtocol 'atom-file-job'
|
|
|
|
|
2013-08-29 12:57:09 +00:00
|
|
|
describe 'protocol.isHandledProtocol', ->
|
2015-07-05 17:53:07 +00:00
|
|
|
it 'returns true if the file scheme can be handled', (done) ->
|
|
|
|
protocol.isHandledProtocol 'file', (result) ->
|
|
|
|
assert.equal result, true
|
|
|
|
done()
|
|
|
|
it 'returns true if the http scheme can be handled', (done) ->
|
|
|
|
protocol.isHandledProtocol 'http', (result) ->
|
|
|
|
assert.equal result, true
|
|
|
|
done()
|
|
|
|
it 'returns true if the https scheme can be handled', (done) ->
|
|
|
|
protocol.isHandledProtocol 'https', (result) ->
|
|
|
|
assert.equal result, true
|
|
|
|
done()
|
|
|
|
it 'returns false if the atom scheme cannot be handled', (done) ->
|
|
|
|
protocol.isHandledProtocol 'atom', (result) ->
|
|
|
|
assert.equal result, false
|
|
|
|
done()
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
describe 'protocol.interceptProtocol', ->
|
2015-07-05 17:53:07 +00:00
|
|
|
it 'throws error when scheme is not a registered one', (done) ->
|
|
|
|
protocol.interceptProtocol 'test-intercept', (error, request) ->
|
|
|
|
assert error instanceof Error
|
|
|
|
done()
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
it 'throws error when scheme is a custom protocol', (done) ->
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.once 'unregistered', (event, scheme) ->
|
2013-09-03 08:50:10 +00:00
|
|
|
assert.equal scheme, 'atom'
|
|
|
|
done()
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.once 'registered', (event, scheme) ->
|
2013-09-03 08:50:10 +00:00
|
|
|
assert.equal scheme, 'atom'
|
2015-07-05 17:53:07 +00:00
|
|
|
protocol.interceptProtocol 'test-intercept', (error, request) ->
|
|
|
|
assert error instanceof Error
|
|
|
|
protocol.unregisterProtocol scheme
|
2013-09-03 08:50:10 +00:00
|
|
|
protocol.registerProtocol('atom', ->)
|
|
|
|
|
|
|
|
it 'returns original job when callback returns nothing', (done) ->
|
|
|
|
targetScheme = 'file'
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.once 'intercepted', (event, scheme) ->
|
2013-09-03 08:50:10 +00:00
|
|
|
assert.equal scheme, targetScheme
|
|
|
|
free = -> protocol.uninterceptProtocol targetScheme
|
|
|
|
$.ajax
|
|
|
|
url: "#{targetScheme}://#{__filename}",
|
|
|
|
success: ->
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.once 'unintercepted', (event, scheme) ->
|
2013-09-03 08:50:10 +00:00
|
|
|
assert.equal scheme, targetScheme
|
|
|
|
done()
|
|
|
|
free()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
free()
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
2015-07-05 17:53:07 +00:00
|
|
|
protocol.interceptProtocol targetScheme, (error, request) ->
|
2013-09-05 10:28:48 +00:00
|
|
|
if process.platform is 'win32'
|
|
|
|
pathInUrl = path.normalize request.url.substr(8)
|
2013-12-17 15:03:36 +00:00
|
|
|
assert.equal pathInUrl.toLowerCase(), __filename.toLowerCase()
|
2013-09-05 10:28:48 +00:00
|
|
|
else
|
|
|
|
assert.equal request.url, "#{targetScheme}://#{__filename}"
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
it 'can override original protocol handler', (done) ->
|
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.once 'intercepted', ->
|
|
|
|
free = -> protocol.uninterceptProtocol 'file'
|
|
|
|
$.ajax
|
|
|
|
url: 'file://fake-host'
|
|
|
|
success: (data) ->
|
|
|
|
protocol.once 'unintercepted', ->
|
|
|
|
assert.equal data, handler()
|
|
|
|
done()
|
|
|
|
free()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
free()
|
|
|
|
protocol.interceptProtocol 'file', handler
|
2015-05-06 20:15:56 +00:00
|
|
|
|
|
|
|
it 'can override http protocol handler', (done) ->
|
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.once 'intercepted', ->
|
|
|
|
protocol.uninterceptProtocol 'http'
|
|
|
|
done()
|
|
|
|
protocol.interceptProtocol 'http', handler
|
2015-05-11 07:26:36 +00:00
|
|
|
|
|
|
|
it 'can override https protocol handler', (done) ->
|
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.once 'intercepted', ->
|
|
|
|
protocol.uninterceptProtocol 'https'
|
|
|
|
done()
|
|
|
|
protocol.interceptProtocol 'https', handler
|
|
|
|
|
|
|
|
it 'can override ws protocol handler', (done) ->
|
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.once 'intercepted', ->
|
|
|
|
protocol.uninterceptProtocol 'ws'
|
|
|
|
done()
|
|
|
|
protocol.interceptProtocol 'ws', handler
|
|
|
|
|
|
|
|
it 'can override wss protocol handler', (done) ->
|
|
|
|
handler = remote.createFunctionWithReturnValue 'valar morghulis'
|
|
|
|
protocol.once 'intercepted', ->
|
|
|
|
protocol.uninterceptProtocol 'wss'
|
|
|
|
done()
|
|
|
|
protocol.interceptProtocol 'wss', handler
|