2013-12-06 14:23:02 +00:00
|
|
|
assert = require 'assert'
|
|
|
|
ipc = require 'ipc'
|
|
|
|
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', ->
|
2013-08-30 05:04:02 +00:00
|
|
|
it 'throws error when scheme is already registered', (done) ->
|
2013-08-24 12:18:12 +00:00
|
|
|
register = -> protocol.registerProtocol('test1', ->)
|
2013-08-30 05:04:02 +00:00
|
|
|
protocol.once 'registered', (scheme) ->
|
|
|
|
assert.equal scheme, 'test1'
|
|
|
|
assert.throws register, /The scheme is already registered/
|
|
|
|
protocol.unregisterProtocol 'test1'
|
|
|
|
done()
|
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) ->
|
2013-08-29 13:12:48 +00:00
|
|
|
protocol.registerProtocol 'test2', (request) ->
|
|
|
|
assert.equal request.url, 'test2://test2'
|
|
|
|
assert.equal request.referrer, window.location.toString()
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
describe 'protocol.isHandledProtocol', ->
|
2013-08-29 13:06:22 +00:00
|
|
|
it 'returns true if the scheme can be handled', ->
|
2013-08-29 12:57:09 +00:00
|
|
|
assert.equal protocol.isHandledProtocol('file'), true
|
|
|
|
assert.equal protocol.isHandledProtocol('http'), true
|
|
|
|
assert.equal protocol.isHandledProtocol('https'), true
|
|
|
|
assert.equal protocol.isHandledProtocol('atom'), false
|
2013-09-03 08:50:10 +00:00
|
|
|
|
|
|
|
describe 'protocol.interceptProtocol', ->
|
|
|
|
it 'throws error when scheme is not a registered one', ->
|
|
|
|
register = -> protocol.interceptProtocol('test-intercept', ->)
|
|
|
|
assert.throws register, /Cannot intercept procotol/
|
|
|
|
|
|
|
|
it 'throws error when scheme is a custom protocol', (done) ->
|
|
|
|
protocol.once 'unregistered', (scheme) ->
|
|
|
|
assert.equal scheme, 'atom'
|
|
|
|
done()
|
|
|
|
protocol.once 'registered', (scheme) ->
|
|
|
|
assert.equal scheme, 'atom'
|
|
|
|
register = -> protocol.interceptProtocol('test-intercept', ->)
|
|
|
|
assert.throws register, /Cannot intercept procotol/
|
|
|
|
protocol.unregisterProtocol scheme
|
|
|
|
protocol.registerProtocol('atom', ->)
|
|
|
|
|
|
|
|
it 'returns original job when callback returns nothing', (done) ->
|
|
|
|
targetScheme = 'file'
|
|
|
|
protocol.once 'intercepted', (scheme) ->
|
|
|
|
assert.equal scheme, targetScheme
|
|
|
|
free = -> protocol.uninterceptProtocol targetScheme
|
|
|
|
$.ajax
|
|
|
|
url: "#{targetScheme}://#{__filename}",
|
|
|
|
success: ->
|
|
|
|
protocol.once 'unintercepted', (scheme) ->
|
|
|
|
assert.equal scheme, targetScheme
|
|
|
|
done()
|
|
|
|
free()
|
|
|
|
error: (xhr, errorType, error) ->
|
|
|
|
free()
|
|
|
|
assert false, 'Got error: ' + errorType + ' ' + error
|
|
|
|
protocol.interceptProtocol targetScheme, (request) ->
|
2013-09-05 10:28:48 +00:00
|
|
|
if process.platform is 'win32'
|
|
|
|
pathInUrl = path.normalize request.url.substr(8)
|
|
|
|
assert.equal pathInUrl, __filename
|
|
|
|
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
|