electron/spec/api-protocol-spec.js

818 lines
23 KiB
JavaScript
Raw Normal View History

2016-01-19 22:49:40 +00:00
const assert = require('assert');
const http = require('http');
const path = require('path');
const qs = require('querystring');
const remote = require('electron').remote;
const protocol = remote.require('electron').protocol;
2016-01-12 02:40:23 +00:00
describe('protocol module', function() {
var protocolName = 'sp';
var text = 'valar morghulis';
var postData = {
2016-01-12 02:40:23 +00:00
name: 'post test',
type: 'string'
};
2016-01-12 02:40:23 +00:00
afterEach(function(done) {
2016-02-17 01:39:11 +00:00
protocol.unregisterProtocol(protocolName, function() {
protocol.uninterceptProtocol('http', function() {
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.register(Any)Protocol', function() {
var emptyHandler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback();
2016-01-12 02:40:23 +00:00
};
2016-01-12 02:40:23 +00:00
it('throws error when scheme is already registered', function(done) {
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error, null);
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.notEqual(error, null);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
it('does not crash when handler is called twice', function(done) {
var doubleHandler = function(request, callback) {
2016-01-12 02:40:23 +00:00
try {
callback(text);
2016-02-17 01:39:11 +00:00
callback();
2016-01-19 22:49:40 +00:00
} catch (error) {
2016-01-19 19:31:47 +00:00
// Ignore error
2016-01-12 02:40:23 +00:00
}
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, doubleHandler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sends error when callback is called with nothing', function(done) {
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-01-12 02:40:23 +00:00
return done('request succeeded but it should not');
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
return done();
}
});
});
});
2016-02-17 01:39:11 +00:00
it('does not crash when callback is called in next tick', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
setImmediate(function() {
callback(text);
2016-01-12 02:40:23 +00:00
});
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.unregisterProtocol', function() {
2016-02-17 01:39:11 +00:00
it('returns error when scheme does not exist', function(done) {
protocol.unregisterProtocol('not-exist', function(error) {
2016-01-12 02:40:23 +00:00
assert.notEqual(error, null);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.registerStringProtocol', function() {
it('sends string as response', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(text);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:46:44 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sets Access-Control-Allow-Origin', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(text);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data, status, request) {
assert.equal(data, text);
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sends object as response', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
data: text,
mimeType: 'text/html'
});
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('fails when sending object other than string', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(new Date);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.registerBufferProtocol', function() {
var buffer = new Buffer(text);
2016-01-12 02:40:23 +00:00
it('sends Buffer as response', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(buffer);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sets Access-Control-Allow-Origin', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(buffer);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data, status, request) {
assert.equal(data, text);
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sends object as response', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
data: buffer,
mimeType: 'text/html'
});
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('fails when sending string', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(text);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.registerFileProtocol', function() {
var filePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'file1');
var fileContent = require('fs').readFileSync(filePath);
var normalPath = path.join(__dirname, 'fixtures', 'pages', 'a.html');
var normalContent = require('fs').readFileSync(normalPath);
2016-01-12 02:40:23 +00:00
it('sends file path as response', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(filePath);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerFileProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:46:44 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, String(fileContent));
return done();
},
error: function(xhr, errorType, error) {
return done(error);
}
});
});
});
2016-01-12 02:40:23 +00:00
it('sets Access-Control-Allow-Origin', function(done) {
var handler = function(request, callback) {
2016-02-17 01:46:44 +00:00
callback(filePath);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerFileProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data, status, request) {
assert.equal(data, String(fileContent));
assert.equal(request.getResponseHeader('Access-Control-Allow-Origin'), '*');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
it('sends object as response', function(done) {
2016-02-17 01:39:11 +00:00
var handler = function(request, callback) {
2016-02-17 01:46:44 +00:00
callback({
2016-01-12 02:40:23 +00:00
path: filePath
});
};
2016-02-17 01:39:11 +00:00
protocol.registerFileProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, String(fileContent));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('can send normal file', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(normalPath);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerFileProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, String(normalContent));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('fails when sending unexist-file', function(done) {
var fakeFilePath = path.join(__dirname, 'fixtures', 'asar', 'a.asar', 'not-exist');
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(fakeFilePath);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('fails when sending unsupported content', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(new Date);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerBufferProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.registerHttpProtocol', function() {
it('sends url as response', function(done) {
var server = http.createServer(function(req, res) {
2016-01-12 02:40:23 +00:00
assert.notEqual(req.headers.accept, '');
res.end(text);
2016-02-17 01:39:11 +00:00
server.close();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:46:44 +00:00
server.listen(0, '127.0.0.1', function() {
2016-02-17 01:39:11 +00:00
var port = server.address().port;
var url = "http://127.0.0.1:" + port;
var handler = function(request, callback) {
callback({
2016-01-12 02:40:23 +00:00
url: url
});
};
2016-02-17 01:39:11 +00:00
protocol.registerHttpProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
it('fails when sending invalid url', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
url: 'url'
});
};
2016-02-17 01:39:11 +00:00
protocol.registerHttpProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('fails when sending unsupported content', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(new Date);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerHttpProtocol(protocolName, handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: protocolName + "://fake-host",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.isProtocolHandled', function() {
it('returns true for file:', function(done) {
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled('file', function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, true);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns true for http:', function(done) {
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled('http', function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, true);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns true for https:', function(done) {
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled('https', function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, true);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns false when scheme is not registred', function(done) {
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled('no-exist', function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, false);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
it('returns true for custom protocol', function(done) {
var emptyHandler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback();
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.registerStringProtocol(protocolName, emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error, null);
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled(protocolName, function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, true);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-02-17 01:39:11 +00:00
it('returns true for intercepted protocol', function(done) {
var emptyHandler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback();
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error, null);
2016-02-17 01:39:11 +00:00
protocol.isProtocolHandled('http', function(result) {
2016-01-12 02:40:23 +00:00
assert.equal(result, true);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.intercept(Any)Protocol', function() {
var emptyHandler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback();
2016-01-12 02:40:23 +00:00
};
2016-01-12 02:40:23 +00:00
it('throws error when scheme is already intercepted', function(done) {
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error, null);
2016-02-17 01:39:11 +00:00
protocol.interceptBufferProtocol('http', emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
assert.notEqual(error, null);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
it('does not crash when handler is called twice', function(done) {
var doubleHandler = function(request, callback) {
2016-01-12 02:40:23 +00:00
try {
callback(text);
2016-02-17 01:46:44 +00:00
callback();
2016-01-19 22:49:40 +00:00
} catch (error) {
2016-01-19 19:31:47 +00:00
// Ignore error
2016-01-12 02:40:23 +00:00
}
};
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', doubleHandler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: 'http://fake-host',
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('sends error when callback is called with nothing', function(done) {
2016-01-12 02:40:23 +00:00
if (process.env.TRAVIS === 'true') {
return done();
}
2016-02-17 01:39:11 +00:00
protocol.interceptBufferProtocol('http', emptyHandler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: 'http://fake-host',
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('request succeeded but it should not');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-01-12 02:40:23 +00:00
assert.equal(errorType, 'error');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.interceptStringProtocol', function() {
it('can intercept http protocol', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(text);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: 'http://fake-host',
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
it('can set content-type', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
mimeType: 'application/json',
data: '{"value": 1}'
});
};
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: 'http://fake-host',
success: function(data) {
assert.equal(typeof data, 'object');
assert.equal(data.value, 1);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('can receive post data', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
var uploadData = request.uploadData[0].bytes.toString();
callback({
2016-01-12 02:40:23 +00:00
data: uploadData
});
};
2016-02-17 01:39:11 +00:00
protocol.interceptStringProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: "http://fake-host",
type: "POST",
data: postData,
success: function(data) {
assert.deepEqual(qs.parse(data), postData);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.interceptBufferProtocol', function() {
it('can intercept http protocol', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
callback(new Buffer(text));
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.interceptBufferProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: 'http://fake-host',
success: function(data) {
assert.equal(data, text);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
it('can receive post data', function(done) {
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
var uploadData = request.uploadData[0].bytes;
callback(uploadData);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.interceptBufferProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: "http://fake-host",
type: "POST",
data: postData,
success: function(data) {
assert.equal(data, $.param(postData));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('protocol.interceptHttpProtocol', function() {
2016-02-17 01:39:11 +00:00
it('can send POST request', function(done) {
var server = http.createServer(function(req, res) {
2016-02-17 01:39:11 +00:00
var body = '';
2016-01-12 02:40:23 +00:00
req.on('data', function(chunk) {
2016-02-17 01:39:11 +00:00
body += chunk;
2016-01-12 02:40:23 +00:00
});
req.on('end', function() {
2016-02-17 01:39:11 +00:00
res.end(body);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
server.close();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
server.listen(0, '127.0.0.1', function() {
var port = server.address().port;
var url = "http://127.0.0.1:" + port;
var handler = function(request, callback) {
2016-02-17 01:39:11 +00:00
var data = {
2016-01-12 02:40:23 +00:00
url: url,
method: 'POST',
uploadData: {
contentType: 'application/x-www-form-urlencoded',
data: request.uploadData[0].bytes.toString()
},
session: null
};
2016-02-17 01:39:11 +00:00
callback(data);
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
protocol.interceptHttpProtocol('http', handler, function(error) {
2016-01-12 02:40:23 +00:00
if (error) {
return done(error);
}
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: "http://fake-host",
type: "POST",
data: postData,
success: function(data) {
assert.deepEqual(qs.parse(data), postData);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
error: function(xhr, errorType, error) {
2016-02-17 01:39:11 +00:00
done(error);
2016-01-12 02:40:23 +00:00
}
});
});
});
});
});
2016-02-17 01:39:11 +00:00
describe('protocol.uninterceptProtocol', function() {
2016-01-12 02:40:23 +00:00
it('returns error when scheme does not exist', function(done) {
2016-02-17 01:39:11 +00:00
protocol.uninterceptProtocol('not-exist', function(error) {
2016-01-12 02:40:23 +00:00
assert.notEqual(error, null);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('returns error when scheme is not intercepted', function(done) {
protocol.uninterceptProtocol('http', function(error) {
2016-01-12 02:40:23 +00:00
assert.notEqual(error, null);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
});