electron/spec/api-web-request-spec.js

418 lines
11 KiB
JavaScript
Raw Normal View History

2016-01-19 22:49:40 +00:00
const assert = require('assert');
const http = require('http');
const qs = require('querystring');
2016-01-19 22:49:40 +00:00
const remote = require('electron').remote;
const session = remote.session;
2016-01-12 02:40:23 +00:00
describe('webRequest module', function() {
var ses = session.defaultSession;
var server = http.createServer(function(req, res) {
2016-01-12 02:40:23 +00:00
var content;
res.setHeader('Custom', ['Header']);
content = req.url;
if (req.headers.accept === '*/*;test/header') {
content += 'header/received';
}
2016-02-17 01:39:11 +00:00
res.end(content);
2016-01-12 02:40:23 +00:00
});
var defaultURL = null;
2016-01-12 02:40:23 +00:00
before(function(done) {
2016-02-17 01:39:11 +00:00
server.listen(0, '127.0.0.1', function() {
2016-01-12 02:40:23 +00:00
var port;
port = server.address().port;
defaultURL = "http://127.0.0.1:" + port + "/";
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
after(function() {
2016-02-17 01:39:11 +00:00
server.close();
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onBeforeRequest', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onBeforeRequest(null);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('can cancel the request', function(done) {
ses.webRequest.onBeforeRequest(function(details, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
cancel: true
});
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('unexpected success');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function() {
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('can filter URLs', function(done) {
var filter = {
2016-01-12 02:40:23 +00:00
urls: [defaultURL + "filter/*"]
};
ses.webRequest.onBeforeRequest(filter, function(details, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
cancel: true
});
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL + "nofilter/test",
success: function(data) {
assert.equal(data, '/nofilter/test');
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL + "filter/test",
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('unexpected success');
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function() {
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}
});
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
2016-01-12 02:40:23 +00:00
it('receives details object', function(done) {
ses.webRequest.onBeforeRequest(function(details, callback) {
assert.equal(typeof details.id, 'number');
assert.equal(typeof details.timestamp, 'number');
assert.equal(details.url, defaultURL);
assert.equal(details.method, 'GET');
assert.equal(details.resourceType, 'xhr');
assert(!details.uploadData);
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data) {
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
it('receives post data in details object', function(done) {
var postData = {
name: 'post test',
type: 'string'
};
ses.webRequest.onBeforeRequest(function(details, callback) {
var data;
assert.equal(details.url, defaultURL);
assert.equal(details.method, 'POST');
assert.equal(details.uploadData.length, 1);
data = qs.parse(details.uploadData[0].bytes.toString());
assert.deepEqual(data, postData);
2016-02-17 01:39:11 +00:00
callback({
cancel: true
});
});
2016-02-17 01:39:11 +00:00
$.ajax({
url: defaultURL,
type: 'POST',
data: postData,
success: function() {
},
error: function() {
done();
}
});
});
2016-02-17 01:39:11 +00:00
it('can redirect the request', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onBeforeRequest(function(details, callback) {
if (details.url === defaultURL) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
redirectURL: defaultURL + "redirect"
});
} else {
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data) {
assert.equal(data, '/redirect');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onBeforeSendHeaders', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onBeforeSendHeaders(null);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('receives details object', function(done) {
ses.webRequest.onBeforeSendHeaders(function(details, callback) {
assert.equal(typeof details.requestHeaders, 'object');
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data) {
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
2016-01-12 02:40:23 +00:00
it('can change the request headers', function(done) {
ses.webRequest.onBeforeSendHeaders(function(details, callback) {
var requestHeaders;
requestHeaders = details.requestHeaders;
requestHeaders.Accept = '*/*;test/header';
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
requestHeaders: requestHeaders
});
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, '/header/received');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
2016-02-17 01:39:11 +00:00
it('resets the whole headers', function(done) {
var requestHeaders = {
2016-01-12 02:40:23 +00:00
Test: 'header'
};
ses.webRequest.onBeforeSendHeaders(function(details, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
requestHeaders: requestHeaders
});
});
ses.webRequest.onSendHeaders(function(details) {
assert.deepEqual(details.requestHeaders, requestHeaders);
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onSendHeaders', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onSendHeaders(null);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('receives details object', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onSendHeaders(function(details) {
2016-02-17 01:39:11 +00:00
assert.equal(typeof details.requestHeaders, 'object');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data) {
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onHeadersReceived', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onHeadersReceived(null);
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('receives details object', function(done) {
ses.webRequest.onHeadersReceived(function(details, callback) {
assert.equal(details.statusLine, 'HTTP/1.1 200 OK');
assert.equal(details.statusCode, 200);
assert.equal(details.responseHeaders['Custom'], 'Header');
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data) {
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
2016-01-12 02:40:23 +00:00
it('can change the response header', function(done) {
ses.webRequest.onHeadersReceived(function(details, callback) {
2016-02-17 01:39:11 +00:00
var responseHeaders = details.responseHeaders;
2016-01-12 02:40:23 +00:00
responseHeaders['Custom'] = ['Changed'];
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
responseHeaders: responseHeaders
});
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data, status, xhr) {
assert.equal(xhr.getResponseHeader('Custom'), 'Changed');
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
2016-02-17 01:39:11 +00:00
it('does not change header by default', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onHeadersReceived(function(details, 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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data, status, xhr) {
assert.equal(xhr.getResponseHeader('Custom'), 'Header');
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onResponseStarted', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onResponseStarted(null);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('receives details object', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onResponseStarted(function(details) {
assert.equal(typeof details.fromCache, 'boolean');
assert.equal(details.statusLine, 'HTTP/1.1 200 OK');
assert.equal(details.statusCode, 200);
2016-02-17 01:39:11 +00:00
assert.equal(details.responseHeaders['Custom'], 'Header');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
success: function(data, status, xhr) {
assert.equal(xhr.getResponseHeader('Custom'), 'Header');
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onBeforeRedirect', function() {
afterEach(function() {
ses.webRequest.onBeforeRedirect(null);
2016-02-17 01:39:11 +00:00
ses.webRequest.onBeforeRequest(null);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('receives details object', function(done) {
var redirectURL = defaultURL + "redirect";
2016-01-12 02:40:23 +00:00
ses.webRequest.onBeforeRequest(function(details, callback) {
if (details.url === defaultURL) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
redirectURL: redirectURL
});
} else {
2016-02-17 01:39:11 +00:00
callback({});
2016-01-12 02:40:23 +00:00
}
});
ses.webRequest.onBeforeRedirect(function(details) {
assert.equal(typeof details.fromCache, 'boolean');
assert.equal(details.statusLine, 'HTTP/1.1 307 Internal Redirect');
assert.equal(details.statusCode, 307);
2016-02-17 01:39:11 +00:00
assert.equal(details.redirectURL, redirectURL);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, '/redirect');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-01-12 02:40:23 +00:00
describe('webRequest.onCompleted', function() {
afterEach(function() {
2016-02-17 01:39:11 +00:00
ses.webRequest.onCompleted(null);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('receives details object', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onCompleted(function(details) {
assert.equal(typeof details.fromCache, 'boolean');
assert.equal(details.statusLine, 'HTTP/1.1 200 OK');
2016-02-17 01:39:11 +00:00
assert.equal(details.statusCode, 200);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
success: function(data) {
2016-01-12 02:40:23 +00:00
assert.equal(data, '/');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
},
2016-01-19 22:49:40 +00:00
error: function(xhr, errorType) {
2016-02-17 01:39:11 +00:00
done(errorType);
2016-01-12 02:40:23 +00:00
}
});
});
});
2016-02-17 01:39:11 +00:00
describe('webRequest.onErrorOccurred', function() {
2016-01-12 02:40:23 +00:00
afterEach(function() {
ses.webRequest.onErrorOccurred(null);
2016-02-17 01:39:11 +00:00
ses.webRequest.onBeforeRequest(null);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('receives details object', function(done) {
2016-01-12 02:40:23 +00:00
ses.webRequest.onBeforeRequest(function(details, callback) {
2016-02-17 01:39:11 +00:00
callback({
2016-01-12 02:40:23 +00:00
cancel: true
});
});
ses.webRequest.onErrorOccurred(function(details) {
assert.equal(details.error, 'net::ERR_BLOCKED_BY_CLIENT');
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
$.ajax({
2016-01-12 02:40:23 +00:00
url: defaultURL,
2016-01-19 22:49:40 +00:00
success: function() {
2016-02-17 01:39:11 +00:00
done('unexpected success');
2016-01-12 02:40:23 +00:00
}
});
});
});
});