Rename protocol.isHandledProtocol to protocol.isProtocolHandled

This commit is contained in:
Cheng Zhao 2015-08-13 21:24:27 +08:00
parent 02714d466c
commit 467ba6b7a9
4 changed files with 222 additions and 243 deletions

View file

@ -57,7 +57,7 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
.SetMethod("registerHttpProtocol", .SetMethod("registerHttpProtocol",
&Protocol::RegisterProtocol<URLRequestFetchJob>) &Protocol::RegisterProtocol<URLRequestFetchJob>)
.SetMethod("unregisterProtocol", &Protocol::UnregisterProtocol) .SetMethod("unregisterProtocol", &Protocol::UnregisterProtocol)
.SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol) .SetMethod("isProtocolHandled", &Protocol::IsProtocolHandled)
.SetMethod("interceptStringProtocol", .SetMethod("interceptStringProtocol",
&Protocol::InterceptProtocol<URLRequestStringJob>) &Protocol::InterceptProtocol<URLRequestStringJob>)
.SetMethod("interceptBufferProtocol", .SetMethod("interceptBufferProtocol",
@ -94,16 +94,16 @@ Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
return PROTOCOL_OK; return PROTOCOL_OK;
} }
void Protocol::IsHandledProtocol(const std::string& scheme, void Protocol::IsProtocolHandled(const std::string& scheme,
const BooleanCallback& callback) { const BooleanCallback& callback) {
content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::PostTaskAndReplyWithResult(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::IsHandledProtocolInIO, base::Bind(&Protocol::IsProtocolHandledInIO,
base::Unretained(this), scheme), base::Unretained(this), scheme),
callback); callback);
} }
bool Protocol::IsHandledProtocolInIO(const std::string& scheme) { bool Protocol::IsProtocolHandledInIO(const std::string& scheme) {
return job_factory_->IsHandledProtocol(scheme); return job_factory_->IsHandledProtocol(scheme);
} }

View file

@ -125,9 +125,9 @@ class Protocol : public mate::Wrappable {
ProtocolError UnregisterProtocolInIO(const std::string& scheme); ProtocolError UnregisterProtocolInIO(const std::string& scheme);
// Whether the protocol has handler registered. // Whether the protocol has handler registered.
void IsHandledProtocol(const std::string& scheme, void IsProtocolHandled(const std::string& scheme,
const BooleanCallback& callback); const BooleanCallback& callback);
bool IsHandledProtocolInIO(const std::string& scheme); bool IsProtocolHandledInIO(const std::string& scheme);
// Replace the protocol handler with a new one. // Replace the protocol handler with a new one.
template<typename RequestJob> template<typename RequestJob>

View file

@ -1,278 +1,257 @@
assert = require 'assert' assert = require 'assert'
ipc = require 'ipc'
http = require 'http' http = require 'http'
path = require 'path' path = require 'path'
remote = require 'remote' remote = require 'remote'
protocol = remote.require 'protocol' protocol = remote.require 'protocol'
describe 'protocol module', -> describe 'protocol module', ->
describe 'protocol.registerProtocol', -> protocolName = 'sp'
it 'error when scheme is already registered', (done) -> text = 'valar morghulis'
register = ->
protocol.registerProtocol 'test1', ((request) ->), (error, scheme) ->
if error?
protocol.unregisterProtocol 'test1', (error, scheme) ->
assert.equal scheme, 'test1'
done()
else
assert.equal scheme, 'test1'
register()
register()
it 'calls the callback when scheme is visited', (done) -> afterEach (done) ->
protocol.registerProtocol 'test2', (request) -> protocol.unregisterProtocol protocolName, -> done()
assert.equal request.url, 'test2://test2'
protocol.unregisterProtocol 'test2'
done()
$.get 'test2://test2', ->
describe 'protocol.unregisterProtocol', -> describe 'protocol.unregisterProtocol', ->
it 'throws error when scheme does not exist', -> it 'returns error when scheme does not exist', (done) ->
protocol.unregisterProtocol 'test3', (->), (error, scheme) -> protocol.unregisterProtocol 'not-exist', (error) ->
if (error) assert.notEqual error, null
assert.equal scheme, 'test3'
done() done()
describe 'registered protocol callback', -> describe 'protocol.register(Any)Protocol', ->
it 'returns string should send the string as request content', (done) -> emptyHandler = (request, callback) -> callback()
handler = remote.createFunctionWithReturnValue 'valar morghulis' it 'throws error when scheme is already registered', (done) ->
protocol.registerProtocol 'atom-string', handler protocol.registerStringProtocol protocolName, emptyHandler, (error) ->
assert.equal error, null
protocol.registerBufferProtocol protocolName, emptyHandler, (error) ->
assert.notEqual error, null
done()
it 'does not crash when handler is called twice', (done) ->
doubleHandler = (request, callback) ->
callback(text)
callback()
protocol.registerStringProtocol protocolName, doubleHandler, (error) ->
$.ajax $.ajax
url: 'atom-string://fake-host' url: "#{protocolName}://fake-host"
success: (data) -> success: (data) ->
assert.equal data, handler() assert.equal data, text
protocol.unregisterProtocol 'atom-string'
done() done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error done(error)
protocol.unregisterProtocol 'atom-string'
it 'returns RequestStringJob should send string', (done) ->
data = 'valar morghulis'
job = new protocol.RequestStringJob(mimeType: 'text/html', data: data)
handler = remote.createFunctionWithReturnValue job
protocol.registerProtocol 'atom-string-job', handler
it 'sends error when callback is called with nothing', (done) ->
protocol.registerBufferProtocol protocolName, emptyHandler, (error) ->
$.ajax $.ajax
url: 'atom-string-job://fake-host' url: "#{protocolName}://fake-host"
success: (response) -> success: (data) ->
assert.equal response, data done('request succeeded but it should not')
protocol.unregisterProtocol 'atom-string-job'
done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error assert.equal errorType, 'error'
protocol.unregisterProtocol 'atom-string-job' done()
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
it 'does not crash when callback is called in next tick', (done) ->
handler = (request, callback) ->
setImmediate -> callback(text)
protocol.registerStringProtocol protocolName, handler, (error) ->
$.ajax $.ajax
url: 'atom-error-job://fake-host' url: "#{protocolName}://fake-host"
success: (response) -> success: (data) ->
assert false, 'should not reach here' assert.equal data, text
done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert errorType, 'error' done(error)
protocol.unregisterProtocol 'atom-error-job'
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)
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)
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() done()
it 'returns RequestHttpJob should send respone', (done) -> describe 'protocol.registerBufferProtocol', ->
buffer = new Buffer(text)
it 'sends Buffer as response', (done) ->
handler = (request, callback) -> callback(buffer)
protocol.registerBufferProtocol protocolName, handler, (error) ->
$.ajax
url: "#{protocolName}://fake-host"
success: (data) ->
assert.equal data, text
done()
error: (xhr, errorType, error) ->
done(error)
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)
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()
describe 'protocol.registerFileProtocol', ->
filePath = path.join __dirname, 'fixtures', 'asar', 'a.asar', 'file1'
fileContent = require('fs').readFileSync(filePath)
normalPath = path.join __dirname, 'fixtures', 'pages', 'a.html'
normalContent = require('fs').readFileSync(normalPath)
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)
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)
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)
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()
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()
describe 'protocol.registerHttpProtocol', ->
it 'sends url as response', (done) ->
server = http.createServer (req, res) -> server = http.createServer (req, res) ->
assert.notEqual req.headers.accept, '' assert.notEqual req.headers.accept, ''
res.writeHead(200, {'Content-Type': 'text/plain'}) res.end(text)
res.end('hello')
server.close() server.close()
server.listen 0, '127.0.0.1', -> server.listen 0, '127.0.0.1', ->
{port} = server.address() {port} = server.address()
url = "http://127.0.0.1:#{port}" url = "http://127.0.0.1:#{port}"
job = new protocol.RequestHttpJob({url}) handler = (request, callback) -> callback({url})
handler = remote.createFunctionWithReturnValue job protocol.registerHttpProtocol protocolName, handler, (error) ->
protocol.registerProtocol 'atom-http-job', handler
$.ajax $.ajax
url: 'atom-http-job://fake-host' url: "#{protocolName}://fake-host"
success: (data) -> success: (data) ->
assert.equal data, 'hello' assert.equal data, text
protocol.unregisterProtocol 'atom-http-job'
done() done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error done(error)
protocol.unregisterProtocol 'atom-http-job'
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
it 'fails when sending invalid url', (done) ->
handler = (request, callback) -> callback({url: 'url'})
protocol.registerHttpProtocol protocolName, handler, (error) ->
$.ajax $.ajax
url: 'atom-buffer-job://fake-host' url: "#{protocolName}://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'
it 'returns RequestFileJob should send file', (done) ->
job = new protocol.RequestFileJob(__filename)
handler = remote.createFunctionWithReturnValue job
protocol.registerProtocol 'atom-file-job', handler
$.ajax
url: 'atom-file-job://' + __filename
success: (data) -> success: (data) ->
content = require('fs').readFileSync __filename done('request succeeded but it should not')
assert.equal data, String(content)
protocol.unregisterProtocol 'atom-file-job'
done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error assert.equal errorType, 'error'
protocol.unregisterProtocol 'atom-file-job' done()
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
it 'fails when sending unsupported content', (done) ->
handler = (request, callback) -> callback(new Date)
protocol.registerHttpProtocol protocolName, handler, (error) ->
$.ajax $.ajax
url: 'atom-file-job://' + p url: "#{protocolName}://fake-host"
success: (data) -> success: (data) ->
content = require('fs').readFileSync(p) done('request succeeded but it should not')
assert.equal data, String(content)
protocol.unregisterProtocol 'atom-file-job'
done()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error assert.equal errorType, 'error'
protocol.unregisterProtocol 'atom-file-job'
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() done()
error: (xhr, errorType, error) ->
assert false, 'Got error: ' + errorType + ' ' + error
protocol.unregisterProtocol 'atom-file-job'
describe 'protocol.isHandledProtocol', -> describe 'protocol.isHandledProtocol', ->
it 'returns true if the file scheme can be handled', (done) -> it 'returns true for file:', (done) ->
protocol.isHandledProtocol 'file', (result) -> protocol.isHandledProtocol 'file', (result) ->
assert.equal result, true assert.equal result, true
done() done()
it 'returns true if the http scheme can be handled', (done) ->
it 'returns true for http:', (done) ->
protocol.isHandledProtocol 'http', (result) -> protocol.isHandledProtocol 'http', (result) ->
assert.equal result, true assert.equal result, true
done() done()
it 'returns true if the https scheme can be handled', (done) ->
it 'returns true for https:', (done) ->
protocol.isHandledProtocol 'https', (result) -> protocol.isHandledProtocol 'https', (result) ->
assert.equal result, true assert.equal result, true
done() done()
it 'returns false if the atom scheme cannot be handled', (done) ->
it 'returns false when scheme is not registred', (done) ->
protocol.isHandledProtocol 'atom', (result) -> protocol.isHandledProtocol 'atom', (result) ->
assert.equal result, false assert.equal result, false
done() done()
describe 'protocol.interceptProtocol', ->
it 'throws error when scheme is not a registered one', (done) ->
protocol.interceptProtocol 'test-intercept', ( ->), (error, scheme) ->
if error?
assert.equal scheme, 'test-intercept'
done()
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'
protocol.interceptProtocol 'test-intercept', (->), (error, newScheme) ->
if error?
assert.equal newScheme, 'test-intercept'
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) ->
if process.platform is 'win32'
pathInUrl = path.normalize request.url.substr(8)
assert.equal pathInUrl.toLowerCase(), __filename.toLowerCase()
else
assert.equal request.url, "#{targetScheme}://#{__filename}"
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
it 'can override http protocol handler', (done) ->
handler = remote.createFunctionWithReturnValue 'valar morghulis'
protocol.once 'intercepted', ->
protocol.uninterceptProtocol 'http'
done()
protocol.interceptProtocol 'http', handler
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

View file

@ -135,7 +135,7 @@ app.on('ready', function() {
app.setApplicationMenu(menu); app.setApplicationMenu(menu);
// Test if using protocol module would crash. // Test if using protocol module would crash.
require('protocol').registerProtocol('test-if-crashes', function() {}); require('protocol').registerStringProtocol('test-if-crashes', function() {});
window = new BrowserWindow({ window = new BrowserWindow({
title: 'Electron Tests', title: 'Electron Tests',